diff options
author | Nicolas Lœuillet <nicolas@loeuillet.org> | 2015-01-31 15:14:10 +0100 |
---|---|---|
committer | Nicolas Lœuillet <nicolas@loeuillet.org> | 2015-01-31 15:14:10 +0100 |
commit | c3235553ddc2bb5965f6fe00e750cfe4aac9ccdf (patch) | |
tree | 271305a039d31059c7af8be220da08b9331baeec /src/Wallabag/CoreBundle/Security | |
parent | 71691fe44a7b2a80f3b9d96d54720cce7994ad08 (diff) | |
download | wallabag-c3235553ddc2bb5965f6fe00e750cfe4aac9ccdf.tar.gz wallabag-c3235553ddc2bb5965f6fe00e750cfe4aac9ccdf.tar.zst wallabag-c3235553ddc2bb5965f6fe00e750cfe4aac9ccdf.zip |
first implementation of security
Diffstat (limited to 'src/Wallabag/CoreBundle/Security')
3 files changed, 140 insertions, 0 deletions
diff --git a/src/Wallabag/CoreBundle/Security/Authentication/Provider/WsseProvider.php b/src/Wallabag/CoreBundle/Security/Authentication/Provider/WsseProvider.php new file mode 100644 index 00000000..ac57e27d --- /dev/null +++ b/src/Wallabag/CoreBundle/Security/Authentication/Provider/WsseProvider.php | |||
@@ -0,0 +1,59 @@ | |||
1 | <?php | ||
2 | namespace Wallabag\CoreBundle\Security\Authentication\Provider; | ||
3 | |||
4 | use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface; | ||
5 | use Symfony\Component\Security\Core\User\UserProviderInterface; | ||
6 | use Symfony\Component\Security\Core\Exception\AuthenticationException; | ||
7 | use Symfony\Component\Security\Core\Exception\NonceExpiredException; | ||
8 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
9 | use Wallabag\CoreBundle\Security\Authentication\Token\WsseUserToken; | ||
10 | |||
11 | class WsseProvider implements AuthenticationProviderInterface | ||
12 | { | ||
13 | private $userProvider; | ||
14 | private $cacheDir; | ||
15 | |||
16 | public function __construct(UserProviderInterface $userProvider, $cacheDir) | ||
17 | { | ||
18 | $this->userProvider = $userProvider; | ||
19 | $this->cacheDir = $cacheDir; | ||
20 | } | ||
21 | |||
22 | public function authenticate(TokenInterface $token) | ||
23 | { | ||
24 | $user = $this->userProvider->loadUserByUsername($token->getUsername()); | ||
25 | |||
26 | if ($user && $this->validateDigest($token->digest, $token->nonce, $token->created, $user->getPassword())) { | ||
27 | $authenticatedToken = new WsseUserToken($user->getRoles()); | ||
28 | $authenticatedToken->setUser($user); | ||
29 | |||
30 | return $authenticatedToken; | ||
31 | } | ||
32 | |||
33 | throw new AuthenticationException('The WSSE authentication failed.'); | ||
34 | } | ||
35 | |||
36 | protected function validateDigest($digest, $nonce, $created, $secret) | ||
37 | { | ||
38 | // Expire le timestamp après 5 minutes | ||
39 | if (time() - strtotime($created) > 300) { | ||
40 | return false; | ||
41 | } | ||
42 | |||
43 | // Valide que le nonce est unique dans les 5 minutes | ||
44 | if (file_exists($this->cacheDir.'/'.$nonce) && file_get_contents($this->cacheDir.'/'.$nonce) + 300 > time()) { | ||
45 | throw new NonceExpiredException('Previously used nonce detected'); | ||
46 | } | ||
47 | file_put_contents($this->cacheDir.'/'.$nonce, time()); | ||
48 | |||
49 | // Valide le Secret | ||
50 | $expected = base64_encode(sha1(base64_decode($nonce).$created.$secret, true)); | ||
51 | |||
52 | return $digest === $expected; | ||
53 | } | ||
54 | |||
55 | public function supports(TokenInterface $token) | ||
56 | { | ||
57 | return $token instanceof WsseUserToken; | ||
58 | } | ||
59 | } \ No newline at end of file | ||
diff --git a/src/Wallabag/CoreBundle/Security/Authentication/Token/WsseUserToken.php b/src/Wallabag/CoreBundle/Security/Authentication/Token/WsseUserToken.php new file mode 100644 index 00000000..b189e22a --- /dev/null +++ b/src/Wallabag/CoreBundle/Security/Authentication/Token/WsseUserToken.php | |||
@@ -0,0 +1,23 @@ | |||
1 | <?php | ||
2 | namespace Wallabag\CoreBundle\Security\Authentication\Token; | ||
3 | |||
4 | use Symfony\Component\Security\Core\Authentication\Token\AbstractToken; | ||
5 | |||
6 | class WsseUserToken extends AbstractToken | ||
7 | { | ||
8 | public $created; | ||
9 | public $digest; | ||
10 | public $nonce; | ||
11 | |||
12 | public function __construct(array $roles = array()) | ||
13 | { | ||
14 | parent::__construct($roles); | ||
15 | |||
16 | $this->setAuthenticated(count($roles) > 0); | ||
17 | } | ||
18 | |||
19 | public function getCredentials() | ||
20 | { | ||
21 | return ''; | ||
22 | } | ||
23 | } \ No newline at end of file | ||
diff --git a/src/Wallabag/CoreBundle/Security/Firewall/WsseListener.php b/src/Wallabag/CoreBundle/Security/Firewall/WsseListener.php new file mode 100644 index 00000000..b2474785 --- /dev/null +++ b/src/Wallabag/CoreBundle/Security/Firewall/WsseListener.php | |||
@@ -0,0 +1,58 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Security\Firewall; | ||
4 | |||
5 | use Symfony\Component\HttpFoundation\Response; | ||
6 | use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
7 | use Symfony\Component\Security\Http\Firewall\ListenerInterface; | ||
8 | use Symfony\Component\Security\Core\Exception\AuthenticationException; | ||
9 | use Symfony\Component\Security\Core\SecurityContextInterface; | ||
10 | use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface; | ||
11 | use Wallabag\CoreBundle\Security\Authentication\Token\WsseUserToken; | ||
12 | |||
13 | class WsseListener implements ListenerInterface | ||
14 | { | ||
15 | protected $securityContext; | ||
16 | protected $authenticationManager; | ||
17 | |||
18 | public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager) | ||
19 | { | ||
20 | $this->securityContext = $securityContext; | ||
21 | $this->authenticationManager = $authenticationManager; | ||
22 | } | ||
23 | |||
24 | public function handle(GetResponseEvent $event) | ||
25 | { | ||
26 | $request = $event->getRequest(); | ||
27 | |||
28 | $wsseRegex = '/UsernameToken Username="([^"]+)", PasswordDigest="([^"]+)", Nonce="([^"]+)", Created="([^"]+)"/'; | ||
29 | if (!$request->headers->has('x-wsse') || 1 !== preg_match($wsseRegex, $request->headers->get('x-wsse'), $matches)) { | ||
30 | return; | ||
31 | } | ||
32 | |||
33 | $token = new WsseUserToken(); | ||
34 | $token->setUser($matches[1]); | ||
35 | |||
36 | $token->digest = $matches[2]; | ||
37 | $token->nonce = $matches[3]; | ||
38 | $token->created = $matches[4]; | ||
39 | |||
40 | try { | ||
41 | $authToken = $this->authenticationManager->authenticate($token); | ||
42 | |||
43 | $this->securityContext->setToken($authToken); | ||
44 | } catch (AuthenticationException $failed) { | ||
45 | // ... you might log something here | ||
46 | |||
47 | // To deny the authentication clear the token. This will redirect to the login page. | ||
48 | // $this->securityContext->setToken(null); | ||
49 | // return; | ||
50 | |||
51 | // Deny authentication with a '403 Forbidden' HTTP response | ||
52 | $response = new Response(); | ||
53 | $response->setStatusCode(403); | ||
54 | $event->setResponse($response); | ||
55 | |||
56 | } | ||
57 | } | ||
58 | } \ No newline at end of file | ||