|
1
|
|
|
<?php namespace nyx\auth\id\protocols\oauth2; |
|
2
|
|
|
|
|
3
|
|
|
// External dependencies |
|
4
|
|
|
use GuzzleHttp\Promise\PromiseInterface as Promise; |
|
5
|
|
|
use nyx\utils; |
|
6
|
|
|
|
|
7
|
|
|
// Internal dependencies |
|
8
|
|
|
use nyx\auth\id\protocols\oauth2; |
|
9
|
|
|
use nyx\auth; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* OAuth 2.0 Provider |
|
13
|
|
|
* |
|
14
|
|
|
* @package Nyx\Auth |
|
15
|
|
|
* @version 0.1.0 |
|
16
|
|
|
* @author Michal Chojnacki <[email protected]> |
|
17
|
|
|
* @copyright 2012-2017 Nyx Dev Team |
|
18
|
|
|
* @link https://github.com/unyx/nyx |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class Provider extends auth\id\Provider implements interfaces\Provider |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* The character separating different scopes in the request. |
|
24
|
|
|
*/ |
|
25
|
|
|
const SCOPE_SEPARATOR = ' '; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array The default access scopes to be requested during the authorization step. |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $defaultScopes = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritDoc} |
|
34
|
|
|
*/ |
|
35
|
|
|
public function authorize(callable $redirect, array $parameters = []) |
|
36
|
|
|
{ |
|
37
|
|
|
$parameters += [ |
|
38
|
|
|
'scope' => implode(static::SCOPE_SEPARATOR, $this->defaultScopes), |
|
39
|
|
|
'response_type' => 'code' |
|
40
|
|
|
]; |
|
41
|
|
|
|
|
42
|
|
|
// The explicitly set Client Credentials will always overwrite the keys' values from the optional |
|
43
|
|
|
// $parameters if they are present. If you want to use other credentials, either use the getAuthorizeUrl() |
|
44
|
|
|
// method directly or instantiate a Provider with different consumer credentials. |
|
45
|
|
|
$parameters['client_id'] = $this->consumer->getId(); |
|
46
|
|
|
$parameters['redirect_uri'] = $this->consumer->getRedirectUri(); |
|
47
|
|
|
|
|
48
|
|
|
// Only doing an isset here - can't easily predict valid values nor force properly randomized values. |
|
49
|
|
|
// Invalid requests will be rejected by the endpoint, after all. |
|
50
|
|
|
$parameters['state'] = $parameters['state'] ?? utils\Random::string(16); |
|
51
|
|
|
|
|
52
|
|
|
// The state gets passed along explicitly as the second argument since it will *always* need to be persisted |
|
53
|
|
|
// in some way by the end-user until it can be discarded after a successful exchange. |
|
54
|
|
|
return $redirect($this->getAuthorizeUrl($parameters), $parameters['state'], $parameters); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritDoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public function exchange(string $code) : Promise |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->request('POST', $this->getExchangeUrl(), null, [ |
|
63
|
|
|
'form_params' => [ |
|
64
|
|
|
'client_id' => $this->consumer->getId(), |
|
65
|
|
|
'client_secret' => $this->consumer->getSecret(), |
|
66
|
|
|
'redirect_uri' => $this->consumer->getRedirectUri(), |
|
67
|
|
|
'grant_type' => 'authorization_code', |
|
68
|
|
|
'code' => $code |
|
69
|
|
|
] |
|
70
|
|
|
])->then(function (array $data) { |
|
71
|
|
|
return $this->createToken($data); |
|
72
|
|
|
}); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritDoc} |
|
77
|
|
|
*/ |
|
78
|
|
|
public function identify(oauth2\Token $token) : Promise |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->request('GET', $this->getIdentifyUrl(), $token)->then(function (array $data) use ($token) { |
|
81
|
|
|
return $this->createIdentity($token, $data); |
|
82
|
|
|
}); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritDoc} |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function getDefaultRequestOptions(auth\interfaces\Token $token = null) : array |
|
89
|
|
|
{ |
|
90
|
|
|
$options = parent::getDefaultRequestOptions($token); |
|
91
|
|
|
|
|
92
|
|
|
// If the $token is explicitly given, it will override the respective authorization header. |
|
93
|
|
|
if (isset($token)) { |
|
94
|
|
|
$options['headers']['Authorization'] = 'Bearer '.$token; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $options; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Creates an OAuth 2.0 Access Token instance based on raw response data. |
|
102
|
|
|
* |
|
103
|
|
|
* @param array $data The raw (response) data to base on. |
|
104
|
|
|
* @return oauth2\Token The resulting OAuth 2.0 Access Token instance. |
|
105
|
|
|
* @throws \RuntimeException When the data did not contain an access token in a recognized format. |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function createToken(array $data) : oauth2\Token |
|
108
|
|
|
{ |
|
109
|
|
|
// The HTTP Client will throw on an unsuccessful response, but we'll double check that we actually got |
|
110
|
|
|
// an access token in response. |
|
111
|
|
|
if (empty($data['access_token'])) { |
|
112
|
|
|
throw new \RuntimeException('The Provider did not return an access token or it was in an unrecognized format.'); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$token = new oauth2\Token($data['access_token']); |
|
116
|
|
|
|
|
117
|
|
|
if (!empty($data['refresh_token'])) { |
|
118
|
|
|
$token->setRefreshToken($data['refresh_token']); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if (!empty($data['expires_in'])) { |
|
122
|
|
|
$token->setExpiry($data['expires_in']); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
// Some providers, like Github or Slack, return the granted scopes along with the Tokens. Let's make |
|
126
|
|
|
// use of that in the base class since an isset isn't exactly expensive and if other providers happen |
|
127
|
|
|
// to return the scopes under a different key, child classes can just remap the value. |
|
128
|
|
|
if (isset($data['scope'])) { |
|
129
|
|
|
$token->setScopes(is_array($data['scope']) ? $data['scope'] : explode(static::SCOPE_SEPARATOR, $data['scope'])); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $token; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|