Skip to content

Commit ca0fbac

Browse files
authored
Merge pull request #16882 from nextcloud/enh/apppassword_rotation
Allow rotation of apppasswords
2 parents 560b985 + cd1f443 commit ca0fbac

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

core/Controller/AppPasswordController.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,28 @@ public function deleteAppPassword() {
138138
$this->tokenProvider->invalidateTokenById($token->getUID(), $token->getId());
139139
return new DataResponse();
140140
}
141+
142+
/**
143+
* @NoAdminRequired
144+
*/
145+
public function rotateAppPassword(): DataResponse {
146+
if (!$this->session->exists('app_password')) {
147+
throw new OCSForbiddenException('no app password in use');
148+
}
149+
150+
$appPassword = $this->session->get('app_password');
151+
152+
try {
153+
$token = $this->tokenProvider->getToken($appPassword);
154+
} catch (InvalidTokenException $e) {
155+
throw new OCSForbiddenException('could not rotate apptoken');
156+
}
157+
158+
$newToken = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
159+
$this->tokenProvider->rotate($token, $appPassword, $newToken);
160+
161+
return new DataResponse([
162+
'apppassword' => $newToken,
163+
]);
164+
}
141165
}

core/routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
['root' => '/core', 'name' => 'WhatsNew#get', 'url' => '/whatsnew', 'verb' => 'GET'],
108108
['root' => '/core', 'name' => 'WhatsNew#dismiss', 'url' => '/whatsnew', 'verb' => 'POST'],
109109
['root' => '/core', 'name' => 'AppPassword#getAppPassword', 'url' => '/getapppassword', 'verb' => 'GET'],
110+
['root' => '/core', 'name' => 'AppPassword#rotateAppPassword', 'url' => '/apppassword/rotate', 'verb' => 'POST'],
110111
['root' => '/core', 'name' => 'AppPassword#deleteAppPassword', 'url' => '/apppassword', 'verb' => 'DELETE'],
111112

112113
['root' => '/collaboration', 'name' => 'CollaborationResources#searchCollections', 'url' => '/resources/collections/search/{filter}', 'verb' => 'GET'],

0 commit comments

Comments
 (0)