1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SocialConnect project |
4
|
|
|
* @author: Patsura Dmitry https://github.com/ovr <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace SocialConnect\OpenIDConnect; |
8
|
|
|
|
9
|
|
|
use SocialConnect\Provider\Exception\InvalidAccessToken; |
10
|
|
|
use SocialConnect\Provider\Exception\InvalidResponse; |
11
|
|
|
|
12
|
|
|
abstract class AbstractProvider extends \SocialConnect\OAuth2\AbstractProvider |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @return array |
16
|
|
|
* @throws InvalidResponse |
17
|
|
|
*/ |
18
|
|
|
public function discover() |
19
|
|
|
{ |
20
|
|
|
$response = $this->httpClient->request( |
21
|
|
|
$this->getOpenIdUrl() |
22
|
|
|
); |
23
|
|
|
|
24
|
|
|
if (!$response->isSuccess()) { |
25
|
|
|
throw new InvalidResponse( |
26
|
|
|
'API response with error code', |
27
|
|
|
$response |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$result = $response->json(true); |
32
|
|
|
if (!$result) { |
33
|
|
|
throw new InvalidResponse( |
34
|
|
|
'API response without valid JSON', |
35
|
|
|
$response |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $result; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return array |
44
|
|
|
* @throws InvalidResponse |
45
|
|
|
*/ |
46
|
|
|
public function getJWKSet() |
47
|
|
|
{ |
48
|
|
|
$spec = $this->discover(); |
49
|
|
|
|
50
|
|
|
if (!isset($spec['jwks_uri'])) { |
51
|
|
|
throw new \RuntimeException('Unknown jwks_uri inside OpenIDConnect specification'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$response = $this->httpClient->request( |
55
|
|
|
$spec['jwks_uri'] |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
if (!$response->isSuccess()) { |
59
|
|
|
throw new InvalidResponse( |
60
|
|
|
'API response with error code', |
61
|
|
|
$response |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$result = $response->json(true); |
66
|
|
|
if (!$result) { |
67
|
|
|
throw new InvalidResponse( |
68
|
|
|
'API response without valid JSON', |
69
|
|
|
$response |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (!isset($result['keys'])) { |
74
|
|
|
throw new InvalidResponse( |
75
|
|
|
'API response without "keys" key inside JSON', |
76
|
|
|
$response |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $result['keys']; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
abstract public function getOpenIdUrl(); |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Default parameters for auth url, can be redeclared inside implementation of the Provider |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function getAuthUrlParameters() |
91
|
|
|
{ |
92
|
|
|
return [ |
93
|
|
|
'client_id' => $this->consumer->getKey(), |
94
|
|
|
'redirect_uri' => $this->getRedirectUrl(), |
95
|
|
|
'response_type' => 'code', |
96
|
|
|
//'response_mode' => 'form_post', |
97
|
|
|
'scope' => 'openid' |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function parseToken($body) |
105
|
|
|
{ |
106
|
|
|
$result = json_decode($body, true); |
107
|
|
|
if ($result) { |
108
|
|
|
$token = new AccessToken($result); |
109
|
|
|
$token->setJwt(new JWT($result['id_token'], $this->getJWKSet())); |
110
|
|
|
|
111
|
|
|
return $token; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
throw new InvalidAccessToken('Provider response with not valid JSON'); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|