summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/private/Authentication/Token/DefaultToken.php10
-rw-r--r--lib/private/Authentication/Token/DefaultTokenProvider.php22
-rw-r--r--lib/private/Authentication/Token/IProvider.php10
-rw-r--r--lib/private/Authentication/Token/IToken.php14
-rw-r--r--tests/lib/Authentication/Token/DefaultTokenProviderTest.php42
5 files changed, 96 insertions, 2 deletions
diff --git a/lib/private/Authentication/Token/DefaultToken.php b/lib/private/Authentication/Token/DefaultToken.php
index e06803d0bfc..52dd6644d2e 100644
--- a/lib/private/Authentication/Token/DefaultToken.php
+++ b/lib/private/Authentication/Token/DefaultToken.php
@@ -29,10 +29,8 @@ use OCP\AppFramework\Db\Entity;
* @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 string getName()
- * @method void setToken(string $token)
* @method string getToken()
* @method void setType(string $type)
* @method int getType()
@@ -174,4 +172,12 @@ class DefaultToken extends Entity implements IToken {
parent::setScope((string)$scope);
}
}
+
+ public function setToken($token) {
+ parent::setToken($token);
+ }
+
+ public function setPassword($password = null) {
+ parent::setPassword($password);
+ }
}
diff --git a/lib/private/Authentication/Token/DefaultTokenProvider.php b/lib/private/Authentication/Token/DefaultTokenProvider.php
index 36a8b1d5464..13407a688d3 100644
--- a/lib/private/Authentication/Token/DefaultTokenProvider.php
+++ b/lib/private/Authentication/Token/DefaultTokenProvider.php
@@ -267,6 +267,28 @@ class DefaultTokenProvider implements IProvider {
}
/**
+ * 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, $oldTokenId, $newTokenId) {
+ 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
*/
diff --git a/lib/private/Authentication/Token/IProvider.php b/lib/private/Authentication/Token/IProvider.php
index e1cc8182ff0..707645a09e9 100644
--- a/lib/private/Authentication/Token/IProvider.php
+++ b/lib/private/Authentication/Token/IProvider.php
@@ -138,4 +138,14 @@ interface IProvider {
* @throws InvalidTokenException
*/
public function setPassword(IToken $token, $tokenId, $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, $oldTokenId, $newTokenId);
}
diff --git a/lib/private/Authentication/Token/IToken.php b/lib/private/Authentication/Token/IToken.php
index a24d31e2ed2..0e32e3adfd6 100644
--- a/lib/private/Authentication/Token/IToken.php
+++ b/lib/private/Authentication/Token/IToken.php
@@ -94,4 +94,18 @@ interface IToken extends JsonSerializable {
* @param array $scope
*/
public function setScope($scope);
+
+ /**
+ * Set the token
+ *
+ * @param string $token
+ */
+ public function setToken($token);
+
+ /**
+ * Set the password
+ *
+ * @param string $password
+ */
+ public function setPassword($password);
}
diff --git a/tests/lib/Authentication/Token/DefaultTokenProviderTest.php b/tests/lib/Authentication/Token/DefaultTokenProviderTest.php
index 08c74961c0d..e2643d315ce 100644
--- a/tests/lib/Authentication/Token/DefaultTokenProviderTest.php
+++ b/tests/lib/Authentication/Token/DefaultTokenProviderTest.php
@@ -418,4 +418,46 @@ class DefaultTokenProviderTest extends TestCase {
$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');
+ }
}