Passed
Push — master ( 54115d...67a0b3 )
by meta
02:30
created

OauthTokenGuard::__construct()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 5
nop 2
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Metaclassing\EnterpriseAuth\Middleware;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Contracts\Auth\Guard;
7
use phpDocumentor\Reflection\Types\Array_;
0 ignored issues
show
Bug introduced by
The type phpDocumentor\Reflection\Types\Array_ 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 Illuminate\Contracts\Auth\UserProvider;
9
use Illuminate\Contracts\Auth\Authenticatable;
10
11
class OauthTokenGuard implements Guard
12
{
13
    protected $request;
14
    protected $provider;
15
    protected $user;
16
17
    /**
18
     * Create a new authentication guard.
19
     *
20
     * @param  \Illuminate\Contracts\Auth\UserProvider  $provider
21
     * @param  \Illuminate\Http\Request  $request
22
     * @return void
23
     */
24
    public function __construct(UserProvider $provider, Request $request)
25
    {
26
        $this->request = $request;
27
        $this->provider = $provider;
28
        $this->user = null;
29
30
        // use the API auth controller helper functions to check the user creds
31
        $apiAuthController = new ApiAuthController();
0 ignored issues
show
Bug introduced by
The type Metaclassing\EnterpriseA...eware\ApiAuthController 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...
32
        $oauthAccessToken = $apiAuthController->extractOauthAccessTokenFromRequest($request);
33
34
        // Check the cache to see if this is a previously authenticated oauth access token
35
        $key = '/oauth/tokens/'.$oauthAccessToken;
36
        if ($oauthAccessToken && \Cache::has($key)) {
37
            $this->user = \Cache::get($key);
38
        } else {
39
            // Check to see if they have newly authenticated with an oauth access token
40
            try {
41
                $this->user = $apiAuthController->validateOauthCreateOrUpdateUserAndGroups($oauthAccessToken);
42
            } catch (\Exception $e) {
43
                //echo 'token auth error: '.$e->getMessage();
44
            }
45
            // Finally check to see if they are authenticated via certificate
46
            try {
47
                $this->user = $apiAuthController->certAuth();
48
            } catch (\Exception $e) {
49
                //echo 'cert auth error: '.$e->getMessage();
50
            }
51
        }
52
    }
53
54
    /**
55
     * Determine if the current user is authenticated.
56
     *
57
     * @return bool
58
     */
59
    public function check()
60
    {
61
        return ! is_null($this->user());
62
    }
63
64
    /**
65
     * Determine if the current user is a guest.
66
     *
67
     * @return bool
68
     */
69
    public function guest()
70
    {
71
        return ! $this->check();
72
    }
73
74
    /**
75
     * Get the currently authenticated user.
76
     *
77
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
78
     */
79
    public function user()
80
    {
81
        if (! is_null($this->user)) {
82
            return $this->user;
83
        }
84
    }
85
86
    /**
87
     * Get the JSON params from the current request.
88
     *
89
     * @return string
90
     */
91
    /*
92
        public function getJsonParams()
93
        {
94
            $jsondata = $this->request->query('jsondata');
95
96
            return (!empty($jsondata) ? json_decode($jsondata, TRUE) : NULL);
97
        }
98
    /**/
99
100
    /**
101
     * Get the ID for the currently authenticated user.
102
     *
103
     * @return string|null
104
     */
105
    public function id()
106
    {
107
        if ($user = $this->user()) {
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
108
            return $this->user()->getAuthIdentifier();
109
        }
110
    }
111
112
    /**
113
     * Validate a user's credentials.
114
     *
115
     * @return bool
116
     */
117
    public function validate(array $credentials = [])
118
    {
119
        return is_null($this->user);
120
    }
121
122
    /**
123
     * Set the current user.
124
     *
125
     * @param  array $user User info
126
     * @return void
127
     */
128
    public function setUser(Authenticatable $user)
129
    {
130
        $this->user = $user;
131
132
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Metaclassing\EnterpriseA...dleware\OauthTokenGuard which is incompatible with the documented return type void.
Loading history...
133
    }
134
}
135