|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Bluz PHP Team |
|
4
|
|
|
* @link https://github.com/bluzphp/skeleton |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace Application\Auth; |
|
10
|
|
|
|
|
11
|
|
|
use Application\Exception; |
|
12
|
|
|
use Application\Users\Table as UsersTable; |
|
13
|
|
|
use Bluz\Auth\AuthException; |
|
14
|
|
|
use Bluz\Proxy\Auth; |
|
15
|
|
|
use Bluz\Proxy\Response; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* CookieProvider |
|
19
|
|
|
* |
|
20
|
|
|
* @package Application\Auth |
|
21
|
|
|
* @author Anton Shevchuk |
|
22
|
|
|
*/ |
|
23
|
|
|
class CookieProvider |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Authenticate user by token |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $token |
|
29
|
|
|
* |
|
30
|
|
|
* @throws AuthException |
|
31
|
|
|
* @throws Exception |
|
32
|
|
|
*/ |
|
33
|
|
|
public static function authenticate($token) |
|
34
|
|
|
{ |
|
35
|
|
|
$authRow = self::verify($token); |
|
36
|
|
|
$user = UsersTable::findRow($authRow->userId); |
|
37
|
|
|
|
|
38
|
|
|
// try to login |
|
39
|
|
|
Table::tryLogin($user); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Check if supplied cookie is valid |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $token |
|
46
|
|
|
* |
|
47
|
|
|
* @return Row |
|
48
|
|
|
* @throws AuthException |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function verify($token) |
|
51
|
|
|
{ |
|
52
|
|
|
/* @var Row $authRow */ |
|
53
|
|
|
$authRow = Table::findRowWhere(['token' => $token, 'provider' => Table::PROVIDER_COOKIE]); |
|
54
|
|
|
|
|
55
|
|
|
if (!$authRow) { |
|
56
|
|
|
throw new AuthException('User can\'t login with cookies'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (strtotime($authRow->expired) < time()) { |
|
60
|
|
|
self::remove($authRow->userId); |
|
61
|
|
|
throw new AuthException('Token has expired'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if ($authRow->token != hash('md5', $token . $authRow->tokenSecret)) { |
|
65
|
|
|
throw new AuthException('Incorrect token'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $authRow; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Create and save Auth record, and send cookies |
|
73
|
|
|
* |
|
74
|
|
|
* @return Row |
|
75
|
|
|
* @throws AuthException |
|
76
|
|
|
* @throws Exception |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function create($user) |
|
79
|
|
|
{ |
|
80
|
|
|
// remove old Auth record |
|
81
|
|
|
self::remove($user->id); |
|
82
|
|
|
|
|
83
|
|
|
$ttl = Auth::getInstance()->getOption('cookie', 'ttl'); |
|
84
|
|
|
|
|
85
|
|
|
// create new auth row |
|
86
|
|
|
$row = new Row(); |
|
87
|
|
|
$row->userId = $user->id; |
|
88
|
|
|
$row->foreignKey = $user->login; |
|
89
|
|
|
$row->provider = Table::PROVIDER_COOKIE; |
|
90
|
|
|
$row->tokenType = Table::TYPE_ACCESS; |
|
91
|
|
|
$row->expired = gmdate('Y-m-d H:i:s', time() + $ttl); |
|
92
|
|
|
|
|
93
|
|
|
// generate secret part is not required |
|
94
|
|
|
// encrypt password and save as token |
|
95
|
|
|
$row->token = bin2hex(random_bytes(32)); |
|
96
|
|
|
$row->save(); |
|
97
|
|
|
|
|
98
|
|
|
Response::setCookie('aToken', $row->token, time() + $ttl); |
|
99
|
|
|
|
|
100
|
|
|
return $row; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Remove Auth record |
|
105
|
|
|
* |
|
106
|
|
|
* @param integer $id |
|
107
|
|
|
* |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
|
|
public static function remove($id) |
|
111
|
|
|
{ |
|
112
|
|
|
// clear previous generated Auth record |
|
113
|
|
|
// works with change password |
|
114
|
|
|
Table::delete( |
|
115
|
|
|
[ |
|
116
|
|
|
'userId' => $id, |
|
117
|
|
|
'provider' => Table::PROVIDER_COOKIE, |
|
118
|
|
|
'tokenType' => Table::TYPE_ACCESS |
|
119
|
|
|
] |
|
120
|
|
|
); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|