Failed Conditions
Push — master ( 349866...67c1d1 )
by Florent
10:56 queued 06:08
created

OAuth2Provider::authenticate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\SecurityBundle\Security\Authentication\Provider;
15
16
use OAuth2Framework\SecurityBundle\Security\Authentication\Token\OAuth2Token;
17
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
18
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
19
use Symfony\Component\Security\Core\Exception\AuthenticationException;
20
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
21
22
final class OAuth2Provider implements AuthenticationProviderInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function authenticate(TokenInterface $token)
28
    {
29
        if (!$token instanceof OAuth2Token) {
30
            return null;
31
        }
32
        $accessToken = $token->getAccessToken();
33
34
        if (true === $accessToken->hasExpired()) {
35
            throw new BadCredentialsException('The access token has expired.');
36
        }
37
38
        try {
39
            $token->setAuthenticated(true);
40
41
            return $token;
42
        } catch (\Exception $e) {
43
            throw new AuthenticationException($e->getMessage(), $e->getCode(), $e);
44
        }
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function supports(TokenInterface $token)
51
    {
52
        return $token instanceof OAuth2Token;
53
    }
54
}
55