From 6c787c374ca82438dc4ecc13155b0b8faca1c3b5 Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Sun, 17 Jan 2016 14:08:42 +0000 Subject: [PATCH] First commit of ResourceServerMiddleware --- src/Middleware/ResourceServerMiddleware.php | 54 +++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/Middleware/ResourceServerMiddleware.php diff --git a/src/Middleware/ResourceServerMiddleware.php b/src/Middleware/ResourceServerMiddleware.php new file mode 100644 index 00000000..b1f26bf2 --- /dev/null +++ b/src/Middleware/ResourceServerMiddleware.php @@ -0,0 +1,54 @@ +server = $server; + } + + /** + * @param \Psr\Http\Message\ServerRequestInterface $request + * @param \Psr\Http\Message\ResponseInterface $response + * @param callable $next + * + * @return \Psr\Http\Message\ResponseInterface + */ + public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) + { + if ($request->hasHeader('authorization') === false) { + $exception = OAuthServerException::accessDenied('Missing authorization header'); + + return $exception->generateHttpResponse($response); + } + + $request = $this->server->getDefaultResponseType()->determineAccessTokenInHeader($request); + + if ($request->getAttribute('oauth_access_token') === null) { + $exception = OAuthServerException::accessDenied('Access token was invalid'); + + return $exception->generateHttpResponse($response); + } + + // Pass the request and response on to the next responder in the chain + return $next($request, $response); + } +}