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

Auth0AuthenticationMiddleware   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 42
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handle() 0 22 4
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