Passed
Push — bugfix/relatives_not_saving ( 6485fa...f1e1c5 )
by Tristan
13:39
created

OidConnectGuard::setProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Services\Auth;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Contracts\Auth\Guard;
7
use Illuminate\Contracts\Auth\UserProvider;
8
use Illuminate\Contracts\Auth\Authenticatable;
9
use Illuminate\Auth\AuthenticationException;
10
use App\Services\Auth\JwtValidator;
11
use App\Services\Auth\RequestTokenParser;
12
use App\Services\Auth\Contracts\TokenRefresher;
13
use App\Exceptions\Auth\TokenStorageException;
14
use App\Exceptions\Auth\TokenRequestException;
15
16
class OidConnectGuard implements Guard {
17
18
    protected $request;
1 ignored issue
show
introduced by
Property \App\Services\Auth\OidConnectGuard::$request does not have @var annotation.
Loading history...
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
19
    protected $provider;
1 ignored issue
show
introduced by
Property \App\Services\Auth\OidConnectGuard::$provider does not have @var annotation.
Loading history...
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
20
    protected $requestTokenParser;
1 ignored issue
show
introduced by
Property \App\Services\Auth\OidConnectGuard::$requestTokenParser does not have @var annotation.
Loading history...
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
21
    protected $jwtValidator;
1 ignored issue
show
introduced by
Property \App\Services\Auth\OidConnectGuard::$jwtValidator does not have @var annotation.
Loading history...
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
22
    protected $tokenRefresher;
1 ignored issue
show
introduced by
Property \App\Services\Auth\OidConnectGuard::$tokenRefresher does not have @var annotation.
Loading history...
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
23
24
    protected $user;
1 ignored issue
show
introduced by
Property \App\Services\Auth\OidConnectGuard::$user does not have @var annotation.
Loading history...
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
25
26
    /**
27
     * Set to true when user() has already ran once.
28
     * @var bool
0 ignored issues
show
Bug introduced by
Expected "boolean" but found "bool" for @var tag in member variable comment
Loading history...
29
     */
30
    protected $userAlreadyAttempted;
31
32
    /**
33
     * Create a new authentication guard.
34
     *
35
     *
36
     * @param UserProvider $provider
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
37
     * @param RequestTokenParser $requestTokenParser
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
38
     * @param JwtValidator $jwtValidator
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
39
     * @param TokenRefresher $tokenRefresher
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
40
     * @param Request $request
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 12 spaces after parameter type; 1 found
Loading history...
41
     */
42
    public function __construct(UserProvider $provider,
0 ignored issues
show
Coding Style introduced by
The first parameter of a multi-line function declaration must be on the line after the opening bracket
Loading history...
43
            RequestTokenParser $requestTokenParser,
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 12
Loading history...
44
            JwtValidator $jwtValidator,
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 12
Loading history...
45
            TokenRefresher $tokenRefresher,
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 12
Loading history...
46
            Request $request) {
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 12
Loading history...
Coding Style introduced by
The closing parenthesis of a multi-line function declaration must be on a new line
Loading history...
47
        $this->request = $request;
48
        $this->provider = $provider;
49
        $this->requestTokenParser = $requestTokenParser;
50
        $this->jwtValidator = $jwtValidator;
51
        $this->tokenRefresher = $tokenRefresher;
52
        $this->user = NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
53
        $this->userAlreadyAttempted = false;
54
    }
55
56
    /**
57
     * Determine if the current user is authenticated.
58
     *
59
     * @return bool
1 ignored issue
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
60
     */
61
    public function check() {
0 ignored issues
show
introduced by
Method \App\Services\Auth\OidConnectGuard::check() does not have return type hint for its return value but it should be possible to add it based on @return annotation "bool".
Loading history...
62
        return !is_null($this->user());
63
    }
64
65
    /**
66
     * Determine if the current user is a guest.
67
     *
68
     * @return bool
1 ignored issue
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
69
     */
70
    public function guest() {
0 ignored issues
show
introduced by
Method \App\Services\Auth\OidConnectGuard::guest() does not have return type hint for its return value but it should be possible to add it based on @return annotation "bool".
Loading history...
71
        return !$this->check();
72
    }
73
74
    /**
75
     * Get the ID for the currently authenticated user.
76
     *
77
     * @return int|null
1 ignored issue
show
Coding Style introduced by
Expected "integer|null" but found "int|null" for function return type
Loading history...
78
     */
79
    public function id()
0 ignored issues
show
introduced by
Method \App\Services\Auth\OidConnectGuard::id() does not have return type hint for its return value but it should be possible to add it based on @return annotation "int|null".
Loading history...
80
    {
81
        if ($this->user()) {
82
            return $this->user()->getAuthIdentifier();
83
        }
84
    }
85
86
    /**
87
     * Set the current user.
88
     *
89
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
90
     * @return void
91
     */
92
    public function setUser(Authenticatable $user)
0 ignored issues
show
introduced by
Method \App\Services\Auth\OidConnectGuard::setUser() does not have return type hint for its return value but it should be possible to add it based on @return annotation "void".
Loading history...
93
    {
94
        $this->user = $user;
95
    }
96
97
    public function user() {
1 ignored issue
show
introduced by
Method \App\Services\Auth\OidConnectGuard::user() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function user()
Loading history...
98
        // If we've already retrieved the user for the current request we can just
99
        // return it back immediately. We do not want to fetch the user data on
100
        // every call to this method because that would be tremendously slow.
101
        if (! is_null($this->user) || $this->userAlreadyAttempted) {
102
            return $this->user;
103
        }
104
105
        $this->userAlreadyAttempted = true;
106
        $user = null;
107
108
        try {
109
            $idToken = $this->requestTokenParser->parse($this->request);
110
        } catch (AuthenticationException $exception) {
111
            //Return a null user is enough, swallow the exception here
112
            return $user;
113
        }
114
115
        if (!$this->jwtValidator->claimsAreValid($idToken) ||
116
                !$this->jwtValidator->signatureIsValid($idToken)) {
117
            return $user;
118
        }
119
120
        //At this point, token is definitely valid
121
        if ($this->jwtValidator->isExpired($idToken)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
122
123
            $iss = $idToken->getClaim("iss");
124
            $sub = $idToken->getClaim("sub");
125
            try {
126
                $idToken = $this->tokenRefresher->refreshIDToken($iss, $sub);
127
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
128
            } catch (TokenStorageException $storageException) {
129
                //DO NOTHING
130
            } catch (TokenRequestException $requestException) {
131
                return $user;
132
            }
133
            $this->requestTokenParser->save($idToken);
134
        }
135
136
        $credentials = $idToken->getClaims();
137
138
        $user = $this->provider->retrieveByCredentials($credentials);
139
140
        $this->user = $user;
141
        return $user;
142
    }
143
144
    public function validate(array $credentials = array()) {
1 ignored issue
show
introduced by
Method \App\Services\Auth\OidConnectGuard::validate() does not have @param annotation for its traversable parameter $credentials.
Loading history...
introduced by
Method \App\Services\Auth\OidConnectGuard::validate() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function validate()
Loading history...
145
        if (empty($credentials['id_token'])) {
146
            return false;
147
        }
148
        $token = $this->requestTokenParser->parseFromString($credentials['id_token']);
0 ignored issues
show
Unused Code introduced by
The assignment to $token is dead and can be removed.
Loading history...
149
150
        return $this->jwtValidator->claimsAreValid($idToken) &&
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $idToken seems to be never defined.
Loading history...
151
            !$this->jwtValidator->isExpired($idToken) &&
152
            $this->jwtValidator->signatureIsValid($idToken);
153
    }
154
155
    /**
156
     * Get the user provider used by the guard.
157
     *
158
     * @return \Illuminate\Contracts\Auth\UserProvider
159
     */
160
    public function getProvider()
0 ignored issues
show
introduced by
Method \App\Services\Auth\OidConnectGuard::getProvider() does not have return type hint for its return value but it should be possible to add it based on @return annotation "\Illuminate\Contracts\Auth\UserProvider".
Loading history...
161
    {
162
        return $this->provider;
163
    }
164
165
    /**
166
     * Set the user provider used by the guard.
167
     *
168
     * @param  \Illuminate\Contracts\Auth\UserProvider  $provider
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
169
     * @return void
170
     */
171
    public function setProvider(UserProvider $provider)
0 ignored issues
show
introduced by
Method \App\Services\Auth\OidConnectGuard::setProvider() does not have return type hint for its return value but it should be possible to add it based on @return annotation "void".
Loading history...
172
    {
173
        $this->provider = $provider;
174
    }
175
176
    /**
177
     * Determine if the current user is authenticated.
178
     *
179
     * @return \Illuminate\Contracts\Auth\Authenticatable
180
     *
181
     * @throws \Illuminate\Auth\AuthenticationException
0 ignored issues
show
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
182
     */
183
    public function authenticate()
0 ignored issues
show
introduced by
Method \App\Services\Auth\OidConnectGuard::authenticate() does not have return type hint for its return value but it should be possible to add it based on @return annotation "\Illuminate\Contracts\Auth\Authenticatable".
Loading history...
184
    {
185
        if (! is_null($user = $this->user())) {
186
            return $user;
187
        }
188
189
        throw new AuthenticationException;
190
    }
191
}
192