aboutsummaryrefslogtreecommitdiffhomepage
path: root/infrastructure/Auth/Middleware/AccessTokenChecker.php
blob: f79f5cb6aab61fe14b940fca500cb2d409343af5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php

namespace Infrastructure\Auth\Middleware;

use Closure;
use Illuminate\Foundation\Application;
use Illuminate\Auth\Middleware\Authenticate;
use Illuminate\Auth\AuthenticationException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

class AccessTokenChecker
{
    private $app;

    private $oAuthMiddleware;

    public function __construct(
        Application $app,
        Authenticate $authenticate
    ) {
        $this->app = $app;
        $this->authenticate = $authenticate;
    }

    public function handle($request, Closure $next, $scopesString = null)
    {
        if ($this->app->environment() !== 'testing') {
            try {
                return $this->authenticate->handle($request, $next, 'api');
            } catch (AuthenticationException $e) {
                throw new UnauthorizedHttpException('Challenge');
            }
        }

        return $next($request);
    }
}