|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Bluz PHP Team |
|
5
|
|
|
* @link https://github.com/bluzphp/skeleton |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace Application\Auth\Provider; |
|
11
|
|
|
|
|
12
|
|
|
use Application\Auth\Row; |
|
13
|
|
|
use Application\Auth\Table; |
|
14
|
|
|
use Application\Users\Row as User; |
|
15
|
|
|
use Bluz\Auth\AuthException; |
|
|
|
|
|
|
16
|
|
|
use Bluz\Db\Exception\DbException; |
|
|
|
|
|
|
17
|
|
|
use Bluz\Db\Exception\InvalidPrimaryKeyException; |
|
|
|
|
|
|
18
|
|
|
use Bluz\Db\Exception\TableNotFoundException; |
|
|
|
|
|
|
19
|
|
|
use Bluz\Proxy\Auth; |
|
|
|
|
|
|
20
|
|
|
use Bluz\Proxy\Response; |
|
|
|
|
|
|
21
|
|
|
use Exception; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Cookie Provider |
|
25
|
|
|
* |
|
26
|
|
|
* @package Application\Auth\Provider |
|
27
|
|
|
*/ |
|
28
|
|
|
class Cookie extends AbstractToken |
|
29
|
|
|
{ |
|
30
|
|
|
public const PROVIDER = Table::PROVIDER_COOKIE; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
* |
|
35
|
|
|
* @return Row |
|
36
|
|
|
* @throws AuthException |
|
37
|
|
|
* @throws DbException |
|
38
|
|
|
*/ |
|
39
|
|
|
protected static function verify(?Row $authRow): void |
|
40
|
|
|
{ |
|
41
|
|
|
if (!$authRow) { |
|
42
|
|
|
throw new AuthException('User can\'t login with cookies'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if (strtotime($authRow->expired) < time()) { |
|
46
|
|
|
self::remove($authRow->userId); |
|
47
|
|
|
throw new AuthException('Token has expired'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if ($authRow->token !== hash('md5', $authRow->token . $authRow->tokenSecret)) { |
|
51
|
|
|
throw new AuthException('Incorrect token'); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
* |
|
59
|
|
|
* @return Row |
|
60
|
|
|
* @throws DbException |
|
61
|
|
|
* @throws InvalidPrimaryKeyException |
|
62
|
|
|
* @throws TableNotFoundException |
|
63
|
|
|
* @throws Exception |
|
64
|
|
|
*/ |
|
65
|
|
|
public static function create(User $user): Row |
|
66
|
|
|
{ |
|
67
|
|
|
// remove old Auth record |
|
68
|
|
|
self::remove($user->id); |
|
69
|
|
|
|
|
70
|
|
|
$ttl = Auth::getInstance()->getOption('cookie', 'ttl'); |
|
71
|
|
|
|
|
72
|
|
|
// create new auth row |
|
73
|
|
|
$authRow = new Row(); |
|
74
|
|
|
|
|
75
|
|
|
$authRow->userId = $user->id; |
|
76
|
|
|
$authRow->foreignKey = $user->login; |
|
77
|
|
|
$authRow->provider = self::PROVIDER; |
|
78
|
|
|
$authRow->tokenType = Table::TYPE_ACCESS; |
|
79
|
|
|
$authRow->expired = gmdate('Y-m-d H:i:s', time() + $ttl); |
|
80
|
|
|
// generate secret part is not required |
|
81
|
|
|
// encrypt password and save as token |
|
82
|
|
|
$authRow->token = bin2hex(random_bytes(32)); |
|
83
|
|
|
|
|
84
|
|
|
$authRow->save(); |
|
85
|
|
|
|
|
86
|
|
|
// Not great, not terrible |
|
87
|
|
|
Response::setCookie('Auth-Token', $authRow->token, time() + $ttl); |
|
88
|
|
|
|
|
89
|
|
|
return $authRow; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths