Completed
Push — 3.x ( e04e11...71225f )
by Дмитрий
05:26 queued 03:37
created

PixelPin::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 * @author Alexander Fedyashov <[email protected]>
6
 */
7
declare(strict_types=1);
8
9
namespace SocialConnect\OpenIDConnect\Provider;
10
11
use SocialConnect\Common\ArrayHydrator;
12
use SocialConnect\JWX\DecodeOptions;
13
use SocialConnect\JWX\JWT;
14
use SocialConnect\OpenIDConnect\AccessToken;
15
use SocialConnect\Provider\AccessTokenInterface;
16
use SocialConnect\OpenIDConnect\AbstractProvider;
17
use SocialConnect\Common\Entity\User;
18
use SocialConnect\Provider\Exception\InvalidAccessToken;
19
20
class PixelPin extends AbstractProvider
21
{
22
    const NAME = 'pixelpin';
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 1
    public function getOpenIdUrl()
28
    {
29 1
        return 'https://login.pixelpin.io/.well-known/openid-configuration';
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 2
    public function getBaseUri()
36
    {
37 2
        return 'https://login.pixelpin.io/';
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 1
    public function getAuthorizeUri()
44
    {
45 1
        return 'https://login.pixelpin.io/connect/authorize';
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 1
    public function getRequestTokenUri()
52
    {
53 1
        return 'https://login.pixelpin.io/connect/token';
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 1
    public function getName()
60
    {
61 1
        return self::NAME;
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67 1
    public function prepareRequest(string $method, string $uri, array &$headers, array &$query, AccessTokenInterface $accessToken = null): void
68
    {
69 1
        if ($accessToken) {
70 1
            $headers['Authorization'] = "Bearer {$accessToken->getToken()}";
71
        }
72 1
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 2
    public function parseToken(string $body)
78
    {
79 2
        if (empty($body)) {
80 1
            throw new InvalidAccessToken('Provider response with empty body');
81
        }
82
83 1
        $result = json_decode($body, true);
84 1
        if ($result) {
85
            $token = new AccessToken([
86
                'access_token' => $result['access_token'],
87
                'id_token' => $result['access_token'],
88
                'token_type' => $result['token_type']
89
            ]);
90
            $token->setJwt(
91
                JWT::decode($result['access_token'], $this->getJWKSet(), new DecodeOptions())
92
            );
93
94
            return $token;
95
        }
96
97 1
        throw new InvalidAccessToken('Provider response with not valid JSON');
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 1
    public function getIdentity(AccessTokenInterface $accessToken)
104
    {
105 1
        $response = $this->request('GET', 'connect/userinfo', [], $accessToken);
106
107 1
        $hydrator = new ArrayHydrator([
108 1
            'sub' => 'id',
109
            'given_name' => 'firstname',
110
            'family_name' => 'lastname',
111
            'email' => 'email',
112
            'display_name' => 'fullname',
113
            'gender' => 'gender',
114
            'phone_number' => 'phone',
115
            'birthdate' => 'birthdate',
116
            'street_address' => 'address',
117
            'town_city' => 'townCity',
118
            'region'   => 'region',
119
            'postal_code' => 'postalCode',
120
            'country' => 'country'
121
        ]);
122
123 1
        return $hydrator->hydrate(new User(), $response);
124
    }
125
}
126