OAuth2Middleware   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 5
dl 0
loc 55
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 29 2
A convertToSymfonyResponse() 0 8 2
1
<?php
2
3
4
namespace Mvdstam\Oauth2ServerLaravel\Http\Middleware;
5
6
7
use Closure;
8
use Illuminate\Http\Request;
9
use League\OAuth2\Server\Exception\OAuthServerException;
10
use League\OAuth2\Server\Middleware\ResourceServerMiddleware;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\ServerRequestInterface;
13
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
14
use Symfony\Component\HttpFoundation\Response;
15
16
class OAuth2Middleware extends ResourceServerMiddleware
17
{
18
19
    /**
20
     * @param $request
21
     * @param Closure $next
22
     * @param string $scopes
23
     * @return Response
24
     */
25 7
    public function handle(/** @noinspection PhpUnusedParameterInspection */
26
        $request, Closure $next, $scopes = '')
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
27
    {
28 7
        return $this->convertToSymfonyResponse(
29 7
            $this(app(ServerRequestInterface::class), app(ResponseInterface::class), function(ServerRequestInterface $request, ResponseInterface $response) use ($next, $scopes) {
30 5
                $tokenScopes = explode(',', $request->getAttribute('oauth_scopes'));
31 5
                $routeScopes = explode('+', $scopes);
32
33
                /*
34
                 * Check if all scopes necessary for this route are present in
35
                 * the current token.
36
                 */
37 5
                if (array_diff($routeScopes, $tokenScopes)) {
38 1
                    return OAuthServerException::invalidScope(implode(' ', $tokenScopes))
39 1
                            ->generateHttpResponse($response);
40
                }
41
42
                // Store request in container
43 4
                app()->instance(ServerRequestInterface::class, $request);
44
45
                /*
46
                 * Normalize request and continue normal operation
47
                 */
48 4
                return $next(
49 4
                    Request::createFromBase((new HttpFoundationFactory)->createRequest($request))
50
                );
51 7
            })
52
        );
53
    }
54
55
    /**
56
     * Normalizes response objects to regular Symfony Response classes
57
     *
58
     * @param mixed $response
59
     * @return Response
60
     */
61 7
    protected function convertToSymfonyResponse($response)
62
    {
63 7
        if ( ! $response instanceof Response) {
64 4
            $response = (new HttpFoundationFactory)->createResponse($response);
65
        }
66
67 7
        return $response;
68
    }
69
70
}
71