-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
fix remember me login #1347
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
Merged
Merged
fix remember me login #1347
Changes from 1 commit
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
Add missing tests and fix PHPDoc
Signed-off-by: Lukas Reschke <[email protected]>
- Loading branch information
commit 9d6e01ef40f7f4d2acab653b33e1af026bcde6c7
There are no files selected for viewing
This file contains hidden or 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; | ||
|
|
@@ -98,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)) { | ||
|
|
@@ -156,6 +162,7 @@ public function getToken($tokenId) { | |
| /** | ||
| * @param string $oldSessionId | ||
| * @param string $sessionId | ||
| * @throws InvalidTokenException | ||
| */ | ||
| public function renewSessionToken($oldSessionId, $sessionId) { | ||
| $token = $this->getToken($oldSessionId); | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -55,6 +55,7 @@ public function getToken($tokenId) ; | |
| /** | ||
| * @param string $oldSessionId | ||
|
Member
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? :)
Member
Author
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); | ||
|
|
||
|
|
||
This file contains hidden or 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,8 +1,8 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @author Christoph Wurst <[email protected]> | ||
| * | ||
| * @copyright Copyright (c) 2016, Lukas Reschke <[email protected]> | ||
| * @copyright Copyright (c) 2016, ownCloud, Inc. | ||
| * @license AGPL-3.0 | ||
| * | ||
|
|
@@ -25,6 +25,7 @@ | |
| use OC\Authentication\Token\DefaultToken; | ||
| use OC\Authentication\Token\DefaultTokenProvider; | ||
| use OC\Authentication\Token\IToken; | ||
| use OCP\AppFramework\Db\Mapper; | ||
| use OCP\AppFramework\Utility\ITimeFactory; | ||
| use OCP\IConfig; | ||
| use OCP\ILogger; | ||
|
|
@@ -34,13 +35,19 @@ | |
|
|
||
| class DefaultTokenProviderTest extends TestCase { | ||
|
|
||
| /** @var DefaultTokenProvider */ | ||
| /** @var DefaultTokenProvider|\PHPUnit_Framework_MockObject_MockObject */ | ||
| private $tokenProvider; | ||
| /** @var Mapper|\PHPUnit_Framework_MockObject_MockObject */ | ||
| private $mapper; | ||
| /** @var ICrypto|\PHPUnit_Framework_MockObject_MockObject */ | ||
| private $crypto; | ||
| /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ | ||
| private $config; | ||
| /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */ | ||
| private $logger; | ||
| /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */ | ||
| private $timeFactory; | ||
| /** @var int */ | ||
| private $time; | ||
|
|
||
| protected function setUp() { | ||
|
|
@@ -262,4 +269,111 @@ public function testInvalidateOldTokens() { | |
| $this->tokenProvider->invalidateOldTokens(); | ||
| } | ||
|
|
||
| public function testRenewSessionTokenWithoutPassword() { | ||
| $token = $this->getMockBuilder(DefaultToken::class) | ||
| ->disableOriginalConstructor() | ||
| ->setMethods(['getUID', 'getLoginName', 'getPassword', 'getName']) | ||
| ->getMock(); | ||
| $token | ||
| ->expects($this->at(0)) | ||
| ->method('getUID') | ||
| ->willReturn('UserUid'); | ||
| $token | ||
| ->expects($this->at(1)) | ||
| ->method('getLoginName') | ||
| ->willReturn('UserLoginName'); | ||
| $token | ||
| ->expects($this->at(2)) | ||
| ->method('getPassword') | ||
| ->willReturn(null); | ||
| $token | ||
| ->expects($this->at(3)) | ||
| ->method('getName') | ||
| ->willReturn('MyTokenName'); | ||
| $this->config | ||
| ->expects($this->exactly(2)) | ||
| ->method('getSystemValue') | ||
| ->with('secret') | ||
| ->willReturn('MyInstanceSecret'); | ||
| $this->mapper | ||
| ->expects($this->at(0)) | ||
| ->method('getToken') | ||
| ->with(hash('sha512', 'oldId' . 'MyInstanceSecret')) | ||
| ->willReturn($token); | ||
| $newToken = new DefaultToken(); | ||
| $newToken->setUid('UserUid'); | ||
| $newToken->setLoginName('UserLoginName'); | ||
| $newToken->setName('MyTokenName'); | ||
| $newToken->setToken(hash('sha512', 'newId' . 'MyInstanceSecret')); | ||
| $newToken->setType(IToken::TEMPORARY_TOKEN); | ||
| $newToken->setLastActivity(1313131); | ||
| $this->mapper | ||
| ->expects($this->at(1)) | ||
| ->method('insert') | ||
| ->with($newToken); | ||
|
|
||
| $this->tokenProvider->renewSessionToken('oldId', 'newId'); | ||
| } | ||
|
|
||
| public function testRenewSessionTokenWithPassword() { | ||
| $token = $this->getMockBuilder(DefaultToken::class) | ||
| ->disableOriginalConstructor() | ||
| ->setMethods(['getUID', 'getLoginName', 'getPassword', 'getName']) | ||
| ->getMock(); | ||
| $token | ||
| ->expects($this->at(0)) | ||
| ->method('getUID') | ||
| ->willReturn('UserUid'); | ||
| $token | ||
| ->expects($this->at(1)) | ||
| ->method('getLoginName') | ||
| ->willReturn('UserLoginName'); | ||
| $token | ||
| ->expects($this->at(2)) | ||
| ->method('getPassword') | ||
| ->willReturn('EncryptedPassword'); | ||
| $token | ||
| ->expects($this->at(3)) | ||
| ->method('getPassword') | ||
| ->willReturn('EncryptedPassword'); | ||
| $token | ||
| ->expects($this->at(4)) | ||
| ->method('getName') | ||
| ->willReturn('MyTokenName'); | ||
| $this->crypto | ||
| ->expects($this->any(0)) | ||
| ->method('decrypt') | ||
| ->with('EncryptedPassword', 'oldIdMyInstanceSecret') | ||
| ->willReturn('ClearTextPassword'); | ||
| $this->crypto | ||
| ->expects($this->any(1)) | ||
| ->method('encrypt') | ||
| ->with('ClearTextPassword', 'newIdMyInstanceSecret') | ||
| ->willReturn('EncryptedPassword'); | ||
| $this->config | ||
| ->expects($this->exactly(4)) | ||
| ->method('getSystemValue') | ||
| ->with('secret') | ||
| ->willReturn('MyInstanceSecret'); | ||
| $this->mapper | ||
| ->expects($this->at(0)) | ||
| ->method('getToken') | ||
| ->with(hash('sha512', 'oldId' . 'MyInstanceSecret')) | ||
| ->willReturn($token); | ||
| $newToken = new DefaultToken(); | ||
| $newToken->setUid('UserUid'); | ||
| $newToken->setLoginName('UserLoginName'); | ||
| $newToken->setName('MyTokenName'); | ||
| $newToken->setToken(hash('sha512', 'newId' . 'MyInstanceSecret')); | ||
| $newToken->setType(IToken::TEMPORARY_TOKEN); | ||
| $newToken->setLastActivity(1313131); | ||
| $newToken->setPassword('EncryptedPassword'); | ||
| $this->mapper | ||
| ->expects($this->at(1)) | ||
| ->method('insert') | ||
| ->with($newToken); | ||
|
|
||
| $this->tokenProvider->renewSessionToken('oldId', 'newId'); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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,5 +1,4 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Copyright (c) 2013 Robin Appelman <[email protected]> | ||
| * This file is licensed under the Affero General Public License version 3 or | ||
|
|
@@ -43,6 +42,12 @@ class SessionTest extends \Test\TestCase { | |
| private $throttler; | ||
| /** @var ISecureRandom|\PHPUnit_Framework_MockObject_MockObject */ | ||
| private $random; | ||
| /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ | ||
| private $manager; | ||
| /** @var ISession|\PHPUnit_Framework_MockObject_MockObject */ | ||
| private $session; | ||
| /** @var Session|\PHPUnit_Framework_MockObject_MockObject */ | ||
| private $userSession; | ||
|
|
||
| protected function setUp() { | ||
| parent::setUp(); | ||
|
|
@@ -55,6 +60,21 @@ protected function setUp() { | |
| $this->config = $this->createMock(IConfig::class); | ||
| $this->throttler = $this->createMock(Throttler::class); | ||
| $this->random = $this->createMock(ISecureRandom::class); | ||
| $this->manager = $this->createMock(IUserManager::class); | ||
| $this->session = $this->createMock(ISession::class); | ||
| $this->userSession = $this->getMockBuilder(Session::class) | ||
| ->setConstructorArgs([ | ||
| $this->manager, | ||
| $this->session, | ||
| $this->timeFactory, | ||
| $this->tokenProvider, | ||
| $this->config, | ||
| $this->random, | ||
| ]) | ||
| ->setMethods([ | ||
| 'setMagicInCookie', | ||
| ]) | ||
| ->getMock(); | ||
|
|
||
| \OC_User::setIncognitoMode(false); | ||
| } | ||
|
|
@@ -1136,4 +1156,27 @@ public function testNoUpdateAuthTokenLastCheckRecent() { | |
|
|
||
| $userSession->logClientIn('john', 'doe', $request, $this->throttler); | ||
| } | ||
|
|
||
| public function testCreateRememberMeToken() { | ||
| $user = $this->createMock(IUser::class); | ||
| $user | ||
| ->expects($this->exactly(2)) | ||
| ->method('getUID') | ||
| ->willReturn('UserUid'); | ||
| $this->random | ||
| ->expects($this->once()) | ||
| ->method('generate') | ||
| ->with(32) | ||
| ->willReturn('LongRandomToken'); | ||
| $this->config | ||
| ->expects($this->once()) | ||
| ->method('setUserValue') | ||
| ->with('UserUid', 'login_token', 'LongRandomToken', 10000); | ||
| $this->userSession | ||
| ->expects($this->once()) | ||
| ->method('setMagicInCookie') | ||
| ->with('UserUid', 'LongRandomToken'); | ||
|
|
||
| $this->userSession->createRememberMeToken($user); | ||
| } | ||
| } | ||
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.