-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
fix remember me login #1347
Merged
Merged
fix remember me login #1347
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d907666
bring back remember-me
ChristophWurst 6f86e46
inject ISecureRandom into user session and use injected config too
ChristophWurst b269ed5
Fix invalid PHPDocs
LukasReschke 271f2a4
Fix typ in constant name
LukasReschke 9d6e01e
Add missing tests and fix PHPDoc
LukasReschke 4da6b20
document what the method does
ChristophWurst 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
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
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
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
<?php | ||
/** | ||
* @copyright Copyright (c) 2016, ownCloud, Inc. | ||
* @copyright Copyright (c) 2016, Christoph Wurst <[email protected]> | ||
* | ||
* @author Christoph Wurst <[email protected]> | ||
* | ||
|
@@ -56,7 +57,11 @@ class DefaultTokenProvider implements IProvider { | |
* @param ILogger $logger | ||
* @param ITimeFactory $time | ||
*/ | ||
public function __construct(DefaultTokenMapper $mapper, ICrypto $crypto, IConfig $config, ILogger $logger, ITimeFactory $time) { | ||
public function __construct(DefaultTokenMapper $mapper, | ||
ICrypto $crypto, | ||
IConfig $config, | ||
ILogger $logger, | ||
ITimeFactory $time) { | ||
$this->mapper = $mapper; | ||
$this->crypto = $crypto; | ||
$this->config = $config; | ||
|
@@ -73,9 +78,10 @@ public function __construct(DefaultTokenMapper $mapper, ICrypto $crypto, IConfig | |
* @param string|null $password | ||
* @param string $name | ||
* @param int $type token type | ||
* @param int $remember whether the session token should be used for remember-me | ||
* @return IToken | ||
*/ | ||
public function generateToken($token, $uid, $loginName, $password, $name, $type = IToken::TEMPORARY_TOKEN) { | ||
public function generateToken($token, $uid, $loginName, $password, $name, $type = IToken::TEMPORARY_TOKEN, $remember = IToken::DO_NOT_REMEMBER) { | ||
$dbToken = new DefaultToken(); | ||
$dbToken->setUid($uid); | ||
$dbToken->setLoginName($loginName); | ||
|
@@ -85,6 +91,7 @@ public function generateToken($token, $uid, $loginName, $password, $name, $type | |
$dbToken->setName($name); | ||
$dbToken->setToken($this->hashToken($token)); | ||
$dbToken->setType($type); | ||
$dbToken->setRemember($remember); | ||
$dbToken->setLastActivity($this->time->getTime()); | ||
|
||
$this->mapper->insert($dbToken); | ||
|
@@ -96,6 +103,7 @@ public function generateToken($token, $uid, $loginName, $password, $name, $type | |
* Save the updated token | ||
* | ||
* @param IToken $token | ||
* @throws InvalidTokenException | ||
*/ | ||
public function updateToken(IToken $token) { | ||
if (!($token instanceof DefaultToken)) { | ||
|
@@ -151,6 +159,28 @@ public function getToken($tokenId) { | |
} | ||
} | ||
|
||
/** | ||
* @param string $oldSessionId | ||
* @param string $sessionId | ||
* @throws InvalidTokenException | ||
*/ | ||
public function renewSessionToken($oldSessionId, $sessionId) { | ||
$token = $this->getToken($oldSessionId); | ||
|
||
$newToken = new DefaultToken(); | ||
$newToken->setUid($token->getUID()); | ||
$newToken->setLoginName($token->getLoginName()); | ||
if (!is_null($token->getPassword())) { | ||
$password = $this->decryptPassword($token->getPassword(), $oldSessionId); | ||
$newToken->setPassword($this->encryptPassword($password, $sessionId)); | ||
} | ||
$newToken->setName($token->getName()); | ||
$newToken->setToken($this->hashToken($sessionId)); | ||
$newToken->setType(IToken::TEMPORARY_TOKEN); | ||
$newToken->setLastActivity($this->time->getTime()); | ||
$this->mapper->insert($newToken); | ||
} | ||
|
||
/** | ||
* @param IToken $savedToken | ||
* @param string $tokenId session token | ||
|
@@ -207,8 +237,11 @@ public function invalidateTokenById(IUser $user, $id) { | |
*/ | ||
public function invalidateOldTokens() { | ||
$olderThan = $this->time->getTime() - (int) $this->config->getSystemValue('session_lifetime', 60 * 60 * 24); | ||
$this->logger->info('Invalidating tokens older than ' . date('c', $olderThan)); | ||
$this->mapper->invalidateOld($olderThan); | ||
$this->logger->info('Invalidating session tokens older than ' . date('c', $olderThan)); | ||
$this->mapper->invalidateOld($olderThan, IToken::DO_NOT_REMEMBER); | ||
$rememberThreshold = $this->time->getTime() - (int) $this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15); | ||
$this->logger->info('Invalidating remembered session tokens older than ' . date('c', $rememberThreshold)); | ||
$this->mapper->invalidateOld($rememberThreshold, IToken::REMEMBER); | ||
} | ||
|
||
/** | ||
|
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 |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
|
||
interface IProvider { | ||
|
||
|
||
/** | ||
* Create and persist a new token | ||
* | ||
|
@@ -37,9 +38,10 @@ interface IProvider { | |
* @param string|null $password | ||
* @param string $name | ||
* @param int $type token type | ||
* @param int $remember whether the session token should be used for remember-me | ||
* @return IToken | ||
*/ | ||
public function generateToken($token, $uid, $loginName, $password, $name, $type = IToken::TEMPORARY_TOKEN); | ||
public function generateToken($token, $uid, $loginName, $password, $name, $type = IToken::TEMPORARY_TOKEN, $remember = IToken::DO_NOT_REMEMBER); | ||
|
||
/** | ||
* Get a token by token id | ||
|
@@ -50,6 +52,15 @@ public function generateToken($token, $uid, $loginName, $password, $name, $type | |
*/ | ||
public function getToken($tokenId) ; | ||
|
||
/** | ||
* Duplicate an existing session token | ||
* | ||
* @param string $oldSessionId | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add at least a single line comment what the function is supposed to do? :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
* @param string $sessionId | ||
* @throws InvalidTokenException | ||
*/ | ||
public function renewSessionToken($oldSessionId, $sessionId); | ||
|
||
/** | ||
* Invalidate (delete) the given session token | ||
* | ||
|
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
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.
Function needs tests. Will write myself later.