AuthMiddleware   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 11

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 49 11
1
<?php
2
/**
3
 * This file handle authenticate middleware.
4
 *
5
 * @link       https://github.com/maab16
6
 * @since      1.0.0
7
 */
8
9
namespace WPB\App\Http\Middleware;
10
11
use Closure;
12
use CodexShaper\Database\Facades\Schema;
13
use CodexShaper\OAuth2\Server\Http\Requests\ServerRequest;
14
use CodexShaper\OAuth2\Server\Manager;
15
use Illuminate\Http\Request;
16
use League\OAuth2\Server\Exception\OAuthServerException;
17
use WPB\App\User;
18
19
/**
20
 * The auth middleware class.
21
 *
22
 * @since      1.0.0
23
 *
24
 * @author     Md Abu Ahsan basir <[email protected]>
25
 */
26
class AuthMiddleware
27
{
28
    /**
29
     * Handle an incoming request.
30
     *
31
     * @param \Illuminate\Http\Request $request   The app http request.
32
     * @param \Closure                 $next      The next closure.
33
     * @param array                    ...$guards The requested guards.
34
     *
35
     * @throws \Exception Throw the exception.
36
     *
37
     * @return mixed
38
     */
39
    public function handle(Request $request, Closure $next, ...$guards)
40
    {
41
        foreach ($guards as $guard) {
42
            if ($guard == 'api') {
43
                if (!Schema::hasTable('oauth_access_tokens') ||
44
                    !Schema::hasTable('oauth_refresh_tokens') ||
45
                    !Schema::hasTable('oauth_personal_access_clients') ||
46
                    !Schema::hasTable('oauth_clients') ||
47
                    !Schema::hasTable('oauth_auth_codes')
48
                ) {
49
                    throw new \Exception('Please install OAuth2 Server Plugin (plugin link) or Implement OAuth2 Server from this link (https://github.com/Codexshaper/oauth2)', 1);
50
                }
51
52
                $manager = new Manager();
53
                $resource_server = $manager->getResourceServer();
54
                $psr_request = ServerRequest::getPsrServerRequest();
55
56
                try {
57
                    $psr = $resource_server->validateAuthenticatedRequest($psr_request);
58
                    $user_id = $manager->validateUserForRequest($psr);
59
60
                    if ($user_id) {
61
                        $user = User::find($user_id);
62
63
                        $request->merge(['user' => $user]);
64
                        $request->merge(['scopes' => $psr->getAttribute('oauth_scopes')]);
65
66
                        $request->setUserResolver(
67
                            function () use ($user) {
68
                                return $user;
69
                            }
70
                        );
71
72
                        return $next($request);
73
                    }
74
                } catch (OAuthServerException $e) {
75
                    throw new \Exception($e->getMessage());
76
                }
77
78
                return $next($request);
79
            }
80
        }
81
82
        if (\is_user_logged_in()) {
0 ignored issues
show
Bug introduced by
The function is_user_logged_in was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

82
        if (/** @scrutinizer ignore-call */ \is_user_logged_in()) {
Loading history...
83
            return $next($request);
84
        }
85
86
        header('Location: '.\get_site_url().'/wp-admin');
0 ignored issues
show
Bug introduced by
The function get_site_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

86
        header('Location: './** @scrutinizer ignore-call */ \get_site_url().'/wp-admin');
Loading history...
87
        die();
88
    }
89
}
90