Skip to content

Commit 3f70d82

Browse files
committed
Http: Refactor Server for clarity, compliance and consistency
- Rename: - `getProxyTls()` -> `proxyHasTls()` - `getProxyBasePath()` -> `getProxyPath()` - `getBaseUri()` -> `getUri()` (and return `Uri`, not `string`) - Remove superfluous `getScheme()` - Add: - `getLocalIpAddress()` - `getLocalPort()` (useful when port is dynamically allocated) - Allow `stop()` to be called when the server is not running - In `listen()`: - Replace `$callback` with `$listener`, which returns a `ServerResponse` instead of receiving control variables by reference - Keep listening for requests until a response has a return value unless a non-negative `$limit` is given - Add request target validity checks - Respond to invalid requests with "400 Bad Request" and don't throw the underlying exception by default - Fix issue where large responses might not be written in full - Throw `LogicException` when: - `getProxy*()` is called on an instance with no proxy - an assertion fails (instead of `HttpServerException`)
1 parent 3eb28ba commit 3f70d82

File tree

5 files changed

+441
-334
lines changed

5 files changed

+441
-334
lines changed

src/Toolkit/Http/OAuth2/OAuth2Client.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Salient\Curler\Curler;
1313
use Salient\Http\Message\Response;
1414
use Salient\Http\Server\Server;
15+
use Salient\Http\Server\ServerResponse;
1516
use Salient\Utility\Arr;
1617
use Salient\Utility\Get;
1718
use Salient\Utility\Json;
@@ -150,7 +151,7 @@ public function __construct()
150151
final protected function getRedirectUri(): ?string
151152
{
152153
return $this->Listener
153-
? sprintf('%s/oauth2/callback', $this->Listener->getBaseUri())
154+
? sprintf('%s/oauth2/callback', $this->Listener->getUri())
154155
: null;
155156
}
156157

@@ -314,8 +315,8 @@ private function authorizeWithAuthorizationCode(array $options = []): AccessToke
314315
Console::log('Follow the link to authorize access:', "\n$url");
315316
Console::info('Waiting for authorization');
316317
$code = $this->Listener->listen(
317-
fn(ServerRequestInterface $request, bool &$continue, &$return): Response =>
318-
$this->receiveAuthorizationCode($request, $continue, $return)
318+
fn(ServerRequestInterface $request): ServerResponse =>
319+
$this->receiveAuthorizationCode($request)
319320
);
320321
} finally {
321322
$this->Listener->stop();
@@ -333,35 +334,38 @@ private function authorizeWithAuthorizationCode(array $options = []): AccessToke
333334
}
334335

335336
/**
336-
* @param mixed $return
337+
* @return ServerResponse<string|null>
337338
*/
338-
private function receiveAuthorizationCode(ServerRequestInterface $request, bool &$continue, &$return): Response
339+
private function receiveAuthorizationCode(ServerRequestInterface $request): ServerResponse
339340
{
340341
if (
341342
Str::upper($request->getMethod()) !== ServerRequestInterface::METHOD_GET
342343
|| $request->getUri()->getPath() !== '/oauth2/callback'
343344
) {
344-
$continue = true;
345-
return new Response(400, 'Invalid request.');
345+
/** @var ServerResponse<string|null> */
346+
return new ServerResponse(400, 'Invalid request.');
346347
}
347348

348349
$state = Cache::getString("{$this->TokenKey}:state");
349350
Cache::delete("{$this->TokenKey}:state");
350351
parse_str($request->getUri()->getQuery(), $fields);
351352
$code = $fields['code'] ?? null;
352353

354+
$return = null;
353355
if (
354356
$state !== null
355357
&& $state === ($fields['state'] ?? null)
356358
&& $code !== null
357359
) {
358360
Console::debug('Authorization code received and verified');
361+
$response = new ServerResponse(200, 'Authorization received. You may now close this window.');
362+
/** @var string */
359363
$return = $code;
360-
return new Response(200, 'Authorization received. You may now close this window.');
364+
} else {
365+
Console::debug('Request did not provide a valid authorization code');
366+
$response = new ServerResponse(400, 'Invalid request. Please try again.');
361367
}
362-
363-
Console::debug('Request did not provide a valid authorization code');
364-
return new Response(400, 'Invalid request. Please try again.');
368+
return $response->withReturnValue($return);
365369
}
366370

367371
/**

0 commit comments

Comments
 (0)