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

OAuth2Provider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 35
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A authenticate() 0 21 4
A supports() 0 4 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