Completed
Push — master ( 65f314...a56b5d )
by Paweł
08:26
created

ExternalOauthProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace SWP\Bundle\CoreBundle\Security\Provider;
4
5
use League\OAuth2\Client\Provider\AbstractProvider;
6
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
7
use Psr\Http\Message\ResponseInterface;
8
use League\OAuth2\Client\Token\AccessToken;
9
10
class ExternalOauthProvider extends AbstractProvider
11
{
12
    protected $base_url;
13
14
    protected $scope_separator;
15
16
    protected $access_token;
17
18
    public function __construct(array $options = [], array $collaborators = [])
19
    {
20
        parent::__construct($options, $collaborators);
21
        $this->base_url = $options['base_url'];
22
        $this->scope_separator = $options['scope_separator'];
23
    }
24
25
    public function getAccessToken($grant, array $options = []): AccessToken
26
    {
27
        if (!isset($this->access_token)) {
28
            $this->access_token = parent::getAccessToken($grant, $options);
29
        }
30
31
        return $this->access_token;
32
    }
33
34
    public function getBaseAuthorizationUrl(): string
35
    {
36
        return $this->base_url.'/authorize';
37
    }
38
39
    public function getBaseAccessTokenUrl(array $params): string
40
    {
41
        return $this->base_url.'/oauth/token';
42
    }
43
44
    public function getResourceOwnerDetailsUrl(AccessToken $token): string
45
    {
46
        return $this->base_url.'/userinfo';
47
    }
48
49
    protected function getDefaultScopes(): string
50
    {
51
        return ['openid', 'profile', 'email'];
52
    }
53
54
    protected function checkResponse(ResponseInterface $response, $data): void
55
    {
56
        if ($response->getStatusCode() >= 400) {
57
            throw new IdentityProviderException(
58
                $response->getReasonPhrase(),
59
                $response->getStatusCode(),
60
                (string) $response->getBody());
61
        }
62
    }
63
64
    protected function createResourceOwner(array $response, AccessToken $token): ExternalOauthResourceOwner
65
    {
66
        return new ExternalOauthResourceOwner($response);
67
    }
68
69
    protected function getScopeSeparator(): string
70
    {
71
        return $this->scope_separator;
72
    }
73
74
    protected function getAuthorizationHeaders($token = null): array
75
    {
76
        if ($token) {
77
            return [
78
                'Authorization' => 'Bearer '.$token,
79
            ];
80
        }
81
82
        return [];
83
    }
84
}
85