本文整理汇总了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');
}
示例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());
}
示例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.');
}
示例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.');
}
示例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);
}
示例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.');
}
示例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);
}
示例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');
}
示例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);
}
示例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;
}
}
示例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.');
}
示例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);
}
示例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.');
}
示例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;
}
示例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');
}