1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Auth; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Base; |
15
|
|
|
use Jitamin\Foundation\Security\PasswordAuthenticationProviderInterface; |
16
|
|
|
use Jitamin\Model\UserModel; |
17
|
|
|
use Jitamin\Services\Identity\DatabaseUserProvider; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* API Token Authentication Provider. |
21
|
|
|
*/ |
22
|
|
|
class ApiTokenAuth extends Base implements PasswordAuthenticationProviderInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* User properties. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $userInfo = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Username. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $username = ''; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Password. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $password = ''; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get authentication provider name. |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
public function getName() |
51
|
|
|
{ |
52
|
|
|
return 'API Access Token'; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Authenticate the user. |
57
|
|
|
* |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
public function authenticate() |
61
|
|
|
{ |
62
|
|
|
if (!isset($this->sessionStorage->scope) || $this->sessionStorage->scope !== 'API') { |
|
|
|
|
63
|
|
|
$this->logger->debug(__METHOD__.': Authentication provider skipped because invalid scope'); |
|
|
|
|
64
|
|
|
|
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$user = $this->db |
|
|
|
|
69
|
|
|
->table(UserModel::TABLE) |
70
|
|
|
->columns('id', 'password') |
71
|
|
|
->eq('username', $this->username) |
72
|
|
|
->eq('api_token', $this->password) |
73
|
|
|
->notNull('api_token') |
74
|
|
|
->eq('is_active', 1) |
75
|
|
|
->findOne(); |
76
|
|
|
|
77
|
|
|
if (!empty($user)) { |
78
|
|
|
$this->userInfo = $user; |
79
|
|
|
|
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get user object. |
88
|
|
|
* |
89
|
|
|
* @return Jitamin\Services\Identity\DatabaseUserProvider |
90
|
|
|
*/ |
91
|
|
|
public function getUser() |
92
|
|
|
{ |
93
|
|
|
if (empty($this->userInfo)) { |
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return new DatabaseUserProvider($this->userInfo); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Set username. |
102
|
|
|
* |
103
|
|
|
* @param string $username |
104
|
|
|
*/ |
105
|
|
|
public function setUsername($username) |
106
|
|
|
{ |
107
|
|
|
$this->username = $username; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Set password. |
112
|
|
|
* |
113
|
|
|
* @param string $password |
114
|
|
|
*/ |
115
|
|
|
public function setPassword($password) |
116
|
|
|
{ |
117
|
|
|
$this->password = $password; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.