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\Common\Exception\InvalidArgumentException; |
13
|
|
|
use SocialConnect\JWX\DecodeOptions; |
14
|
|
|
use SocialConnect\JWX\JWT; |
15
|
|
|
use SocialConnect\OpenIDConnect\AccessToken; |
16
|
|
|
use SocialConnect\Provider\AccessTokenInterface; |
17
|
|
|
use SocialConnect\OpenIDConnect\AbstractProvider; |
18
|
|
|
use SocialConnect\Common\Entity\User; |
19
|
|
|
use SocialConnect\Provider\Exception\InvalidAccessToken; |
20
|
|
|
|
21
|
|
|
class PixelPin extends AbstractProvider |
22
|
|
|
{ |
23
|
|
|
const NAME = 'pixelpin'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
1 |
|
public function getOpenIdUrl() |
29
|
|
|
{ |
30
|
1 |
|
return 'https://login.pixelpin.io/.well-known/openid-configuration'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
2 |
|
public function getBaseUri() |
37
|
|
|
{ |
38
|
2 |
|
return 'https://login.pixelpin.io/'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
1 |
|
public function getAuthorizeUri() |
45
|
|
|
{ |
46
|
1 |
|
return 'https://login.pixelpin.io/connect/authorize'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
1 |
|
public function getRequestTokenUri() |
53
|
|
|
{ |
54
|
1 |
|
return 'https://login.pixelpin.io/connect/token'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
1 |
|
public function getName() |
61
|
|
|
{ |
62
|
1 |
|
return self::NAME; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritDoc} |
67
|
|
|
*/ |
68
|
1 |
|
public function prepareRequest(string $method, string $uri, array &$headers, array &$query, ?AccessTokenInterface $accessToken = null): void |
69
|
|
|
{ |
70
|
1 |
|
if ($accessToken) { |
71
|
1 |
|
$headers['Authorization'] = "Bearer {$accessToken->getToken()}"; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
2 |
|
public function parseToken(string $body) |
79
|
|
|
{ |
80
|
2 |
|
if (empty($body)) { |
81
|
1 |
|
throw new InvalidAccessToken('Provider response with empty body'); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
$result = json_decode($body, true); |
85
|
1 |
|
if ($result) { |
86
|
|
|
$token = new AccessToken([ |
87
|
|
|
'access_token' => $result['access_token'], |
88
|
|
|
'id_token' => $result['access_token'], |
89
|
|
|
'token_type' => $result['token_type'] |
90
|
|
|
]); |
91
|
|
|
$token->setJwt( |
92
|
|
|
JWT::decode($result['access_token'], $this->getJWKSet(), new DecodeOptions()) |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
return $token; |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
throw new InvalidAccessToken('Provider response with not valid JSON'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
public function extractIdentity(AccessTokenInterface $accessToken) |
106
|
|
|
{ |
107
|
|
|
if (!$accessToken instanceof AccessToken) { |
108
|
|
|
throw new InvalidArgumentException( |
109
|
|
|
'$accessToken must be instance AccessToken' |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$jwt = $accessToken->getJwt(); |
114
|
|
|
|
115
|
|
|
$hydrator = new ArrayHydrator([ |
116
|
|
|
'sub' => 'id', |
117
|
|
|
'email' => 'email', |
118
|
|
|
'email_verified' => 'emailVerified', |
119
|
|
|
]); |
120
|
|
|
|
121
|
|
|
/** @var User $user */ |
122
|
|
|
$user = $hydrator->hydrate(new User(), $jwt->getPayload()); |
123
|
|
|
|
124
|
|
|
return $user; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
1 |
|
public function getIdentity(AccessTokenInterface $accessToken) |
131
|
|
|
{ |
132
|
1 |
|
$response = $this->request('GET', 'connect/userinfo', [], $accessToken); |
133
|
|
|
|
134
|
1 |
|
$hydrator = new ArrayHydrator([ |
135
|
|
|
'sub' => 'id', |
136
|
|
|
'given_name' => 'firstname', |
137
|
|
|
'family_name' => 'lastname', |
138
|
|
|
'email' => 'email', |
139
|
|
|
'display_name' => 'fullname', |
140
|
|
|
'gender' => 'gender', |
141
|
|
|
'phone_number' => 'phone', |
142
|
|
|
'birthdate' => 'birthdate', |
143
|
|
|
'street_address' => 'address', |
144
|
|
|
'town_city' => 'townCity', |
145
|
|
|
'region' => 'region', |
146
|
|
|
'postal_code' => 'postalCode', |
147
|
|
|
'country' => 'country' |
148
|
|
|
]); |
149
|
|
|
|
150
|
1 |
|
return $hydrator->hydrate(new User(), $response); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|