Add publickey provider to manager

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2018-05-29 13:38:26 +02:00
parent 4bbc21cb21
commit d03d16a936
No known key found for this signature in database
GPG Key ID: F941078878347C0C
1 changed files with 56 additions and 51 deletions

View File

@ -32,8 +32,12 @@ class Manager implements IProvider {
/** @var DefaultTokenProvider */ /** @var DefaultTokenProvider */
private $defaultTokenProvider; private $defaultTokenProvider;
public function __construct(DefaultTokenProvider $defaultTokenProvider) { /** @var PublicKeyTokenProvider */
private $publicKeyTokenProvider;
public function __construct(DefaultTokenProvider $defaultTokenProvider, PublicKeyTokenProvider $publicKeyTokenProvider) {
$this->defaultTokenProvider = $defaultTokenProvider; $this->defaultTokenProvider = $defaultTokenProvider;
$this->publicKeyTokenProvider = $publicKeyTokenProvider;
} }
/** /**
@ -76,6 +80,8 @@ class Manager implements IProvider {
public function updateToken(IToken $token) { public function updateToken(IToken $token) {
if ($token instanceof DefaultToken) { if ($token instanceof DefaultToken) {
$this->defaultTokenProvider->updateToken($token); $this->defaultTokenProvider->updateToken($token);
} else if ($token instanceof PublicKeyToken) {
$this->publicKeyTokenProvider->updateToken($token);
} else { } else {
throw new InvalidTokenException(); throw new InvalidTokenException();
} }
@ -90,22 +96,18 @@ class Manager implements IProvider {
public function updateTokenActivity(IToken $token) { public function updateTokenActivity(IToken $token) {
if ($token instanceof DefaultToken) { if ($token instanceof DefaultToken) {
$this->defaultTokenProvider->updateTokenActivity($token); $this->defaultTokenProvider->updateTokenActivity($token);
} else if ($token instanceof PublicKeyToken) {
$this->publicKeyTokenProvider->updateTokenActivity($token);
} else { } else {
throw new InvalidTokenException(); throw new InvalidTokenException();
} }
} }
/**
* Get all tokens of a user
*
* The provider may limit the number of result rows in case of an abuse
* where a high number of (session) tokens is generated
*
* @param IUser $user
* @return IToken[]
*/
public function getTokenByUser(string $uid): array { public function getTokenByUser(string $uid): array {
return $this->defaultTokenProvider->getTokenByUser($uid); $old = $this->defaultTokenProvider->getTokenByUser($uid);
$new = $this->publicKeyTokenProvider->getTokenByUser($uid);
return array_merge($old, $new);
} }
/** /**
@ -116,8 +118,11 @@ class Manager implements IProvider {
* @return IToken * @return IToken
*/ */
public function getToken(string $tokenId): IToken { public function getToken(string $tokenId): IToken {
// TODO: first try new token then old token try {
return $this->defaultTokenProvider->getToken($tokenId); return $this->publicKeyTokenProvider->getToken($tokenId);
} catch (InvalidTokenException $e) {
return $this->defaultTokenProvider->getToken($tokenId);
}
} }
/** /**
@ -128,8 +133,11 @@ class Manager implements IProvider {
* @return IToken * @return IToken
*/ */
public function getTokenById(int $tokenId): IToken { public function getTokenById(int $tokenId): IToken {
// TODO: Find a way to distinguis between tokens try {
return $this->defaultTokenProvider->getTokenById($tokenId); $this->publicKeyTokenProvider->getTokenById($tokenId);
} catch (InvalidTokenException $e) {
return $this->defaultTokenProvider->getTokenById($tokenId);
}
} }
/** /**
@ -138,9 +146,12 @@ class Manager implements IProvider {
* @throws InvalidTokenException * @throws InvalidTokenException
*/ */
public function renewSessionToken(string $oldSessionId, string $sessionId) { public function renewSessionToken(string $oldSessionId, string $sessionId) {
// TODO: first try new then old try {
// TODO: if old move to new token type $this->publicKeyTokenProvider->renewSessionToken($oldSessionId, $sessionId);
$this->defaultTokenProvider->renewSessionToken($oldSessionId, $sessionId); } catch (InvalidTokenException $e) {
//TODO: Move to new token
$this->defaultTokenProvider->renewSessionToken($oldSessionId, $sessionId);
}
} }
/** /**
@ -151,59 +162,53 @@ class Manager implements IProvider {
* @return string * @return string
*/ */
public function getPassword(IToken $savedToken, string $tokenId): string { public function getPassword(IToken $savedToken, string $tokenId): string {
//TODO convert to new token type
if ($savedToken instanceof DefaultToken) { if ($savedToken instanceof DefaultToken) {
//TODO convert to new token type
return $this->defaultTokenProvider->getPassword($savedToken, $tokenId); return $this->defaultTokenProvider->getPassword($savedToken, $tokenId);
} }
}
/** if ($savedToken instanceof PublicKeyToken) {
* Encrypt and set the password of the given token return $this->publicKeyTokenProvider->getPassword($savedToken, $tokenId);
* }
* @param IToken $token }
* @param string $tokenId
* @param string $password public function setPassword(IToken $token, string $tokenId, string $password) {
* @throws InvalidTokenException
*/ if ($token instanceof DefaultToken) {
public function setPassword(IToken $token, string $tokenId, string $password) { //TODO conver to new token
//TODO conver to new token $this->defaultTokenProvider->setPassword($token, $tokenId, $password);
if ($token instanceof DefaultToken) { }
$this->defaultTokenProvider->setPassword($token, $tokenId, $password);
if ($tokenId instanceof PublicKeyToken) {
$this->publicKeyTokenProvider->setPassword($token, $tokenId, $password);
} }
} }
/**
* Invalidate (delete) the given session token
*
* @param string $token
*/
public function invalidateToken(string $token) { public function invalidateToken(string $token) {
// TODO: check both providers
$this->defaultTokenProvider->invalidateToken($token); $this->defaultTokenProvider->invalidateToken($token);
$this->publicKeyTokenProvider->invalidateToken($token);
} }
/**
* Invalidate (delete) the given token
*
* @param IUser $user
* @param int $id
*/
public function invalidateTokenById(string $uid, int $id) { public function invalidateTokenById(string $uid, int $id) {
//TODO find way to distinguis between tokens
$this->defaultTokenProvider->invalidateTokenById($uid, $id); $this->defaultTokenProvider->invalidateTokenById($uid, $id);
$this->publicKeyTokenProvider->invalidateTokenById($uid, $id);
} }
/**
* Invalidate (delete) old session tokens
*/
public function invalidateOldTokens() { public function invalidateOldTokens() {
//Call on both
$this->defaultTokenProvider->invalidateOldTokens(); $this->defaultTokenProvider->invalidateOldTokens();
$this->publicKeyTokenProvider->invalidateOldTokens();
} }
public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken { public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken {
// Migrate to new token
return $this->defaultTokenProvider->rotate($token, $oldTokenId, $newTokenId); if ($token instanceof DefaultToken) {
//TODO Migrate to new token
return $this->defaultTokenProvider->rotate($token, $oldTokenId, $newTokenId);
}
if ($token instanceof PublicKeyToken) {
return $this->publicKeyTokenProvider->rotate($token, $oldTokenId, $newTokenId);
}
} }