1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Palladium\Service; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Code for creating new identities |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
use Palladium\Entity as Entity; |
10
|
|
|
use Palladium\Component\DataMapper; |
11
|
|
|
use Palladium\Exception\IdentityConflict; |
12
|
|
|
use Palladium\Repository\Identity as Repository; |
13
|
|
|
use Psr\Log\LoggerInterface; |
14
|
|
|
|
15
|
|
|
class Registration |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
const DEFAULT_TOKEN_LIFESPAN = 28800; // 8 hours |
19
|
|
|
const DEFAULT_NONCE_LIFESPAN = 7200; // 2 hours |
20
|
|
|
const DEFAULT_HASH_COST = 12; |
21
|
|
|
|
22
|
|
|
private $repository; |
23
|
|
|
private $accountMapper; |
24
|
|
|
private $logger; |
25
|
|
|
private $hashCost; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param Repository $repository Repository for abstracting persistence layer structures |
29
|
|
|
* @param LoggerInterface $logger PSR-3 compatible logger |
30
|
|
|
* @param int $hashCost Optional value for setting the cost of hashing algorythm (default: 12) |
31
|
|
|
*/ |
32
|
8 |
|
public function __construct(Repository $repository, DataMapper $accountMapper, LoggerInterface $logger, $hashCost = Registration::DEFAULT_HASH_COST) |
33
|
|
|
{ |
34
|
8 |
|
$this->repository = $repository; |
35
|
8 |
|
$this->accountMapper = $accountMapper; |
36
|
8 |
|
$this->logger = $logger; |
37
|
8 |
|
$this->hashCost = $hashCost; |
38
|
8 |
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @throws IdentityConflict if attempting to register a new identity, with the same identifier |
43
|
|
|
*/ |
44
|
5 |
|
public function createStandardIdentity(string $identifier, string $password, int $tokenLifespan = Registration::DEFAULT_TOKEN_LIFESPAN): Entity\StandardIdentity |
45
|
|
|
{ |
46
|
5 |
|
$identity = new Entity\StandardIdentity; |
47
|
|
|
|
48
|
5 |
|
$identity->setIdentifier($identifier); |
49
|
5 |
|
$identity->setPassword($password, $this->hashCost); |
50
|
5 |
|
$identity->setTokenEndOfLife(time() + $tokenLifespan); |
51
|
|
|
|
52
|
5 |
|
$this->prepareNewIdentity($identity); |
53
|
|
|
|
54
|
5 |
|
if ($this->repository->has($identity)) { |
55
|
3 |
|
$this->logger->notice('identifier already registered', [ |
56
|
|
|
'input' => [ |
57
|
3 |
|
'identifier' => $identifier, |
58
|
|
|
], |
59
|
|
|
]); |
60
|
|
|
|
61
|
3 |
|
throw new IdentityConflict; |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
$this->repository->save($identity); |
65
|
|
|
|
66
|
2 |
|
return $identity; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
1 |
|
public function createNonceIdentity($accountId, $identityLifespan = Registration::DEFAULT_NONCE_LIFESPAN): Entity\NonceIdentity |
71
|
|
|
{ |
72
|
1 |
|
$identity = new Entity\NonceIdentity; |
73
|
|
|
|
74
|
1 |
|
$identity->setAccountId($accountId); |
75
|
1 |
|
$identity->setExpiresOn(time() + $identityLifespan); |
76
|
1 |
|
$identity->setStatus(Entity\Identity::STATUS_ACTIVE); |
77
|
1 |
|
$identity->generateNewNonce(); |
78
|
1 |
|
$identity->generateNewKey($this->hashCost); |
79
|
|
|
|
80
|
1 |
|
$this->repository->save($identity); |
81
|
|
|
|
82
|
1 |
|
$this->logger->info('new single-use identity created', [ |
83
|
|
|
'user' => [ |
84
|
1 |
|
'account' => $identity->getAccountId(), |
85
|
1 |
|
'identity' => $identity->getId(), |
86
|
|
|
], |
87
|
|
|
]); |
88
|
|
|
|
89
|
1 |
|
return $identity; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
5 |
|
private function prepareNewIdentity(Entity\StandardIdentity $identity) |
94
|
|
|
{ |
95
|
5 |
|
$identity->setStatus(Entity\Identity::STATUS_NEW); |
96
|
5 |
|
$identity->generateToken(); |
97
|
5 |
|
$identity->setTokenAction(Entity\Identity::ACTION_VERIFY); |
98
|
5 |
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
2 |
|
public function bindAccountToIdentity(int $accountId, Entity\Identity $identity) |
102
|
|
|
{ |
103
|
2 |
|
$identity->setAccountId($accountId); |
104
|
2 |
|
$this->accountMapper->store($identity); |
|
|
|
|
105
|
|
|
|
106
|
2 |
|
$this->logger->info('new identifier registered', [ |
107
|
|
|
'user' => [ |
108
|
2 |
|
'account' => $identity->getAccountId(), |
109
|
2 |
|
'identity' => $identity->getId(), |
110
|
|
|
], |
111
|
|
|
]); |
112
|
2 |
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
1 |
|
public function verifyStandardIdentity(Entity\StandardIdentity $identity) |
116
|
|
|
{ |
117
|
1 |
|
$identity->setStatus(Entity\Identity::STATUS_ACTIVE); |
118
|
1 |
|
$identity->clearToken(); |
119
|
|
|
|
120
|
1 |
|
$this->repository->save($identity); |
121
|
|
|
|
122
|
1 |
|
$this->logger->info('identity verified', [ |
123
|
|
|
'input' => [ |
124
|
1 |
|
'token' => $identity->getToken(), |
|
|
|
|
125
|
|
|
], |
126
|
|
|
'user' => [ |
127
|
1 |
|
'account' => $identity->getAccountId(), |
128
|
1 |
|
'identity' => $identity->getId(), |
129
|
|
|
], |
130
|
|
|
]); |
131
|
1 |
|
} |
132
|
|
|
} |
133
|
|
|
|