summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorRoeland Jago Douma <roeland@famdouma.nl>2018-10-03 11:48:02 +0200
committerRoeland Jago Douma <roeland@famdouma.nl>2018-10-03 19:05:20 +0200
commit78273cb1e61870782d36e7b045abdfcee0cb647b (patch)
treea7d3ba3eddf649923743648d7faf2e13456b3d3c /core
parent893d2daeced67bddb2a277f8d126b3b27d78fcb0 (diff)
downloadnextcloud-server-78273cb1e61870782d36e7b045abdfcee0cb647b.tar.gz
nextcloud-server-78273cb1e61870782d36e7b045abdfcee0cb647b.zip
Add an endppoint for clients to request an app password
Now that we allow enforcing 2 factor auth it make sense if we also allow and endpoint where the clients can in the background fetch an apppassword if they were configured before the login flow was present. Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
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'],
],
]);