1 | <?php |
||
12 | class MockOAuth2Server |
||
13 | { |
||
14 | const KEY_TOKEN_EXPIRES_IN = 'tokenExpiresIn'; |
||
15 | const KEY_TOKEN_PATH = 'tokenPath'; |
||
16 | const KEY_TOKEN_INVALID_COUNT = 'tokenInvalidCount'; |
||
17 | const KEY_EXPECTED_QUERY_COUNT = 'expectedQueryCount'; |
||
18 | |||
19 | /** @var array */ |
||
20 | protected $options; |
||
21 | |||
22 | /** |
||
23 | * @var HandlerStack |
||
24 | */ |
||
25 | private $handlerStack; |
||
26 | |||
27 | /** |
||
28 | * @var int |
||
29 | */ |
||
30 | private $tokenInvalidCount = 0; |
||
31 | |||
32 | /** |
||
33 | * @param array $options |
||
34 | */ |
||
35 | public function __construct(array $options = []) |
||
59 | |||
60 | /** |
||
61 | * @return HandlerStack |
||
62 | */ |
||
63 | public function getHandlerStack() |
||
67 | |||
68 | /** |
||
69 | * @param RequestInterface $request |
||
70 | * @param array $options |
||
71 | * |
||
72 | * @return array |
||
73 | */ |
||
74 | protected function getResult(RequestInterface $request, array $options) |
||
75 | { |
||
76 | if ($request->getUri()->getPath() === $this->options[self::KEY_TOKEN_PATH]) { |
||
77 | $response = $this->oauth2Token($request, $options); |
||
78 | } elseif (strpos($request->getUri()->getPath(), '/api/') !== false) { |
||
79 | $response = $this->mockApiCall($request); |
||
80 | } |
||
81 | if (!isset($response)) { |
||
82 | throw new \RuntimeException("Mock server cannot handle given request URI"); |
||
83 | } |
||
84 | |||
85 | return $response; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param RequestInterface $request |
||
90 | * @param array $options |
||
91 | * |
||
92 | * @return array |
||
93 | */ |
||
94 | protected function oauth2Token(RequestInterface $request, $options) |
||
95 | { |
||
96 | $body = $request->getBody()->__toString(); |
||
97 | $requestBody = []; |
||
98 | parse_str($body, $requestBody); |
||
99 | $grantType = $requestBody['grant_type']; |
||
100 | switch ($grantType) { |
||
101 | case 'password': |
||
102 | return $this->grantTypePassword($requestBody); |
||
103 | |||
104 | case 'client_credentials': |
||
105 | return $this->grantTypeClientCredentials($options); |
||
106 | |||
107 | case 'refresh_token': |
||
108 | return $this->grantTypeRefreshToken($requestBody); |
||
109 | |||
110 | case 'urn:ietf:params:oauth:grant-type:jwt-bearer': |
||
111 | return $this->grantTypeJwtBearer($requestBody); |
||
112 | } |
||
113 | throw new \RuntimeException("Test grant type not implemented: $grantType"); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @return array |
||
118 | */ |
||
119 | protected function validTokenResponse() |
||
135 | |||
136 | /** |
||
137 | * @param array $requestBody |
||
138 | * |
||
139 | * @return array |
||
140 | * The response as expected by the MockHandler. |
||
141 | */ |
||
142 | protected function grantTypePassword(array $requestBody) |
||
143 | { |
||
144 | if ($requestBody['username'] != 'validUsername' || $requestBody['password'] != 'validPassword') { |
||
145 | // @todo correct response headers |
||
146 | return new Response(401); |
||
147 | } |
||
148 | |||
149 | return $this->validTokenResponse(); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param array $options |
||
154 | * |
||
155 | * @return array |
||
156 | * The response as expected by the MockHandler. |
||
157 | */ |
||
158 | protected function grantTypeClientCredentials(array $options) |
||
159 | { |
||
160 | if (!isset($options['auth']) || !isset($options['auth'][1]) || $options['auth'][1] != 'testSecret') { |
||
161 | // @todo correct response headers |
||
162 | return new Response(401); |
||
163 | } |
||
164 | |||
165 | return $this->validTokenResponse(); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param array $requestBody |
||
170 | * |
||
171 | * @return array |
||
172 | */ |
||
173 | protected function grantTypeRefreshToken(array $requestBody) |
||
181 | |||
182 | /** |
||
183 | * @param array $requestBody |
||
184 | * |
||
185 | * @return array |
||
186 | */ |
||
187 | protected function grantTypeJwtBearer(array $requestBody) |
||
195 | |||
196 | /** |
||
197 | * @param RequestInterface $request |
||
198 | * |
||
199 | * @return array |
||
200 | */ |
||
201 | protected function mockApiCall(RequestInterface $request) |
||
220 | } |
||
221 |