<commit_before><?php

namespace League\OAuth2\Server\Middleware;

use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Server;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class ResourceServerMiddleware
{
    /**
     * @var \League\OAuth2\Server\Server
     */
    private $server;


    /**
     * ResourceServerMiddleware constructor.
     *
     * @param \League\OAuth2\Server\Server $server
     */
    public function __construct(Server $server)
    {
        $this->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->getResponseType()->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);
    }
}<commit_msg>Use the error returned from the response type<commit_after><?php

namespace League\OAuth2\Server\Middleware;

use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Server;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class ResourceServerMiddleware
{
    /**
     * @var \League\OAuth2\Server\Server
     */
    private $server;


    /**
     * ResourceServerMiddleware constructor.
     *
     * @param \League\OAuth2\Server\Server $server
     */
    public function __construct(Server $server)
    {
        $this->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->getResponseType()->determineAccessTokenInHeader($request);

        if ($request->getAttribute('oauth_access_token') === null) {
            $exception = OAuthServerException::accessDenied($request->getAttribute('oauth_access_token_error'));

            return $exception->generateHttpResponse($response);
        }

        // Pass the request and response on to the next responder in the chain
        return $next($request, $response);
    }
}
{'fraction_non_alphanumeric': 0.11446503287507472, 'fraction_numerical': 0.002988643156007173, 'mean_word_length': 2.882830626450116, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4639904', 'n_tokens_mistral': 894, 'n_tokens_neox': 881, 'n_words': 172}