|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2016 François Kooman <[email protected]>. |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* |
|
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
10
|
|
|
* |
|
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14
|
|
|
* See the License for the specific language governing permissions and |
|
15
|
|
|
* limitations under the License. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace fkooman\OAuth\Client; |
|
19
|
|
|
|
|
20
|
|
|
use fkooman\OAuth\Client\Exception\OAuthException; |
|
21
|
|
|
|
|
22
|
|
|
class OAuth2Client |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var Provider */ |
|
25
|
|
|
private $provider; |
|
26
|
|
|
|
|
27
|
|
|
/** @var HttpClientInterface */ |
|
28
|
|
|
private $httpClient; |
|
29
|
|
|
|
|
30
|
|
|
/** @var RandomInterface */ |
|
31
|
|
|
private $random; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct(Provider $provider, HttpClientInterface $httpClient, RandomInterface $random = null) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->provider = $provider; |
|
36
|
|
|
$this->httpClient = $httpClient; |
|
37
|
|
|
if (is_null($random)) { |
|
38
|
|
|
$random = new Random(); |
|
39
|
|
|
} |
|
40
|
|
|
$this->random = $random; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getAuthorizationRequestUri($scope, $redirectUri) |
|
44
|
|
|
{ |
|
45
|
|
|
$state = $this->random->get(); |
|
46
|
|
|
|
|
47
|
|
|
$queryParams = http_build_query( |
|
48
|
|
|
[ |
|
49
|
|
|
'client_id' => $this->provider->getId(), |
|
50
|
|
|
'redirect_uri' => $redirectUri, |
|
51
|
|
|
'scope' => $scope, |
|
52
|
|
|
'state' => $state, |
|
53
|
|
|
'response_type' => 'code', |
|
54
|
|
|
], |
|
55
|
|
|
'&' |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
return sprintf( |
|
59
|
|
|
'%s%s%s', |
|
60
|
|
|
$this->provider->getAuthorizationEndpoint(), |
|
61
|
|
|
false === strpos($this->provider->getAuthorizationEndpoint(), '?') ? '?' : '&', |
|
62
|
|
|
$queryParams |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function getAccessToken($authorizationRequestUri, $authorizationResponseCode, $authorizationResponseState) |
|
67
|
|
|
{ |
|
68
|
|
|
// parse our authorizationRequestUri to extract the state |
|
69
|
|
|
if (false === strpos($authorizationRequestUri, '?')) { |
|
70
|
|
|
throw new OAuthException('invalid authorizationRequestUri'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
parse_str(explode('?', $authorizationRequestUri)[1], $queryParams); |
|
74
|
|
|
|
|
75
|
|
|
if (!isset($queryParams['state'])) { |
|
76
|
|
|
throw new OAuthException('state missing from authorizationRequestUri'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if (!isset($queryParams['redirect_uri'])) { |
|
80
|
|
|
throw new OAuthException('redirect_uri missing from authorizationRequestUri'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if ($authorizationResponseState !== $queryParams['state']) { |
|
84
|
|
|
throw new OAuthException('state from authorizationRequestUri MUST match authorizationResponseState'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// prepare access_token request |
|
88
|
|
|
$tokenRequestData = [ |
|
89
|
|
|
'client_id' => $this->provider->getId(), |
|
90
|
|
|
'grant_type' => 'authorization_code', |
|
91
|
|
|
'code' => $authorizationResponseCode, |
|
92
|
|
|
'redirect_uri' => $queryParams['redirect_uri'], |
|
93
|
|
|
]; |
|
94
|
|
|
|
|
95
|
|
|
$responseData = self::validateTokenResponse( |
|
96
|
|
|
$this->httpClient->post( |
|
97
|
|
|
$this->provider, |
|
98
|
|
|
$tokenRequestData |
|
99
|
|
|
) |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
return new AccessToken( |
|
103
|
|
|
$responseData['access_token'], |
|
104
|
|
|
$responseData['token_type'], |
|
105
|
|
|
$responseData['scope'], |
|
106
|
|
|
$responseData['expire_in'] |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
private static function validateTokenResponse(array $responseData) |
|
111
|
|
|
{ |
|
112
|
|
|
if (!isset($responseData['access_token'])) { |
|
113
|
|
|
throw new OAuthException('no access_token received from token endpoint'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
if (!isset($responseData['token_type'])) { |
|
117
|
|
|
throw new OAuthException('no token_type received from token endpoint'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if (!isset($responseData['scope'])) { |
|
121
|
|
|
$responseData['scope'] = null; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if (!isset($responseData['expires_in'])) { |
|
125
|
|
|
$responseData['expire_in'] = null; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $responseData; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|