Skip to content

Commit

Permalink
token based auth
Browse files Browse the repository at this point in the history
* Add InvalidTokenException
* add DefaultTokenMapper and use it to check if a auth token exists
* create new token for the browser session if none exists
hash stored token; save user agent
* encrypt login password when creating the token
  • Loading branch information
ChristophWurst authored and DeepDiver1975 committed May 11, 2016
1 parent f39e163 commit d8cde41
Show file tree
Hide file tree
Showing 16 changed files with 596 additions and 250 deletions.
13 changes: 9 additions & 4 deletions core/Application.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* @author Bernhard Posselt <dev@bernhard-posselt.com>
* @author Christoph Wurst <christoph@owncloud.com>
* @author Lukas Reschke <lukas@owncloud.com>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Roeland Jago Douma <rullzer@owncloud.com>
Expand Down Expand Up @@ -28,12 +29,13 @@

use OC\AppFramework\Utility\SimpleContainer;
use OC\AppFramework\Utility\TimeFactory;
use OC\Core\Controller\AvatarController;
use OC\Core\Controller\LoginController;
use \OCP\AppFramework\App;
use OC\Core\Controller\LostController;
use OC\Core\Controller\UserController;
use OC\Core\Controller\AvatarController;
use \OCP\Util;
use OC_Defaults;
use OCP\AppFramework\App;
use OCP\Util;

/**
* Class Application
Expand Down Expand Up @@ -132,14 +134,17 @@ public function __construct(array $urlParams=array()){
$container->registerService('UserSession', function(SimpleContainer $c) {
return $c->query('ServerContainer')->getUserSession();
});
$container->registerService('Session', function(SimpleContainer $c) {
return $c->query('ServerContainer')->getSession();
});
$container->registerService('Cache', function(SimpleContainer $c) {
return $c->query('ServerContainer')->getCache();
});
$container->registerService('UserFolder', function(SimpleContainer $c) {
return $c->query('ServerContainer')->getUserFolder();
});
$container->registerService('Defaults', function() {
return new \OC_Defaults;
return new OC_Defaults;
});
$container->registerService('Mailer', function(SimpleContainer $c) {
return $c->query('ServerContainer')->getMailer();
Expand Down
75 changes: 52 additions & 23 deletions core/Controller/LoginController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

/**
* @author Christoph Wurst <christoph@owncloud.com>
* @author Lukas Reschke <lukas@owncloud.com>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
Expand All @@ -21,7 +23,11 @@

namespace OC\Core\Controller;

use OC\Setup;
use OC;
use OC\User\Session;
use OC_App;
use OC_User;
use OC_Util;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\TemplateResponse;
Expand All @@ -31,17 +37,21 @@
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;

class LoginController extends Controller {

/** @var IUserManager */
private $userManager;

/** @var IConfig */
private $config;

/** @var ISession */
private $session;
/** @var IUserSession */

/** @var Session */
private $userSession;

/** @var IURLGenerator */
private $urlGenerator;

