Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bfe2a36

Browse files
committed
Merge pull request FriendsOfSymfony#117 from mtotheikle/support-post-auth-checks
Add support for the OAuthProvider to perform post auth checks
2 parents d2d96b3 + a5d1a9c commit bfe2a36

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

Resources/config/security.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<service id="fos_oauth_server.security.authentication.provider" class="%fos_oauth_server.security.authentication.provider.class%" public="false">
1515
<argument /> <!-- user provider -->
1616
<argument type="service" id="fos_oauth_server.server" />
17+
<argument type="service" id="security.user_checker" />
1718
</service>
1819

1920
<service id="fos_oauth_server.security.authentication.listener" class="%fos_oauth_server.security.authentication.listener.class%" public="false">

Security/Authentication/Provider/OAuthProvider.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
1717
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1818
use Symfony\Component\Security\Core\Exception\AuthenticationException;
19+
use Symfony\Component\Security\Core\Exception\AccountStatusException;
1920
use Symfony\Component\Security\Core\User\UserProviderInterface;
21+
use Symfony\Component\Security\Core\User\UserCheckerInterface;
22+
2023
use OAuth2\OAuth2;
2124
use OAuth2\OAuth2ServerException;
25+
use OAuth2\OAuth2AuthenticateException;
2226

2327
/**
2428
* OAuthProvider class.
@@ -35,15 +39,20 @@ class OAuthProvider implements AuthenticationProviderInterface
3539
* @var \OAuth2\OAuth2
3640
*/
3741
protected $serverService;
42+
/**
43+
* @var \Symfony\Component\Security\Core\User\UserChecker
44+
*/
45+
protected $userChecker;
3846

3947
/**
4048
* @param \Symfony\Component\Security\Core\User\UserProviderInterface $userProvider The user provider.
4149
* @param \OAuth2\OAuth2 $serverService The OAuth2 server service.
4250
*/
43-
public function __construct(UserProviderInterface $userProvider, OAuth2 $serverService)
51+
public function __construct(UserProviderInterface $userProvider, OAuth2 $serverService, UserCheckerInterface $userChecker)
4452
{
4553
$this->userProvider = $userProvider;
4654
$this->serverService = $serverService;
55+
$this->userChecker = $userChecker;
4756
}
4857

4958
/**
@@ -75,6 +84,18 @@ public function authenticate(TokenInterface $token)
7584
$token->setToken($tokenString);
7685

7786
if (null !== $user) {
87+
88+
try {
89+
$this->userChecker->checkPostAuth($user);
90+
} catch (AccountStatusException $e) {
91+
throw new OAuth2AuthenticateException(OAuth2::HTTP_UNAUTHORIZED,
92+
OAuth2::TOKEN_TYPE_BEARER,
93+
$this->serverService->getVariable(OAuth2::CONFIG_WWW_REALM),
94+
'access_denied',
95+
$e->getMessage()
96+
);
97+
}
98+
7899
$token->setUser($user);
79100
}
80101

Tests/Security/Authentification/Provider/OAuthProviderTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ class OAuthProviderTest extends \PHPUnit_Framework_TestCase
2121
protected $userProvider;
2222
protected $provider;
2323
protected $serverService;
24+
protected $userChecker;
2425

2526
public function setUp()
2627
{
2728
$this->user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
2829
$this->userProvider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
2930
$this->serverService = $this->getMock('OAuth2\OAuth2', array('verifyAccessToken'), array(), '', false);
30-
$this->provider = new OAuthProvider($this->userProvider, $this->serverService);
31+
$this->userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
32+
$this->provider = new OAuthProvider($this->userProvider, $this->serverService, $this->userChecker);
3133
}
3234

3335
public function testAuthenticateReturnsTokenIfValid()

0 commit comments

Comments
 (0)