Skip to content

Instantly share code, notes, and snippets.

@gsomoza
Created March 3, 2017 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsomoza/41a32263b0cad61ecab2d959b79bc3d5 to your computer and use it in GitHub Desktop.
Save gsomoza/41a32263b0cad61ecab2d959b79bc3d5 to your computer and use it in GitHub Desktop.
<?php
interface RequestAuthorizer
{
public function __construct(AccessToken $accessToken);
public function isAuthorized(RequestInterface $request): bool;
public function authorize(RequestInterface $request): void;
}
class AccessTokenMiddleware
{
/** @var RequestAuthorizer **/
private $tokenService;
public function __invoke(callable $handler)
{
return function (RequestInterface $request, array $options) use ($handler) {
$request = $this->authorizeRequest($request);
return $handler($request, $options);
};
}
private function authorizeRequest(RequestInterface $request): RequestInterface
{
if ($this->shouldSkipAuthorizationForUrl($uri)
|| $this->tokenService->isAuthorized($request, $this->accessToken) // this is what changed
) {
return $request;
} else {
$this->checkAccessToken();
return $this->tokenService->authorize($request, $this->accessToken); // this also changed
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment