當前位置: 首頁>>代碼示例>>PHP>>正文


PHP TokenInterface::getUsername方法代碼示例

本文整理匯總了PHP中Symfony\Component\Security\Core\Authentication\Token\TokenInterface::getUsername方法的典型用法代碼示例。如果您正苦於以下問題:PHP TokenInterface::getUsername方法的具體用法?PHP TokenInterface::getUsername怎麽用?PHP TokenInterface::getUsername使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Symfony\Component\Security\Core\Authentication\Token\TokenInterface的用法示例。


在下文中一共展示了TokenInterface::getUsername方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: authenticateToken

 /**
  * @inheritdoc
  */
 public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
 {
     try {
         $user = $userProvider->loadUserByUsername($token->getUsername());
     } catch (UsernameNotFoundException $e) {
         throw new CustomUserMessageAuthenticationException('Invalid username or password');
     }
     $username = $token->getUsername();
     $password = $token->getCredentials();
     $sessionCreationResult = null;
     try {
         $sessionCreationResult = $this->userSessionRepository->createSessionIdByCredentials($username, $password);
     } catch (\InvalidArgumentException $e) {
         throw new CustomUserMessageAuthenticationException('Invalid username or password');
     } catch (RepositoryInfrastructureException $e) {
         throw new CustomUserMessageAuthenticationException('Cannot connect to Revive service');
     }
     $passwordValid = $sessionCreationResult !== null && UserSessionCreationAuthenticationResult::isSuccess($sessionCreationResult->getSessionCreationAuthenticationResult());
     if ($passwordValid) {
         $sessionId = $sessionCreationResult->getSessionId();
         $roles = [];
         $roles[] = 'USER';
         if (UserSessionCreationAuthorizationSessionCreationResult::isSuccess($sessionCreationResult->getSessionCreationAuthorizationSessionCreation())) {
             $roles[] = 'ADMIN';
         }
         $token = new ReviveAuthenticationToken($user, $sessionId, $providerKey, $roles);
         return $token;
     }
     throw new CustomUserMessageAuthenticationException('Invalid username or password');
 }
開發者ID:athlan,項目名稱:revive-symfony-security-bundle,代碼行數:33,代碼來源:LoginFormAuthenticator.php

示例2: authenticateToken

 public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
 {
     $user = $userProvider->loadUserByUsername($token->getUsername());
     if (!$user) {
         throw new AuthenticationException('User not found');
     }
     return new PreAuthenticatedToken($user, $token->getUsername(), $providerKey, $user->getRoles());
 }
開發者ID:ruslan-polutsygan,項目名稱:dev-bundle,代碼行數:8,代碼來源:UserAuthenticator.php

示例3: authenticate

 public function authenticate(TokenInterface $token)
 {
     $user = $this->userProvider->loadUserByUsername($token->getUsername());
     if ($user && ($token->isAuthenticated() || $this->crowd->isauthenticationvalid($token->getUsername(), $token->getCredentials()))) {
         $authenticatedToken = new CrowdToken($user->getRoles());
         $authenticatedToken->setUser($user);
         return $authenticatedToken;
     }
     throw new AuthenticationException('The Crowd authentication failed.');
 }
開發者ID:GCth,項目名稱:crowd-auth-bundle,代碼行數:10,代碼來源:CrowdProvider.php

示例4: authenticate

 /**
  * @param TokenInterface $token
  * @return WsseToken|TokenInterface
  */
 public function authenticate(TokenInterface $token)
 {
     $user = $this->userProvider->loadUserByUsername($token->getUsername());
     if ($user && $this->validateDigest($token->getAttribute('digest'), $token->getAttribute('nonce'), $token->getAttribute('created'), $this->getSecret($user), $this->getSalt($user), $user)) {
         $authenticatedToken = new WsseToken($user->getRoles());
         $authenticatedToken->setUser($user);
         $authenticatedToken->setAuthenticated(true);
         return $authenticatedToken;
     }
     $this->logger->error(sprintf('Attempt of unauthorized access for user: %s', $token->getUsername()));
     throw new AuthenticationException(' Incorrect email or password.');
 }
