@@ -39,303 +39,303 @@ |
||
| 39 | 39 | |
| 40 | 40 | class DefaultTokenProvider implements IProvider { |
| 41 | 41 | |
| 42 | - /** @var DefaultTokenMapper */ |
|
| 43 | - private $mapper; |
|
| 44 | - |
|
| 45 | - /** @var ICrypto */ |
|
| 46 | - private $crypto; |
|
| 47 | - |
|
| 48 | - /** @var IConfig */ |
|
| 49 | - private $config; |
|
| 50 | - |
|
| 51 | - /** @var ILogger $logger */ |
|
| 52 | - private $logger; |
|
| 53 | - |
|
| 54 | - /** @var ITimeFactory $time */ |
|
| 55 | - private $time; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @param DefaultTokenMapper $mapper |
|
| 59 | - * @param ICrypto $crypto |
|
| 60 | - * @param IConfig $config |
|
| 61 | - * @param ILogger $logger |
|
| 62 | - * @param ITimeFactory $time |
|
| 63 | - */ |
|
| 64 | - public function __construct(DefaultTokenMapper $mapper, |
|
| 65 | - ICrypto $crypto, |
|
| 66 | - IConfig $config, |
|
| 67 | - ILogger $logger, |
|
| 68 | - ITimeFactory $time) { |
|
| 69 | - $this->mapper = $mapper; |
|
| 70 | - $this->crypto = $crypto; |
|
| 71 | - $this->config = $config; |
|
| 72 | - $this->logger = $logger; |
|
| 73 | - $this->time = $time; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Create and persist a new token |
|
| 78 | - * |
|
| 79 | - * @param string $token |
|
| 80 | - * @param string $uid |
|
| 81 | - * @param string $loginName |
|
| 82 | - * @param string|null $password |
|
| 83 | - * @param string $name |
|
| 84 | - * @param int $type token type |
|
| 85 | - * @param int $remember whether the session token should be used for remember-me |
|
| 86 | - * @return IToken |
|
| 87 | - */ |
|
| 88 | - public function generateToken(string $token, |
|
| 89 | - string $uid, |
|
| 90 | - string $loginName, |
|
| 91 | - $password, |
|
| 92 | - string $name, |
|
| 93 | - int $type = IToken::TEMPORARY_TOKEN, |
|
| 94 | - int $remember = IToken::DO_NOT_REMEMBER): IToken { |
|
| 95 | - $dbToken = new DefaultToken(); |
|
| 96 | - $dbToken->setUid($uid); |
|
| 97 | - $dbToken->setLoginName($loginName); |
|
| 98 | - if (!is_null($password)) { |
|
| 99 | - $dbToken->setPassword($this->encryptPassword($password, $token)); |
|
| 100 | - } |
|
| 101 | - $dbToken->setName($name); |
|
| 102 | - $dbToken->setToken($this->hashToken($token)); |
|
| 103 | - $dbToken->setType($type); |
|
| 104 | - $dbToken->setRemember($remember); |
|
| 105 | - $dbToken->setLastActivity($this->time->getTime()); |
|
| 106 | - $dbToken->setLastCheck($this->time->getTime()); |
|
| 107 | - $dbToken->setVersion(DefaultToken::VERSION); |
|
| 108 | - |
|
| 109 | - $this->mapper->insert($dbToken); |
|
| 110 | - |
|
| 111 | - return $dbToken; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Save the updated token |
|
| 116 | - * |
|
| 117 | - * @param IToken $token |
|
| 118 | - * @throws InvalidTokenException |
|
| 119 | - */ |
|
| 120 | - public function updateToken(IToken $token) { |
|
| 121 | - if (!($token instanceof DefaultToken)) { |
|
| 122 | - throw new InvalidTokenException(); |
|
| 123 | - } |
|
| 124 | - $this->mapper->update($token); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Update token activity timestamp |
|
| 129 | - * |
|
| 130 | - * @throws InvalidTokenException |
|
| 131 | - * @param IToken $token |
|
| 132 | - */ |
|
| 133 | - public function updateTokenActivity(IToken $token) { |
|
| 134 | - if (!($token instanceof DefaultToken)) { |
|
| 135 | - throw new InvalidTokenException(); |
|
| 136 | - } |
|
| 137 | - /** @var DefaultToken $token */ |
|
| 138 | - $now = $this->time->getTime(); |
|
| 139 | - if ($token->getLastActivity() < ($now - 60)) { |
|
| 140 | - // Update token only once per minute |
|
| 141 | - $token->setLastActivity($now); |
|
| 142 | - $this->mapper->update($token); |
|
| 143 | - } |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - public function getTokenByUser(string $uid): array { |
|
| 147 | - return $this->mapper->getTokenByUser($uid); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Get a token by token |
|
| 152 | - * |
|
| 153 | - * @param string $tokenId |
|
| 154 | - * @throws InvalidTokenException |
|
| 155 | - * @throws ExpiredTokenException |
|
| 156 | - * @return IToken |
|
| 157 | - */ |
|
| 158 | - public function getToken(string $tokenId): IToken { |
|
| 159 | - try { |
|
| 160 | - $token = $this->mapper->getToken($this->hashToken($tokenId)); |
|
| 161 | - } catch (DoesNotExistException $ex) { |
|
| 162 | - throw new InvalidTokenException(); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - if ((int)$token->getExpires() !== 0 && $token->getExpires() < $this->time->getTime()) { |
|
| 166 | - throw new ExpiredTokenException($token); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - return $token; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Get a token by token id |
|
| 174 | - * |
|
| 175 | - * @param int $tokenId |
|
| 176 | - * @throws InvalidTokenException |
|
| 177 | - * @throws ExpiredTokenException |
|
| 178 | - * @return IToken |
|
| 179 | - */ |
|
| 180 | - public function getTokenById(int $tokenId): IToken { |
|
| 181 | - try { |
|
| 182 | - $token = $this->mapper->getTokenById($tokenId); |
|
| 183 | - } catch (DoesNotExistException $ex) { |
|
| 184 | - throw new InvalidTokenException(); |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - if ((int)$token->getExpires() !== 0 && $token->getExpires() < $this->time->getTime()) { |
|
| 188 | - throw new ExpiredTokenException($token); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - return $token; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * @param string $oldSessionId |
|
| 196 | - * @param string $sessionId |
|
| 197 | - * @throws InvalidTokenException |
|
| 198 | - */ |
|
| 199 | - public function renewSessionToken(string $oldSessionId, string $sessionId) { |
|
| 200 | - $token = $this->getToken($oldSessionId); |
|
| 201 | - |
|
| 202 | - $newToken = new DefaultToken(); |
|
| 203 | - $newToken->setUid($token->getUID()); |
|
| 204 | - $newToken->setLoginName($token->getLoginName()); |
|
| 205 | - if (!is_null($token->getPassword())) { |
|
| 206 | - $password = $this->decryptPassword($token->getPassword(), $oldSessionId); |
|
| 207 | - $newToken->setPassword($this->encryptPassword($password, $sessionId)); |
|
| 208 | - } |
|
| 209 | - $newToken->setName($token->getName()); |
|
| 210 | - $newToken->setToken($this->hashToken($sessionId)); |
|
| 211 | - $newToken->setType(IToken::TEMPORARY_TOKEN); |
|
| 212 | - $newToken->setRemember($token->getRemember()); |
|
| 213 | - $newToken->setLastActivity($this->time->getTime()); |
|
| 214 | - $this->mapper->insert($newToken); |
|
| 215 | - $this->mapper->delete($token); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * @param IToken $savedToken |
|
| 220 | - * @param string $tokenId session token |
|
| 221 | - * @throws InvalidTokenException |
|
| 222 | - * @throws PasswordlessTokenException |
|
| 223 | - * @return string |
|
| 224 | - */ |
|
| 225 | - public function getPassword(IToken $savedToken, string $tokenId): string { |
|
| 226 | - $password = $savedToken->getPassword(); |
|
| 227 | - if (is_null($password)) { |
|
| 228 | - throw new PasswordlessTokenException(); |
|
| 229 | - } |
|
| 230 | - return $this->decryptPassword($password, $tokenId); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Encrypt and set the password of the given token |
|
| 235 | - * |
|
| 236 | - * @param IToken $token |
|
| 237 | - * @param string $tokenId |
|
| 238 | - * @param string $password |
|
| 239 | - * @throws InvalidTokenException |
|
| 240 | - */ |
|
| 241 | - public function setPassword(IToken $token, string $tokenId, string $password) { |
|
| 242 | - if (!($token instanceof DefaultToken)) { |
|
| 243 | - throw new InvalidTokenException(); |
|
| 244 | - } |
|
| 245 | - /** @var DefaultToken $token */ |
|
| 246 | - $token->setPassword($this->encryptPassword($password, $tokenId)); |
|
| 247 | - $this->mapper->update($token); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * Invalidate (delete) the given session token |
|
| 252 | - * |
|
| 253 | - * @param string $token |
|
| 254 | - */ |
|
| 255 | - public function invalidateToken(string $token) { |
|
| 256 | - $this->mapper->invalidate($this->hashToken($token)); |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - public function invalidateTokenById(string $uid, int $id) { |
|
| 260 | - $this->mapper->deleteById($uid, $id); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Invalidate (delete) old session tokens |
|
| 265 | - */ |
|
| 266 | - public function invalidateOldTokens() { |
|
| 267 | - $olderThan = $this->time->getTime() - (int) $this->config->getSystemValue('session_lifetime', 60 * 60 * 24); |
|
| 268 | - $this->logger->debug('Invalidating session tokens older than ' . date('c', $olderThan), ['app' => 'cron']); |
|
| 269 | - $this->mapper->invalidateOld($olderThan, IToken::DO_NOT_REMEMBER); |
|
| 270 | - $rememberThreshold = $this->time->getTime() - (int) $this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15); |
|
| 271 | - $this->logger->debug('Invalidating remembered session tokens older than ' . date('c', $rememberThreshold), ['app' => 'cron']); |
|
| 272 | - $this->mapper->invalidateOld($rememberThreshold, IToken::REMEMBER); |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * Rotate the token. Usefull for for example oauth tokens |
|
| 277 | - * |
|
| 278 | - * @param IToken $token |
|
| 279 | - * @param string $oldTokenId |
|
| 280 | - * @param string $newTokenId |
|
| 281 | - * @return IToken |
|
| 282 | - */ |
|
| 283 | - public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken { |
|
| 284 | - try { |
|
| 285 | - $password = $this->getPassword($token, $oldTokenId); |
|
| 286 | - $token->setPassword($this->encryptPassword($password, $newTokenId)); |
|
| 287 | - } catch (PasswordlessTokenException $e) { |
|
| 288 | - |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - $token->setToken($this->hashToken($newTokenId)); |
|
| 292 | - $this->updateToken($token); |
|
| 293 | - |
|
| 294 | - return $token; |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * @param string $token |
|
| 299 | - * @return string |
|
| 300 | - */ |
|
| 301 | - private function hashToken(string $token): string { |
|
| 302 | - $secret = $this->config->getSystemValue('secret'); |
|
| 303 | - return hash('sha512', $token . $secret); |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Encrypt the given password |
|
| 308 | - * |
|
| 309 | - * The token is used as key |
|
| 310 | - * |
|
| 311 | - * @param string $password |
|
| 312 | - * @param string $token |
|
| 313 | - * @return string encrypted password |
|
| 314 | - */ |
|
| 315 | - private function encryptPassword(string $password, string $token): string { |
|
| 316 | - $secret = $this->config->getSystemValue('secret'); |
|
| 317 | - return $this->crypto->encrypt($password, $token . $secret); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * Decrypt the given password |
|
| 322 | - * |
|
| 323 | - * The token is used as key |
|
| 324 | - * |
|
| 325 | - * @param string $password |
|
| 326 | - * @param string $token |
|
| 327 | - * @throws InvalidTokenException |
|
| 328 | - * @return string the decrypted key |
|
| 329 | - */ |
|
| 330 | - private function decryptPassword(string $password, string $token): string { |
|
| 331 | - $secret = $this->config->getSystemValue('secret'); |
|
| 332 | - try { |
|
| 333 | - return $this->crypto->decrypt($password, $token . $secret); |
|
| 334 | - } catch (Exception $ex) { |
|
| 335 | - // Delete the invalid token |
|
| 336 | - $this->invalidateToken($token); |
|
| 337 | - throw new InvalidTokenException(); |
|
| 338 | - } |
|
| 339 | - } |
|
| 42 | + /** @var DefaultTokenMapper */ |
|
| 43 | + private $mapper; |
|
| 44 | + |
|
| 45 | + /** @var ICrypto */ |
|
| 46 | + private $crypto; |
|
| 47 | + |
|
| 48 | + /** @var IConfig */ |
|
| 49 | + private $config; |
|
| 50 | + |
|
| 51 | + /** @var ILogger $logger */ |
|
| 52 | + private $logger; |
|
| 53 | + |
|
| 54 | + /** @var ITimeFactory $time */ |
|
| 55 | + private $time; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @param DefaultTokenMapper $mapper |
|
| 59 | + * @param ICrypto $crypto |
|
| 60 | + * @param IConfig $config |
|
| 61 | + * @param ILogger $logger |
|
| 62 | + * @param ITimeFactory $time |
|
| 63 | + */ |
|
| 64 | + public function __construct(DefaultTokenMapper $mapper, |
|
| 65 | + ICrypto $crypto, |
|
| 66 | + IConfig $config, |
|
| 67 | + ILogger $logger, |
|
| 68 | + ITimeFactory $time) { |
|
| 69 | + $this->mapper = $mapper; |
|
| 70 | + $this->crypto = $crypto; |
|
| 71 | + $this->config = $config; |
|
| 72 | + $this->logger = $logger; |
|
| 73 | + $this->time = $time; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Create and persist a new token |
|
| 78 | + * |
|
| 79 | + * @param string $token |
|
| 80 | + * @param string $uid |
|
| 81 | + * @param string $loginName |
|
| 82 | + * @param string|null $password |
|
| 83 | + * @param string $name |
|
| 84 | + * @param int $type token type |
|
| 85 | + * @param int $remember whether the session token should be used for remember-me |
|
| 86 | + * @return IToken |
|
| 87 | + */ |
|
| 88 | + public function generateToken(string $token, |
|
| 89 | + string $uid, |
|
| 90 | + string $loginName, |
|
| 91 | + $password, |
|
| 92 | + string $name, |
|
| 93 | + int $type = IToken::TEMPORARY_TOKEN, |
|
| 94 | + int $remember = IToken::DO_NOT_REMEMBER): IToken { |
|
| 95 | + $dbToken = new DefaultToken(); |
|
| 96 | + $dbToken->setUid($uid); |
|
| 97 | + $dbToken->setLoginName($loginName); |
|
| 98 | + if (!is_null($password)) { |
|
| 99 | + $dbToken->setPassword($this->encryptPassword($password, $token)); |
|
| 100 | + } |
|
| 101 | + $dbToken->setName($name); |
|
| 102 | + $dbToken->setToken($this->hashToken($token)); |
|
| 103 | + $dbToken->setType($type); |
|
| 104 | + $dbToken->setRemember($remember); |
|
| 105 | + $dbToken->setLastActivity($this->time->getTime()); |
|
| 106 | + $dbToken->setLastCheck($this->time->getTime()); |
|
| 107 | + $dbToken->setVersion(DefaultToken::VERSION); |
|
| 108 | + |
|
| 109 | + $this->mapper->insert($dbToken); |
|
| 110 | + |
|
| 111 | + return $dbToken; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Save the updated token |
|
| 116 | + * |
|
| 117 | + * @param IToken $token |
|
| 118 | + * @throws InvalidTokenException |
|
| 119 | + */ |
|
| 120 | + public function updateToken(IToken $token) { |
|
| 121 | + if (!($token instanceof DefaultToken)) { |
|
| 122 | + throw new InvalidTokenException(); |
|
| 123 | + } |
|
| 124 | + $this->mapper->update($token); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Update token activity timestamp |
|
| 129 | + * |
|
| 130 | + * @throws InvalidTokenException |
|
| 131 | + * @param IToken $token |
|
| 132 | + */ |
|
| 133 | + public function updateTokenActivity(IToken $token) { |
|
| 134 | + if (!($token instanceof DefaultToken)) { |
|
| 135 | + throw new InvalidTokenException(); |
|
| 136 | + } |
|
| 137 | + /** @var DefaultToken $token */ |
|
| 138 | + $now = $this->time->getTime(); |
|
| 139 | + if ($token->getLastActivity() < ($now - 60)) { |
|
| 140 | + // Update token only once per minute |
|
| 141 | + $token->setLastActivity($now); |
|
| 142 | + $this->mapper->update($token); |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + public function getTokenByUser(string $uid): array { |
|
| 147 | + return $this->mapper->getTokenByUser($uid); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Get a token by token |
|
| 152 | + * |
|
| 153 | + * @param string $tokenId |
|
| 154 | + * @throws InvalidTokenException |
|
| 155 | + * @throws ExpiredTokenException |
|
| 156 | + * @return IToken |
|
| 157 | + */ |
|
| 158 | + public function getToken(string $tokenId): IToken { |
|
| 159 | + try { |
|
| 160 | + $token = $this->mapper->getToken($this->hashToken($tokenId)); |
|
| 161 | + } catch (DoesNotExistException $ex) { |
|
| 162 | + throw new InvalidTokenException(); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + if ((int)$token->getExpires() !== 0 && $token->getExpires() < $this->time->getTime()) { |
|
| 166 | + throw new ExpiredTokenException($token); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + return $token; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Get a token by token id |
|
| 174 | + * |
|
| 175 | + * @param int $tokenId |
|
| 176 | + * @throws InvalidTokenException |
|
| 177 | + * @throws ExpiredTokenException |
|
| 178 | + * @return IToken |
|
| 179 | + */ |
|
| 180 | + public function getTokenById(int $tokenId): IToken { |
|
| 181 | + try { |
|
| 182 | + $token = $this->mapper->getTokenById($tokenId); |
|
| 183 | + } catch (DoesNotExistException $ex) { |
|
| 184 | + throw new InvalidTokenException(); |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + if ((int)$token->getExpires() !== 0 && $token->getExpires() < $this->time->getTime()) { |
|
| 188 | + throw new ExpiredTokenException($token); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + return $token; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * @param string $oldSessionId |
|
| 196 | + * @param string $sessionId |
|
| 197 | + * @throws InvalidTokenException |
|
| 198 | + */ |
|
| 199 | + public function renewSessionToken(string $oldSessionId, string $sessionId) { |
|
| 200 | + $token = $this->getToken($oldSessionId); |
|
| 201 | + |
|
| 202 | + $newToken = new DefaultToken(); |
|
| 203 | + $newToken->setUid($token->getUID()); |
|
| 204 | + $newToken->setLoginName($token->getLoginName()); |
|
| 205 | + if (!is_null($token->getPassword())) { |
|
| 206 | + $password = $this->decryptPassword($token->getPassword(), $oldSessionId); |
|
| 207 | + $newToken->setPassword($this->encryptPassword($password, $sessionId)); |
|
| 208 | + } |
|
| 209 | + $newToken->setName($token->getName()); |
|
| 210 | + $newToken->setToken($this->hashToken($sessionId)); |
|
| 211 | + $newToken->setType(IToken::TEMPORARY_TOKEN); |
|
| 212 | + $newToken->setRemember($token->getRemember()); |
|
| 213 | + $newToken->setLastActivity($this->time->getTime()); |
|
| 214 | + $this->mapper->insert($newToken); |
|
| 215 | + $this->mapper->delete($token); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * @param IToken $savedToken |
|
| 220 | + * @param string $tokenId session token |
|
| 221 | + * @throws InvalidTokenException |
|
| 222 | + * @throws PasswordlessTokenException |
|
| 223 | + * @return string |
|
| 224 | + */ |
|
| 225 | + public function getPassword(IToken $savedToken, string $tokenId): string { |
|
| 226 | + $password = $savedToken->getPassword(); |
|
| 227 | + if (is_null($password)) { |
|
| 228 | + throw new PasswordlessTokenException(); |
|
| 229 | + } |
|
| 230 | + return $this->decryptPassword($password, $tokenId); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Encrypt and set the password of the given token |
|
| 235 | + * |
|
| 236 | + * @param IToken $token |
|
| 237 | + * @param string $tokenId |
|
| 238 | + * @param string $password |
|
| 239 | + * @throws InvalidTokenException |
|
| 240 | + */ |
|
| 241 | + public function setPassword(IToken $token, string $tokenId, string $password) { |
|
| 242 | + if (!($token instanceof DefaultToken)) { |
|
| 243 | + throw new InvalidTokenException(); |
|
| 244 | + } |
|
| 245 | + /** @var DefaultToken $token */ |
|
| 246 | + $token->setPassword($this->encryptPassword($password, $tokenId)); |
|
| 247 | + $this->mapper->update($token); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * Invalidate (delete) the given session token |
|
| 252 | + * |
|
| 253 | + * @param string $token |
|
| 254 | + */ |
|
| 255 | + public function invalidateToken(string $token) { |
|
| 256 | + $this->mapper->invalidate($this->hashToken($token)); |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + public function invalidateTokenById(string $uid, int $id) { |
|
| 260 | + $this->mapper->deleteById($uid, $id); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Invalidate (delete) old session tokens |
|
| 265 | + */ |
|
| 266 | + public function invalidateOldTokens() { |
|
| 267 | + $olderThan = $this->time->getTime() - (int) $this->config->getSystemValue('session_lifetime', 60 * 60 * 24); |
|
| 268 | + $this->logger->debug('Invalidating session tokens older than ' . date('c', $olderThan), ['app' => 'cron']); |
|
| 269 | + $this->mapper->invalidateOld($olderThan, IToken::DO_NOT_REMEMBER); |
|
| 270 | + $rememberThreshold = $this->time->getTime() - (int) $this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15); |
|
| 271 | + $this->logger->debug('Invalidating remembered session tokens older than ' . date('c', $rememberThreshold), ['app' => 'cron']); |
|
| 272 | + $this->mapper->invalidateOld($rememberThreshold, IToken::REMEMBER); |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * Rotate the token. Usefull for for example oauth tokens |
|
| 277 | + * |
|
| 278 | + * @param IToken $token |
|
| 279 | + * @param string $oldTokenId |
|
| 280 | + * @param string $newTokenId |
|
| 281 | + * @return IToken |
|
| 282 | + */ |
|
| 283 | + public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken { |
|
| 284 | + try { |
|
| 285 | + $password = $this->getPassword($token, $oldTokenId); |
|
| 286 | + $token->setPassword($this->encryptPassword($password, $newTokenId)); |
|
| 287 | + } catch (PasswordlessTokenException $e) { |
|
| 288 | + |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + $token->setToken($this->hashToken($newTokenId)); |
|
| 292 | + $this->updateToken($token); |
|
| 293 | + |
|
| 294 | + return $token; |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * @param string $token |
|
| 299 | + * @return string |
|
| 300 | + */ |
|
| 301 | + private function hashToken(string $token): string { |
|
| 302 | + $secret = $this->config->getSystemValue('secret'); |
|
| 303 | + return hash('sha512', $token . $secret); |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Encrypt the given password |
|
| 308 | + * |
|
| 309 | + * The token is used as key |
|
| 310 | + * |
|
| 311 | + * @param string $password |
|
| 312 | + * @param string $token |
|
| 313 | + * @return string encrypted password |
|
| 314 | + */ |
|
| 315 | + private function encryptPassword(string $password, string $token): string { |
|
| 316 | + $secret = $this->config->getSystemValue('secret'); |
|
| 317 | + return $this->crypto->encrypt($password, $token . $secret); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * Decrypt the given password |
|
| 322 | + * |
|
| 323 | + * The token is used as key |
|
| 324 | + * |
|
| 325 | + * @param string $password |
|
| 326 | + * @param string $token |
|
| 327 | + * @throws InvalidTokenException |
|
| 328 | + * @return string the decrypted key |
|
| 329 | + */ |
|
| 330 | + private function decryptPassword(string $password, string $token): string { |
|
| 331 | + $secret = $this->config->getSystemValue('secret'); |
|
| 332 | + try { |
|
| 333 | + return $this->crypto->decrypt($password, $token . $secret); |
|
| 334 | + } catch (Exception $ex) { |
|
| 335 | + // Delete the invalid token |
|
| 336 | + $this->invalidateToken($token); |
|
| 337 | + throw new InvalidTokenException(); |
|
| 338 | + } |
|
| 339 | + } |
|
| 340 | 340 | |
| 341 | 341 | } |
@@ -162,7 +162,7 @@ discard block |
||
| 162 | 162 | throw new InvalidTokenException(); |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | - if ((int)$token->getExpires() !== 0 && $token->getExpires() < $this->time->getTime()) { |
|
| 165 | + if ((int) $token->getExpires() !== 0 && $token->getExpires() < $this->time->getTime()) { |
|
| 166 | 166 | throw new ExpiredTokenException($token); |
| 167 | 167 | } |
| 168 | 168 | |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | throw new InvalidTokenException(); |
| 185 | 185 | } |
| 186 | 186 | |
| 187 | - if ((int)$token->getExpires() !== 0 && $token->getExpires() < $this->time->getTime()) { |
|
| 187 | + if ((int) $token->getExpires() !== 0 && $token->getExpires() < $this->time->getTime()) { |
|
| 188 | 188 | throw new ExpiredTokenException($token); |
| 189 | 189 | } |
| 190 | 190 | |
@@ -265,10 +265,10 @@ discard block |
||
| 265 | 265 | */ |
| 266 | 266 | public function invalidateOldTokens() { |
| 267 | 267 | $olderThan = $this->time->getTime() - (int) $this->config->getSystemValue('session_lifetime', 60 * 60 * 24); |
| 268 | - $this->logger->debug('Invalidating session tokens older than ' . date('c', $olderThan), ['app' => 'cron']); |
|
| 268 | + $this->logger->debug('Invalidating session tokens older than '.date('c', $olderThan), ['app' => 'cron']); |
|
| 269 | 269 | $this->mapper->invalidateOld($olderThan, IToken::DO_NOT_REMEMBER); |
| 270 | 270 | $rememberThreshold = $this->time->getTime() - (int) $this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15); |
| 271 | - $this->logger->debug('Invalidating remembered session tokens older than ' . date('c', $rememberThreshold), ['app' => 'cron']); |
|
| 271 | + $this->logger->debug('Invalidating remembered session tokens older than '.date('c', $rememberThreshold), ['app' => 'cron']); |
|
| 272 | 272 | $this->mapper->invalidateOld($rememberThreshold, IToken::REMEMBER); |
| 273 | 273 | } |
| 274 | 274 | |
@@ -300,7 +300,7 @@ discard block |
||
| 300 | 300 | */ |
| 301 | 301 | private function hashToken(string $token): string { |
| 302 | 302 | $secret = $this->config->getSystemValue('secret'); |
| 303 | - return hash('sha512', $token . $secret); |
|
| 303 | + return hash('sha512', $token.$secret); |
|
| 304 | 304 | } |
| 305 | 305 | |
| 306 | 306 | /** |
@@ -314,7 +314,7 @@ discard block |
||
| 314 | 314 | */ |
| 315 | 315 | private function encryptPassword(string $password, string $token): string { |
| 316 | 316 | $secret = $this->config->getSystemValue('secret'); |
| 317 | - return $this->crypto->encrypt($password, $token . $secret); |
|
| 317 | + return $this->crypto->encrypt($password, $token.$secret); |
|
| 318 | 318 | } |
| 319 | 319 | |
| 320 | 320 | /** |
@@ -330,7 +330,7 @@ discard block |
||
| 330 | 330 | private function decryptPassword(string $password, string $token): string { |
| 331 | 331 | $secret = $this->config->getSystemValue('secret'); |
| 332 | 332 | try { |
| 333 | - return $this->crypto->decrypt($password, $token . $secret); |
|
| 333 | + return $this->crypto->decrypt($password, $token.$secret); |
|
| 334 | 334 | } catch (Exception $ex) { |
| 335 | 335 | // Delete the invalid token |
| 336 | 336 | $this->invalidateToken($token); |
@@ -32,289 +32,289 @@ |
||
| 32 | 32 | use OCP\Security\ICrypto; |
| 33 | 33 | |
| 34 | 34 | class PublicKeyTokenProvider implements IProvider { |
| 35 | - /** @var PublicKeyTokenMapper */ |
|
| 36 | - private $mapper; |
|
| 37 | - |
|
| 38 | - /** @var ICrypto */ |
|
| 39 | - private $crypto; |
|
| 40 | - |
|
| 41 | - /** @var IConfig */ |
|
| 42 | - private $config; |
|
| 43 | - |
|
| 44 | - /** @var ILogger $logger */ |
|
| 45 | - private $logger; |
|
| 46 | - |
|
| 47 | - /** @var ITimeFactory $time */ |
|
| 48 | - private $time; |
|
| 49 | - |
|
| 50 | - public function __construct(PublicKeyTokenMapper $mapper, |
|
| 51 | - ICrypto $crypto, |
|
| 52 | - IConfig $config, |
|
| 53 | - ILogger $logger, |
|
| 54 | - ITimeFactory $time) { |
|
| 55 | - $this->mapper = $mapper; |
|
| 56 | - $this->crypto = $crypto; |
|
| 57 | - $this->config = $config; |
|
| 58 | - $this->logger = $logger; |
|
| 59 | - $this->time = $time; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - public function generateToken(string $token, |
|
| 63 | - string $uid, |
|
| 64 | - string $loginName, |
|
| 65 | - $password, |
|
| 66 | - string $name, |
|
| 67 | - int $type = IToken::TEMPORARY_TOKEN, |
|
| 68 | - int $remember = IToken::DO_NOT_REMEMBER): IToken { |
|
| 69 | - $dbToken = $this->newToken($token, $uid, $loginName, $password, $name, $type, $remember); |
|
| 70 | - |
|
| 71 | - $this->mapper->insert($dbToken); |
|
| 72 | - |
|
| 73 | - return $dbToken; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - public function getToken(string $tokenId): IToken { |
|
| 77 | - try { |
|
| 78 | - $token = $this->mapper->getToken($this->hashToken($tokenId)); |
|
| 79 | - } catch (DoesNotExistException $ex) { |
|
| 80 | - throw new InvalidTokenException(); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - if ((int)$token->getExpires() !== 0 && $token->getExpires() < $this->time->getTime()) { |
|
| 84 | - throw new ExpiredTokenException($token); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - return $token; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - public function getTokenById(int $tokenId): IToken { |
|
| 91 | - try { |
|
| 92 | - $token = $this->mapper->getTokenById($tokenId); |
|
| 93 | - } catch (DoesNotExistException $ex) { |
|
| 94 | - throw new InvalidTokenException(); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - if ((int)$token->getExpires() !== 0 && $token->getExpires() < $this->time->getTime()) { |
|
| 98 | - throw new ExpiredTokenException($token); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - return $token; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - public function renewSessionToken(string $oldSessionId, string $sessionId) { |
|
| 105 | - $token = $this->getToken($oldSessionId); |
|
| 106 | - |
|
| 107 | - if (!($token instanceof PublicKeyToken)) { |
|
| 108 | - throw new InvalidTokenException(); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - $password = null; |
|
| 112 | - if (!is_null($token->getPassword())) { |
|
| 113 | - $privateKey = $this->decrypt($token->getPrivateKey(), $oldSessionId); |
|
| 114 | - $password = $this->decryptPassword($token->getPassword(), $privateKey); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - $this->generateToken( |
|
| 118 | - $sessionId, |
|
| 119 | - $token->getUID(), |
|
| 120 | - $token->getLoginName(), |
|
| 121 | - $password, |
|
| 122 | - $token->getName(), |
|
| 123 | - IToken::TEMPORARY_TOKEN, |
|
| 124 | - $token->getRemember() |
|
| 125 | - ); |
|
| 126 | - |
|
| 127 | - $this->mapper->delete($token); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - public function invalidateToken(string $token) { |
|
| 131 | - $this->mapper->invalidate($this->hashToken($token)); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - public function invalidateTokenById(string $uid, int $id) { |
|
| 135 | - $this->mapper->deleteById($uid, $id); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - public function invalidateOldTokens() { |
|
| 139 | - $olderThan = $this->time->getTime() - (int) $this->config->getSystemValue('session_lifetime', 60 * 60 * 24); |
|
| 140 | - $this->logger->debug('Invalidating session tokens older than ' . date('c', $olderThan), ['app' => 'cron']); |
|
| 141 | - $this->mapper->invalidateOld($olderThan, IToken::DO_NOT_REMEMBER); |
|
| 142 | - $rememberThreshold = $this->time->getTime() - (int) $this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15); |
|
| 143 | - $this->logger->debug('Invalidating remembered session tokens older than ' . date('c', $rememberThreshold), ['app' => 'cron']); |
|
| 144 | - $this->mapper->invalidateOld($rememberThreshold, IToken::REMEMBER); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - public function updateToken(IToken $token) { |
|
| 148 | - if (!($token instanceof PublicKeyToken)) { |
|
| 149 | - throw new InvalidTokenException(); |
|
| 150 | - } |
|
| 151 | - $this->mapper->update($token); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - public function updateTokenActivity(IToken $token) { |
|
| 155 | - if (!($token instanceof PublicKeyToken)) { |
|
| 156 | - throw new InvalidTokenException(); |
|
| 157 | - } |
|
| 158 | - /** @var DefaultToken $token */ |
|
| 159 | - $now = $this->time->getTime(); |
|
| 160 | - if ($token->getLastActivity() < ($now - 60)) { |
|
| 161 | - // Update token only once per minute |
|
| 162 | - $token->setLastActivity($now); |
|
| 163 | - $this->mapper->update($token); |
|
| 164 | - } |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - public function getTokenByUser(string $uid): array { |
|
| 168 | - return $this->mapper->getTokenByUser($uid); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - public function getPassword(IToken $token, string $tokenId): string { |
|
| 172 | - if (!($token instanceof PublicKeyToken)) { |
|
| 173 | - throw new InvalidTokenException(); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - if ($token->getPassword() === null) { |
|
| 177 | - throw new PasswordlessTokenException(); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - // Decrypt private key with tokenId |
|
| 181 | - $privateKey = $this->decrypt($token->getPrivateKey(), $tokenId); |
|
| 182 | - |
|
| 183 | - // Decrypt password with private key |
|
| 184 | - return $this->decryptPassword($token->getPassword(), $privateKey); |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - public function setPassword(IToken $token, string $tokenId, string $password) { |
|
| 188 | - if (!($token instanceof PublicKeyToken)) { |
|
| 189 | - throw new InvalidTokenException(); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - // When changing passwords all temp tokens are deleted |
|
| 193 | - $this->mapper->deleteTempToken($token); |
|
| 194 | - |
|
| 195 | - // Update the password for all tokens |
|
| 196 | - $tokens = $this->mapper->getTokenByUser($token->getUID()); |
|
| 197 | - foreach ($tokens as $t) { |
|
| 198 | - $publicKey = $t->getPublicKey(); |
|
| 199 | - $t->setPassword($this->encryptPassword($password, $publicKey)); |
|
| 200 | - $this->updateToken($t); |
|
| 201 | - } |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken { |
|
| 205 | - if (!($token instanceof PublicKeyToken)) { |
|
| 206 | - throw new InvalidTokenException(); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - // Decrypt private key with oldTokenId |
|
| 210 | - $privateKey = $this->decrypt($token->getPrivateKey(), $oldTokenId); |
|
| 211 | - // Encrypt with the new token |
|
| 212 | - $token->setPrivateKey($this->encrypt($privateKey, $newTokenId)); |
|
| 213 | - |
|
| 214 | - $token->setToken($this->hashToken($newTokenId)); |
|
| 215 | - $this->updateToken($token); |
|
| 216 | - |
|
| 217 | - return $token; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - private function encrypt(string $plaintext, string $token): string { |
|
| 221 | - $secret = $this->config->getSystemValue('secret'); |
|
| 222 | - return $this->crypto->encrypt($plaintext, $token . $secret); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * @throws InvalidTokenException |
|
| 227 | - */ |
|
| 228 | - private function decrypt(string $cipherText, string $token): string { |
|
| 229 | - $secret = $this->config->getSystemValue('secret'); |
|
| 230 | - try { |
|
| 231 | - return $this->crypto->decrypt($cipherText, $token . $secret); |
|
| 232 | - } catch (\Exception $ex) { |
|
| 233 | - // Delete the invalid token |
|
| 234 | - $this->invalidateToken($token); |
|
| 235 | - throw new InvalidTokenException(); |
|
| 236 | - } |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - private function encryptPassword(string $password, string $publicKey): string { |
|
| 240 | - openssl_public_encrypt($password, $encryptedPassword, $publicKey, OPENSSL_PKCS1_OAEP_PADDING); |
|
| 241 | - $encryptedPassword = base64_encode($encryptedPassword); |
|
| 242 | - |
|
| 243 | - return $encryptedPassword; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - private function decryptPassword(string $encryptedPassword, string $privateKey): string { |
|
| 247 | - $encryptedPassword = base64_decode($encryptedPassword); |
|
| 248 | - openssl_private_decrypt($encryptedPassword, $password, $privateKey, OPENSSL_PKCS1_OAEP_PADDING); |
|
| 249 | - |
|
| 250 | - return $password; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - private function hashToken(string $token): string { |
|
| 254 | - $secret = $this->config->getSystemValue('secret'); |
|
| 255 | - return hash('sha512', $token . $secret); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * Convert a DefaultToken to a publicKeyToken |
|
| 260 | - * This will also be updated directly in the Database |
|
| 261 | - */ |
|
| 262 | - public function convertToken(DefaultToken $defaultToken, string $token, $password): PublicKeyToken { |
|
| 263 | - $pkToken = $this->newToken( |
|
| 264 | - $token, |
|
| 265 | - $defaultToken->getUID(), |
|
| 266 | - $defaultToken->getLoginName(), |
|
| 267 | - $password, |
|
| 268 | - $defaultToken->getName(), |
|
| 269 | - $defaultToken->getType(), |
|
| 270 | - $defaultToken->getRemember() |
|
| 271 | - ); |
|
| 272 | - |
|
| 273 | - $pkToken->setExpires($defaultToken->getExpires()); |
|
| 274 | - $pkToken->setId($defaultToken->getId()); |
|
| 275 | - |
|
| 276 | - return $this->mapper->update($pkToken); |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - private function newToken(string $token, |
|
| 280 | - string $uid, |
|
| 281 | - string $loginName, |
|
| 282 | - $password, |
|
| 283 | - string $name, |
|
| 284 | - int $type, |
|
| 285 | - int $remember): PublicKeyToken { |
|
| 286 | - $dbToken = new PublicKeyToken(); |
|
| 287 | - $dbToken->setUid($uid); |
|
| 288 | - $dbToken->setLoginName($loginName); |
|
| 289 | - |
|
| 290 | - $config = [ |
|
| 291 | - 'digest_alg' => 'sha512', |
|
| 292 | - 'private_key_bits' => 2048, |
|
| 293 | - ]; |
|
| 294 | - |
|
| 295 | - // Generate new key |
|
| 296 | - $res = openssl_pkey_new($config); |
|
| 297 | - openssl_pkey_export($res, $privateKey); |
|
| 298 | - |
|
| 299 | - // Extract the public key from $res to $pubKey |
|
| 300 | - $publicKey = openssl_pkey_get_details($res); |
|
| 301 | - $publicKey = $publicKey['key']; |
|
| 302 | - |
|
| 303 | - $dbToken->setPublicKey($publicKey); |
|
| 304 | - $dbToken->setPrivateKey($this->encrypt($privateKey, $token)); |
|
| 305 | - |
|
| 306 | - if (!is_null($password)) { |
|
| 307 | - $dbToken->setPassword($this->encryptPassword($password, $publicKey)); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - $dbToken->setName($name); |
|
| 311 | - $dbToken->setToken($this->hashToken($token)); |
|
| 312 | - $dbToken->setType($type); |
|
| 313 | - $dbToken->setRemember($remember); |
|
| 314 | - $dbToken->setLastActivity($this->time->getTime()); |
|
| 315 | - $dbToken->setLastCheck($this->time->getTime()); |
|
| 316 | - $dbToken->setVersion(PublicKeyToken::VERSION); |
|
| 317 | - |
|
| 318 | - return $dbToken; |
|
| 319 | - } |
|
| 35 | + /** @var PublicKeyTokenMapper */ |
|
| 36 | + private $mapper; |
|
| 37 | + |
|
| 38 | + /** @var ICrypto */ |
|
| 39 | + private $crypto; |
|
| 40 | + |
|
| 41 | + /** @var IConfig */ |
|
| 42 | + private $config; |
|
| 43 | + |
|
| 44 | + /** @var ILogger $logger */ |
|
| 45 | + private $logger; |
|
| 46 | + |
|
| 47 | + /** @var ITimeFactory $time */ |
|
| 48 | + private $time; |
|
| 49 | + |
|
| 50 | + public function __construct(PublicKeyTokenMapper $mapper, |
|
| 51 | + ICrypto $crypto, |
|
| 52 | + IConfig $config, |
|
| 53 | + ILogger $logger, |
|
| 54 | + ITimeFactory $time) { |
|
| 55 | + $this->mapper = $mapper; |
|
| 56 | + $this->crypto = $crypto; |
|
| 57 | + $this->config = $config; |
|
| 58 | + $this->logger = $logger; |
|
| 59 | + $this->time = $time; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + public function generateToken(string $token, |
|
| 63 | + string $uid, |
|
| 64 | + string $loginName, |
|
| 65 | + $password, |
|
| 66 | + string $name, |
|
| 67 | + int $type = IToken::TEMPORARY_TOKEN, |
|
| 68 | + int $remember = IToken::DO_NOT_REMEMBER): IToken { |
|
| 69 | + $dbToken = $this->newToken($token, $uid, $loginName, $password, $name, $type, $remember); |
|
| 70 | + |
|
| 71 | + $this->mapper->insert($dbToken); |
|
| 72 | + |
|
| 73 | + return $dbToken; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + public function getToken(string $tokenId): IToken { |
|
| 77 | + try { |
|
| 78 | + $token = $this->mapper->getToken($this->hashToken($tokenId)); |
|
| 79 | + } catch (DoesNotExistException $ex) { |
|
| 80 | + throw new InvalidTokenException(); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + if ((int)$token->getExpires() !== 0 && $token->getExpires() < $this->time->getTime()) { |
|
| 84 | + throw new ExpiredTokenException($token); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + return $token; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + public function getTokenById(int $tokenId): IToken { |
|
| 91 | + try { |
|
| 92 | + $token = $this->mapper->getTokenById($tokenId); |
|
| 93 | + } catch (DoesNotExistException $ex) { |
|
| 94 | + throw new InvalidTokenException(); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + if ((int)$token->getExpires() !== 0 && $token->getExpires() < $this->time->getTime()) { |
|
| 98 | + throw new ExpiredTokenException($token); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + return $token; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + public function renewSessionToken(string $oldSessionId, string $sessionId) { |
|
| 105 | + $token = $this->getToken($oldSessionId); |
|
| 106 | + |
|
| 107 | + if (!($token instanceof PublicKeyToken)) { |
|
| 108 | + throw new InvalidTokenException(); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + $password = null; |
|
| 112 | + if (!is_null($token->getPassword())) { |
|
| 113 | + $privateKey = $this->decrypt($token->getPrivateKey(), $oldSessionId); |
|
| 114 | + $password = $this->decryptPassword($token->getPassword(), $privateKey); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + $this->generateToken( |
|
| 118 | + $sessionId, |
|
| 119 | + $token->getUID(), |
|
| 120 | + $token->getLoginName(), |
|
| 121 | + $password, |
|
| 122 | + $token->getName(), |
|
| 123 | + IToken::TEMPORARY_TOKEN, |
|
| 124 | + $token->getRemember() |
|
| 125 | + ); |
|
| 126 | + |
|
| 127 | + $this->mapper->delete($token); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + public function invalidateToken(string $token) { |
|
| 131 | + $this->mapper->invalidate($this->hashToken($token)); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + public function invalidateTokenById(string $uid, int $id) { |
|
| 135 | + $this->mapper->deleteById($uid, $id); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + public function invalidateOldTokens() { |
|
| 139 | + $olderThan = $this->time->getTime() - (int) $this->config->getSystemValue('session_lifetime', 60 * 60 * 24); |
|
| 140 | + $this->logger->debug('Invalidating session tokens older than ' . date('c', $olderThan), ['app' => 'cron']); |
|
| 141 | + $this->mapper->invalidateOld($olderThan, IToken::DO_NOT_REMEMBER); |
|
| 142 | + $rememberThreshold = $this->time->getTime() - (int) $this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15); |
|
| 143 | + $this->logger->debug('Invalidating remembered session tokens older than ' . date('c', $rememberThreshold), ['app' => 'cron']); |
|
| 144 | + $this->mapper->invalidateOld($rememberThreshold, IToken::REMEMBER); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + public function updateToken(IToken $token) { |
|
| 148 | + if (!($token instanceof PublicKeyToken)) { |
|
| 149 | + throw new InvalidTokenException(); |
|
| 150 | + } |
|
| 151 | + $this->mapper->update($token); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + public function updateTokenActivity(IToken $token) { |
|
| 155 | + if (!($token instanceof PublicKeyToken)) { |
|
| 156 | + throw new InvalidTokenException(); |
|
| 157 | + } |
|
| 158 | + /** @var DefaultToken $token */ |
|
| 159 | + $now = $this->time->getTime(); |
|
| 160 | + if ($token->getLastActivity() < ($now - 60)) { |
|
| 161 | + // Update token only once per minute |
|
| 162 | + $token->setLastActivity($now); |
|
| 163 | + $this->mapper->update($token); |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + public function getTokenByUser(string $uid): array { |
|
| 168 | + return $this->mapper->getTokenByUser($uid); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + public function getPassword(IToken $token, string $tokenId): string { |
|
| 172 | + if (!($token instanceof PublicKeyToken)) { |
|
| 173 | + throw new InvalidTokenException(); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + if ($token->getPassword() === null) { |
|
| 177 | + throw new PasswordlessTokenException(); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + // Decrypt private key with tokenId |
|
| 181 | + $privateKey = $this->decrypt($token->getPrivateKey(), $tokenId); |
|
| 182 | + |
|
| 183 | + // Decrypt password with private key |
|
| 184 | + return $this->decryptPassword($token->getPassword(), $privateKey); |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + public function setPassword(IToken $token, string $tokenId, string $password) { |
|
| 188 | + if (!($token instanceof PublicKeyToken)) { |
|
| 189 | + throw new InvalidTokenException(); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + // When changing passwords all temp tokens are deleted |
|
| 193 | + $this->mapper->deleteTempToken($token); |
|
| 194 | + |
|
| 195 | + // Update the password for all tokens |
|
| 196 | + $tokens = $this->mapper->getTokenByUser($token->getUID()); |
|
| 197 | + foreach ($tokens as $t) { |
|
| 198 | + $publicKey = $t->getPublicKey(); |
|
| 199 | + $t->setPassword($this->encryptPassword($password, $publicKey)); |
|
| 200 | + $this->updateToken($t); |
|
| 201 | + } |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken { |
|
| 205 | + if (!($token instanceof PublicKeyToken)) { |
|
| 206 | + throw new InvalidTokenException(); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + // Decrypt private key with oldTokenId |
|
| 210 | + $privateKey = $this->decrypt($token->getPrivateKey(), $oldTokenId); |
|
| 211 | + // Encrypt with the new token |
|
| 212 | + $token->setPrivateKey($this->encrypt($privateKey, $newTokenId)); |
|
| 213 | + |
|
| 214 | + $token->setToken($this->hashToken($newTokenId)); |
|
| 215 | + $this->updateToken($token); |
|
| 216 | + |
|
| 217 | + return $token; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + private function encrypt(string $plaintext, string $token): string { |
|
| 221 | + $secret = $this->config->getSystemValue('secret'); |
|
| 222 | + return $this->crypto->encrypt($plaintext, $token . $secret); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * @throws InvalidTokenException |
|
| 227 | + */ |
|
| 228 | + private function decrypt(string $cipherText, string $token): string { |
|
| 229 | + $secret = $this->config->getSystemValue('secret'); |
|
| 230 | + try { |
|
| 231 | + return $this->crypto->decrypt($cipherText, $token . $secret); |
|
| 232 | + } catch (\Exception $ex) { |
|
| 233 | + // Delete the invalid token |
|
| 234 | + $this->invalidateToken($token); |
|
| 235 | + throw new InvalidTokenException(); |
|
| 236 | + } |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + private function encryptPassword(string $password, string $publicKey): string { |
|
| 240 | + openssl_public_encrypt($password, $encryptedPassword, $publicKey, OPENSSL_PKCS1_OAEP_PADDING); |
|
| 241 | + $encryptedPassword = base64_encode($encryptedPassword); |
|
| 242 | + |
|
| 243 | + return $encryptedPassword; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + private function decryptPassword(string $encryptedPassword, string $privateKey): string { |
|
| 247 | + $encryptedPassword = base64_decode($encryptedPassword); |
|
| 248 | + openssl_private_decrypt($encryptedPassword, $password, $privateKey, OPENSSL_PKCS1_OAEP_PADDING); |
|
| 249 | + |
|
| 250 | + return $password; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + private function hashToken(string $token): string { |
|
| 254 | + $secret = $this->config->getSystemValue('secret'); |
|
| 255 | + return hash('sha512', $token . $secret); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * Convert a DefaultToken to a publicKeyToken |
|
| 260 | + * This will also be updated directly in the Database |
|
| 261 | + */ |
|
| 262 | + public function convertToken(DefaultToken $defaultToken, string $token, $password): PublicKeyToken { |
|
| 263 | + $pkToken = $this->newToken( |
|
| 264 | + $token, |
|
| 265 | + $defaultToken->getUID(), |
|
| 266 | + $defaultToken->getLoginName(), |
|
| 267 | + $password, |
|
| 268 | + $defaultToken->getName(), |
|
| 269 | + $defaultToken->getType(), |
|
| 270 | + $defaultToken->getRemember() |
|
| 271 | + ); |
|
| 272 | + |
|
| 273 | + $pkToken->setExpires($defaultToken->getExpires()); |
|
| 274 | + $pkToken->setId($defaultToken->getId()); |
|
| 275 | + |
|
| 276 | + return $this->mapper->update($pkToken); |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + private function newToken(string $token, |
|
| 280 | + string $uid, |
|
| 281 | + string $loginName, |
|
| 282 | + $password, |
|
| 283 | + string $name, |
|
| 284 | + int $type, |
|
| 285 | + int $remember): PublicKeyToken { |
|
| 286 | + $dbToken = new PublicKeyToken(); |
|
| 287 | + $dbToken->setUid($uid); |
|
| 288 | + $dbToken->setLoginName($loginName); |
|
| 289 | + |
|
| 290 | + $config = [ |
|
| 291 | + 'digest_alg' => 'sha512', |
|
| 292 | + 'private_key_bits' => 2048, |
|
| 293 | + ]; |
|
| 294 | + |
|
| 295 | + // Generate new key |
|
| 296 | + $res = openssl_pkey_new($config); |
|
| 297 | + openssl_pkey_export($res, $privateKey); |
|
| 298 | + |
|
| 299 | + // Extract the public key from $res to $pubKey |
|
| 300 | + $publicKey = openssl_pkey_get_details($res); |
|
| 301 | + $publicKey = $publicKey['key']; |
|
| 302 | + |
|
| 303 | + $dbToken->setPublicKey($publicKey); |
|
| 304 | + $dbToken->setPrivateKey($this->encrypt($privateKey, $token)); |
|
| 305 | + |
|
| 306 | + if (!is_null($password)) { |
|
| 307 | + $dbToken->setPassword($this->encryptPassword($password, $publicKey)); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + $dbToken->setName($name); |
|
| 311 | + $dbToken->setToken($this->hashToken($token)); |
|
| 312 | + $dbToken->setType($type); |
|
| 313 | + $dbToken->setRemember($remember); |
|
| 314 | + $dbToken->setLastActivity($this->time->getTime()); |
|
| 315 | + $dbToken->setLastCheck($this->time->getTime()); |
|
| 316 | + $dbToken->setVersion(PublicKeyToken::VERSION); |
|
| 317 | + |
|
| 318 | + return $dbToken; |
|
| 319 | + } |
|
| 320 | 320 | } |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | throw new InvalidTokenException(); |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | - if ((int)$token->getExpires() !== 0 && $token->getExpires() < $this->time->getTime()) { |
|
| 83 | + if ((int) $token->getExpires() !== 0 && $token->getExpires() < $this->time->getTime()) { |
|
| 84 | 84 | throw new ExpiredTokenException($token); |
| 85 | 85 | } |
| 86 | 86 | |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | throw new InvalidTokenException(); |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | - if ((int)$token->getExpires() !== 0 && $token->getExpires() < $this->time->getTime()) { |
|
| 97 | + if ((int) $token->getExpires() !== 0 && $token->getExpires() < $this->time->getTime()) { |
|
| 98 | 98 | throw new ExpiredTokenException($token); |
| 99 | 99 | } |
| 100 | 100 | |
@@ -137,10 +137,10 @@ discard block |
||
| 137 | 137 | |
| 138 | 138 | public function invalidateOldTokens() { |
| 139 | 139 | $olderThan = $this->time->getTime() - (int) $this->config->getSystemValue('session_lifetime', 60 * 60 * 24); |
| 140 | - $this->logger->debug('Invalidating session tokens older than ' . date('c', $olderThan), ['app' => 'cron']); |
|
| 140 | + $this->logger->debug('Invalidating session tokens older than '.date('c', $olderThan), ['app' => 'cron']); |
|
| 141 | 141 | $this->mapper->invalidateOld($olderThan, IToken::DO_NOT_REMEMBER); |
| 142 | 142 | $rememberThreshold = $this->time->getTime() - (int) $this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15); |
| 143 | - $this->logger->debug('Invalidating remembered session tokens older than ' . date('c', $rememberThreshold), ['app' => 'cron']); |
|
| 143 | + $this->logger->debug('Invalidating remembered session tokens older than '.date('c', $rememberThreshold), ['app' => 'cron']); |
|
| 144 | 144 | $this->mapper->invalidateOld($rememberThreshold, IToken::REMEMBER); |
| 145 | 145 | } |
| 146 | 146 | |
@@ -219,7 +219,7 @@ discard block |
||
| 219 | 219 | |
| 220 | 220 | private function encrypt(string $plaintext, string $token): string { |
| 221 | 221 | $secret = $this->config->getSystemValue('secret'); |
| 222 | - return $this->crypto->encrypt($plaintext, $token . $secret); |
|
| 222 | + return $this->crypto->encrypt($plaintext, $token.$secret); |
|
| 223 | 223 | } |
| 224 | 224 | |
| 225 | 225 | /** |
@@ -228,7 +228,7 @@ discard block |
||
| 228 | 228 | private function decrypt(string $cipherText, string $token): string { |
| 229 | 229 | $secret = $this->config->getSystemValue('secret'); |
| 230 | 230 | try { |
| 231 | - return $this->crypto->decrypt($cipherText, $token . $secret); |
|
| 231 | + return $this->crypto->decrypt($cipherText, $token.$secret); |
|
| 232 | 232 | } catch (\Exception $ex) { |
| 233 | 233 | // Delete the invalid token |
| 234 | 234 | $this->invalidateToken($token); |
@@ -252,7 +252,7 @@ discard block |
||
| 252 | 252 | |
| 253 | 253 | private function hashToken(string $token): string { |
| 254 | 254 | $secret = $this->config->getSystemValue('secret'); |
| 255 | - return hash('sha512', $token . $secret); |
|
| 255 | + return hash('sha512', $token.$secret); |
|
| 256 | 256 | } |
| 257 | 257 | |
| 258 | 258 | /** |