GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( a72aee...8d5499 )
by Steeven
04:03 queued 12s
created

User::logout()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
cc 2
eloc 3
c 4
b 1
f 1
nc 2
nop 0
dl 0
loc 6
rs 10
1
<?php
2
/**
3
 * This file is part of the O2System Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Security\Authentication;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Cache\Item;
0 ignored issues
show
Bug introduced by
The type O2System\Cache\Item 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...
19
use O2System\Spl\Traits\Collectors\ConfigCollectorTrait;
20
use Psr\Cache\CacheItemPoolInterface;
21
22
/**
23
 * Class User
24
 * @package O2System\Security\Authentication
25
 */
26
class User
27
{
28
    use ConfigCollectorTrait;
29
30
    // ------------------------------------------------------------------------
31
32
    /**
33
     * User::__construct
34
     */
35
    public function __construct()
36
    {
37
        $this->setConfig([
38
            'password'    => [
39
                'algorithm' => PASSWORD_DEFAULT,
40
                'options'   => [],
41
            ],
42
            'msisdnRegex' => '/^\+[1-9]{1}[0-9]{3,14}$/',
43
            'maxAttempts' => 5,
44
            'sso'         => [
45
                'enable' => false,
46
                'server' => base_url(),
0 ignored issues
show
Bug introduced by
The function base_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

46
                'server' => /** @scrutinizer ignore-call */ base_url(),
Loading history...
47
            ],
48
        ]);
49
    }
50
51
    // ------------------------------------------------------------------------
52
53
    /**
54
     * User::setPasswordAlgorithm
55
     *
56
     * @param $algorithm
57
     *
58
     * @return static
59
     */
60
    public function setPasswordAlgorithm($algorithm)
61
    {
62
        if (in_array($algorithm, [PASSWORD_DEFAULT, PASSWORD_BCRYPT, PASSWORD_ARGON2I])) {
63
            $this->algorithm = $algorithm;
0 ignored issues
show
Bug Best Practice introduced by
The property algorithm does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
64
        }
65
66
        return $this;
67
    }
68
69
    // ------------------------------------------------------------------------
70
71
    /**
72
     * User::setPasswordOptions
73
     *
74
     * @param array $options
75
     *
76
     * @return static
77
     */
78
    public function setPasswordOptions(array $options)
79
    {
80
        $this->options = $options;
0 ignored issues
show
Bug Best Practice introduced by
The property options does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
81
82
        return $this;
83
    }
84
85
    // ------------------------------------------------------------------------
86
87
    /**
88
     * User::passwordRehash
89
     *
90
     * @param string $password
91
     *
92
     * @return bool|string
93
     */
94
    public function passwordRehash($password)
95
    {
96
        if (password_needs_rehash(
97
            $password,
98
            $this->config[ 'password' ][ 'algorithm' ],
99
            $this->config[ 'password' ][ 'options' ]
100
        )) {
101
            return $this->passwordHash($password);
102
        }
103
104
        return $password;
105
    }
106
107
    // ------------------------------------------------------------------------
108
109
    /**
110
     * User::passwordHash
111
     *
112
     * @param string $password
113
     *
114
     * @return bool|string
115
     */
116
    public function passwordHash($password)
117
    {
118
        return password_hash(
119
            $password,
120
            $this->config[ 'password' ][ 'algorithm' ],
121
            $this->config[ 'password' ][ 'options' ]
122
        );
123
    }
124
125
    // ------------------------------------------------------------------------
126
127
    /**
128
     * User::passwordVerify
129
     *
130
     * @param string $password
131
     * @param string $hash
132
     *
133
     * @return bool
134
     */
135
    public function passwordVerify($password, $hash)
136
    {
137
        return password_verify($password, $hash);
138
    }
139
140
    // ------------------------------------------------------------------------
141
142
    /**
143
     * User::attempt
144
     */
145
    public function attempt()
146
    {
147
        $_SESSION[ 'userAttempts' ] = $this->getAttempts() + 1;
148
    }
149
150
    // ------------------------------------------------------------------------
151
152
    /**
153
     * User::getAttempt
154
     *
155
     * @return int
156
     */
157
    public function getAttempts()
158
    {
159
        $currentAttempts = 0;
160
        if (isset($_SESSION[ 'userAttempts' ])) {
161
            $currentAttempts = (int)$_SESSION[ 'userAttempts' ];
162
        }
163
164
        return (int)$currentAttempts;
165
    }
166
167
    // ------------------------------------------------------------------------
168
169
    /**
170
     * User::login
171
     *
172
     * @param array $account
173
     */
174
    public function login(array $account)
175
    {
176
        $_SESSION[ 'account' ] = $account;
177
        unset($_SESSION[ 'userAttempts' ]);
178
    }
179
180
    // ------------------------------------------------------------------------
181
182
    /**
183
     * User::signOn
184
     *
185
     * @param array $account
186
     *
187
     * @throws \Exception
188
     */
189
    public function signOn(array $account)
190
    {
191
        $cacheItemPool = $this->getCacheItemPool();
192
        $virtualUserId = md5(json_encode($account) . mt_srand() . time());
0 ignored issues
show
Bug introduced by
Are you sure mt_srand() of type void can be used in concatenation? ( Ignorable by Annotation )

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

192
        $virtualUserId = md5(json_encode($account) . /** @scrutinizer ignore-type */ mt_srand() . time());
Loading history...
Bug introduced by
Are you sure the usage of mt_srand() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
193
        $cacheItemPool->save(new Item('sso-' . $virtualUserId, $account, false));
194
195
        set_cookie('ssid', $virtualUserId);
196
    }
197
198
    // ------------------------------------------------------------------------
199
200
    /**
201
     * User::getCacheItemPool
202
     *
203
     * @return CacheItemPoolInterface
204
     */
205
    protected function getCacheItemPool()
206
    {
207
        $cacheItemPool = cache()->getObject('default');
0 ignored issues
show
Bug introduced by
The function cache 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

207
        $cacheItemPool = /** @scrutinizer ignore-call */ cache()->getObject('default');
Loading history...
208
209
        if (cache()->exists('sso')) {
210
            $cacheItemPool = cache()->getObject('sso');
211
        }
212
213
        return $cacheItemPool;
214
    }
215
216
    // ------------------------------------------------------------------------
217
218
    /**
219
     * User::loggedIn
220
     *
221
     * @return bool
222
     * @throws \Psr\Cache\InvalidArgumentException
223
     */
224
    public function loggedIn()
225
    {
226
        if (isset($_SESSION[ 'account' ])) {
227
            return true;
228
        } elseif($this->tokenOn()) {
229
            return true;
230
        } elseif ($this->signedOn()) {
231
            return true;
232
        }
233
234
        return false;
235
    }
236
237
    // ------------------------------------------------------------------------
238
239
    /**
240
     * User::tokenOn
241
     */
242
    public function tokenOn()
243
    {
244
        if(false !== ($token = input()->bearerToken())) {
0 ignored issues
show
introduced by
The condition false !== $token = input()->bearerToken() is always true.
Loading history...
Bug introduced by
The method bearerToken() does not exist on O2System\Kernel\Cli\Input. ( Ignorable by Annotation )

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

244
        if(false !== ($token = input()->/** @scrutinizer ignore-call */ bearerToken())) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
245
            $_SESSION['account'] = (new JsonWebToken())->decode($token);
246
247
            globals()->store('account', $_SESSION['account']);
0 ignored issues
show
Bug introduced by
The function globals 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

247
            /** @scrutinizer ignore-call */ 
248
            globals()->store('account', $_SESSION['account']);
Loading history...
248
249
            return true;
250
        }
251
252
        return false;
253
    }
254
255
    // ------------------------------------------------------------------------
256
257
    /**
258
     * User::signedOn
259
     *
260
     * @return bool
261
     * @throws \Psr\Cache\InvalidArgumentException
262
     */
263
    public function signedOn()
264
    {
265
        if ($virtualUserId = input()->cookie('ssid')) {
266
            $cacheItemPool = $this->getCacheItemPool();
267
268
            if($cacheItemPool->hasItem('sso-' . $virtualUserId)) {
269
270
                $item = $cacheItemPool->getItem('sso-' . input()->cookie('ssid'));
271
                $_SESSION['account'] = $item->get();
272
273
                globals()->store('account', $_SESSION['account']);
0 ignored issues
show
Bug introduced by
The function globals 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

273
                /** @scrutinizer ignore-call */ 
274
                globals()->store('account', $_SESSION['account']);
Loading history...
274
275
                return true;
276
            }
277
        }
278
279
        return false;
280
    }
281
282
    // ------------------------------------------------------------------------
283
284
    /**
285
     * User::logout
286
     */
287
    public function logout()
288
    {
289
        $this->signOff();
290
291
        if (isset($_SESSION[ 'account' ])) {
292
            unset($_SESSION[ 'account' ]);
293
        }
294
    }
295
296
    // ------------------------------------------------------------------------
297
298
    /**
299
     * User::signOff
300
     *
301
     * @throws \Psr\Cache\InvalidArgumentException
302
     */
303
    public function signOff()
304
    {
305
        if ($virtualUserId = input()->cookie('ssid')) {
306
            $cacheItemPool = $this->getCacheItemPool();
307
            $cacheItemPool->deleteItem('sso-' . $virtualUserId);
308
            delete_cookie('ssid');
309
        }
310
    }
311
}