1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
<?php
namespace BeSimple\SsoAuthBundle\Security\Core\Authentication\Provider;
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use BeSimple\SsoAuthBundle\Sso\Manager;
use BeSimple\SsoAuthBundle\Security\Core\Authentication\Token\SsoToken;
use BeSimple\SsoAuthBundle\Sso\ValidationInterface;
use BeSimple\SsoAuthBundle\Security\Core\User\UserFactoryInterface;
use Acme\SecurityBundle\Entity\User;
class SsoAuthenticationProvider implements AuthenticationProviderInterface
{
/**
* @var UserProviderInterface
*/
private $userProvider;
/**
* @var UserCheckerInterface
*/
private $userChecker;
/**
* @var bool
*/
private $createUsers;
/**
* @var array
*/
private $createdUsersRoles;
/**
* @var bool
*/
private $hideUserNotFound;
/**
* Constructor.
*
* @param \Symfony\Component\Security\Core\User\UserProviderInterface $userProvider
* @param \Symfony\Component\Security\Core\User\UserCheckerInterface $userChecker
* @param bool $createUsers
* @param array $createdUsersRoles
* @param bool $hideUserNotFound
*
* @internal param bool $canCreateUser
*/
public function __construct(UserProviderInterface $userProvider, UserCheckerInterface $userChecker, $createUsers = false, array $createdUsersRoles = array('ROLE_USER'), $hideUserNotFound = true)
{
$this->userProvider = $userProvider;
$this->userChecker = $userChecker;
$this->createUsers = $createUsers;
$this->createdUsersRoles = $createdUsersRoles;
$this->hideUserNotFound = $hideUserNotFound;
}
/**
* @throws \Symfony\Component\Security\Core\Exception\AuthenticationServiceException
*
* @param \Symfony\Component\Security\Core\Authentication\Token\TokenInterface $token
*
* @return SsoToken|null
*/
public function authenticate(TokenInterface $token)
{
if (!$this->supports($token)) {
return null;
}
$validation = $token->getManager()->validateCredentials($token->getCredentials());
if (!$validation->isSuccess()) {
throw new BadCredentialsException('Authentication has not been validated by SSO provider.');
}
//$user = $this->provideUser($validation->getUsername(), $validation->getAttributes());
//$this->userChecker->checkPreAuth($user);
//$this->userChecker->checkPostAuth($user);
$roles[] = "ROLE_USER";
//$session = new Session();
//$session->set('username', $this->username);
//$authenticatedToken = new SsoToken($token->getManager(), $token->getCredentials(), $user, $user->getRoles(), $validation->getAttributes());
$authenticatedToken = new SsoToken($token->getManager(), $token->getCredentials(), $validation->getUsername(), $roles, $validation->getAttributes());
foreach ($token->getAttributes() as $name => $value) {
if ('sso:validation' == $name) {
continue;
}
$authenticatedToken->setAttribute($name, $value);
}
return $authenticatedToken;
}
/**
* {@inheritdoc}
*/
public function supports(TokenInterface $token)
{
return $token instanceof SsoToken;
}
/**
* @throws \Symfony\Component\Security\Core\Exception\UsernameNotFoundException
* @throws \Symfony\Component\Security\Core\Exception\BadCredentialsException
*
* @param string $username
* @param array $attributes
*
* @return UserInterface
*/
protected function provideUser($username, array $attributes = array())
{
try {
$user = $this->retrieveUser($username);
} catch (UsernameNotFoundException $notFound) {
if ($this->createUsers && $this->userProvider instanceof UserFactoryInterface) {
$user = $this->createUser($username, $attributes);
} elseif ($this->hideUserNotFound) {
throw new BadCredentialsException('Bad credentials', 0, $notFound);
} else {
throw $notFound;
}
}
return $user;
}
/**
* @throws \Symfony\Component\Security\Core\Exception\AuthenticationServiceException
*
* @param string $username
*
* @return UserInterface
*/
protected function retrieveUser($username)
{
try {
$user = $this->userProvider->loadUserByUsername($username);
if (!$user instanceof UserInterface) {
throw new AuthenticationServiceException('The user provider must return an UserInterface object.');
}
} catch (UsernameNotFoundException $notFound) {
throw $notFound;
} catch (\Exception $repositoryProblem) {
throw new AuthenticationServiceException($repositoryProblem->getMessage(), $username, 0, $repositoryProblem);
}
return $user;
}
/**
* @throws AuthenticationServiceException
*
* @param string $username
* @param array $attributes
*
* @return UserInterface
*/
protected function createUser($username, array $attributes = array())
{
if (!$this->userProvider instanceof UserFactoryInterface) {
throw new AuthenticationServiceException('UserProvider must implement UserFactoryInterface to create unknown users.');
}
try {
$user = $this->userProvider->createUser($username, $this->createdUsersRoles, $attributes);
if (!$user instanceof UserInterface) {
throw new AuthenticationServiceException('The user provider must create an UserInterface object.');
}
} catch (\Exception $repositoryProblem) {
throw new AuthenticationServiceException($repositoryProblem->getMessage(), 0, $repositoryProblem);
}
return $user;
}
} |
Partager