Expand All @@ -51,16 +61,12 @@ class LoginController extends Controller {
* @param IUserManager $userManager
* @param IConfig $config
* @param ISession $session
* @param IUserSession $userSession
* @param Session $userSession
* @param IURLGenerator $urlGenerator
*/
function __construct($appName,
IRequest $request,
IUserManager $userManager,
IConfig $config,
ISession $session,
IUserSession $userSession,
IURLGenerator $urlGenerator) {
function __construct($appName, IRequest $request, IUserManager $userManager,
IConfig $config, ISession $session, Session $userSession,
IURLGenerator $urlGenerator) {
parent::__construct($appName, $request);
$this->userManager = $userManager;
$this->config = $config;
Expand Down Expand Up @@ -96,18 +102,16 @@ public function logout() {
*
* @return TemplateResponse
*/
public function showLoginForm($user,
$redirect_url,
$remember_login) {
if($this->userSession->isLoggedIn()) {
return new RedirectResponse(\OC_Util::getDefaultPageUrl());
public function showLoginForm($user, $redirect_url, $remember_login) {
if ($this->userSession->isLoggedIn()) {
return new RedirectResponse(OC_Util::getDefaultPageUrl());
}

$parameters = array();
$loginMessages = $this->session->get('loginMessages');
$errors = [];
$messages = [];
if(is_array($loginMessages)) {
if (is_array($loginMessages)) {
list($errors, $messages) = $loginMessages;
}
$this->session->remove('loginMessages');
Expand Down Expand Up @@ -137,8 +141,8 @@ public function showLoginForm($user,
}
}

$parameters['alt_login'] = \OC_App::getAlternativeLogIns();
$parameters['rememberLoginAllowed'] = \OC_Util::rememberLoginAllowed();
$parameters['alt_login'] = OC_App::getAlternativeLogIns();
$parameters['rememberLoginAllowed'] = OC_Util::rememberLoginAllowed();
$parameters['rememberLoginState'] = !empty($remember_login) ? $remember_login : 0;

if (!is_null($user) && $user !== '') {
Expand All @@ -150,11 +154,36 @@ public function showLoginForm($user,
}

return new TemplateResponse(
$this->appName,
'login',
$parameters,
'guest'
$this->appName, 'login', $parameters, 'guest'
);
}

/**
* @NoCSRFRequired
* @PublicPage
* @UseSession
*
* @param string $user
* @param string $password
* @param string $redirect_url
* @return RedirectResponse
*/
public function tryLogin($user, $password, $redirect_url) {
// TODO: Add all the insane error handling
if ($this->userManager->checkPassword($user, $password) === false) {
return new RedirectResponse($this->urlGenerator->linkToRoute('login#showLoginForm'));
}
$this->userSession->createSessionToken($user, $password);
if (!is_null($redirect_url) && $this->userSession->isLoggedIn()) {
$location = OC::$server->getURLGenerator()->getAbsoluteURL(urldecode($redirect_url));
// Deny the redirect if the URL contains a @
// This prevents unvalidated redirects like ?redirect_url=:user@domain.com
if (strpos($location, '@') === false) {
return new RedirectResponse($location);
}
}
// TODO: Show invalid login warning
return new RedirectResponse($this->urlGenerator->linkTo('files', 'index'));
}

}
3 changes: 2 additions & 1 deletion core/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@
['name' => 'avatar#postCroppedAvatar', 'url' => '/avatar/cropped', 'verb' => 'POST'],
['name' => 'avatar#getTmpAvatar', 'url' => '/avatar/tmp', 'verb' => 'GET'],
['name' => 'avatar#postAvatar', 'url' => '/avatar/', 'verb' => 'POST'],
['name' => 'login#tryLogin', 'url' => '/login', 'verb' => 'POST'],
['name' => 'login#showLoginForm', 'url' => '/login', 'verb' => 'GET'],
['name' => 'login#logout', 'url' => '/logout', 'verb' => 'GET'],
]
],
]);

// Post installation check
Expand Down
2 changes: 1 addition & 1 deletion core/templates/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
?>

<!--[if IE 8]><style>input[type="checkbox"]{padding:0;}</style><![endif]-->
<form method="post" name="login" action="<?php p(OC::$WEBROOT) ?>/">
<form method="post" name="login">
<fieldset>
<?php if (!empty($_['redirect_url'])) {
print_unescaped('<input type="hidden" name="redirect_url" value="' . \OCP\Util::sanitizeHTML($_['redirect_url']) . '">');
Expand Down
60 changes: 60 additions & 0 deletions db_structure.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,66 @@

</table>

<table>
<name>*dbprefix*authtoken</name>

<declaration>

<field>
<name>id</name>
<type>integer</type>
<default>0</default>
<notnull>true</notnull>
<autoincrement>1</autoincrement>
<unsigned>true</unsigned>
<length>4</length>
</field>

<!-- Foreign Key users::uid -->
<field>
<name>uid</name>
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>64</length>
</field>

<field>
<name>password</name>
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>100</length>
</field>

<field>
<name>name</name>
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>100</length>
</field>

<field>
<name>token</name>
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>100</length>
</field>

<index>
<name>authtoken_token_index</name>
<unique>true</unique>
<field>
<name>token</name>
<sorting>ascending</sorting>
</field>
</index>

</declaration>
</table>

<table>

<!--
Expand Down
Loading

0 comments on commit d8cde41

Please sign in to comment.