開發者ID:gitter-badger,項目名稱:diamantedesk-application,代碼行數:16,代碼來源:WsseProvider.php

示例5: authenticateToken

 /**
  * @param TokenInterface $token
  * @param UserProviderInterface $userProvider
  * @param $providerKey
  * @throw AuthenticationException
  * @thorw BadCredentialsException
  * @return UsernamePasswordToken
  */
 public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
 {
     try {
         $user = $userProvider->loadUserByUsername($token->getUsername());
     } catch (UsernameNotFoundException $e) {
         throw new AuthenticationException(sprintf('Can\'t find user by "%s" username', $token->getUsername()));
     }
     if (true === $this->pamAuth($token->getUsername(), $token->getCredentials())) {
         return new UsernamePasswordToken($user, $user->getPassword(), $providerKey, in_array($user->getUsername(), $this->rootUsers) ? ['ROLE_USER', 'ROLE_ADMIN'] : ['ROLE_USER']);
     }
     throw new BadCredentialsException('Bad credentials', 403);
 }
開發者ID:spolischook,項目名稱:ylang-ylang,代碼行數:20,代碼來源:PamAuthenticator.php

示例6: authenticate

 public function authenticate(TokenInterface $token)
 {
     $user = $this->userProvider->loadUserByUsername($token->getUsername());
     if ($user) {
         if ($this->validateDigest((string) $token->digest, $token->getUsername(), $token->nonce, $token->created, $user->getAuthSecret())) {
             $authenticatedToken = new WsseUserToken(array('IS_AUTHENTICATED'));
             $authenticatedToken->setUser($user);
             $authenticatedToken->setAuthenticated(TRUE);
             return $authenticatedToken;
         }
     }
     throw new AuthenticationException('The WSSE authentication failed.');
 }
開發者ID:richsage,項目名稱:MjhWsseBundle,代碼行數:13,代碼來源:WsseProvider.php

示例7: onAuthenticationSuccess

 /**
  * {@inheritdoc}
  */
 public function onAuthenticationSuccess(Request $request, TokenInterface $token)
 {
     if ($request->isXmlHttpRequest()) {
         return new JsonResponse(['success' => true, 'username' => $token->getUsername()]);
     }
     return parent::onAuthenticationSuccess($request, $token);
 }
開發者ID:okwinza,項目名稱:Sylius,代碼行數:10,代碼來源:AuthenticationSuccessHandler.php

示例8: authenticateToken

 /**
  * Function used for user authentication based on token object
  *
  * @param  \Symfony\Component\Security\Core\Authentication\Token\TokenInterface        $token
  * @param  \Symfony\Component\Security\Core\User\UserProviderInterface                 $userProvider
  * @param  string                                                                      $providerKey
  * @return \Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken
  * @throws BadCredentialsException
  */
 public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
 {
     $passwordValid = false;
     // Load user object
     try {
         $user = $userProvider->loadUserByUsername($token->getUsername());
     } catch (UsernameNotFoundException $e) {
         throw new BadCredentialsException('Invalid username or password', 0, $e);
     }
     try {
         $this->userChecker->checkPreAuth($user);
         // Call the correct authentication method
         if (null !== $this->ldapManager && $user->isLdapEnabled()) {
             $passwordValid = $this->checkAuthenticationLdap($user, $token);
         } else {
             $passwordValid = $this->checkAuthentication($user, $token);
         }
         $this->userChecker->checkPostAuth($user);
     } catch (BadCredentialsException $e) {
         if ($this->hideUserNotFoundExceptions) {
             throw new BadCredentialsException('Invalid username or password', 0, $e);
         }
         throw $e;
     }
     // Set the authenticated token
     if ($passwordValid) {
         return new UsernamePasswordToken($user, $user->getPassword(), $providerKey, $user->getRoles());
     }
     throw new BadCredentialsException('Invalid username or password');
 }
開發者ID:hkmshb,項目名稱:OpitHrm,代碼行數:39,代碼來源:LdapAuthenticator.php

