-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new auth flow basics #4479
Merged
Merged
Add new auth flow basics #4479
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6a16df7
Add new auth flow
LukasReschke 05e1092
Correctly case the stateToken
rullzer bb5e5ef
Do not remove the state token to early
rullzer aae079a
AppToken to 72 chars
rullzer 61af3f4
Fix auth flow background color and redirect view layout
jancborchardt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,238 @@ | ||
<?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) { | ||
if(!$this->isValidToken($stateToken)) { | ||
$this->session->remove(self::stateName); | ||
return $this->stateTokenForbiddenResponse(); | ||
} | ||
|
||
$this->session->remove(self::stateName); | ||
|
||
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(72); | ||
$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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.picker-window { | ||
display: block; | ||
padding: 10px; | ||
margin-bottom: 20px; | ||
background-color: rgba(0,0,0,.3); | ||
color: #fff; | ||
border-radius: 3px; | ||
cursor: default; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
jQuery(document).ready(function() { | ||
$('#app-token-login').click(function (e) { | ||
e.preventDefault(); | ||
$(this).addClass('hidden'); | ||
$('#redirect-link').addClass('hidden'); | ||
$('#app-token-login-field').removeClass('hidden'); | ||
}); | ||
|
||
$('#submit-app-token-login').click(function(e) { | ||
e.preventDefault(); | ||
window.location.href = 'nc://' + encodeURIComponent($('#user').val()) + ':' + encodeURIComponent($('#password').val()) + '@' + encodeURIComponent($('#serverHost').val()); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
jQuery(document).ready(function() { | ||
$('#submit-redirect-form').trigger('click'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?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/>. | ||
* | ||
*/ | ||
|
||
script('core', 'login/authpicker'); | ||
style('core', 'login/authpicker'); | ||
|
||
/** @var array $_ */ | ||
/** @var \OCP\IURLGenerator $urlGenerator */ | ||
$urlGenerator = $_['urlGenerator']; | ||
?> | ||
|
||
<div class="picker-window"> | ||
<p class="info"> | ||
<?php p($l->t('You are about to grant "%s" access to your %s account.', [$_['client'], $_['instanceName']])) ?> | ||
</p> | ||
|
||
<br/> | ||
|
||
<p id="redirect-link"> | ||
<a href="<?php p($urlGenerator->linkToRouteAbsolute('core.ClientFlowLogin.redirectPage', ['stateToken' => $_['stateToken']])) ?>"> | ||
<input type="submit" class="login primary icon-confirm-white" value="<?php p('Grant access') ?>"> | ||
</a> | ||
</p> | ||
|
||
<fieldset id="app-token-login-field" class="hidden"> | ||
<p class="grouptop"> | ||
<input type="text" name="user" id="user" placeholder="<?php p($l->t('Username')) ?>"> | ||
<label for="user" class="infield"><?php p($l->t('Username')) ?></label> | ||
</p> | ||
<p class="groupbottom"> | ||
<input type="password" name="password" id="password" placeholder="<?php p($l->t('App token')) ?>"> | ||
<label for="password" class="infield"><?php p($l->t('Password')) ?></label> | ||
</p> | ||
<input type="hidden" id="serverHost" value="<?php p($_['serverHost']) ?>" /> | ||
<input id="submit-app-token-login" type="submit" class="login primary icon-confirm-white" value="<?php p('Grant access') ?>"> | ||
</fieldset> | ||
</div> | ||
|
||
<a id="app-token-login" class="warning" href="#"><?php p($l->t('Alternative login using app token')) ?></a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?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/>. | ||
* | ||
*/ | ||
script('core', 'login/redirect'); | ||
style('core', 'login/authpicker'); | ||
|
||
/** @var array $_ */ | ||
/** @var \OCP\IURLGenerator $urlGenerator */ | ||
$urlGenerator = $_['urlGenerator']; | ||
?> | ||
|
||
<div class="picker-window"> | ||
<p class="info"><?php p($l->t('Redirecting …')) ?></p> | ||
</div> | ||
|
||
<form method="POST" action="<?php p($urlGenerator->linkToRouteAbsolute('core.ClientFlowLogin.generateAppPassword')) ?>"> | ||
<input type="hidden" name="requesttoken" value="<?php p($_['requesttoken']) ?>" /> | ||
<input type="hidden" name="stateToken" value="<?php p($_['stateToken']) ?>" /> | ||
<input id="submit-redirect-form" type="submit" class="hidden "/> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so is
/flow
OK? or do we want something else?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed with @LukasReschke lets go with this for now. And at some point come up with anonymous capabilities that would allow auto discovery of the endpoint.