Failed Conditions
Push — master ( cccd95...989cb2 )
by Florent
03:57
created

OAuth2Provider::authenticate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
rs 9.0534
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
class OAuth2Provider implements AuthenticationProviderInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function authenticate(TokenInterface $token)
28
    {
29
        if (!$this->supports($token)) {
30
            return null;
31
        }
32
33
        /** @var OAuth2Token $token */
34
        $accessToken = $token->getAccessToken();
35
36
        if (true === $accessToken->hasExpired()) {
37
            throw new BadCredentialsException('The access token has expired.');
38
        }
39
40
        try {
41
            $token->setAuthenticated(true);
42
43
            return $token;
44
        } catch (\Exception $e) {
45
            throw new AuthenticationException($e->getMessage(), $e->getCode(), $e);
46
        }
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function supports(TokenInterface $token)
53
    {
54
        return $token instanceof OAuth2Token;
55
    }
56
}
57