1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Palladium\Mapper; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* SQL logic for authentication attempts using username/password |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
use Palladium\Component\DataMapper; |
10
|
|
|
use Palladium\Entity as Entity; |
11
|
|
|
use PDOStatement; |
12
|
|
|
use PDO; |
13
|
|
|
|
14
|
|
|
class StandardIdentity extends DataMapper |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param Entity\StandardIdentity $entity |
19
|
|
|
*/ |
20
|
1 |
|
public function exists(Entity\StandardIdentity $entity) |
21
|
|
|
{ |
22
|
|
|
$sql = "SELECT 1 |
23
|
1 |
|
FROM {$this->table} |
24
|
|
|
WHERE type = :type |
25
|
|
|
AND fingerprint = :fingerprint |
26
|
|
|
AND identifier = :identifier |
27
|
|
|
AND (expires_on IS NULL OR expires_on > :now)"; |
28
|
|
|
|
29
|
1 |
|
$statement = $this->connection->prepare($sql); |
30
|
|
|
|
31
|
1 |
|
$statement->bindValue(':type', Entity\StandardIdentity::TYPE_STANDARD); |
32
|
1 |
|
$statement->bindValue(':fingerprint', $entity->getFingerprint()); |
33
|
1 |
|
$statement->bindValue(':identifier', $entity->getIdentifier()); |
34
|
1 |
|
$statement->bindValue(':now', time()); |
35
|
|
|
|
36
|
1 |
|
$statement->execute(); |
37
|
1 |
|
$data = $statement->fetch(PDO::FETCH_ASSOC); |
38
|
|
|
|
39
|
1 |
|
return empty($data) === false; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Entity\StandardIdentity $entity |
45
|
|
|
*/ |
46
|
2 |
|
public function fetch(Entity\StandardIdentity $entity) |
47
|
|
|
{ |
48
|
2 |
|
if ($entity->getId()) { |
49
|
1 |
|
$this->fetchById($entity); |
50
|
1 |
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
$this->fetchByIdentifier($entity); |
54
|
1 |
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
1 |
|
private function fetchByIdentifier(Entity\StandardIdentity $entity) |
58
|
|
|
{ |
59
|
|
|
$sql = "SELECT identity_id AS id, |
60
|
|
|
account_id AS accountId, |
61
|
|
|
hash AS hash, |
62
|
|
|
status AS status, |
63
|
|
|
used_on AS lastUsed, |
64
|
|
|
token AS token, |
65
|
|
|
token_action AS tokenAction, |
66
|
|
|
token_expires_on AS tokenEndOfLife, |
67
|
|
|
token_payload AS tokenPayload |
68
|
1 |
|
FROM {$this->table} |
69
|
|
|
WHERE type = :type |
70
|
|
|
AND fingerprint = :fingerprint |
71
|
|
|
AND identifier = :identifier"; |
72
|
|
|
|
73
|
1 |
|
$statement = $this->connection->prepare($sql); |
74
|
|
|
|
75
|
1 |
|
$statement->bindValue(':type', $entity->getType()); |
76
|
1 |
|
$statement->bindValue(':identifier', $entity->getIdentifier()); |
77
|
1 |
|
$statement->bindValue(':fingerprint', $entity->getFingerprint()); |
78
|
|
|
|
79
|
1 |
|
$statement->execute(); |
80
|
|
|
|
81
|
1 |
|
$data = $statement->fetch(PDO::FETCH_ASSOC); |
82
|
|
|
|
83
|
1 |
|
if ($data) { |
84
|
1 |
|
if ($data['tokenPayload'] !== null) { |
85
|
1 |
|
$data['tokenPayload'] = json_decode($data['tokenPayload'], true); |
86
|
|
|
} |
87
|
1 |
|
$this->applyValues($entity, $data); |
88
|
|
|
} |
89
|
1 |
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
1 |
|
private function fetchById(Entity\StandardIdentity $entity) |
93
|
|
|
{ |
94
|
|
|
$sql = "SELECT identity_id AS id, |
95
|
|
|
identifier AS identifier, |
96
|
|
|
account_id AS accountId, |
97
|
|
|
hash AS hash, |
98
|
|
|
status AS status, |
99
|
|
|
used_on AS lastUsed, |
100
|
|
|
token AS token, |
101
|
|
|
token_action AS tokenAction, |
102
|
|
|
token_expires_on AS tokenEndOfLife, |
103
|
|
|
token_payload AS tokenPayload |
104
|
1 |
|
FROM {$this->table} |
105
|
|
|
WHERE type = :type |
106
|
|
|
AND identity_id = :id"; |
107
|
|
|
|
108
|
1 |
|
$statement = $this->connection->prepare($sql); |
109
|
|
|
|
110
|
1 |
|
$statement->bindValue(':type', $entity->getType()); |
111
|
1 |
|
$statement->bindValue(':id', $entity->getId()); |
112
|
|
|
|
113
|
1 |
|
$statement->execute(); |
114
|
|
|
|
115
|
1 |
|
$data = $statement->fetch(PDO::FETCH_ASSOC); |
116
|
|
|
|
117
|
1 |
|
if ($data) { |
118
|
1 |
|
if ($data['tokenPayload'] !== null) { |
119
|
1 |
|
$data['tokenPayload'] = json_decode($data['tokenPayload'], true); |
120
|
|
|
} |
121
|
1 |
|
$this->applyValues($entity, $data); |
122
|
|
|
} |
123
|
1 |
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param Entity\StandardIdentity $entity |
128
|
|
|
*/ |
129
|
3 |
|
public function store(Entity\StandardIdentity $entity) |
130
|
|
|
{ |
131
|
3 |
|
if ($entity->getId() === null) { |
132
|
1 |
|
$this->createIdentity($entity); |
133
|
1 |
|
return; |
134
|
|
|
} |
135
|
|
|
|
136
|
2 |
|
$this->updateIdentity($entity); |
137
|
2 |
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
1 |
|
private function createIdentity(Entity\StandardIdentity $entity) |
141
|
|
|
{ |
142
|
1 |
|
$sql = "INSERT INTO {$this->table} |
143
|
|
|
(type, status, identifier, fingerprint, hash, created_on, token, token_action, token_expires_on, token_payload) |
144
|
|
|
VALUES (:type, :status, :identifier, :fingerprint, :hash, :created, :token, :action, :token_eol, :payload)"; |
145
|
|
|
|
146
|
1 |
|
$statement = $this->connection->prepare($sql); |
147
|
|
|
|
148
|
1 |
|
$statement->bindValue(':type', Entity\StandardIdentity::TYPE_STANDARD); |
149
|
1 |
|
$statement->bindValue(':status', Entity\StandardIdentity::STATUS_NEW); |
150
|
1 |
|
$statement->bindValue(':identifier', $entity->getIdentifier()); |
151
|
1 |
|
$statement->bindValue(':fingerprint', $entity->getFingerprint()); |
152
|
1 |
|
$statement->bindValue(':hash', $entity->getHash()); |
153
|
1 |
|
$statement->bindValue(':created', time()); |
154
|
|
|
|
155
|
1 |
|
$this->bindToken($statement, $entity); |
156
|
|
|
|
157
|
1 |
|
$statement->execute(); |
158
|
|
|
|
159
|
1 |
|
$entity->setId($this->connection->lastInsertId()); |
160
|
1 |
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
2 |
|
private function updateIdentity(Entity\StandardIdentity $entity) |
164
|
|
|
{ |
165
|
2 |
|
$sql = "UPDATE {$this->table} |
166
|
|
|
SET hash = :hash, |
167
|
|
|
identifier = :identifier, |
168
|
|
|
fingerprint = :fingerprint, |
169
|
|
|
status = :status, |
170
|
|
|
used_on = :used, |
171
|
|
|
expires_on = :expires, |
172
|
|
|
token = :token, |
173
|
|
|
token_action = :action, |
174
|
|
|
token_expires_on = :token_eol, |
175
|
|
|
token_payload = :payload |
176
|
|
|
WHERE identity_id = :id"; |
177
|
|
|
|
178
|
2 |
|
$statement = $this->connection->prepare($sql); |
179
|
|
|
|
180
|
2 |
|
$statement->bindValue(':id', $entity->getId()); |
181
|
2 |
|
$statement->bindValue(':hash', $entity->getHash()); |
182
|
2 |
|
$statement->bindValue(':identifier', $entity->getIdentifier()); |
183
|
2 |
|
$statement->bindValue(':fingerprint', $entity->getFingerprint()); |
184
|
2 |
|
$statement->bindValue(':status', $entity->getStatus()); |
185
|
2 |
|
$statement->bindValue(':used', $entity->getLastUsed()); |
186
|
2 |
|
$statement->bindValue(':expires', $entity->getExpiresOn()); |
187
|
|
|
|
188
|
2 |
|
$this->bindToken($statement, $entity); |
189
|
|
|
|
190
|
2 |
|
$statement->execute(); |
191
|
2 |
|
} |
192
|
|
|
|
193
|
|
|
|
194
|
3 |
|
private function bindToken(PDOStatement $statement, Entity\StandardIdentity $entity) |
195
|
|
|
{ |
196
|
3 |
|
$statement->bindValue(':token', $entity->getToken()); |
197
|
3 |
|
$statement->bindValue(':action', $entity->getTokenAction()); |
198
|
3 |
|
$statement->bindValue(':token_eol', $entity->getTokenEndOfLife()); |
199
|
|
|
|
200
|
3 |
|
$payload = $entity->getTokenPayload(); |
201
|
3 |
|
if ($payload !== null) { |
202
|
1 |
|
$payload = json_encode($payload); |
203
|
|
|
} |
204
|
|
|
|
205
|
3 |
|
$statement->bindValue(':payload', $payload); |
206
|
3 |
|
} |
207
|
|
|
} |
208
|
|
|
|