Created
March 3, 2017 20:49
-
-
Save gsomoza/41a32263b0cad61ecab2d959b79bc3d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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