summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2018-10-03 22:26:26 +0200
committerGitHub <noreply@github.com>2018-10-03 22:26:26 +0200
commitac330077d83d699234e0d8f112c9feca95017d98 (patch)
treee6ff0a1c657d6aa76f9555276c12350e21818aef /core
parent67d04f2d57f676b1271bfeb248cb1b915f4fa475 (diff)
parent78273cb1e61870782d36e7b045abdfcee0cb647b (diff)
downloadnextcloud-server-ac330077d83d699234e0d8f112c9feca95017d98.tar.gz
nextcloud-server-ac330077d83d699234e0d8f112c9feca95017d98.zip
Merge pull request #11576 from nextcloud/feature/endpoint_for_clients_to_fetch_apppassword
Allow clients to request an apppassword if they still use the real password
Diffstat (limited to 'core')
-rw-r--r--core/Controller/AppPasswordController.php108
-rw-r--r--core/routes.php1
2 files changed, 109 insertions, 0 deletions
diff --git a/core/Controller/AppPasswordController.php b/core/Controller/AppPasswordController.php
new file mode 100644
index 00000000000..ce06730694e
--- /dev/null
+++ b/core/Controller/AppPasswordController.php
@@ -0,0 +1,108 @@
+<?php
+declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OC\Core\Controller;
+
+use OC\Authentication\Token\IProvider;
+use OC\Authentication\Token\IToken;
+use OCP\AppFramework\Http\DataResponse;
+use OCP\AppFramework\OCS\OCSForbiddenException;
+use OCP\Authentication\Exceptions\CredentialsUnavailableException;
+use OCP\Authentication\Exceptions\PasswordUnavailableException;
+use OCP\Authentication\LoginCredentials\IStore;
+use OCP\IRequest;
+use OCP\ISession;
+use OCP\Security\ISecureRandom;
+
+class AppPasswordController extends \OCP\AppFramework\OCSController {
+
+ /** @var ISession */
+ private $session;
+
+ /** @var ISecureRandom */
+ private $random;
+
+ /** @var IProvider */
+ private $tokenProvider;
+
+ /** @var IStore */
+ private $credentialStore;
+
+ public function __construct(string $appName,
+ IRequest $request,
+ ISession $session,
+ ISecureRandom $random,
+ IProvider $tokenProvider,
+ IStore $credentialStore) {
+ parent::__construct($appName, $request);
+
+ $this->session = $session;
+ $this->random = $random;
+ $this->tokenProvider = $tokenProvider;
+ $this->credentialStore = $credentialStore;
+ }
+
+ /**
+ * @NoAdminRequired
+ *
+ * @return DataResponse
+ * @throws OCSForbiddenException
+ */
+ public function getAppPassword(): DataResponse {
+ // We do not allow the creation of new tokens if this is an app password
+ if ($this->session->exists('app_password')) {
+ throw new OCSForbiddenException('You cannot request an new apppassword with an apppassword');
+ }
+
+ try {
+ $credentials = $this->credentialStore->getLoginCredentials();
+ } catch (CredentialsUnavailableException $e) {
+ throw new OCSForbiddenException();
+ }
+
+ try {
+ $password = $credentials->getPassword();
+ } catch (PasswordUnavailableException $e) {
+ $password = null;
+ }
+
+ $userAgent = $this->request->getHeader('USER_AGENT');
+
+ $token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
+
+ $this->tokenProvider->generateToken(
+ $token,
+ $credentials->getUID(),
+ $credentials->getLoginName(),
+ $password,
+ $userAgent,
+ IToken::PERMANENT_TOKEN,
+ IToken::DO_NOT_REMEMBER
+ );
+
+ return new DataResponse([
+ 'apppassword' => $token
+ ]);
+ }
+}
diff --git a/core/routes.php b/core/routes.php
index 4ac51dc2364..f3884a765a2 100644
--- a/core/routes.php
+++ b/core/routes.php
@@ -81,6 +81,7 @@ $application->registerRoutes($this, [
['root' => '/core', 'name' => 'AutoComplete#get', 'url' => '/autocomplete/get', 'verb' => 'GET'],
['root' => '/core', 'name' => 'WhatsNew#get', 'url' => '/whatsnew', 'verb' => 'GET'],
['root' => '/core', 'name' => 'WhatsNew#dismiss', 'url' => '/whatsnew', 'verb' => 'POST'],
+ ['root' => '/core', 'name' => 'AppPassword#getAppPassword', 'url' => '/getapppassword', 'verb' => 'GET'],
],
]);