@@ -35,275 +35,275 @@ |
||
35 | 35 | |
36 | 36 | class DefaultTokenProvider implements IProvider { |
37 | 37 | |
38 | - /** @var DefaultTokenMapper */ |
|
39 | - private $mapper; |
|
38 | + /** @var DefaultTokenMapper */ |
|
39 | + private $mapper; |
|
40 | 40 | |
41 | - /** @var ICrypto */ |
|
42 | - private $crypto; |
|
41 | + /** @var ICrypto */ |
|
42 | + private $crypto; |
|
43 | 43 | |
44 | - /** @var IConfig */ |
|
45 | - private $config; |
|
44 | + /** @var IConfig */ |
|
45 | + private $config; |
|
46 | 46 | |
47 | - /** @var ILogger $logger */ |
|
48 | - private $logger; |
|
47 | + /** @var ILogger $logger */ |
|
48 | + private $logger; |
|
49 | 49 | |
50 | - /** @var ITimeFactory $time */ |
|
51 | - private $time; |
|
50 | + /** @var ITimeFactory $time */ |
|
51 | + private $time; |
|
52 | 52 | |
53 | - /** |
|
54 | - * @param DefaultTokenMapper $mapper |
|
55 | - * @param ICrypto $crypto |
|
56 | - * @param IConfig $config |
|
57 | - * @param ILogger $logger |
|
58 | - * @param ITimeFactory $time |
|
59 | - */ |
|
60 | - public function __construct(DefaultTokenMapper $mapper, |
|
61 | - ICrypto $crypto, |
|
62 | - IConfig $config, |
|
63 | - ILogger $logger, |
|
64 | - ITimeFactory $time) { |
|
65 | - $this->mapper = $mapper; |
|
66 | - $this->crypto = $crypto; |
|
67 | - $this->config = $config; |
|
68 | - $this->logger = $logger; |
|
69 | - $this->time = $time; |
|
70 | - } |
|
53 | + /** |
|
54 | + * @param DefaultTokenMapper $mapper |
|
55 | + * @param ICrypto $crypto |
|
56 | + * @param IConfig $config |
|
57 | + * @param ILogger $logger |
|
58 | + * @param ITimeFactory $time |
|
59 | + */ |
|
60 | + public function __construct(DefaultTokenMapper $mapper, |
|
61 | + ICrypto $crypto, |
|
62 | + IConfig $config, |
|
63 | + ILogger $logger, |
|
64 | + ITimeFactory $time) { |
|
65 | + $this->mapper = $mapper; |
|
66 | + $this->crypto = $crypto; |
|
67 | + $this->config = $config; |
|
68 | + $this->logger = $logger; |
|
69 | + $this->time = $time; |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Create and persist a new token |
|
74 | - * |
|
75 | - * @param string $token |
|
76 | - * @param string $uid |
|
77 | - * @param string $loginName |
|
78 | - * @param string|null $password |
|
79 | - * @param string $name |
|
80 | - * @param int $type token type |
|
81 | - * @param int $remember whether the session token should be used for remember-me |
|
82 | - * @return IToken |
|
83 | - */ |
|
84 | - public function generateToken($token, $uid, $loginName, $password, $name, $type = IToken::TEMPORARY_TOKEN, $remember = IToken::DO_NOT_REMEMBER) { |
|
85 | - $dbToken = new DefaultToken(); |
|
86 | - $dbToken->setUid($uid); |
|
87 | - $dbToken->setLoginName($loginName); |
|
88 | - if (!is_null($password)) { |
|
89 | - $dbToken->setPassword($this->encryptPassword($password, $token)); |
|
90 | - } |
|
91 | - $dbToken->setName($name); |
|
92 | - $dbToken->setToken($this->hashToken($token)); |
|
93 | - $dbToken->setType($type); |
|
94 | - $dbToken->setRemember($remember); |
|
95 | - $dbToken->setLastActivity($this->time->getTime()); |
|
96 | - $dbToken->setLastCheck($this->time->getTime()); |
|
72 | + /** |
|
73 | + * Create and persist a new token |
|
74 | + * |
|
75 | + * @param string $token |
|
76 | + * @param string $uid |
|
77 | + * @param string $loginName |
|
78 | + * @param string|null $password |
|
79 | + * @param string $name |
|
80 | + * @param int $type token type |
|
81 | + * @param int $remember whether the session token should be used for remember-me |
|
82 | + * @return IToken |
|
83 | + */ |
|
84 | + public function generateToken($token, $uid, $loginName, $password, $name, $type = IToken::TEMPORARY_TOKEN, $remember = IToken::DO_NOT_REMEMBER) { |
|
85 | + $dbToken = new DefaultToken(); |
|
86 | + $dbToken->setUid($uid); |
|
87 | + $dbToken->setLoginName($loginName); |
|
88 | + if (!is_null($password)) { |
|
89 | + $dbToken->setPassword($this->encryptPassword($password, $token)); |
|
90 | + } |
|
91 | + $dbToken->setName($name); |
|
92 | + $dbToken->setToken($this->hashToken($token)); |
|
93 | + $dbToken->setType($type); |
|
94 | + $dbToken->setRemember($remember); |
|
95 | + $dbToken->setLastActivity($this->time->getTime()); |
|
96 | + $dbToken->setLastCheck($this->time->getTime()); |
|
97 | 97 | |
98 | - $this->mapper->insert($dbToken); |
|
98 | + $this->mapper->insert($dbToken); |
|
99 | 99 | |
100 | - return $dbToken; |
|
101 | - } |
|
100 | + return $dbToken; |
|
101 | + } |
|
102 | 102 | |
103 | - /** |
|
104 | - * Save the updated token |
|
105 | - * |
|
106 | - * @param IToken $token |
|
107 | - * @throws InvalidTokenException |
|
108 | - */ |
|
109 | - public function updateToken(IToken $token) { |
|
110 | - if (!($token instanceof DefaultToken)) { |
|
111 | - throw new InvalidTokenException(); |
|
112 | - } |
|
113 | - $this->mapper->update($token); |
|
114 | - } |
|
103 | + /** |
|
104 | + * Save the updated token |
|
105 | + * |
|
106 | + * @param IToken $token |
|
107 | + * @throws InvalidTokenException |
|
108 | + */ |
|
109 | + public function updateToken(IToken $token) { |
|
110 | + if (!($token instanceof DefaultToken)) { |
|
111 | + throw new InvalidTokenException(); |
|
112 | + } |
|
113 | + $this->mapper->update($token); |
|
114 | + } |
|
115 | 115 | |
116 | - /** |
|
117 | - * Update token activity timestamp |
|
118 | - * |
|
119 | - * @throws InvalidTokenException |
|
120 | - * @param IToken $token |
|
121 | - */ |
|
122 | - public function updateTokenActivity(IToken $token) { |
|
123 | - if (!($token instanceof DefaultToken)) { |
|
124 | - throw new InvalidTokenException(); |
|
125 | - } |
|
126 | - /** @var DefaultToken $token */ |
|
127 | - $now = $this->time->getTime(); |
|
128 | - if ($token->getLastActivity() < ($now - 60)) { |
|
129 | - // Update token only once per minute |
|
130 | - $token->setLastActivity($now); |
|
131 | - $this->mapper->update($token); |
|
132 | - } |
|
133 | - } |
|
116 | + /** |
|
117 | + * Update token activity timestamp |
|
118 | + * |
|
119 | + * @throws InvalidTokenException |
|
120 | + * @param IToken $token |
|
121 | + */ |
|
122 | + public function updateTokenActivity(IToken $token) { |
|
123 | + if (!($token instanceof DefaultToken)) { |
|
124 | + throw new InvalidTokenException(); |
|
125 | + } |
|
126 | + /** @var DefaultToken $token */ |
|
127 | + $now = $this->time->getTime(); |
|
128 | + if ($token->getLastActivity() < ($now - 60)) { |
|
129 | + // Update token only once per minute |
|
130 | + $token->setLastActivity($now); |
|
131 | + $this->mapper->update($token); |
|
132 | + } |
|
133 | + } |
|
134 | 134 | |
135 | - /** |
|
136 | - * Get all tokens of a user |
|
137 | - * |
|
138 | - * The provider may limit the number of result rows in case of an abuse |
|
139 | - * where a high number of (session) tokens is generated |
|
140 | - * |
|
141 | - * @param IUser $user |
|
142 | - * @return IToken[] |
|
143 | - */ |
|
144 | - public function getTokenByUser(IUser $user) { |
|
145 | - return $this->mapper->getTokenByUser($user); |
|
146 | - } |
|
135 | + /** |
|
136 | + * Get all tokens of a user |
|
137 | + * |
|
138 | + * The provider may limit the number of result rows in case of an abuse |
|
139 | + * where a high number of (session) tokens is generated |
|
140 | + * |
|
141 | + * @param IUser $user |
|
142 | + * @return IToken[] |
|
143 | + */ |
|
144 | + public function getTokenByUser(IUser $user) { |
|
145 | + return $this->mapper->getTokenByUser($user); |
|
146 | + } |
|
147 | 147 | |
148 | - /** |
|
149 | - * Get a token by token |
|
150 | - * |
|
151 | - * @param string $tokenId |
|
152 | - * @throws InvalidTokenException |
|
153 | - * @return DefaultToken |
|
154 | - */ |
|
155 | - public function getToken($tokenId) { |
|
156 | - try { |
|
157 | - return $this->mapper->getToken($this->hashToken($tokenId)); |
|
158 | - } catch (DoesNotExistException $ex) { |
|
159 | - throw new InvalidTokenException(); |
|
160 | - } |
|
161 | - } |
|
148 | + /** |
|
149 | + * Get a token by token |
|
150 | + * |
|
151 | + * @param string $tokenId |
|
152 | + * @throws InvalidTokenException |
|
153 | + * @return DefaultToken |
|
154 | + */ |
|
155 | + public function getToken($tokenId) { |
|
156 | + try { |
|
157 | + return $this->mapper->getToken($this->hashToken($tokenId)); |
|
158 | + } catch (DoesNotExistException $ex) { |
|
159 | + throw new InvalidTokenException(); |
|
160 | + } |
|
161 | + } |
|
162 | 162 | |
163 | - /** |
|
164 | - * Get a token by token id |
|
165 | - * |
|
166 | - * @param string $tokenId |
|
167 | - * @throws InvalidTokenException |
|
168 | - * @return DefaultToken |
|
169 | - */ |
|
170 | - public function getTokenById($tokenId) { |
|
171 | - try { |
|
172 | - return $this->mapper->getTokenById($tokenId); |
|
173 | - } catch (DoesNotExistException $ex) { |
|
174 | - throw new InvalidTokenException(); |
|
175 | - } |
|
176 | - } |
|
163 | + /** |
|
164 | + * Get a token by token id |
|
165 | + * |
|
166 | + * @param string $tokenId |
|
167 | + * @throws InvalidTokenException |
|
168 | + * @return DefaultToken |
|
169 | + */ |
|
170 | + public function getTokenById($tokenId) { |
|
171 | + try { |
|
172 | + return $this->mapper->getTokenById($tokenId); |
|
173 | + } catch (DoesNotExistException $ex) { |
|
174 | + throw new InvalidTokenException(); |
|
175 | + } |
|
176 | + } |
|
177 | 177 | |
178 | - /** |
|
179 | - * @param string $oldSessionId |
|
180 | - * @param string $sessionId |
|
181 | - * @throws InvalidTokenException |
|
182 | - */ |
|
183 | - public function renewSessionToken($oldSessionId, $sessionId) { |
|
184 | - $token = $this->getToken($oldSessionId); |
|
178 | + /** |
|
179 | + * @param string $oldSessionId |
|
180 | + * @param string $sessionId |
|
181 | + * @throws InvalidTokenException |
|
182 | + */ |
|
183 | + public function renewSessionToken($oldSessionId, $sessionId) { |
|
184 | + $token = $this->getToken($oldSessionId); |
|
185 | 185 | |
186 | - $newToken = new DefaultToken(); |
|
187 | - $newToken->setUid($token->getUID()); |
|
188 | - $newToken->setLoginName($token->getLoginName()); |
|
189 | - if (!is_null($token->getPassword())) { |
|
190 | - $password = $this->decryptPassword($token->getPassword(), $oldSessionId); |
|
191 | - $newToken->setPassword($this->encryptPassword($password, $sessionId)); |
|
192 | - } |
|
193 | - $newToken->setName($token->getName()); |
|
194 | - $newToken->setToken($this->hashToken($sessionId)); |
|
195 | - $newToken->setType(IToken::TEMPORARY_TOKEN); |
|
196 | - $newToken->setRemember($token->getRemember()); |
|
197 | - $newToken->setLastActivity($this->time->getTime()); |
|
198 | - $this->mapper->insert($newToken); |
|
199 | - $this->mapper->delete($token); |
|
200 | - } |
|
186 | + $newToken = new DefaultToken(); |
|
187 | + $newToken->setUid($token->getUID()); |
|
188 | + $newToken->setLoginName($token->getLoginName()); |
|
189 | + if (!is_null($token->getPassword())) { |
|
190 | + $password = $this->decryptPassword($token->getPassword(), $oldSessionId); |
|
191 | + $newToken->setPassword($this->encryptPassword($password, $sessionId)); |
|
192 | + } |
|
193 | + $newToken->setName($token->getName()); |
|
194 | + $newToken->setToken($this->hashToken($sessionId)); |
|
195 | + $newToken->setType(IToken::TEMPORARY_TOKEN); |
|
196 | + $newToken->setRemember($token->getRemember()); |
|
197 | + $newToken->setLastActivity($this->time->getTime()); |
|
198 | + $this->mapper->insert($newToken); |
|
199 | + $this->mapper->delete($token); |
|
200 | + } |
|
201 | 201 | |
202 | - /** |
|
203 | - * @param IToken $savedToken |
|
204 | - * @param string $tokenId session token |
|
205 | - * @throws InvalidTokenException |
|
206 | - * @throws PasswordlessTokenException |
|
207 | - * @return string |
|
208 | - */ |
|
209 | - public function getPassword(IToken $savedToken, $tokenId) { |
|
210 | - $password = $savedToken->getPassword(); |
|
211 | - if (is_null($password)) { |
|
212 | - throw new PasswordlessTokenException(); |
|
213 | - } |
|
214 | - return $this->decryptPassword($password, $tokenId); |
|
215 | - } |
|
202 | + /** |
|
203 | + * @param IToken $savedToken |
|
204 | + * @param string $tokenId session token |
|
205 | + * @throws InvalidTokenException |
|
206 | + * @throws PasswordlessTokenException |
|
207 | + * @return string |
|
208 | + */ |
|
209 | + public function getPassword(IToken $savedToken, $tokenId) { |
|
210 | + $password = $savedToken->getPassword(); |
|
211 | + if (is_null($password)) { |
|
212 | + throw new PasswordlessTokenException(); |
|
213 | + } |
|
214 | + return $this->decryptPassword($password, $tokenId); |
|
215 | + } |
|
216 | 216 | |
217 | - /** |
|
218 | - * Encrypt and set the password of the given token |
|
219 | - * |
|
220 | - * @param IToken $token |
|
221 | - * @param string $tokenId |
|
222 | - * @param string $password |
|
223 | - * @throws InvalidTokenException |
|
224 | - */ |
|
225 | - public function setPassword(IToken $token, $tokenId, $password) { |
|
226 | - if (!($token instanceof DefaultToken)) { |
|
227 | - throw new InvalidTokenException(); |
|
228 | - } |
|
229 | - /** @var DefaultToken $token */ |
|
230 | - $token->setPassword($this->encryptPassword($password, $tokenId)); |
|
231 | - $this->mapper->update($token); |
|
232 | - } |
|
217 | + /** |
|
218 | + * Encrypt and set the password of the given token |
|
219 | + * |
|
220 | + * @param IToken $token |
|
221 | + * @param string $tokenId |
|
222 | + * @param string $password |
|
223 | + * @throws InvalidTokenException |
|
224 | + */ |
|
225 | + public function setPassword(IToken $token, $tokenId, $password) { |
|
226 | + if (!($token instanceof DefaultToken)) { |
|
227 | + throw new InvalidTokenException(); |
|
228 | + } |
|
229 | + /** @var DefaultToken $token */ |
|
230 | + $token->setPassword($this->encryptPassword($password, $tokenId)); |
|
231 | + $this->mapper->update($token); |
|
232 | + } |
|
233 | 233 | |
234 | - /** |
|
235 | - * Invalidate (delete) the given session token |
|
236 | - * |
|
237 | - * @param string $token |
|
238 | - */ |
|
239 | - public function invalidateToken($token) { |
|
240 | - $this->mapper->invalidate($this->hashToken($token)); |
|
241 | - } |
|
234 | + /** |
|
235 | + * Invalidate (delete) the given session token |
|
236 | + * |
|
237 | + * @param string $token |
|
238 | + */ |
|
239 | + public function invalidateToken($token) { |
|
240 | + $this->mapper->invalidate($this->hashToken($token)); |
|
241 | + } |
|
242 | 242 | |
243 | - /** |
|
244 | - * Invalidate (delete) the given token |
|
245 | - * |
|
246 | - * @param IUser $user |
|
247 | - * @param int $id |
|
248 | - */ |
|
249 | - public function invalidateTokenById(IUser $user, $id) { |
|
250 | - $this->mapper->deleteById($user, $id); |
|
251 | - } |
|
243 | + /** |
|
244 | + * Invalidate (delete) the given token |
|
245 | + * |
|
246 | + * @param IUser $user |
|
247 | + * @param int $id |
|
248 | + */ |
|
249 | + public function invalidateTokenById(IUser $user, $id) { |
|
250 | + $this->mapper->deleteById($user, $id); |
|
251 | + } |
|
252 | 252 | |
253 | - /** |
|
254 | - * Invalidate (delete) old session tokens |
|
255 | - */ |
|
256 | - public function invalidateOldTokens() { |
|
257 | - $olderThan = $this->time->getTime() - (int) $this->config->getSystemValue('session_lifetime', 60 * 60 * 24); |
|
258 | - $this->logger->debug('Invalidating session tokens older than ' . date('c', $olderThan), ['app' => 'cron']); |
|
259 | - $this->mapper->invalidateOld($olderThan, IToken::DO_NOT_REMEMBER); |
|
260 | - $rememberThreshold = $this->time->getTime() - (int) $this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15); |
|
261 | - $this->logger->debug('Invalidating remembered session tokens older than ' . date('c', $rememberThreshold), ['app' => 'cron']); |
|
262 | - $this->mapper->invalidateOld($rememberThreshold, IToken::REMEMBER); |
|
263 | - } |
|
253 | + /** |
|
254 | + * Invalidate (delete) old session tokens |
|
255 | + */ |
|
256 | + public function invalidateOldTokens() { |
|
257 | + $olderThan = $this->time->getTime() - (int) $this->config->getSystemValue('session_lifetime', 60 * 60 * 24); |
|
258 | + $this->logger->debug('Invalidating session tokens older than ' . date('c', $olderThan), ['app' => 'cron']); |
|
259 | + $this->mapper->invalidateOld($olderThan, IToken::DO_NOT_REMEMBER); |
|
260 | + $rememberThreshold = $this->time->getTime() - (int) $this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15); |
|
261 | + $this->logger->debug('Invalidating remembered session tokens older than ' . date('c', $rememberThreshold), ['app' => 'cron']); |
|
262 | + $this->mapper->invalidateOld($rememberThreshold, IToken::REMEMBER); |
|
263 | + } |
|
264 | 264 | |
265 | - /** |
|
266 | - * @param string $token |
|
267 | - * @return string |
|
268 | - */ |
|
269 | - private function hashToken($token) { |
|
270 | - $secret = $this->config->getSystemValue('secret'); |
|
271 | - return hash('sha512', $token . $secret); |
|
272 | - } |
|
265 | + /** |
|
266 | + * @param string $token |
|
267 | + * @return string |
|
268 | + */ |
|
269 | + private function hashToken($token) { |
|
270 | + $secret = $this->config->getSystemValue('secret'); |
|
271 | + return hash('sha512', $token . $secret); |
|
272 | + } |
|
273 | 273 | |
274 | - /** |
|
275 | - * Encrypt the given password |
|
276 | - * |
|
277 | - * The token is used as key |
|
278 | - * |
|
279 | - * @param string $password |
|
280 | - * @param string $token |
|
281 | - * @return string encrypted password |
|
282 | - */ |
|
283 | - private function encryptPassword($password, $token) { |
|
284 | - $secret = $this->config->getSystemValue('secret'); |
|
285 | - return $this->crypto->encrypt($password, $token . $secret); |
|
286 | - } |
|
274 | + /** |
|
275 | + * Encrypt the given password |
|
276 | + * |
|
277 | + * The token is used as key |
|
278 | + * |
|
279 | + * @param string $password |
|
280 | + * @param string $token |
|
281 | + * @return string encrypted password |
|
282 | + */ |
|
283 | + private function encryptPassword($password, $token) { |
|
284 | + $secret = $this->config->getSystemValue('secret'); |
|
285 | + return $this->crypto->encrypt($password, $token . $secret); |
|
286 | + } |
|
287 | 287 | |
288 | - /** |
|
289 | - * Decrypt the given password |
|
290 | - * |
|
291 | - * The token is used as key |
|
292 | - * |
|
293 | - * @param string $password |
|
294 | - * @param string $token |
|
295 | - * @throws InvalidTokenException |
|
296 | - * @return string the decrypted key |
|
297 | - */ |
|
298 | - private function decryptPassword($password, $token) { |
|
299 | - $secret = $this->config->getSystemValue('secret'); |
|
300 | - try { |
|
301 | - return $this->crypto->decrypt($password, $token . $secret); |
|
302 | - } catch (Exception $ex) { |
|
303 | - // Delete the invalid token |
|
304 | - $this->invalidateToken($token); |
|
305 | - throw new InvalidTokenException(); |
|
306 | - } |
|
307 | - } |
|
288 | + /** |
|
289 | + * Decrypt the given password |
|
290 | + * |
|
291 | + * The token is used as key |
|
292 | + * |
|
293 | + * @param string $password |
|
294 | + * @param string $token |
|
295 | + * @throws InvalidTokenException |
|
296 | + * @return string the decrypted key |
|
297 | + */ |
|
298 | + private function decryptPassword($password, $token) { |
|
299 | + $secret = $this->config->getSystemValue('secret'); |
|
300 | + try { |
|
301 | + return $this->crypto->decrypt($password, $token . $secret); |
|
302 | + } catch (Exception $ex) { |
|
303 | + // Delete the invalid token |
|
304 | + $this->invalidateToken($token); |
|
305 | + throw new InvalidTokenException(); |
|
306 | + } |
|
307 | + } |
|
308 | 308 | |
309 | 309 | } |