|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CommerceGuys\Guzzle\Oauth2\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Ring\Client\MockHandler; |
|
6
|
|
|
|
|
7
|
|
|
class MockOAuth2Server |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var array */ |
|
10
|
|
|
protected $options; |
|
11
|
|
|
|
|
12
|
|
|
public function __construct(array $options = []) |
|
13
|
|
|
{ |
|
14
|
|
|
$defaults = [ |
|
15
|
|
|
'tokenExpiresIn' => 3600, |
|
16
|
|
|
'tokenPath' => '/oauth2/token', |
|
17
|
|
|
]; |
|
18
|
|
|
$this->options = $options + $defaults; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @return MockHandler |
|
23
|
|
|
*/ |
|
24
|
|
|
public function getHandler() |
|
25
|
|
|
{ |
|
26
|
|
|
return new MockHandler(function (array $request) { |
|
27
|
|
|
return $this->getResult($request); |
|
28
|
|
|
}); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param array $request |
|
33
|
|
|
* |
|
34
|
|
|
* @return array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function getResult(array $request) |
|
37
|
|
|
{ |
|
38
|
|
|
if ($request['uri'] === $this->options['tokenPath']) { |
|
39
|
|
|
$response = $this->oauth2Token($request); |
|
40
|
|
|
} elseif (strpos($request['uri'], 'api/') !== false) { |
|
41
|
|
|
$response = $this->mockApiCall($request); |
|
42
|
|
|
} |
|
43
|
|
|
if (!isset($response)) { |
|
44
|
|
|
throw new \RuntimeException("Mock server cannot handle given request URI"); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $response; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param array $request |
|
52
|
|
|
* |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function oauth2Token(array $request) |
|
56
|
|
|
{ |
|
57
|
|
|
/** @var \GuzzleHttp\Post\PostBody $body */ |
|
58
|
|
|
$body = $request['body']; |
|
59
|
|
|
$requestBody = $body->getFields(); |
|
60
|
|
|
$grantType = $requestBody['grant_type']; |
|
61
|
|
|
switch ($grantType) { |
|
62
|
|
|
case 'password': |
|
63
|
|
|
return $this->grantTypePassword($requestBody); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
case 'client_credentials': |
|
66
|
|
|
return $this->grantTypeClientCredentials($request); |
|
67
|
|
|
|
|
68
|
|
|
case 'refresh_token': |
|
69
|
|
|
return $this->grantTypeRefreshToken($requestBody); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
case 'urn:ietf:params:oauth:grant-type:jwt-bearer': |
|
72
|
|
|
return $this->grantTypeJwtBearer($requestBody); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
throw new \RuntimeException("Test grant type not implemented: $grantType"); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function validTokenResponse() |
|
81
|
|
|
{ |
|
82
|
|
|
$token = [ |
|
83
|
|
|
'access_token' => 'testToken', |
|
84
|
|
|
'refresh_token' => 'testRefreshTokenFromServer', |
|
85
|
|
|
'token_type' => 'bearer', |
|
86
|
|
|
]; |
|
87
|
|
|
|
|
88
|
|
|
if (isset($this->options['tokenExpires'])) { |
|
89
|
|
|
$token['expires'] = $this->options['tokenExpires']; |
|
90
|
|
|
} elseif (isset($this->options['tokenExpiresIn'])) { |
|
91
|
|
|
$token['expires_in'] = $this->options['tokenExpiresIn']; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return [ |
|
95
|
|
|
'status' => 200, |
|
96
|
|
|
'body' => json_encode($token), |
|
97
|
|
|
]; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param array $requestBody |
|
102
|
|
|
* |
|
103
|
|
|
* @return array |
|
104
|
|
|
* The response as expected by the MockHandler. |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function grantTypePassword(array $requestBody) |
|
107
|
|
|
{ |
|
108
|
|
|
if ($requestBody['username'] != 'validUsername' || $requestBody['password'] != 'validPassword') { |
|
109
|
|
|
// @todo correct response headers |
|
110
|
|
|
return ['status' => 401]; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $this->validTokenResponse(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param array $request |
|
118
|
|
|
* |
|
119
|
|
|
* @return array |
|
120
|
|
|
* The response as expected by the MockHandler. |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function grantTypeClientCredentials(array $request) |
|
123
|
|
|
{ |
|
124
|
|
|
if ($request['client']['auth'][1] != 'testSecret') { |
|
125
|
|
|
// @todo correct response headers |
|
126
|
|
|
return ['status' => 401]; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $this->validTokenResponse(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param array $requestBody |
|
134
|
|
|
* |
|
135
|
|
|
* @return array |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function grantTypeRefreshToken(array $requestBody) |
|
138
|
|
|
{ |
|
139
|
|
|
if ($requestBody['refresh_token'] != 'testRefreshToken') { |
|
140
|
|
|
return ['status' => 401]; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $this->validTokenResponse(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param array $requestBody |
|
148
|
|
|
* |
|
149
|
|
|
* @return array |
|
150
|
|
|
*/ |
|
151
|
|
|
protected function grantTypeJwtBearer(array $requestBody) |
|
152
|
|
|
{ |
|
153
|
|
|
if (!array_key_exists('assertion', $requestBody)) { |
|
154
|
|
|
return ['status' => 401]; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return $this->validTokenResponse(); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param array $request |
|
162
|
|
|
* |
|
163
|
|
|
* @return array |
|
164
|
|
|
*/ |
|
165
|
|
|
protected function mockApiCall(array $request) |
|
166
|
|
|
{ |
|
167
|
|
|
if (!isset($request['headers']['Authorization']) || $request['headers']['Authorization'][0] != 'Bearer testToken') { |
|
168
|
|
|
return ['status' => 401]; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return ['status' => 200, 'body' => json_encode('Hello World!')]; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.