|
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
|
|
|
const PROVIDER = Table::PROVIDER_EQUALS; |
|
25
|
|
|
|
|
26
|
|
|
public static function authenticate($username, $password = '') |
|
27
|
|
|
{ |
|
28
|
|
|
$authRow = self::verify($username, $password); |
|
29
|
|
|
$user = UsersTable::findRow($authRow->userId); |
|
30
|
|
|
|
|
31
|
|
|
// try to login |
|
32
|
|
|
Table::tryLogin($user); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public static function verify($username, $password = '') : Row |
|
36
|
|
|
{ |
|
37
|
|
|
/* @var Row $authRow */ |
|
38
|
|
|
$authRow = Table::findRowWhere(['foreignKey' => $username, 'provider' => Table::PROVIDER_EQUALS]); |
|
39
|
|
|
|
|
40
|
|
|
if (!$authRow) { |
|
41
|
|
|
throw new AuthException('User can\'t login with password'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// verify password |
|
45
|
|
|
if (!Table::verify($password, $authRow->token)) { |
|
46
|
|
|
throw new AuthException('Wrong password'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $authRow; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public static function create($user, $password = '') : Row |
|
53
|
|
|
{ |
|
54
|
|
|
// remove old Auth record |
|
55
|
|
|
self::remove($user->id); |
|
56
|
|
|
|
|
57
|
|
|
// create new auth row |
|
58
|
|
|
$row = new Row(); |
|
59
|
|
|
$row->userId = $user->id; |
|
60
|
|
|
$row->foreignKey = $user->login; |
|
61
|
|
|
$row->provider = Table::PROVIDER_EQUALS; |
|
62
|
|
|
$row->tokenType = Table::TYPE_ACCESS; |
|
63
|
|
|
|
|
64
|
|
|
// generate secret part is not required |
|
65
|
|
|
// encrypt password and save as token |
|
66
|
|
|
$row->token = Table::hash($password); |
|
67
|
|
|
$row->save(); |
|
68
|
|
|
|
|
69
|
|
|
return $row; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|