|
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\Provider; |
|
10
|
|
|
|
|
11
|
|
|
use Application\Auth\Row; |
|
12
|
|
|
use Application\Auth\Table; |
|
13
|
|
|
use Application\Users\Table as UsersTable; |
|
14
|
|
|
use Bluz\Auth\AuthException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Equals Provider |
|
18
|
|
|
* |
|
19
|
|
|
* @package Application\Auth |
|
20
|
|
|
* @author Anton Shevchuk |
|
21
|
|
|
*/ |
|
22
|
|
|
class Equals extends AbstractProvider |
|
23
|
|
|
{ |
|
24
|
|
|
public const PROVIDER = Table::PROVIDER_EQUALS; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $username |
|
28
|
|
|
* @param string $password |
|
29
|
|
|
* |
|
30
|
|
|
* @throws AuthException |
|
31
|
|
|
* @throws \Bluz\Db\Exception\DbException |
|
32
|
|
|
* @throws \Bluz\Db\Exception\InvalidPrimaryKeyException |
|
33
|
|
|
* @throws \Application\Exception |
|
34
|
|
|
*/ |
|
35
|
2 |
|
public static function authenticate($username, $password = ''): void |
|
36
|
|
|
{ |
|
37
|
2 |
|
$authRow = self::verify($username, $password); |
|
38
|
1 |
|
$user = UsersTable::findRow($authRow->userId); |
|
39
|
|
|
|
|
40
|
|
|
// try to login |
|
41
|
1 |
|
Table::tryLogin($user); |
|
42
|
1 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param string $username |
|
46
|
|
|
* @param string $password |
|
47
|
|
|
* |
|
48
|
|
|
* @return Row |
|
49
|
|
|
* @throws AuthException |
|
50
|
|
|
* @throws \Application\Exception |
|
51
|
|
|
* @throws \Bluz\Db\Exception\DbException |
|
52
|
|
|
*/ |
|
53
|
4 |
|
public static function verify($username, $password = ''): Row |
|
54
|
|
|
{ |
|
55
|
|
|
/* @var Row $authRow */ |
|
56
|
4 |
|
$authRow = Table::findRowWhere(['foreignKey' => $username, 'provider' => Table::PROVIDER_EQUALS]); |
|
57
|
|
|
|
|
58
|
4 |
|
if (!$authRow) { |
|
59
|
|
|
throw new AuthException('User can\'t login with password'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// verify password |
|
63
|
4 |
|
if (!Table::verify($password, $authRow->token)) { |
|
64
|
2 |
|
throw new AuthException('Wrong password'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
return $authRow; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param $user |
|
72
|
|
|
* @param string $password |
|
73
|
|
|
* |
|
74
|
|
|
* @return Row |
|
75
|
|
|
* @throws \Application\Exception |
|
76
|
|
|
* @throws \Bluz\Db\Exception\DbException |
|
77
|
|
|
* @throws \Bluz\Db\Exception\InvalidPrimaryKeyException |
|
78
|
|
|
* @throws \Bluz\Db\Exception\TableNotFoundException |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function create($user, $password = ''): Row |
|
81
|
|
|
{ |
|
82
|
|
|
// remove old Auth record |
|
83
|
|
|
self::remove($user->id); |
|
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_EQUALS; |
|
90
|
|
|
$row->tokenType = Table::TYPE_ACCESS; |
|
91
|
|
|
|
|
92
|
|
|
// generate secret part is not required |
|
93
|
|
|
// encrypt password and save as token |
|
94
|
|
|
$row->token = Table::hash($password); |
|
95
|
|
|
$row->save(); |
|
96
|
|
|
|
|
97
|
|
|
return $row; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|