Make renewSessionToken return the new token

Avoids directly getting the token again. We just inserted it so it and
have all the info. So that query is just a waste.

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2019-10-08 11:01:53 +02:00
parent f663154adf
commit 5122629bb0
No known key found for this signature in database
GPG Key ID: F941078878347C0C
6 changed files with 24 additions and 19 deletions

View File

@ -196,8 +196,9 @@ class DefaultTokenProvider implements IProvider {
* @param string $oldSessionId
* @param string $sessionId
* @throws InvalidTokenException
* @return IToken
*/
public function renewSessionToken(string $oldSessionId, string $sessionId) {
public function renewSessionToken(string $oldSessionId, string $sessionId): IToken {
$token = $this->getToken($oldSessionId);
$newToken = new DefaultToken();
@ -214,6 +215,8 @@ class DefaultTokenProvider implements IProvider {
$newToken->setLastActivity($this->time->getTime());
$this->mapper->insert($newToken);
$this->mapper->delete($token);
return $newToken;
}
/**

View File

@ -84,8 +84,9 @@ interface IProvider {
* @param string $sessionId
* @throws InvalidTokenException
* @throws \RuntimeException when OpenSSL reports a problem
* @return IToken The new token
*/
public function renewSessionToken(string $oldSessionId, string $sessionId);
public function renewSessionToken(string $oldSessionId, string $sessionId): IToken;
/**
* Invalidate (delete) the given session token

View File

@ -158,14 +158,15 @@ class Manager implements IProvider {
* @param string $oldSessionId
* @param string $sessionId
* @throws InvalidTokenException
* @return IToken
*/
public function renewSessionToken(string $oldSessionId, string $sessionId) {
public function renewSessionToken(string $oldSessionId, string $sessionId): IToken {
try {
$this->publicKeyTokenProvider->renewSessionToken($oldSessionId, $sessionId);
return $this->publicKeyTokenProvider->renewSessionToken($oldSessionId, $sessionId);
} catch (ExpiredTokenException $e) {
throw $e;
} catch (InvalidTokenException $e) {
$this->defaultTokenProvider->renewSessionToken($oldSessionId, $sessionId);
return $this->defaultTokenProvider->renewSessionToken($oldSessionId, $sessionId);
}
}

View File

@ -129,7 +129,7 @@ class PublicKeyTokenProvider implements IProvider {
return $token;
}
public function renewSessionToken(string $oldSessionId, string $sessionId) {
public function renewSessionToken(string $oldSessionId, string $sessionId): IToken {
$this->cache->clear();
$token = $this->getToken($oldSessionId);
@ -144,7 +144,7 @@ class PublicKeyTokenProvider implements IProvider {
$password = $this->decryptPassword($token->getPassword(), $privateKey);
}
$this->generateToken(
$newToken = $this->generateToken(
$sessionId,
$token->getUID(),
$token->getLoginName(),
@ -155,6 +155,8 @@ class PublicKeyTokenProvider implements IProvider {
);
$this->mapper->delete($token);
return $newToken;
}
public function invalidateToken(string $token) {

View File

@ -861,7 +861,7 @@ class Session implements IUserSession, Emitter {
try {
$sessionId = $this->session->getId();
$this->tokenProvider->renewSessionToken($oldSessionId, $sessionId);
$token = $this->tokenProvider->renewSessionToken($oldSessionId, $sessionId);
} catch (SessionNotAvailableException $ex) {
return false;
} catch (InvalidTokenException $ex) {
@ -870,7 +870,6 @@ class Session implements IUserSession, Emitter {
}
$this->setMagicInCookie($user->getUID(), $newToken);
$token = $this->tokenProvider->getToken($sessionId);
//login
$this->setUser($user);

View File

@ -595,25 +595,24 @@ class SessionTest extends \Test\TestCase {
->method('setUserValue')
->with('foo', 'login_token', 'abcdefg123456', 10000);
$session->expects($this->once())
->method('getId')
->will($this->returnValue($sessionId));
$this->tokenProvider->expects($this->once())
->method('renewSessionToken')
->with($oldSessionId, $sessionId)
->will($this->returnValue(true));
$tokenObject = $this->createMock(IToken::class);
$tokenObject->expects($this->once())
->method('getLoginName')
->willReturn('foobar');
$tokenObject->method('getId')
->willReturn(42);
$session->expects($this->once())
->method('getId')
->will($this->returnValue($sessionId));
$this->tokenProvider->expects($this->once())
->method('getToken')
->with($sessionId)
->method('renewSessionToken')
->with($oldSessionId, $sessionId)
->willReturn($tokenObject);
$this->tokenProvider->expects($this->never())
->method('getToken');
$user->expects($this->any())
->method('getUID')
->will($this->returnValue('foo'));