Passed
Push — master ( e5b31a...cf340d )
by Arthur
04:48
created

Auth0AuthenticationMiddleware::handle()   A

Complexity

Conditions 4
Paths 10

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 10
nop 2
dl 0
loc 22
ccs 0
cts 17
cp 0
crap 20
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Modules\Auth0\Middleware;
4
5
use Auth0\Login\Contract\Auth0UserRepository;
0 ignored issues
show
Bug introduced by
The type Auth0\Login\Contract\Auth0UserRepository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Auth0\SDK\Exception\CoreException;
0 ignored issues
show
Bug introduced by
The type Auth0\SDK\Exception\CoreException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Auth0\SDK\Exception\InvalidTokenException;
0 ignored issues
show
Bug introduced by
The type Auth0\SDK\Exception\InvalidTokenException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Closure;
9
10
class Auth0AuthenticationMiddleware
11
{
12
    protected $auth0Service;
13
14
    /**
15
     * Auth0AuthenticationMiddleware constructor.
16
     */
17
    public function __construct(Auth0UserRepository $auth0Service)
18
    {
19
        $this->auth0Service = $auth0Service;
20
    }
21
22
    /**
23
     * Handle an incoming request.
24
     *
25
     * @param \Illuminate\Http\Request $request
26
     * @param \Closure                 $next
27
     *
28
     * @return mixed
29
     */
30
    public function handle($request, Closure $next)
31
    {
32
        $auth0 = \App::make('auth0');
33
34
        $accessToken = $request->bearerToken();
35
36
        try {
37
            $tokenInfo = $auth0->decodeJWT($accessToken);
38
            $user = $this->auth0Service->getUserByDecodedJWT($tokenInfo);
39
40
            if (!$user) {
41
                return response()->json(['error' => 'Unauthorized user.'], 401);
42
            }
43
44
            \Auth::login($user);
45
        } catch (InvalidTokenException $e) {
46
            return response()->json(['error' => 'Invalid or no token set.'], 401);
47
        } catch (CoreException $e) {
48
            return response()->json(['error' => $e->getMessage()], 401);
49
        }
50
51
        return $next($request);
52
    }
53
}
54