Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/private/Authentication/Token/DefaultToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
* @method void setId(int $id)
* @method void setUid(string $uid);
* @method void setLoginName(string $loginname)
* @method void setPassword(string $password)
* @method void setName(string $name)
* @method void setToken(string $token)
* @method string getToken()
* @method void setType(int $type)
* @method int getType()
Expand Down Expand Up @@ -173,4 +171,12 @@ public function getName(): string {
public function getRemember(): int {
return parent::getRemember();
}

public function setToken(string $token) {
parent::setToken($token);
}

public function setPassword(string $password = null) {
parent::setPassword($password);
}
}
22 changes: 22 additions & 0 deletions lib/private/Authentication/Token/DefaultTokenProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,28 @@ public function invalidateOldTokens() {
$this->mapper->invalidateOld($rememberThreshold, IToken::REMEMBER);
}

/**
* Rotate the token. Usefull for for example oauth tokens
*
* @param IToken $token
* @param string $oldTokenId
* @param string $newTokenId
* @return IToken
*/
public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken {
try {
$password = $this->getPassword($token, $oldTokenId);
$token->setPassword($this->encryptPassword($password, $newTokenId));
} catch (PasswordlessTokenException $e) {

}

$token->setToken($this->hashToken($newTokenId));
$this->updateToken($token);

return $token;
}

/**
* @param string $token
* @return string
Expand Down
10 changes: 10 additions & 0 deletions lib/private/Authentication/Token/IProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,14 @@ public function getPassword(IToken $token, string $tokenId): string;
* @throws InvalidTokenException
*/
public function setPassword(IToken $token, string $tokenId, string $password);

/**
* Rotate the token. Usefull for for example oauth tokens
*
* @param IToken $token
* @param string $oldTokenId
* @param string $newTokenId
* @return IToken
*/
public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken;
}
23 changes: 23 additions & 0 deletions lib/private/Authentication/Token/IToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,30 @@ public function getScopeAsArray(): array;
*/
public function setScope($scope);

/**
* Get the name of the token
* @return string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick for future PRs: return type annotations are not necessary here 😉

*/
public function getName(): string;

/**
* Get the remember state of the token
*
* @return int
*/
public function getRemember(): int;

/**
* Set the token
*
* @param string $token
*/
public function setToken(string $token);

/**
* Set the password
*
* @param string $password
*/
public function setPassword(string $password);
}
42 changes: 42 additions & 0 deletions tests/lib/Authentication/Token/DefaultTokenProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,46 @@ public function testGetInvalidTokenById() {

$this->tokenProvider->getTokenById(42);
}

public function testRotate() {
$token = new DefaultToken();
$token->setPassword('oldencryptedpassword');

$this->config->method('getSystemValue')
->with('secret')
->willReturn('mysecret');

$this->crypto->method('decrypt')
->with('oldencryptedpassword', 'oldtokenmysecret')
->willReturn('mypassword');
$this->crypto->method('encrypt')
->with('mypassword', 'newtokenmysecret')
->willReturn('newencryptedpassword');

$this->mapper->expects($this->once())
->method('update')
->with($this->callback(function (DefaultToken $token) {
return $token->getPassword() === 'newencryptedpassword' &&
$token->getToken() === hash('sha512', 'newtokenmysecret');
}));

$this->tokenProvider->rotate($token, 'oldtoken', 'newtoken');
}

public function testRotateNoPassword() {
$token = new DefaultToken();

$this->config->method('getSystemValue')
->with('secret')
->willReturn('mysecret');

$this->mapper->expects($this->once())
->method('update')
->with($this->callback(function (DefaultToken $token) {
return $token->getPassword() === null &&
$token->getToken() === hash('sha512', 'newtokenmysecret');
}));

$this->tokenProvider->rotate($token, 'oldtoken', 'newtoken');
}
}