示例9: onAuthenticationSuccess

 /**
  * 
  * @param Request $request
  * @param Response $response
  * @param TokenInterface $token
  * @return void
  */
 public function onAuthenticationSuccess(Request $request, TokenInterface $token)
 {
     //trigger_error(var_export($token->getUsername() , 1));
     $this->em->getRepository('SportnetzwerkSpnBundle:Player')->updateOnlineFlag($token->getUsername(), 1);
     //kdos: must have when using an ajax login
     return new JsonResponse(array('data' => 'Credentials ok'), 200);
 }
開發者ID:kotscho,項目名稱:sportnetzwerk,代碼行數:14,代碼來源:LoginHandler.php

示例10: authenticate

 public function authenticate(TokenInterface $token)
 {
     if (strlen($token->getOAuthToken()) === 0) {
         $url = $this->remoteApiUrl . "/oauth/v2/token?" . "client_id=" . $this->remoteApiId . "&client_secret=" . $this->remoteApiSecret . "&grant_type=password" . "&username=" . $token->getUser() . "&password=" . $token->getPassword();
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         $apiResponse = json_decode(curl_exec($ch));
         curl_close($ch);
         if (isset($apiResponse->access_token)) {
             $user = $this->userManager->createUser();
             $user->setUsername($token->getUsername());
             $authenticatedToken = new OAuthUserToken($user->getRoles());
             $authenticatedToken->setUser($user);
             $authenticatedToken->setOAuthToken($apiResponse->access_token);
             $authenticatedToken->setRefreshToken($apiResponse->refresh_token);
             $authenticatedToken->setTokenType($apiResponse->token_type);
             // We take 3 minutes less (180 seconds) just to be sure.
             $authenticatedToken->setExpireTime(time() + $apiResponse->expires_in - 180);
             return $authenticatedToken;
         } elseif (isset($apiResponse->error_description)) {
             throw new AuthenticationException($apiResponse->error_description);
         } else {
             throw new AuthenticationException('The OAuth authentication failed.');
         }
     } else {
         return $token;
     }
 }
開發者ID:bankette,項目名稱:OAuth_Password_Client,代碼行數:29,代碼來源:OAuthProvider.php

示例11: authenticateToken

 /**
  * Attempt to authenticate the provided token using the provided user provider.
  * @param TokenInterface $token
  * @param UserProviderInterface $userProvider
  * @param string $providerKey
  * @return UsernamePasswordToken
  * @throws BadCredentialsException
  */
 public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
 {
     if (($user = $userProvider->loadUserByUsername($token->getUsername())) && $user->getPassword() == $token->getCredentials()) {
         return new UsernamePasswordToken($user, $user->getPassword(), $providerKey, $user->getRoles());
     }
     throw new BadCredentialsException('The presented password is invalid.');
 }
開發者ID:mlukman,項目名稱:securilex,代碼行數:15,代碼來源:PlaintextPasswordAuthenticationFactory.php

示例12: onAuthenticationSuccess

 /**
  * {@inheritDoc}
  */
 public function onAuthenticationSuccess(Request $request, TokenInterface $token)
 {
     if ($request->isXmlHttpRequest()) {
         $json = array('ok' => true, 'username' => $token->getUsername());
         return new \Symfony\Component\HttpFoundation\JsonResponse($json);
     }
     return parent::onAuthenticationSuccess($request, $token);
 }
開發者ID:symforce,項目名稱:symforce-user,代碼行數:11,代碼來源:AjaxAuthenticationSuccessHandler.php

示例13: authenticate

 public function authenticate(TokenInterface $token)
 {
     if ($token->getUsername() == 'new_user_registration') {
         return $token;
     } else {
         $this->user = $this->userProvider->loadUserByUsername(array($token->getUsername()));
         if ($this->user) {
             $plainUserPassword = base64_decode($token->encryptedPass);
             if ($this->_hash_equals(crypt($plainUserPassword, $this->user->getSalt()), $this->user->getPassword())) {
                 $authenticatedToken = new CustomAuthToken($this->user->getRoles());
                 $authenticatedToken->setUser($this->user);
                 return $authenticatedToken;
             }
         }
     }
     throw new AuthenticationException('Authentication failed.');
 }
