aboutsummaryrefslogtreecommitdiffstats
path: root/core/Controller/ClientFlowLoginController.php
diff options
context:
space:
mode:
authorLukas Reschke <lukas@statuscode.ch>2017-04-24 21:11:48 +0200
committerRoeland Jago Douma <roeland@famdouma.nl>2017-04-25 20:18:49 +0200
commit6a16df728858de1a021d27c1406c2cf1dfd86784 (patch)
treea7315871cac7b4ca7cbb2d7c0074c96e82f1b001 /core/Controller/ClientFlowLoginController.php
parentdb94b5d4af711f6e18aac0c9d4b0357a3b9123d1 (diff)
downloadnextcloud-server-6a16df728858de1a021d27c1406c2cf1dfd86784.tar.gz
nextcloud-server-6a16df728858de1a021d27c1406c2cf1dfd86784.zip
Add new auth flow
This implements the basics for the new app-password based authentication flow for our clients. The current implementation tries to keep it as simple as possible and works the following way: 1. Unauthenticated client opens `/index.php/login/flow` 2. User will be asked whether they want to grant access to the client 3. If accepted the user has the chance to do so using existing App Token or automatically generate an app password. If the user chooses to use an existing app token then that one will simply be redirected to the `nc://` protocol handler. While we can improve on that in the future, I think keeping this smaller at the moment has its advantages. Also, in the near future we have to think about an automatic migration endpoint so there's that anyways :-) If the user chooses to use the regular login the following happens: 1. A session state token is written to the session 2. User is redirected to the login page 3. If successfully authenticated they will be redirected to a page redirecting to the POST controller 4. The POST controller will check if the CSRF token as well as the state token is correct, if yes the user will be redirected to the `nc://` protocol handler. This approach is quite simple but also allows to be extended in the future. One could for example allow external websites to consume this authentication endpoint as well. Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
Diffstat (limited to 'core/Controller/ClientFlowLoginController.php')
-rw-r--r--core/Controller/ClientFlowLoginController.php236
1 files changed, 236 insertions, 0 deletions
diff --git a/core/Controller/ClientFlowLoginController.php b/core/Controller/ClientFlowLoginController.php
new file mode 100644
index 00000000000..891910b8d09
--- /dev/null
+++ b/core/Controller/ClientFlowLoginController.php
@@ -0,0 +1,236 @@
+<?php
+/**
+ * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
+ *
+ * @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\Exceptions\InvalidTokenException;
+use OC\Authentication\Exceptions\PasswordlessTokenException;
+use OC\Authentication\Token\IProvider;
+use OC\Authentication\Token\IToken;
+use OCP\AppFramework\Controller;
+use OCP\AppFramework\Http;
+use OCP\AppFramework\Http\Response;
+use OCP\AppFramework\Http\TemplateResponse;
+use OCP\Defaults;
+use OCP\IL10N;
+use OCP\IRequest;
+use OCP\ISession;
+use OCP\IURLGenerator;
+use OCP\IUserSession;
+use OCP\Security\ISecureRandom;
+use OCP\Session\Exceptions\SessionNotAvailableException;
+
+class ClientFlowLoginController extends Controller {
+ /** @var IUserSession */
+ private $userSession;
+ /** @var IL10N */
+ private $l10n;
+ /** @var Defaults */
+ private $defaults;
+ /** @var ISession */
+ private $session;
+ /** @var IProvider */
+ private $tokenProvider;
+ /** @var ISecureRandom */
+ private $random;
+ /** @var IURLGenerator */
+ private $urlGenerator;
+
+ const stateName = 'client.flow.state.token';
+
+ /**
+ * @param string $appName
+ * @param IRequest $request
+ * @param IUserSession $userSession
+ * @param IL10N $l10n
+ * @param Defaults $defaults
+ * @param ISession $session
+ * @param IProvider $tokenProvider
+ * @param ISecureRandom $random
+ * @param IURLGenerator $urlGenerator
+ */
+ public function __construct($appName,
+ IRequest $request,
+ IUserSession $userSession,
+ IL10N $l10n,
+ Defaults $defaults,
+ ISession $session,
+ IProvider $tokenProvider,
+ ISecureRandom $random,
+ IURLGenerator $urlGenerator) {
+ parent::__construct($appName, $request);
+ $this->userSession = $userSession;
+ $this->l10n = $l10n;
+ $this->defaults = $defaults;
+ $this->session = $session;
+ $this->tokenProvider = $tokenProvider;
+ $this->random = $random;
+ $this->urlGenerator = $urlGenerator;
+ }
+
+ /**
+ * @return string
+ */
+ private function getClientName() {
+ return $this->request->getHeader('USER_AGENT') !== null ? $this->request->getHeader('USER_AGENT') : 'unknown';
+ }
+
+ /**
+ * @param string $stateToken
+ * @return bool
+ */
+ private function isValidToken($stateToken) {
+ $currentToken = $this->session->get(self::stateName);
+ if(!is_string($stateToken) || !is_string($currentToken)) {
+ return false;
+ }
+ return hash_equals($currentToken, $stateToken);
+ }
+
+ /**
+ * @return TemplateResponse
+ */
+ private function stateTokenForbiddenResponse() {
+ $response = new TemplateResponse(
+ $this->appName,
+ '403',
+ [
+ 'file' => $this->l10n->t('State token does not match'),
+ ],
+ 'guest'
+ );
+ $response->setStatus(Http::STATUS_FORBIDDEN);
+ return $response;
+ }
+
+ /**
+ * @PublicPage
+ * @NoCSRFRequired
+ * @UseSession
+ *
+ * @return TemplateResponse
+ */
+ public function showAuthPickerPage() {
+ if($this->userSession->isLoggedIn()) {
+ return new TemplateResponse(
+ $this->appName,
+ '403',
+ [
+ 'file' => $this->l10n->t('Auth flow can only be started unauthenticated.'),
+ ],
+ 'guest'
+ );
+ }
+
+ $stateToken = $this->random->generate(
+ 64,
+ ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_DIGITS
+ );
+ $this->session->set(self::stateName, $stateToken);
+
+ return new TemplateResponse(
+ $this->appName,
+ 'loginflow/authpicker',
+ [
+ 'client' => $this->getClientName(),
+ 'instanceName' => $this->defaults->getName(),
+ 'urlGenerator' => $this->urlGenerator,
+ 'stateToken' => $stateToken,
+ 'serverHost' => $this->request->getServerHost(),
+ ],
+ 'guest'
+ );
+ }
+
+ /**
+ * @NoAdminRequired
+ * @NoCSRFRequired
+ * @UseSession
+ *
+ * @param string $stateToken
+ * @return TemplateResponse
+ */
+ public function redirectPage($stateToken = '') {
+ if(!$this->isValidToken($stateToken)) {
+ return $this->stateTokenForbiddenResponse();
+ }
+
+ return new TemplateResponse(
+ $this->appName,
+ 'loginflow/redirect',
+ [
+ 'urlGenerator' => $this->urlGenerator,
+ 'stateToken' => $stateToken,
+ ],
+ 'empty'
+ );
+ }
+
+ /**
+ * @NoAdminRequired
+ * @UseSession
+ *
+ * @param string $stateToken
+ * @return Http\RedirectResponse|Response
+ */
+ public function generateAppPassword($stateToken) {
+ $this->session->remove(self::stateName);
+ if(!$this->isValidToken($stateToken)) {
+ return $this->stateTokenForbiddenResponse();
+ }
+
+ try {
+ $sessionId = $this->session->getId();
+ } catch (SessionNotAvailableException $ex) {
+ $response = new Response();
+ $response->setStatus(Http::STATUS_FORBIDDEN);
+ return $response;
+ }
+
+ try {
+ $sessionToken = $this->tokenProvider->getToken($sessionId);
+ $loginName = $sessionToken->getLoginName();
+ try {
+ $password = $this->tokenProvider->getPassword($sessionToken, $sessionId);
+ } catch (PasswordlessTokenException $ex) {
+ $password = null;
+ }
+ } catch (InvalidTokenException $ex) {
+ $response = new Response();
+ $response->setStatus(Http::STATUS_FORBIDDEN);
+ return $response;
+ }
+
+ $token = $this->random->generate(60);
+ $this->tokenProvider->generateToken(
+ $token,
+ $this->userSession->getUser()->getUID(),
+ $loginName,
+ $password,
+ $this->getClientName(),
+ IToken::PERMANENT_TOKEN,
+ IToken::DO_NOT_REMEMBER
+ );
+
+ return new Http\RedirectResponse('nc://' . urlencode($loginName) . ':' . urlencode($token) . '@' . $this->request->getServerHost());
+ }
+
+}