JWTAuth::handle()   B
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 2
nop 2
dl 0
loc 29
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace AtlassianConnectCore\Http\Middleware;
4
5
use AtlassianConnectCore\Services\TenantService;
6
use Illuminate\Support\Facades\Auth;
7
8
/**
9
 * Class JWTAuth
10
 *
11
 * @package AtlassianConnectCore\Http\Middleware
12
 */
13
class JWTAuth
14
{
15
    /**
16
     * @var TenantService
17
     */
18
    protected $tenantService;
19
20
    /**
21
     * JWTAuth constructor.
22
     *
23
     * @param TenantService $tenantService
24
     */
25
    public function __construct(TenantService $tenantService)
26
    {
27
        $this->tenantService = $tenantService;
28
    }
29
30
    /**
31
     * Handle an incoming request.
32
     *
33
     * @param  \Illuminate\Http\Request  $request
34
     * @param  \Closure  $next
35
     *
36
     * @throws \Illuminate\Validation\UnauthorizedException
37
     *
38
     * @return mixed
39
     */
40
    public function handle($request, \Closure $next)
41
    {
42
        // If we have add-on running locally we don't need to sign all requests with JWT token
43
        // Of course you can provide it if you want. Otherwise request will be signed automatically
44
        $jwt = request('jwt', request()->header('Authorization'));
45
46
        if(app()->isLocal() && !$jwt) {
0 ignored issues
show
Bug introduced by
The method isLocal() does not exist on Illuminate\Container\Container. It seems like you code against a sub-type of Illuminate\Container\Container such as Illuminate\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        if(app()->/** @scrutinizer ignore-call */ isLocal() && !$jwt) {
Loading history...
47
            if(!$tenant = $this->tenantService->dummy()) {
48
                throw new \Illuminate\Validation\UnauthorizedException(
49
                    'You should have at least one dummy tenant to get it working locally'
50
                );
51
            }
52
53
            $jwt = \AtlassianConnectCore\Helpers\JWTHelper::create(
54
                $request->url(),
55
                $request->method(),
56
                $tenant->client_key,
57
                $tenant->shared_secret
58
            );
59
60
            $request->query->add(['jwt' => $jwt]);
61
        }
62
63
        // Authenticate user
64
        if(!Auth::attempt()) {
65
            throw new \Illuminate\Validation\UnauthorizedException();
66
        }
67
68
        return $next($request);
69
    }
70
}
71