-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractProvider.php
More file actions
130 lines (110 loc) · 3.49 KB
/
AbstractProvider.php
File metadata and controls
130 lines (110 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* SocialConnect project
* @author: Patsura Dmitry https://github.com/ovr <talk@dmtry.me>
*/
declare(strict_types=1);
namespace SocialConnect\OpenIDConnect;
use SocialConnect\Common\Entity\User;
use SocialConnect\JWX\DecodeOptions;
use SocialConnect\JWX\JWKSet;
use SocialConnect\JWX\JWT;
use SocialConnect\Provider\AccessTokenInterface;
use SocialConnect\Provider\Exception\InvalidAccessToken;
use SocialConnect\Provider\Exception\InvalidResponse;
abstract class AbstractProvider extends \SocialConnect\OAuth2\AbstractProvider
{
/**
* @return array
* @throws InvalidResponse
* @throws \Psr\Http\Client\ClientExceptionInterface
*/
public function discover(): array
{
return $this->hydrateResponse(
$this->executeRequest(
$this->httpStack->createRequest('GET', $this->getOpenIdUrl())
)
);
}
/**
* @return JWKSet
* @throws InvalidResponse
* @throws \Psr\Http\Client\ClientExceptionInterface
*/
public function getJWKSet(): JWKSet
{
$spec = $this->discover();
if (!isset($spec['jwks_uri'])) {
throw new \RuntimeException('Unknown jwks_uri inside OpenIDConnect specification');
}
$response = $this->executeRequest(
$this->httpStack->createRequest('GET', $spec['jwks_uri'])
);
$result = $this->hydrateResponse($response);
if (!isset($result['keys'])) {
throw new InvalidResponse(
'API response without "keys" key inside JSON',
$response
);
}
return new JWKSet($result);
}
/**
* @return string
*/
abstract public function getOpenIdUrl();
/**
* {@inheritdoc}
*/
public function getAuthUrlParameters(): array
{
$parameters = parent::getAuthUrlParameters();
// special parameters only required for OpenIDConnect
$parameters['client_id'] = $this->consumer->getKey();
$parameters['redirect_uri'] = $this->getRedirectUrl();
$parameters['response_type'] = 'code';
// Optional field...
//$parameters['response_mode'] = 'form_post';
$parameters['scope'] = 'openid';
return $parameters;
}
/**
* {@inheritdoc}
*/
public function parseToken(string $body)
{
if (empty($body)) {
throw new InvalidAccessToken('Provider response with empty body');
}
$result = json_decode($body, true);
if ($result) {
$token = new AccessToken($result);
$token->setJwt(
JWT::decode($result['id_token'], $this->getJWKSet(), new DecodeOptions())
);
return $token;
}
throw new InvalidAccessToken('Provider response with not valid JSON');
}
/**
* Extract data from JWT->payload and create User
* In some providers it's possible to get email/emailVerified or another data from JWT
* And due this fact it's not needed to do server request to get identity
*
* @param AccessTokenInterface $accessToken
* @return User
*/
abstract public function extractIdentity(AccessTokenInterface $accessToken);
/**
* {@inheritDoc}
*/
public function createAccessToken(array $information)
{
$token = new AccessToken($information);
$token->setJwt(
JWT::decode($information['id_token'], $this->getJWKSet(), new DecodeOptions())
);
return $token;
}
}