開發者ID:syedomair,項目名稱:SymfonyAPIProject,代碼行數:17,代碼來源:CustomAuthProvider.php

示例14: authenticateToken

 /**
  * @param TokenInterface $token
  * @param UserProviderInterface $userProvider
  * @param $providerKey
  * @return UsernamePasswordToken
  */
 public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
 {
     $user = $userProvider->loadUserByUsername($token->getUsername());
     $params = ["client_id" => $this->config['client_id'], "client_secret" => $this->config['client_secret'], "email" => $token->getUsername(), "password" => $token->getCredentials()];
     try {
         $storage = $this->emailTokenProvider->authentificate($this->config['endpoint'], $params, $this->config['grant']);
     } catch (BadAuthentificationException $e) {
         // CAUTION: this message will be returned to the client
         // (so don't put any un-trusted messages / error strings here)
         throw new CustomUserMessageAuthenticationException('Invalid credentials');
     }
     $emailToken = new EmailToken($user, $user->getPassword(), $providerKey, $user->getRoles());
     $emailToken->setAccessToken($storage['accessToken']);
     $emailToken->setRefreshToken($storage['refreshToken']);
     $emailToken->setExpiresIn($storage['expiresIn']);
     return $emailToken;
 }
開發者ID:NathanVss,項目名稱:oauth-extension-bundle,代碼行數:23,代碼來源:EmailAuthenticator.php

示例15: authenticateToken

 /**
  * Function used for user authentication based on token object
  *
  * @param  \Symfony\Component\Security\Core\Authentication\Token\TokenInterface        $token
  * @param  \Symfony\Component\Security\Core\User\UserProviderInterface                 $userProvider
  * @param  type                                                                        $providerKey
  * @return \Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken
  * @throws BadCredentialsException
  */
 public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
 {
     $passwordValid = false;
     // Loda user object
     try {
         $user = $userProvider->loadUserByUsername($token->getUsername());
     } catch (UsernameNotFoundException $e) {
         throw new BadCredentialsException('Invalid username or password', 0, $e);
     }
     // Check if ldap extension is enabled and user's ldap flag is set.
     if (null !== $this->ldapManager && $user->isLdapEnabled()) {
         try {
             $this->ldapManager->bind($token->getUsername(), $token->getCredentials());
             $passwordValid = (bool) $this->ldapManager->getBoundUser();
             if (null !== $this->logger && !$token->isAuthenticated()) {
                 $this->logger->info("[LdapAuthenticator] Ldap authentication successful.", array('user' => $this->ldapManager->getBoundUser()));
             }
         } catch (\Zend\Ldap\Exception\LdapException $e) {
             throw new BadCredentialsException('Invalid username or password', 0, $e);
         }
     } else {
         $currentUser = $token->getUser();
         if ($currentUser instanceof UserInterface) {
             if ($currentUser->getPassword() !== $user->getPassword()) {
                 throw new BadCredentialsException('The credentials were changed from another session.');
             } else {
                 $passwordValid = true;
             }
         } else {
             if ("" === ($presentedPassword = $token->getCredentials())) {
                 throw new BadCredentialsException('Invalid username or password.');
             }
             if (!($passwordValid = $this->encoderFactory->getEncoder($user)->isPasswordValid($user->getPassword(), $presentedPassword, $user->getSalt()))) {
                 throw new BadCredentialsException('Invalid username or password.');
             }
         }
         if (null !== $this->logger && !$token->isAuthenticated()) {
             $this->logger->info("[LdapAuthenticator] Local authentication successful.", array('user' => $user->getUsername()));
         }
     }
     // Set the authenticated token
     if ($passwordValid) {
         return new UsernamePasswordToken($user, $user->getPassword(), $providerKey, $user->getRoles());
     }
     throw new BadCredentialsException('Invalid username or password');
 }
開發者ID:joeflack4,項目名稱:OpitHrm,代碼行數:55,代碼來源:LdapAuthenticator.php


注:本文中的Symfony\Component\Security\Core\Authentication\Token\TokenInterface::getUsername方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。