Find this useful? Enter your email to receive occasional updates for securing PHP code.

Signing you up...

Thank you for signing up!

PHP Decode

<?php namespace Test\User; use OC\AppFramework\Http\Request; use OC\Authentication\Events..

Decoded Output download

<?php
 namespace Test\User; use OC\AppFramework\Http\Request; use OC\Authentication\Events\LoginFailed; use OC\Authentication\Exceptions\InvalidTokenException; use OC\Authentication\Exceptions\PasswordLoginForbiddenException; use OC\Authentication\Token\IProvider; use OC\Authentication\Token\IToken; use OC\Security\CSRF\CsrfTokenManager; use OC\Session\Memory; use OC\User\LoginException; use OC\User\Manager; use OC\User\Session; use OC\User\User; use OCA\DAV\Connector\Sabre\Auth; use OCP\AppFramework\Utility\ITimeFactory; use OCP\EventDispatcher\IEventDispatcher; use OCP\ICacheFactory; use OCP\IConfig; use OCP\IRequest; use OCP\IRequestId; use OCP\ISession; use OCP\IUser; use OCP\Lockdown\ILockdownManager; use OCP\Security\Bruteforce\IThrottler; use OCP\Security\ISecureRandom; use OCP\User\Events\PostLoginEvent; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; class SessionTest extends \Test\TestCase { private $timeFactory; private $tokenProvider; private $config; private $throttler; private $random; private $manager; private $session; private $userSession; private $lockdownManager; private $logger; private $dispatcher; protected function setUp() : void { parent::setUp(); $this->timeFactory = $this->createMock(ITimeFactory::class); $this->timeFactory->expects($this->any())->method("getTime")->willReturn(10000); $this->tokenProvider = $this->createMock(IProvider::class); $this->config = $this->createMock(IConfig::class); $this->throttler = $this->createMock(IThrottler::class); $this->random = $this->createMock(ISecureRandom::class); $this->manager = $this->createMock(Manager::class); $this->session = $this->createMock(ISession::class); $this->lockdownManager = $this->createMock(ILockdownManager::class); $this->logger = $this->createMock(LoggerInterface::class); $this->dispatcher = $this->createMock(IEventDispatcher::class); $this->userSession = $this->getMockBuilder(Session::class)->setConstructorArgs(array($this->manager, $this->session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->setMethods(array("setMagicInCookie"))->getMock(); \OC_User::setIncognitoMode(false); } public function isLoggedInData() { return array(array(true), array(false)); } public function testIsLoggedIn($isLoggedIn) { $session = $this->getMockBuilder(Memory::class)->setConstructorArgs(array(''))->getMock(); $manager = $this->createMock(Manager::class); $userSession = $this->getMockBuilder(Session::class)->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->setMethods(array("getUser"))->getMock(); $user = new User("sepp", null, $this->createMock(IEventDispatcher::class)); $userSession->expects($this->once())->method("getUser")->willReturn($isLoggedIn ? $user : null); $this->assertEquals($isLoggedIn, $userSession->isLoggedIn()); } public function testSetUser() { $session = $this->getMockBuilder(Memory::class)->setConstructorArgs(array(''))->getMock(); $session->expects($this->once())->method("set")->with("user_id", "foo"); $manager = $this->createMock(Manager::class); $backend = $this->createMock(\Test\Util\User\Dummy::class); $user = $this->createMock(IUser::class); $user->expects($this->once())->method("getUID")->willReturn("foo"); $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); $userSession->setUser($user); } public function testLoginValidPasswordEnabled() { $session = $this->getMockBuilder(Memory::class)->setConstructorArgs(array(''))->getMock(); $session->expects($this->once())->method("regenerateId"); $this->tokenProvider->expects($this->once())->method("getToken")->with("bar")->will($this->throwException(new InvalidTokenException())); $session->expects($this->exactly(2))->method("set")->with($this->callback(function ($key) { switch ($key) { case "user_id": case "loginname": return true; break; default: return false; break; } }, "foo")); $managerMethods = get_class_methods(Manager::class); $mockedManagerMethods = array_diff($managerMethods, array("__construct", "emit", "listen")); $manager = $this->getMockBuilder(Manager::class)->setMethods($mockedManagerMethods)->setConstructorArgs(array($this->config, $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class)))->getMock(); $backend = $this->createMock(\Test\Util\User\Dummy::class); $user = $this->createMock(IUser::class); $user->expects($this->any())->method("isEnabled")->willReturn(true); $user->expects($this->any())->method("getUID")->willReturn("foo"); $user->expects($this->once())->method("updateLastLoginTimestamp"); $manager->expects($this->once())->method("checkPasswordNoLogging")->with("foo", "bar")->willReturn($user); $userSession = $this->getMockBuilder(Session::class)->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->setMethods(array("prepareUserLogin"))->getMock(); $userSession->expects($this->once())->method("prepareUserLogin"); $this->dispatcher->expects($this->once())->method("dispatchTyped")->with($this->callback(function (PostLoginEvent $e) { return $e->getUser()->getUID() === "foo" && $e->getPassword() === "bar" && $e->isTokenLogin() === false; })); $userSession->login("foo", "bar"); $this->assertEquals($user, $userSession->getUser()); } public function testLoginValidPasswordDisabled() { $this->expectException(LoginException::class); $session = $this->getMockBuilder(Memory::class)->setConstructorArgs(array(''))->getMock(); $session->expects($this->never())->method("set"); $session->expects($this->once())->method("regenerateId"); $this->tokenProvider->expects($this->once())->method("getToken")->with("bar")->will($this->throwException(new InvalidTokenException())); $managerMethods = get_class_methods(Manager::class); $mockedManagerMethods = array_diff($managerMethods, array("__construct", "emit", "listen")); $manager = $this->getMockBuilder(Manager::class)->setMethods($mockedManagerMethods)->setConstructorArgs(array($this->config, $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class)))->getMock(); $user = $this->createMock(IUser::class); $user->expects($this->any())->method("isEnabled")->willReturn(false); $user->expects($this->never())->method("updateLastLoginTimestamp"); $manager->expects($this->once())->method("checkPasswordNoLogging")->with("foo", "bar")->willReturn($user); $this->dispatcher->expects($this->never())->method("dispatch"); $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); $userSession->login("foo", "bar"); } public function testLoginInvalidPassword() { $session = $this->getMockBuilder(Memory::class)->setConstructorArgs(array(''))->getMock(); $managerMethods = get_class_methods(Manager::class); $mockedManagerMethods = array_diff($managerMethods, array("__construct", "emit", "listen")); $manager = $this->getMockBuilder(Manager::class)->setMethods($mockedManagerMethods)->setConstructorArgs(array($this->config, $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class)))->getMock(); $backend = $this->createMock(\Test\Util\User\Dummy::class); $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); $user = $this->createMock(IUser::class); $session->expects($this->never())->method("set"); $session->expects($this->once())->method("regenerateId"); $this->tokenProvider->expects($this->once())->method("getToken")->with("bar")->will($this->throwException(new InvalidTokenException())); $user->expects($this->never())->method("isEnabled"); $user->expects($this->never())->method("updateLastLoginTimestamp"); $manager->expects($this->once())->method("checkPasswordNoLogging")->with("foo", "bar")->willReturn(false); $this->dispatcher->expects($this->never())->method("dispatch"); $userSession->login("foo", "bar"); } public function testLoginNonExisting() { $session = $this->getMockBuilder(Memory::class)->setConstructorArgs(array(''))->getMock(); $manager = $this->createMock(Manager::class); $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); $session->expects($this->never())->method("set"); $session->expects($this->once())->method("regenerateId"); $this->tokenProvider->expects($this->once())->method("getToken")->with("bar")->will($this->throwException(new InvalidTokenException())); $manager->expects($this->once())->method("checkPasswordNoLogging")->with("foo", "bar")->willReturn(false); $userSession->login("foo", "bar"); } public function testLogClientInNoTokenPasswordWith2fa() { $this->expectException(PasswordLoginForbiddenException::class); $manager = $this->createMock(Manager::class); $session = $this->createMock(ISession::class); $request = $this->createMock(IRequest::class); $userSession = $this->getMockBuilder(Session::class)->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->setMethods(array("login", "supportsCookies", "createSessionToken", "getUser"))->getMock(); $this->tokenProvider->expects($this->once())->method("getToken")->with("doe")->will($this->throwException(new InvalidTokenException())); $this->config->expects($this->once())->method("getSystemValueBool")->with("token_auth_enforced", false)->willReturn(true); $request->expects($this->any())->method("getRemoteAddress")->willReturn("192.168.0.1"); $this->throttler->expects($this->once())->method("sleepDelayOrThrowOnMax")->with("192.168.0.1"); $this->throttler->expects($this->any())->method("getDelay")->with("192.168.0.1")->willReturn(0); $userSession->logClientIn("john", "doe", $request, $this->throttler); } public function testLogClientInUnexist() { $manager = $this->createMock(Manager::class); $session = $this->createMock(ISession::class); $request = $this->createMock(IRequest::class); $userSession = $this->getMockBuilder(Session::class)->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->setMethods(array("login", "supportsCookies", "createSessionToken", "getUser"))->getMock(); $this->tokenProvider->expects($this->once())->method("getToken")->with("doe")->will($this->throwException(new InvalidTokenException())); $this->config->expects($this->once())->method("getSystemValueBool")->with("token_auth_enforced", false)->willReturn(false); $manager->method("getByEmail")->with("unexist")->willReturn(array()); $this->assertFalse($userSession->logClientIn("unexist", "doe", $request, $this->throttler)); } public function testLogClientInWithTokenPassword() { $manager = $this->createMock(Manager::class); $session = $this->createMock(ISession::class); $request = $this->createMock(IRequest::class); $userSession = $this->getMockBuilder(Session::class)->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->setMethods(array("isTokenPassword", "login", "supportsCookies", "createSessionToken", "getUser"))->getMock(); $userSession->expects($this->once())->method("isTokenPassword")->willReturn(true); $userSession->expects($this->once())->method("login")->with("john", "I-AM-AN-APP-PASSWORD")->willReturn(true); $session->expects($this->once())->method("set")->with("app_password", "I-AM-AN-APP-PASSWORD"); $request->expects($this->any())->method("getRemoteAddress")->willReturn("192.168.0.1"); $this->throttler->expects($this->once())->method("sleepDelayOrThrowOnMax")->with("192.168.0.1"); $this->throttler->expects($this->any())->method("getDelay")->with("192.168.0.1")->willReturn(0); $this->assertTrue($userSession->logClientIn("john", "I-AM-AN-APP-PASSWORD", $request, $this->throttler)); } public function testLogClientInNoTokenPasswordNo2fa() { $this->expectException(PasswordLoginForbiddenException::class); $manager = $this->createMock(Manager::class); $session = $this->createMock(ISession::class); $request = $this->createMock(IRequest::class); $userSession = $this->getMockBuilder(Session::class)->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->setMethods(array("login", "isTwoFactorEnforced"))->getMock(); $this->tokenProvider->expects($this->once())->method("getToken")->with("doe")->will($this->throwException(new InvalidTokenException())); $this->config->expects($this->once())->method("getSystemValueBool")->with("token_auth_enforced", false)->willReturn(false); $userSession->expects($this->once())->method("isTwoFactorEnforced")->with("john")->willReturn(true); $request->expects($this->any())->method("getRemoteAddress")->willReturn("192.168.0.1"); $this->throttler->expects($this->once())->method("sleepDelayOrThrowOnMax")->with("192.168.0.1"); $this->throttler->expects($this->any())->method("getDelay")->with("192.168.0.1")->willReturn(0); $userSession->logClientIn("john", "doe", $request, $this->throttler); } public function testTryTokenLoginNoHeaderNoSessionCookie() : void { $request = $this->createMock(IRequest::class); $this->config->expects(self::once())->method("getSystemValueString")->with("instanceid")->willReturn("abc123"); $request->method("getHeader")->with("Authorization")->willReturn(''); $request->method("getCookie")->with("abc123")->willReturn(null); $this->tokenProvider->expects(self::never())->method("getToken"); $loginResult = $this->userSession->tryTokenLogin($request); self::assertFalse($loginResult); } public function testTryTokenLoginAuthorizationHeaderTokenNotFound() : void { $request = $this->createMock(IRequest::class); $request->method("getHeader")->with("Authorization")->willReturn("Bearer abcde-12345"); $this->tokenProvider->expects(self::once())->method("getToken")->with("abcde-12345")->willThrowException(new InvalidTokenException()); $loginResult = $this->userSession->tryTokenLogin($request); self::assertFalse($loginResult); } public function testTryTokenLoginSessionIdTokenNotFound() : void { $request = $this->createMock(IRequest::class); $this->config->expects(self::once())->method("getSystemValueString")->with("instanceid")->willReturn("abc123"); $request->method("getHeader")->with("Authorization")->willReturn(''); $request->method("getCookie")->with("abc123")->willReturn("abcde12345"); $this->session->expects(self::once())->method("getId")->willReturn("abcde12345"); $this->tokenProvider->expects(self::once())->method("getToken")->with("abcde12345")->willThrowException(new InvalidTokenException()); $loginResult = $this->userSession->tryTokenLogin($request); self::assertFalse($loginResult); } public function testRememberLoginValidToken() { $session = $this->getMockBuilder(Memory::class)->setConstructorArgs(array(''))->getMock(); $managerMethods = get_class_methods(Manager::class); $mockedManagerMethods = array_diff($managerMethods, array("__construct", "emit", "listen")); $manager = $this->getMockBuilder(Manager::class)->setMethods($mockedManagerMethods)->setConstructorArgs(array($this->config, $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class)))->getMock(); $userSession = $this->getMockBuilder(Session::class)->setMethods(array("setMagicInCookie", "setLoginName"))->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->getMock(); $user = $this->createMock(IUser::class); $token = "goodToken"; $oldSessionId = "sess321"; $sessionId = "sess123"; $session->expects($this->once())->method("regenerateId"); $manager->expects($this->once())->method("get")->with("foo")->willReturn($user); $this->config->expects($this->once())->method("getUserKeys")->with("foo", "login_token")->willReturn(array($token)); $this->config->expects($this->once())->method("deleteUserValue")->with("foo", "login_token", $token); $this->random->expects($this->once())->method("generate")->with(32)->willReturn("abcdefg123456"); $this->config->expects($this->once())->method("setUserValue")->with("foo", "login_token", "abcdefg123456", 10000); $tokenObject = $this->createMock(IToken::class); $tokenObject->expects($this->once())->method("getLoginName")->willReturn("foobar"); $tokenObject->method("getId")->willReturn(42); $session->expects($this->once())->method("getId")->willReturn($sessionId); $this->tokenProvider->expects($this->once())->method("renewSessionToken")->with($oldSessionId, $sessionId)->willReturn($tokenObject); $this->tokenProvider->expects($this->never())->method("getToken"); $user->expects($this->any())->method("getUID")->willReturn("foo"); $userSession->expects($this->once())->method("setMagicInCookie"); $user->expects($this->once())->method("updateLastLoginTimestamp"); $setUID = false; $session->method("set")->willReturnCallback(function ($k, $v) use(&$setUID) { if ($k === "user_id" && $v === "foo") { $setUID = true; } }); $userSession->expects($this->once())->method("setLoginName")->willReturn("foobar"); $granted = $userSession->loginWithCookie("foo", $token, $oldSessionId); $this->assertTrue($setUID); $this->assertTrue($granted); } public function testRememberLoginInvalidSessionToken() { $session = $this->getMockBuilder(Memory::class)->setConstructorArgs(array(''))->getMock(); $managerMethods = get_class_methods(Manager::class); $mockedManagerMethods = array_diff($managerMethods, array("__construct", "emit", "listen")); $manager = $this->getMockBuilder(Manager::class)->setMethods($mockedManagerMethods)->setConstructorArgs(array($this->config, $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class)))->getMock(); $userSession = $this->getMockBuilder(Session::class)->setMethods(array("setMagicInCookie"))->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->getMock(); $user = $this->createMock(IUser::class); $token = "goodToken"; $oldSessionId = "sess321"; $sessionId = "sess123"; $session->expects($this->once())->method("regenerateId"); $manager->expects($this->once())->method("get")->with("foo")->willReturn($user); $this->config->expects($this->once())->method("getUserKeys")->with("foo", "login_token")->willReturn(array($token)); $this->config->expects($this->once())->method("deleteUserValue")->with("foo", "login_token", $token); $this->config->expects($this->once())->method("setUserValue"); $session->expects($this->once())->method("getId")->willReturn($sessionId); $this->tokenProvider->expects($this->once())->method("renewSessionToken")->with($oldSessionId, $sessionId)->will($this->throwException(new InvalidTokenException())); $user->expects($this->never())->method("getUID")->willReturn("foo"); $userSession->expects($this->never())->method("setMagicInCookie"); $user->expects($this->never())->method("updateLastLoginTimestamp"); $session->expects($this->never())->method("set")->with("user_id", "foo"); $granted = $userSession->loginWithCookie("foo", $token, $oldSessionId); $this->assertFalse($granted); } public function testRememberLoginInvalidToken() { $session = $this->getMockBuilder(Memory::class)->setConstructorArgs(array(''))->getMock(); $managerMethods = get_class_methods(Manager::class); $mockedManagerMethods = array_diff($managerMethods, array("__construct", "emit", "listen")); $manager = $this->getMockBuilder(Manager::class)->setMethods($mockedManagerMethods)->setConstructorArgs(array($this->config, $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class)))->getMock(); $userSession = $this->getMockBuilder(Session::class)->setMethods(array("setMagicInCookie"))->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->getMock(); $user = $this->createMock(IUser::class); $token = "goodToken"; $oldSessionId = "sess321"; $session->expects($this->once())->method("regenerateId"); $manager->expects($this->once())->method("get")->with("foo")->willReturn($user); $this->config->expects($this->once())->method("getUserKeys")->with("foo", "login_token")->willReturn(array("anothertoken")); $this->config->expects($this->never())->method("deleteUserValue")->with("foo", "login_token", $token); $this->tokenProvider->expects($this->never())->method("renewSessionToken"); $userSession->expects($this->never())->method("setMagicInCookie"); $user->expects($this->never())->method("updateLastLoginTimestamp"); $session->expects($this->never())->method("set")->with("user_id", "foo"); $granted = $userSession->loginWithCookie("foo", $token, $oldSessionId); $this->assertFalse($granted); } public function testRememberLoginInvalidUser() { $session = $this->getMockBuilder(Memory::class)->setConstructorArgs(array(''))->getMock(); $managerMethods = get_class_methods(Manager::class); $mockedManagerMethods = array_diff($managerMethods, array("__construct", "emit", "listen")); $manager = $this->getMockBuilder(Manager::class)->setMethods($mockedManagerMethods)->setConstructorArgs(array($this->config, $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class)))->getMock(); $userSession = $this->getMockBuilder(Session::class)->setMethods(array("setMagicInCookie"))->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->getMock(); $token = "goodToken"; $oldSessionId = "sess321"; $session->expects($this->once())->method("regenerateId"); $manager->expects($this->once())->method("get")->with("foo")->willReturn(null); $this->config->expects($this->never())->method("getUserKeys")->with("foo", "login_token")->willReturn(array("anothertoken")); $this->tokenProvider->expects($this->never())->method("renewSessionToken"); $userSession->expects($this->never())->method("setMagicInCookie"); $session->expects($this->never())->method("set")->with("user_id", "foo"); $granted = $userSession->loginWithCookie("foo", $token, $oldSessionId); $this->assertFalse($granted); } public function testActiveUserAfterSetSession() { $users = array("foo" => new User("foo", null, $this->createMock(IEventDispatcher::class)), "bar" => new User("bar", null, $this->createMock(IEventDispatcher::class))); $manager = $this->getMockBuilder(Manager::class)->disableOriginalConstructor()->getMock(); $manager->expects($this->any())->method("get")->willReturnCallback(function ($uid) use($users) { return $users[$uid]; }); $session = new Memory(''); $session->set("user_id", "foo"); $userSession = $this->getMockBuilder(Session::class)->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->setMethods(array("validateSession"))->getMock(); $userSession->expects($this->any())->method("validateSession"); $this->assertEquals($users["foo"], $userSession->getUser()); $session2 = new Memory(''); $session2->set("user_id", "bar"); $userSession->setSession($session2); $this->assertEquals($users["bar"], $userSession->getUser()); } public function testCreateSessionToken() { $manager = $this->createMock(Manager::class); $session = $this->createMock(ISession::class); $user = $this->createMock(IUser::class); $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); $requestId = $this->createMock(IRequestId::class); $config = $this->createMock(IConfig::class); $csrf = $this->getMockBuilder(CsrfTokenManager::class)->disableOriginalConstructor()->getMock(); $request = new Request(array("server" => array("HTTP_USER_AGENT" => "Firefox")), $requestId, $config, $csrf); $uid = "user123"; $loginName = "User123"; $password = "passme"; $sessionId = "abcxyz"; $manager->expects($this->once())->method("get")->with($uid)->willReturn($user); $session->expects($this->once())->method("getId")->willReturn($sessionId); $this->tokenProvider->expects($this->once())->method("getToken")->with($password)->will($this->throwException(new InvalidTokenException())); $this->tokenProvider->expects($this->once())->method("generateToken")->with($sessionId, $uid, $loginName, $password, "Firefox", IToken::TEMPORARY_TOKEN, IToken::DO_NOT_REMEMBER); $this->assertTrue($userSession->createSessionToken($request, $uid, $loginName, $password)); } public function testCreateRememberedSessionToken() { $manager = $this->createMock(Manager::class); $session = $this->createMock(ISession::class); $user = $this->createMock(IUser::class); $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); $requestId = $this->createMock(IRequestId::class); $config = $this->createMock(IConfig::class); $csrf = $this->getMockBuilder(CsrfTokenManager::class)->disableOriginalConstructor()->getMock(); $request = new Request(array("server" => array("HTTP_USER_AGENT" => "Firefox")), $requestId, $config, $csrf); $uid = "user123"; $loginName = "User123"; $password = "passme"; $sessionId = "abcxyz"; $manager->expects($this->once())->method("get")->with($uid)->willReturn($user); $session->expects($this->once())->method("getId")->willReturn($sessionId); $this->tokenProvider->expects($this->once())->method("getToken")->with($password)->will($this->throwException(new InvalidTokenException())); $this->tokenProvider->expects($this->once())->method("generateToken")->with($sessionId, $uid, $loginName, $password, "Firefox", IToken::TEMPORARY_TOKEN, IToken::REMEMBER); $this->assertTrue($userSession->createSessionToken($request, $uid, $loginName, $password, true)); } public function testCreateSessionTokenWithTokenPassword() { $manager = $this->getMockBuilder(Manager::class)->disableOriginalConstructor()->getMock(); $session = $this->createMock(ISession::class); $token = $this->createMock(IToken::class); $user = $this->createMock(IUser::class); $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); $requestId = $this->createMock(IRequestId::class); $config = $this->createMock(IConfig::class); $csrf = $this->getMockBuilder(CsrfTokenManager::class)->disableOriginalConstructor()->getMock(); $request = new Request(array("server" => array("HTTP_USER_AGENT" => "Firefox")), $requestId, $config, $csrf); $uid = "user123"; $loginName = "User123"; $password = "iamatoken"; $realPassword = "passme"; $sessionId = "abcxyz"; $manager->expects($this->once())->method("get")->with($uid)->willReturn($user); $session->expects($this->once())->method("getId")->willReturn($sessionId); $this->tokenProvider->expects($this->once())->method("getToken")->with($password)->willReturn($token); $this->tokenProvider->expects($this->once())->method("getPassword")->with($token, $password)->willReturn($realPassword); $this->tokenProvider->expects($this->once())->method("generateToken")->with($sessionId, $uid, $loginName, $realPassword, "Firefox", IToken::TEMPORARY_TOKEN, IToken::DO_NOT_REMEMBER); $this->assertTrue($userSession->createSessionToken($request, $uid, $loginName, $password)); } public function testCreateSessionTokenWithNonExistentUser() { $manager = $this->getMockBuilder(Manager::class)->disableOriginalConstructor()->getMock(); $session = $this->createMock(ISession::class); $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); $request = $this->createMock(IRequest::class); $uid = "user123"; $loginName = "User123"; $password = "passme"; $manager->expects($this->once())->method("get")->with($uid)->willReturn(null); $this->assertFalse($userSession->createSessionToken($request, $uid, $loginName, $password)); } 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); } public function testTryBasicAuthLoginValid() { $request = $this->createMock(Request::class); $request->method("__get")->willReturn(array("PHP_AUTH_USER" => "username", "PHP_AUTH_PW" => "password")); $request->method("__isset")->with("server")->willReturn(true); $davAuthenticatedSet = false; $lastPasswordConfirmSet = false; $this->session->method("set")->willReturnCallback(function ($k, $v) use(&$davAuthenticatedSet, &$lastPasswordConfirmSet) { switch ($k) { case Auth::DAV_AUTHENTICATED: $davAuthenticatedSet = $v; return; case "last-password-confirm": $lastPasswordConfirmSet = 1000; return; default: throw new \Exception(); } }); $userSession = $this->getMockBuilder(Session::class)->setConstructorArgs(array($this->manager, $this->session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->setMethods(array("logClientIn", "getUser"))->getMock(); $userSession->expects($this->once())->method("logClientIn")->with($this->equalTo("username"), $this->equalTo("password"), $this->equalTo($request), $this->equalTo($this->throttler))->willReturn(true); $user = $this->createMock(IUser::class); $user->method("getUID")->willReturn("username"); $userSession->expects($this->once())->method("getUser")->willReturn($user); $this->assertTrue($userSession->tryBasicAuthLogin($request, $this->throttler)); $this->assertSame("username", $davAuthenticatedSet); $this->assertSame(1000, $lastPasswordConfirmSet); } public function testTryBasicAuthLoginNoLogin() { $request = $this->createMock(Request::class); $request->method("__get")->willReturn(array()); $request->method("__isset")->with("server")->willReturn(true); $this->session->expects($this->never())->method($this->anything()); $userSession = $this->getMockBuilder(Session::class)->setConstructorArgs(array($this->manager, $this->session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->setMethods(array("logClientIn"))->getMock(); $userSession->expects($this->never())->method("logClientIn"); $this->assertFalse($userSession->tryBasicAuthLogin($request, $this->throttler)); } public function testUpdateTokens() { $this->tokenProvider->expects($this->once())->method("updatePasswords")->with("uid", "pass"); $this->userSession->updateTokens("uid", "pass"); } public function testLogClientInThrottlerUsername() { $manager = $this->createMock(Manager::class); $session = $this->createMock(ISession::class); $request = $this->createMock(IRequest::class); $userSession = $this->getMockBuilder(Session::class)->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->setMethods(array("isTokenPassword", "login", "supportsCookies", "createSessionToken", "getUser"))->getMock(); $userSession->expects($this->once())->method("isTokenPassword")->willReturn(true); $userSession->expects($this->once())->method("login")->with("john", "I-AM-AN-PASSWORD")->willReturn(false); $session->expects($this->never())->method("set"); $request->method("getRemoteAddress")->willReturn("192.168.0.1"); $this->throttler->expects($this->exactly(2))->method("sleepDelayOrThrowOnMax")->with("192.168.0.1"); $this->throttler->expects($this->any())->method("getDelay")->with("192.168.0.1")->willReturn(0); $this->throttler->expects($this->once())->method("registerAttempt")->with("login", "192.168.0.1", array("user" => "john")); $this->dispatcher->expects($this->once())->method("dispatchTyped")->with(new LoginFailed("john", "I-AM-AN-PASSWORD")); $this->assertFalse($userSession->logClientIn("john", "I-AM-AN-PASSWORD", $request, $this->throttler)); } public function testLogClientInThrottlerEmail() { $manager = $this->createMock(Manager::class); $session = $this->createMock(ISession::class); $request = $this->createMock(IRequest::class); $userSession = $this->getMockBuilder(Session::class)->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->setMethods(array("isTokenPassword", "login", "supportsCookies", "createSessionToken", "getUser"))->getMock(); $userSession->expects($this->once())->method("isTokenPassword")->willReturn(false); $userSession->expects($this->once())->method("login")->with("[email protected]", "I-AM-AN-PASSWORD")->willReturn(false); $manager->method("getByEmail")->with("[email protected]")->willReturn(array()); $session->expects($this->never())->method("set"); $request->method("getRemoteAddress")->willReturn("192.168.0.1"); $this->throttler->expects($this->exactly(2))->method("sleepDelayOrThrowOnMax")->with("192.168.0.1"); $this->throttler->expects($this->any())->method("getDelay")->with("192.168.0.1")->willReturn(0); $this->throttler->expects($this->once())->method("registerAttempt")->with("login", "192.168.0.1", array("user" => "[email protected]")); $this->dispatcher->expects($this->once())->method("dispatchTyped")->with(new LoginFailed("[email protected]", "I-AM-AN-PASSWORD")); $this->assertFalse($userSession->logClientIn("[email protected]", "I-AM-AN-PASSWORD", $request, $this->throttler)); } } ?>

Did this file decode correctly?

Original Code

<?php
 namespace Test\User; use OC\AppFramework\Http\Request; use OC\Authentication\Events\LoginFailed; use OC\Authentication\Exceptions\InvalidTokenException; use OC\Authentication\Exceptions\PasswordLoginForbiddenException; use OC\Authentication\Token\IProvider; use OC\Authentication\Token\IToken; use OC\Security\CSRF\CsrfTokenManager; use OC\Session\Memory; use OC\User\LoginException; use OC\User\Manager; use OC\User\Session; use OC\User\User; use OCA\DAV\Connector\Sabre\Auth; use OCP\AppFramework\Utility\ITimeFactory; use OCP\EventDispatcher\IEventDispatcher; use OCP\ICacheFactory; use OCP\IConfig; use OCP\IRequest; use OCP\IRequestId; use OCP\ISession; use OCP\IUser; use OCP\Lockdown\ILockdownManager; use OCP\Security\Bruteforce\IThrottler; use OCP\Security\ISecureRandom; use OCP\User\Events\PostLoginEvent; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; class SessionTest extends \Test\TestCase { private $timeFactory; private $tokenProvider; private $config; private $throttler; private $random; private $manager; private $session; private $userSession; private $lockdownManager; private $logger; private $dispatcher; protected function setUp() : void { parent::setUp(); $this->timeFactory = $this->createMock(ITimeFactory::class); $this->timeFactory->expects($this->any())->method("\x67\145\x74\124\x69\155\145")->willReturn(10000); $this->tokenProvider = $this->createMock(IProvider::class); $this->config = $this->createMock(IConfig::class); $this->throttler = $this->createMock(IThrottler::class); $this->random = $this->createMock(ISecureRandom::class); $this->manager = $this->createMock(Manager::class); $this->session = $this->createMock(ISession::class); $this->lockdownManager = $this->createMock(ILockdownManager::class); $this->logger = $this->createMock(LoggerInterface::class); $this->dispatcher = $this->createMock(IEventDispatcher::class); $this->userSession = $this->getMockBuilder(Session::class)->setConstructorArgs(array($this->manager, $this->session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->setMethods(array("\x73\145\164\115\x61\x67\151\x63\x49\156\103\x6f\157\x6b\151\145"))->getMock(); \OC_User::setIncognitoMode(false); } public function isLoggedInData() { return array(array(true), array(false)); } public function testIsLoggedIn($isLoggedIn) { $session = $this->getMockBuilder(Memory::class)->setConstructorArgs(array(''))->getMock(); $manager = $this->createMock(Manager::class); $userSession = $this->getMockBuilder(Session::class)->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->setMethods(array("\x67\145\x74\x55\163\145\162"))->getMock(); $user = new User("\163\x65\x70\x70", null, $this->createMock(IEventDispatcher::class)); $userSession->expects($this->once())->method("\147\x65\x74\x55\x73\x65\162")->willReturn($isLoggedIn ? $user : null); $this->assertEquals($isLoggedIn, $userSession->isLoggedIn()); } public function testSetUser() { $session = $this->getMockBuilder(Memory::class)->setConstructorArgs(array(''))->getMock(); $session->expects($this->once())->method("\163\145\x74")->with("\x75\163\x65\x72\137\x69\144", "\x66\x6f\x6f"); $manager = $this->createMock(Manager::class); $backend = $this->createMock(\Test\Util\User\Dummy::class); $user = $this->createMock(IUser::class); $user->expects($this->once())->method("\147\x65\164\125\x49\104")->willReturn("\146\157\157"); $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); $userSession->setUser($user); } public function testLoginValidPasswordEnabled() { $session = $this->getMockBuilder(Memory::class)->setConstructorArgs(array(''))->getMock(); $session->expects($this->once())->method("\x72\145\x67\145\156\x65\162\141\164\145\x49\144"); $this->tokenProvider->expects($this->once())->method("\147\145\x74\124\x6f\153\x65\156")->with("\x62\x61\x72")->will($this->throwException(new InvalidTokenException())); $session->expects($this->exactly(2))->method("\x73\x65\164")->with($this->callback(function ($key) { switch ($key) { case "\x75\163\x65\x72\x5f\151\144": case "\x6c\157\147\151\x6e\x6e\141\155\145": return true; break; default: return false; break; } }, "\146\x6f\157")); $managerMethods = get_class_methods(Manager::class); $mockedManagerMethods = array_diff($managerMethods, array("\x5f\137\x63\x6f\156\x73\x74\162\165\143\x74", "\145\x6d\x69\x74", "\x6c\151\163\164\x65\156")); $manager = $this->getMockBuilder(Manager::class)->setMethods($mockedManagerMethods)->setConstructorArgs(array($this->config, $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class)))->getMock(); $backend = $this->createMock(\Test\Util\User\Dummy::class); $user = $this->createMock(IUser::class); $user->expects($this->any())->method("\x69\163\x45\156\141\142\154\145\144")->willReturn(true); $user->expects($this->any())->method("\147\145\x74\x55\x49\x44")->willReturn("\146\157\157"); $user->expects($this->once())->method("\x75\x70\144\x61\164\145\114\x61\163\x74\x4c\x6f\147\151\x6e\124\x69\x6d\x65\163\164\141\x6d\x70"); $manager->expects($this->once())->method("\x63\x68\x65\x63\153\x50\x61\163\163\x77\157\x72\144\116\157\114\157\x67\x67\x69\x6e\x67")->with("\x66\157\157", "\x62\x61\162")->willReturn($user); $userSession = $this->getMockBuilder(Session::class)->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->setMethods(array("\x70\x72\x65\x70\141\162\145\x55\163\145\x72\114\x6f\x67\x69\x6e"))->getMock(); $userSession->expects($this->once())->method("\x70\162\x65\x70\x61\x72\145\125\163\145\162\x4c\x6f\147\151\x6e"); $this->dispatcher->expects($this->once())->method("\x64\x69\163\x70\141\x74\x63\150\x54\171\160\145\144")->with($this->callback(function (PostLoginEvent $e) { return $e->getUser()->getUID() === "\146\x6f\157" && $e->getPassword() === "\x62\x61\x72" && $e->isTokenLogin() === false; })); $userSession->login("\146\157\157", "\x62\141\x72"); $this->assertEquals($user, $userSession->getUser()); } public function testLoginValidPasswordDisabled() { $this->expectException(LoginException::class); $session = $this->getMockBuilder(Memory::class)->setConstructorArgs(array(''))->getMock(); $session->expects($this->never())->method("\x73\145\164"); $session->expects($this->once())->method("\162\145\147\x65\156\145\162\141\x74\145\111\x64"); $this->tokenProvider->expects($this->once())->method("\x67\x65\164\x54\157\153\145\x6e")->with("\142\x61\x72")->will($this->throwException(new InvalidTokenException())); $managerMethods = get_class_methods(Manager::class); $mockedManagerMethods = array_diff($managerMethods, array("\137\137\143\x6f\156\163\164\162\x75\143\x74", "\x65\155\x69\164", "\x6c\x69\x73\164\145\156")); $manager = $this->getMockBuilder(Manager::class)->setMethods($mockedManagerMethods)->setConstructorArgs(array($this->config, $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class)))->getMock(); $user = $this->createMock(IUser::class); $user->expects($this->any())->method("\151\163\105\x6e\141\x62\x6c\145\144")->willReturn(false); $user->expects($this->never())->method("\165\x70\x64\x61\164\x65\x4c\141\163\x74\114\x6f\x67\151\156\124\x69\x6d\x65\x73\x74\141\x6d\x70"); $manager->expects($this->once())->method("\143\150\145\143\153\120\141\x73\x73\167\157\x72\x64\116\157\x4c\157\x67\x67\151\x6e\147")->with("\x66\157\157", "\142\141\x72")->willReturn($user); $this->dispatcher->expects($this->never())->method("\144\x69\x73\x70\x61\x74\x63\x68"); $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); $userSession->login("\x66\157\157", "\142\141\x72"); } public function testLoginInvalidPassword() { $session = $this->getMockBuilder(Memory::class)->setConstructorArgs(array(''))->getMock(); $managerMethods = get_class_methods(Manager::class); $mockedManagerMethods = array_diff($managerMethods, array("\x5f\137\x63\x6f\x6e\x73\164\162\165\143\164", "\145\x6d\151\x74", "\154\x69\x73\164\145\x6e")); $manager = $this->getMockBuilder(Manager::class)->setMethods($mockedManagerMethods)->setConstructorArgs(array($this->config, $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class)))->getMock(); $backend = $this->createMock(\Test\Util\User\Dummy::class); $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); $user = $this->createMock(IUser::class); $session->expects($this->never())->method("\x73\x65\x74"); $session->expects($this->once())->method("\162\145\x67\x65\156\x65\x72\x61\x74\145\x49\144"); $this->tokenProvider->expects($this->once())->method("\147\145\x74\124\157\153\x65\x6e")->with("\x62\x61\162")->will($this->throwException(new InvalidTokenException())); $user->expects($this->never())->method("\151\x73\105\156\141\142\154\x65\144"); $user->expects($this->never())->method("\165\x70\144\141\164\145\114\x61\163\x74\x4c\x6f\147\x69\x6e\x54\x69\x6d\x65\x73\x74\141\155\x70"); $manager->expects($this->once())->method("\x63\x68\x65\143\x6b\x50\x61\163\163\167\x6f\162\x64\x4e\x6f\114\157\x67\147\x69\x6e\147")->with("\146\x6f\157", "\x62\141\162")->willReturn(false); $this->dispatcher->expects($this->never())->method("\x64\151\163\160\141\164\x63\x68"); $userSession->login("\146\157\x6f", "\142\141\162"); } public function testLoginNonExisting() { $session = $this->getMockBuilder(Memory::class)->setConstructorArgs(array(''))->getMock(); $manager = $this->createMock(Manager::class); $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); $session->expects($this->never())->method("\x73\145\x74"); $session->expects($this->once())->method("\162\x65\x67\145\x6e\x65\162\141\x74\145\111\x64"); $this->tokenProvider->expects($this->once())->method("\147\x65\x74\124\x6f\x6b\145\x6e")->with("\x62\141\162")->will($this->throwException(new InvalidTokenException())); $manager->expects($this->once())->method("\143\150\x65\143\x6b\x50\x61\163\x73\167\x6f\162\x64\116\157\x4c\157\x67\x67\x69\x6e\147")->with("\x66\157\157", "\142\141\162")->willReturn(false); $userSession->login("\x66\157\157", "\x62\x61\162"); } public function testLogClientInNoTokenPasswordWith2fa() { $this->expectException(PasswordLoginForbiddenException::class); $manager = $this->createMock(Manager::class); $session = $this->createMock(ISession::class); $request = $this->createMock(IRequest::class); $userSession = $this->getMockBuilder(Session::class)->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->setMethods(array("\154\157\147\x69\x6e", "\x73\165\160\x70\157\x72\x74\x73\x43\x6f\157\153\x69\x65\163", "\x63\162\x65\141\x74\x65\x53\145\x73\x73\x69\x6f\156\124\157\153\x65\x6e", "\x67\x65\x74\125\163\x65\162"))->getMock(); $this->tokenProvider->expects($this->once())->method("\x67\145\164\124\157\153\145\x6e")->with("\x64\x6f\145")->will($this->throwException(new InvalidTokenException())); $this->config->expects($this->once())->method("\x67\x65\x74\x53\171\163\x74\x65\x6d\x56\141\154\165\145\102\157\157\154")->with("\x74\x6f\x6b\145\156\x5f\x61\165\164\150\x5f\x65\x6e\x66\x6f\162\143\x65\144", false)->willReturn(true); $request->expects($this->any())->method("\147\x65\x74\122\145\155\157\164\x65\x41\144\144\x72\145\163\163")->willReturn("\61\71\62\56\61\66\x38\56\x30\x2e\61"); $this->throttler->expects($this->once())->method("\163\x6c\145\x65\160\104\145\x6c\141\x79\x4f\x72\x54\x68\162\x6f\x77\117\x6e\115\x61\x78")->with("\x31\x39\x32\x2e\61\x36\70\x2e\x30\x2e\61"); $this->throttler->expects($this->any())->method("\147\145\164\104\x65\x6c\141\x79")->with("\61\x39\62\56\x31\66\x38\x2e\60\56\61")->willReturn(0); $userSession->logClientIn("\152\157\x68\x6e", "\x64\157\145", $request, $this->throttler); } public function testLogClientInUnexist() { $manager = $this->createMock(Manager::class); $session = $this->createMock(ISession::class); $request = $this->createMock(IRequest::class); $userSession = $this->getMockBuilder(Session::class)->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->setMethods(array("\x6c\x6f\147\151\x6e", "\163\x75\x70\160\x6f\162\164\x73\x43\157\157\x6b\151\145\163", "\x63\x72\145\x61\x74\x65\123\x65\x73\163\x69\157\x6e\x54\x6f\x6b\145\x6e", "\147\x65\x74\125\x73\x65\x72"))->getMock(); $this->tokenProvider->expects($this->once())->method("\147\145\x74\124\157\x6b\145\x6e")->with("\144\x6f\x65")->will($this->throwException(new InvalidTokenException())); $this->config->expects($this->once())->method("\147\145\164\123\x79\163\x74\145\x6d\126\141\x6c\165\x65\102\x6f\x6f\154")->with("\x74\x6f\153\145\x6e\137\x61\165\x74\150\137\145\x6e\146\157\162\x63\x65\x64", false)->willReturn(false); $manager->method("\147\x65\164\x42\171\x45\x6d\x61\151\154")->with("\165\x6e\145\x78\151\163\x74")->willReturn(array()); $this->assertFalse($userSession->logClientIn("\165\156\145\170\x69\x73\164", "\144\x6f\x65", $request, $this->throttler)); } public function testLogClientInWithTokenPassword() { $manager = $this->createMock(Manager::class); $session = $this->createMock(ISession::class); $request = $this->createMock(IRequest::class); $userSession = $this->getMockBuilder(Session::class)->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->setMethods(array("\x69\x73\x54\x6f\153\145\x6e\x50\141\x73\163\x77\x6f\162\144", "\x6c\x6f\147\x69\156", "\x73\x75\160\160\x6f\x72\164\x73\103\x6f\x6f\153\151\x65\163", "\143\162\x65\x61\164\x65\x53\145\163\163\151\157\156\x54\157\153\x65\x6e", "\x67\145\x74\x55\163\x65\x72"))->getMock(); $userSession->expects($this->once())->method("\x69\x73\x54\x6f\x6b\145\156\x50\x61\163\163\167\157\162\144")->willReturn(true); $userSession->expects($this->once())->method("\x6c\x6f\x67\x69\156")->with("\x6a\x6f\150\x6e", "\111\55\x41\x4d\x2d\101\x4e\55\101\x50\x50\55\x50\101\x53\x53\127\x4f\122\104")->willReturn(true); $session->expects($this->once())->method("\x73\x65\164")->with("\x61\x70\160\x5f\x70\x61\x73\x73\x77\157\162\144", "\x49\55\x41\x4d\55\x41\x4e\55\101\x50\120\x2d\x50\x41\x53\x53\127\x4f\x52\104"); $request->expects($this->any())->method("\x67\x65\x74\x52\x65\x6d\x6f\x74\145\x41\144\x64\x72\x65\163\x73")->willReturn("\x31\x39\62\x2e\61\x36\70\56\x30\56\x31"); $this->throttler->expects($this->once())->method("\x73\154\145\x65\160\x44\145\x6c\141\x79\x4f\x72\124\150\162\157\167\x4f\x6e\x4d\x61\170")->with("\61\x39\62\x2e\x31\x36\70\56\60\x2e\61"); $this->throttler->expects($this->any())->method("\x67\x65\x74\104\145\x6c\x61\171")->with("\61\71\x32\x2e\x31\66\70\x2e\x30\56\61")->willReturn(0); $this->assertTrue($userSession->logClientIn("\x6a\x6f\x68\x6e", "\111\55\x41\115\x2d\101\116\55\x41\x50\x50\55\x50\101\x53\x53\x57\x4f\x52\104", $request, $this->throttler)); } public function testLogClientInNoTokenPasswordNo2fa() { $this->expectException(PasswordLoginForbiddenException::class); $manager = $this->createMock(Manager::class); $session = $this->createMock(ISession::class); $request = $this->createMock(IRequest::class); $userSession = $this->getMockBuilder(Session::class)->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->setMethods(array("\x6c\x6f\x67\x69\156", "\x69\163\124\x77\157\106\141\x63\x74\157\162\105\x6e\x66\157\x72\x63\145\144"))->getMock(); $this->tokenProvider->expects($this->once())->method("\x67\145\164\124\x6f\153\145\x6e")->with("\144\x6f\x65")->will($this->throwException(new InvalidTokenException())); $this->config->expects($this->once())->method("\x67\145\164\123\x79\163\164\x65\x6d\126\x61\154\165\x65\x42\157\x6f\x6c")->with("\164\157\153\x65\156\x5f\141\x75\164\x68\x5f\145\156\146\x6f\x72\143\145\x64", false)->willReturn(false); $userSession->expects($this->once())->method("\x69\x73\124\x77\x6f\x46\x61\143\x74\157\162\x45\156\146\x6f\162\143\145\x64")->with("\x6a\157\x68\x6e")->willReturn(true); $request->expects($this->any())->method("\147\x65\x74\122\145\x6d\157\x74\145\101\x64\x64\x72\x65\163\163")->willReturn("\x31\71\x32\56\x31\x36\70\56\x30\56\x31"); $this->throttler->expects($this->once())->method("\x73\154\x65\x65\x70\x44\145\x6c\x61\x79\117\162\x54\x68\162\157\x77\117\x6e\x4d\141\170")->with("\61\x39\62\56\61\66\x38\56\60\56\61"); $this->throttler->expects($this->any())->method("\147\x65\164\104\145\154\141\171")->with("\61\71\x32\x2e\x31\66\70\56\60\x2e\x31")->willReturn(0); $userSession->logClientIn("\152\157\x68\156", "\x64\157\x65", $request, $this->throttler); } public function testTryTokenLoginNoHeaderNoSessionCookie() : void { $request = $this->createMock(IRequest::class); $this->config->expects(self::once())->method("\x67\145\164\x53\171\x73\x74\145\x6d\x56\x61\154\x75\x65\x53\x74\x72\x69\x6e\x67")->with("\151\x6e\x73\x74\141\156\x63\145\151\144")->willReturn("\x61\142\x63\61\62\63"); $request->method("\x67\145\164\x48\145\x61\144\x65\162")->with("\101\165\164\150\x6f\162\151\172\141\x74\151\157\156")->willReturn(''); $request->method("\147\x65\x74\103\157\x6f\x6b\151\x65")->with("\x61\142\143\61\x32\63")->willReturn(null); $this->tokenProvider->expects(self::never())->method("\x67\145\x74\x54\157\153\145\x6e"); $loginResult = $this->userSession->tryTokenLogin($request); self::assertFalse($loginResult); } public function testTryTokenLoginAuthorizationHeaderTokenNotFound() : void { $request = $this->createMock(IRequest::class); $request->method("\147\x65\x74\x48\145\x61\x64\145\x72")->with("\101\165\x74\150\157\162\x69\172\x61\164\151\157\x6e")->willReturn("\102\x65\141\x72\145\162\x20\141\x62\x63\144\145\x2d\x31\x32\x33\x34\x35"); $this->tokenProvider->expects(self::once())->method("\x67\x65\x74\x54\x6f\153\145\x6e")->with("\141\x62\x63\x64\145\x2d\x31\62\x33\x34\x35")->willThrowException(new InvalidTokenException()); $loginResult = $this->userSession->tryTokenLogin($request); self::assertFalse($loginResult); } public function testTryTokenLoginSessionIdTokenNotFound() : void { $request = $this->createMock(IRequest::class); $this->config->expects(self::once())->method("\x67\x65\164\123\x79\163\164\x65\x6d\126\x61\154\165\x65\x53\164\162\151\x6e\147")->with("\151\x6e\163\x74\x61\x6e\x63\x65\x69\144")->willReturn("\141\x62\143\61\x32\x33"); $request->method("\147\145\164\x48\145\x61\x64\x65\162")->with("\101\x75\x74\x68\157\162\x69\x7a\141\x74\x69\157\156")->willReturn(''); $request->method("\147\x65\x74\103\157\x6f\x6b\x69\x65")->with("\x61\x62\x63\61\62\x33")->willReturn("\x61\142\143\x64\x65\x31\62\63\64\x35"); $this->session->expects(self::once())->method("\147\x65\x74\111\144")->willReturn("\141\142\x63\x64\x65\x31\x32\x33\64\x35"); $this->tokenProvider->expects(self::once())->method("\x67\145\x74\124\157\x6b\145\156")->with("\141\x62\x63\144\145\x31\x32\x33\64\65")->willThrowException(new InvalidTokenException()); $loginResult = $this->userSession->tryTokenLogin($request); self::assertFalse($loginResult); } public function testRememberLoginValidToken() { $session = $this->getMockBuilder(Memory::class)->setConstructorArgs(array(''))->getMock(); $managerMethods = get_class_methods(Manager::class); $mockedManagerMethods = array_diff($managerMethods, array("\137\137\143\x6f\x6e\163\164\162\x75\x63\x74", "\x65\155\151\164", "\x6c\x69\x73\164\x65\156")); $manager = $this->getMockBuilder(Manager::class)->setMethods($mockedManagerMethods)->setConstructorArgs(array($this->config, $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class)))->getMock(); $userSession = $this->getMockBuilder(Session::class)->setMethods(array("\163\145\164\115\141\x67\151\143\111\x6e\x43\x6f\x6f\x6b\x69\145", "\163\145\x74\114\x6f\x67\x69\x6e\116\141\x6d\x65"))->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->getMock(); $user = $this->createMock(IUser::class); $token = "\147\x6f\x6f\144\x54\157\153\x65\156"; $oldSessionId = "\163\x65\x73\x73\63\x32\x31"; $sessionId = "\163\x65\163\x73\61\x32\63"; $session->expects($this->once())->method("\162\145\147\145\156\x65\162\x61\x74\x65\111\x64"); $manager->expects($this->once())->method("\x67\145\x74")->with("\x66\x6f\x6f")->willReturn($user); $this->config->expects($this->once())->method("\147\x65\164\x55\163\145\x72\x4b\145\x79\163")->with("\x66\x6f\157", "\154\x6f\147\151\156\137\x74\157\153\x65\x6e")->willReturn(array($token)); $this->config->expects($this->once())->method("\144\x65\154\x65\x74\x65\x55\x73\x65\x72\x56\x61\x6c\165\145")->with("\146\x6f\157", "\x6c\157\147\x69\156\137\164\x6f\153\145\156", $token); $this->random->expects($this->once())->method("\x67\145\x6e\145\162\141\x74\x65")->with(32)->willReturn("\x61\142\143\x64\145\x66\x67\61\x32\x33\x34\65\x36"); $this->config->expects($this->once())->method("\163\145\x74\125\x73\145\162\x56\141\154\165\x65")->with("\x66\157\x6f", "\x6c\157\x67\151\156\x5f\x74\157\153\145\x6e", "\141\x62\143\x64\145\146\147\x31\x32\63\64\65\66", 10000); $tokenObject = $this->createMock(IToken::class); $tokenObject->expects($this->once())->method("\x67\x65\x74\x4c\157\x67\x69\156\116\x61\x6d\145")->willReturn("\146\157\157\x62\141\x72"); $tokenObject->method("\147\x65\164\x49\x64")->willReturn(42); $session->expects($this->once())->method("\x67\145\164\x49\144")->willReturn($sessionId); $this->tokenProvider->expects($this->once())->method("\x72\x65\x6e\x65\167\x53\x65\163\163\x69\157\x6e\x54\x6f\x6b\x65\x6e")->with($oldSessionId, $sessionId)->willReturn($tokenObject); $this->tokenProvider->expects($this->never())->method("\147\145\164\x54\x6f\x6b\x65\x6e"); $user->expects($this->any())->method("\147\145\x74\125\111\x44")->willReturn("\x66\x6f\157"); $userSession->expects($this->once())->method("\163\x65\x74\115\141\x67\151\143\x49\x6e\x43\157\x6f\153\151\145"); $user->expects($this->once())->method("\165\160\x64\x61\164\145\114\x61\x73\164\x4c\157\x67\151\x6e\124\x69\155\x65\x73\164\141\155\160"); $setUID = false; $session->method("\163\x65\164")->willReturnCallback(function ($k, $v) use(&$setUID) { if ($k === "\165\163\x65\162\x5f\x69\144" && $v === "\x66\x6f\157") { $setUID = true; } }); $userSession->expects($this->once())->method("\x73\x65\164\114\157\x67\151\156\x4e\x61\x6d\x65")->willReturn("\x66\157\157\x62\x61\x72"); $granted = $userSession->loginWithCookie("\146\x6f\157", $token, $oldSessionId); $this->assertTrue($setUID); $this->assertTrue($granted); } public function testRememberLoginInvalidSessionToken() { $session = $this->getMockBuilder(Memory::class)->setConstructorArgs(array(''))->getMock(); $managerMethods = get_class_methods(Manager::class); $mockedManagerMethods = array_diff($managerMethods, array("\x5f\137\143\157\x6e\163\164\x72\165\143\x74", "\x65\x6d\151\164", "\x6c\x69\163\x74\145\156")); $manager = $this->getMockBuilder(Manager::class)->setMethods($mockedManagerMethods)->setConstructorArgs(array($this->config, $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class)))->getMock(); $userSession = $this->getMockBuilder(Session::class)->setMethods(array("\163\x65\x74\x4d\141\x67\151\143\x49\x6e\x43\157\x6f\x6b\x69\x65"))->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->getMock(); $user = $this->createMock(IUser::class); $token = "\147\x6f\x6f\x64\124\157\153\x65\156"; $oldSessionId = "\x73\x65\x73\x73\x33\62\x31"; $sessionId = "\163\145\x73\x73\61\x32\63"; $session->expects($this->once())->method("\x72\x65\x67\145\x6e\145\x72\x61\x74\x65\x49\144"); $manager->expects($this->once())->method("\x67\145\164")->with("\x66\157\157")->willReturn($user); $this->config->expects($this->once())->method("\x67\145\x74\125\x73\145\x72\113\145\171\x73")->with("\146\x6f\x6f", "\x6c\157\x67\x69\x6e\x5f\x74\157\x6b\145\x6e")->willReturn(array($token)); $this->config->expects($this->once())->method("\144\145\x6c\145\164\x65\125\163\145\162\x56\x61\154\x75\145")->with("\146\x6f\157", "\x6c\x6f\x67\151\156\x5f\x74\157\153\x65\x6e", $token); $this->config->expects($this->once())->method("\163\145\164\125\163\x65\x72\126\141\154\x75\145"); $session->expects($this->once())->method("\x67\x65\164\111\x64")->willReturn($sessionId); $this->tokenProvider->expects($this->once())->method("\x72\145\x6e\145\x77\x53\x65\163\163\x69\x6f\x6e\124\x6f\153\x65\156")->with($oldSessionId, $sessionId)->will($this->throwException(new InvalidTokenException())); $user->expects($this->never())->method("\147\x65\164\125\111\104")->willReturn("\146\157\x6f"); $userSession->expects($this->never())->method("\x73\x65\164\x4d\x61\x67\151\143\111\x6e\x43\x6f\x6f\153\151\x65"); $user->expects($this->never())->method("\x75\160\x64\x61\x74\x65\x4c\141\x73\x74\x4c\x6f\x67\x69\156\124\151\x6d\x65\163\164\141\x6d\x70"); $session->expects($this->never())->method("\x73\x65\164")->with("\x75\x73\x65\x72\137\151\x64", "\x66\157\x6f"); $granted = $userSession->loginWithCookie("\x66\x6f\157", $token, $oldSessionId); $this->assertFalse($granted); } public function testRememberLoginInvalidToken() { $session = $this->getMockBuilder(Memory::class)->setConstructorArgs(array(''))->getMock(); $managerMethods = get_class_methods(Manager::class); $mockedManagerMethods = array_diff($managerMethods, array("\137\137\143\x6f\x6e\163\x74\x72\165\143\x74", "\x65\155\151\164", "\x6c\x69\163\164\x65\x6e")); $manager = $this->getMockBuilder(Manager::class)->setMethods($mockedManagerMethods)->setConstructorArgs(array($this->config, $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class)))->getMock(); $userSession = $this->getMockBuilder(Session::class)->setMethods(array("\163\x65\164\x4d\x61\x67\x69\143\111\x6e\103\157\157\153\x69\145"))->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->getMock(); $user = $this->createMock(IUser::class); $token = "\147\157\157\144\124\x6f\x6b\x65\156"; $oldSessionId = "\x73\145\x73\x73\63\x32\x31"; $session->expects($this->once())->method("\x72\x65\147\145\156\x65\x72\x61\164\x65\111\x64"); $manager->expects($this->once())->method("\x67\x65\x74")->with("\146\x6f\x6f")->willReturn($user); $this->config->expects($this->once())->method("\x67\145\164\x55\x73\145\x72\x4b\145\x79\163")->with("\146\157\x6f", "\x6c\157\x67\151\x6e\x5f\x74\x6f\153\145\156")->willReturn(array("\x61\x6e\157\164\x68\x65\162\x74\x6f\x6b\145\x6e")); $this->config->expects($this->never())->method("\144\x65\x6c\145\x74\145\125\x73\x65\162\x56\141\x6c\165\x65")->with("\x66\x6f\x6f", "\154\x6f\x67\x69\x6e\x5f\x74\x6f\x6b\x65\x6e", $token); $this->tokenProvider->expects($this->never())->method("\162\145\156\145\x77\x53\x65\163\163\x69\157\x6e\x54\x6f\153\145\156"); $userSession->expects($this->never())->method("\163\145\164\115\x61\x67\x69\x63\x49\156\103\x6f\x6f\153\x69\x65"); $user->expects($this->never())->method("\x75\160\144\x61\x74\x65\x4c\x61\163\x74\114\157\147\151\156\x54\x69\x6d\x65\163\164\141\x6d\160"); $session->expects($this->never())->method("\x73\x65\x74")->with("\x75\163\x65\162\137\151\144", "\x66\157\157"); $granted = $userSession->loginWithCookie("\146\157\x6f", $token, $oldSessionId); $this->assertFalse($granted); } public function testRememberLoginInvalidUser() { $session = $this->getMockBuilder(Memory::class)->setConstructorArgs(array(''))->getMock(); $managerMethods = get_class_methods(Manager::class); $mockedManagerMethods = array_diff($managerMethods, array("\x5f\x5f\x63\x6f\156\163\x74\162\165\x63\x74", "\145\x6d\x69\164", "\154\x69\x73\164\x65\156")); $manager = $this->getMockBuilder(Manager::class)->setMethods($mockedManagerMethods)->setConstructorArgs(array($this->config, $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class)))->getMock(); $userSession = $this->getMockBuilder(Session::class)->setMethods(array("\163\x65\164\115\x61\x67\151\143\111\x6e\103\157\157\x6b\151\145"))->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->getMock(); $token = "\147\157\157\x64\x54\x6f\x6b\145\156"; $oldSessionId = "\163\145\x73\163\63\x32\61"; $session->expects($this->once())->method("\x72\x65\147\x65\156\x65\x72\141\x74\x65\111\144"); $manager->expects($this->once())->method("\147\145\x74")->with("\x66\x6f\x6f")->willReturn(null); $this->config->expects($this->never())->method("\147\145\x74\x55\x73\145\162\113\x65\x79\x73")->with("\x66\x6f\x6f", "\x6c\157\147\x69\156\x5f\x74\157\153\x65\x6e")->willReturn(array("\x61\156\157\x74\150\145\162\x74\x6f\x6b\x65\156")); $this->tokenProvider->expects($this->never())->method("\162\x65\x6e\145\x77\x53\x65\x73\x73\151\x6f\156\x54\157\x6b\145\156"); $userSession->expects($this->never())->method("\x73\145\164\x4d\x61\147\151\x63\111\156\103\x6f\157\153\151\x65"); $session->expects($this->never())->method("\163\x65\x74")->with("\x75\163\145\x72\x5f\x69\144", "\146\x6f\157"); $granted = $userSession->loginWithCookie("\146\x6f\157", $token, $oldSessionId); $this->assertFalse($granted); } public function testActiveUserAfterSetSession() { $users = array("\146\157\x6f" => new User("\x66\x6f\157", null, $this->createMock(IEventDispatcher::class)), "\x62\141\162" => new User("\x62\x61\x72", null, $this->createMock(IEventDispatcher::class))); $manager = $this->getMockBuilder(Manager::class)->disableOriginalConstructor()->getMock(); $manager->expects($this->any())->method("\x67\145\x74")->willReturnCallback(function ($uid) use($users) { return $users[$uid]; }); $session = new Memory(''); $session->set("\x75\x73\x65\x72\x5f\x69\144", "\146\x6f\x6f"); $userSession = $this->getMockBuilder(Session::class)->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->setMethods(array("\166\x61\x6c\x69\x64\141\164\x65\123\145\163\163\151\157\x6e"))->getMock(); $userSession->expects($this->any())->method("\166\x61\154\151\144\141\x74\145\123\145\163\163\x69\157\x6e"); $this->assertEquals($users["\146\157\x6f"], $userSession->getUser()); $session2 = new Memory(''); $session2->set("\165\x73\x65\162\x5f\x69\x64", "\142\x61\x72"); $userSession->setSession($session2); $this->assertEquals($users["\142\x61\x72"], $userSession->getUser()); } public function testCreateSessionToken() { $manager = $this->createMock(Manager::class); $session = $this->createMock(ISession::class); $user = $this->createMock(IUser::class); $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); $requestId = $this->createMock(IRequestId::class); $config = $this->createMock(IConfig::class); $csrf = $this->getMockBuilder(CsrfTokenManager::class)->disableOriginalConstructor()->getMock(); $request = new Request(array("\x73\x65\x72\166\x65\162" => array("\110\x54\124\120\x5f\125\x53\x45\x52\x5f\x41\x47\105\x4e\124" => "\x46\151\162\x65\x66\157\x78")), $requestId, $config, $csrf); $uid = "\165\163\145\162\61\62\63"; $loginName = "\125\x73\x65\x72\61\x32\x33"; $password = "\160\x61\163\163\x6d\x65"; $sessionId = "\x61\142\143\170\x79\x7a"; $manager->expects($this->once())->method("\147\145\x74")->with($uid)->willReturn($user); $session->expects($this->once())->method("\147\145\x74\x49\x64")->willReturn($sessionId); $this->tokenProvider->expects($this->once())->method("\x67\145\164\x54\157\153\x65\156")->with($password)->will($this->throwException(new InvalidTokenException())); $this->tokenProvider->expects($this->once())->method("\147\x65\156\145\x72\141\164\145\124\x6f\153\x65\x6e")->with($sessionId, $uid, $loginName, $password, "\x46\x69\162\145\x66\x6f\x78", IToken::TEMPORARY_TOKEN, IToken::DO_NOT_REMEMBER); $this->assertTrue($userSession->createSessionToken($request, $uid, $loginName, $password)); } public function testCreateRememberedSessionToken() { $manager = $this->createMock(Manager::class); $session = $this->createMock(ISession::class); $user = $this->createMock(IUser::class); $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); $requestId = $this->createMock(IRequestId::class); $config = $this->createMock(IConfig::class); $csrf = $this->getMockBuilder(CsrfTokenManager::class)->disableOriginalConstructor()->getMock(); $request = new Request(array("\163\145\x72\x76\145\162" => array("\x48\124\x54\x50\x5f\125\x53\105\122\x5f\101\107\105\x4e\124" => "\x46\151\x72\145\x66\157\170")), $requestId, $config, $csrf); $uid = "\x75\163\x65\x72\61\x32\x33"; $loginName = "\125\163\x65\162\61\x32\63"; $password = "\x70\141\x73\163\x6d\x65"; $sessionId = "\141\x62\x63\170\171\x7a"; $manager->expects($this->once())->method("\147\x65\x74")->with($uid)->willReturn($user); $session->expects($this->once())->method("\147\145\164\111\x64")->willReturn($sessionId); $this->tokenProvider->expects($this->once())->method("\x67\x65\164\124\x6f\153\x65\156")->with($password)->will($this->throwException(new InvalidTokenException())); $this->tokenProvider->expects($this->once())->method("\147\x65\156\145\x72\141\164\145\x54\x6f\153\x65\156")->with($sessionId, $uid, $loginName, $password, "\106\151\162\x65\146\157\170", IToken::TEMPORARY_TOKEN, IToken::REMEMBER); $this->assertTrue($userSession->createSessionToken($request, $uid, $loginName, $password, true)); } public function testCreateSessionTokenWithTokenPassword() { $manager = $this->getMockBuilder(Manager::class)->disableOriginalConstructor()->getMock(); $session = $this->createMock(ISession::class); $token = $this->createMock(IToken::class); $user = $this->createMock(IUser::class); $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); $requestId = $this->createMock(IRequestId::class); $config = $this->createMock(IConfig::class); $csrf = $this->getMockBuilder(CsrfTokenManager::class)->disableOriginalConstructor()->getMock(); $request = new Request(array("\x73\x65\x72\x76\145\x72" => array("\110\x54\x54\120\137\125\123\x45\x52\137\101\x47\x45\x4e\x54" => "\106\151\162\145\146\157\170")), $requestId, $config, $csrf); $uid = "\x75\163\x65\x72\x31\62\63"; $loginName = "\x55\163\x65\x72\61\62\63"; $password = "\x69\x61\x6d\x61\x74\157\153\x65\x6e"; $realPassword = "\x70\141\x73\x73\x6d\145"; $sessionId = "\141\x62\x63\x78\x79\172"; $manager->expects($this->once())->method("\x67\x65\164")->with($uid)->willReturn($user); $session->expects($this->once())->method("\147\145\x74\x49\144")->willReturn($sessionId); $this->tokenProvider->expects($this->once())->method("\x67\x65\164\x54\157\153\145\156")->with($password)->willReturn($token); $this->tokenProvider->expects($this->once())->method("\x67\x65\164\120\141\x73\x73\167\157\162\x64")->with($token, $password)->willReturn($realPassword); $this->tokenProvider->expects($this->once())->method("\x67\x65\x6e\x65\x72\141\x74\x65\x54\x6f\153\145\x6e")->with($sessionId, $uid, $loginName, $realPassword, "\x46\151\x72\145\x66\157\x78", IToken::TEMPORARY_TOKEN, IToken::DO_NOT_REMEMBER); $this->assertTrue($userSession->createSessionToken($request, $uid, $loginName, $password)); } public function testCreateSessionTokenWithNonExistentUser() { $manager = $this->getMockBuilder(Manager::class)->disableOriginalConstructor()->getMock(); $session = $this->createMock(ISession::class); $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); $request = $this->createMock(IRequest::class); $uid = "\165\163\145\162\61\x32\63"; $loginName = "\x55\x73\145\162\x31\x32\63"; $password = "\x70\x61\x73\x73\x6d\x65"; $manager->expects($this->once())->method("\147\x65\x74")->with($uid)->willReturn(null); $this->assertFalse($userSession->createSessionToken($request, $uid, $loginName, $password)); } public function testCreateRememberMeToken() { $user = $this->createMock(IUser::class); $user->expects($this->exactly(2))->method("\x67\x65\164\125\x49\104")->willReturn("\x55\x73\x65\x72\x55\x69\x64"); $this->random->expects($this->once())->method("\x67\145\156\145\162\x61\x74\145")->with(32)->willReturn("\114\157\x6e\147\x52\x61\156\x64\x6f\155\x54\157\153\145\x6e"); $this->config->expects($this->once())->method("\163\x65\164\x55\x73\x65\x72\x56\141\x6c\x75\145")->with("\x55\163\145\162\x55\x69\144", "\154\x6f\147\x69\x6e\137\x74\157\153\145\x6e", "\114\x6f\x6e\147\x52\141\x6e\x64\x6f\x6d\x54\157\x6b\145\x6e", 10000); $this->userSession->expects($this->once())->method("\x73\x65\x74\115\x61\x67\151\143\111\156\103\157\x6f\153\x69\145")->with("\x55\x73\x65\162\x55\151\144", "\x4c\157\x6e\x67\x52\x61\x6e\144\x6f\155\x54\157\x6b\145\x6e"); $this->userSession->createRememberMeToken($user); } public function testTryBasicAuthLoginValid() { $request = $this->createMock(Request::class); $request->method("\137\137\x67\145\164")->willReturn(array("\x50\110\120\137\101\x55\124\x48\x5f\125\123\x45\122" => "\x75\163\145\x72\156\x61\x6d\145", "\x50\x48\120\137\x41\x55\124\110\x5f\x50\127" => "\160\141\x73\163\x77\x6f\162\144")); $request->method("\x5f\137\x69\x73\x73\145\x74")->with("\163\x65\x72\166\x65\162")->willReturn(true); $davAuthenticatedSet = false; $lastPasswordConfirmSet = false; $this->session->method("\163\145\x74")->willReturnCallback(function ($k, $v) use(&$davAuthenticatedSet, &$lastPasswordConfirmSet) { switch ($k) { case Auth::DAV_AUTHENTICATED: $davAuthenticatedSet = $v; return; case "\154\x61\x73\164\x2d\160\x61\163\163\x77\x6f\162\x64\x2d\x63\157\156\x66\151\162\155": $lastPasswordConfirmSet = 1000; return; default: throw new \Exception(); } }); $userSession = $this->getMockBuilder(Session::class)->setConstructorArgs(array($this->manager, $this->session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->setMethods(array("\154\157\x67\x43\x6c\151\145\156\x74\111\156", "\x67\145\164\x55\163\x65\162"))->getMock(); $userSession->expects($this->once())->method("\x6c\157\x67\103\154\x69\x65\x6e\x74\x49\156")->with($this->equalTo("\x75\x73\145\162\x6e\141\155\x65"), $this->equalTo("\160\x61\163\x73\x77\x6f\x72\x64"), $this->equalTo($request), $this->equalTo($this->throttler))->willReturn(true); $user = $this->createMock(IUser::class); $user->method("\147\x65\x74\125\111\x44")->willReturn("\165\163\x65\162\156\141\155\145"); $userSession->expects($this->once())->method("\x67\x65\x74\x55\x73\x65\162")->willReturn($user); $this->assertTrue($userSession->tryBasicAuthLogin($request, $this->throttler)); $this->assertSame("\165\x73\145\x72\x6e\x61\155\x65", $davAuthenticatedSet); $this->assertSame(1000, $lastPasswordConfirmSet); } public function testTryBasicAuthLoginNoLogin() { $request = $this->createMock(Request::class); $request->method("\x5f\x5f\147\145\x74")->willReturn(array()); $request->method("\x5f\x5f\x69\x73\x73\x65\164")->with("\163\145\x72\166\145\x72")->willReturn(true); $this->session->expects($this->never())->method($this->anything()); $userSession = $this->getMockBuilder(Session::class)->setConstructorArgs(array($this->manager, $this->session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->setMethods(array("\154\157\147\103\x6c\x69\145\156\x74\x49\x6e"))->getMock(); $userSession->expects($this->never())->method("\x6c\x6f\147\103\154\151\x65\156\164\111\156"); $this->assertFalse($userSession->tryBasicAuthLogin($request, $this->throttler)); } public function testUpdateTokens() { $this->tokenProvider->expects($this->once())->method("\x75\x70\144\141\164\x65\x50\141\163\x73\167\x6f\162\144\x73")->with("\x75\x69\x64", "\x70\x61\163\x73"); $this->userSession->updateTokens("\x75\151\144", "\x70\141\x73\x73"); } public function testLogClientInThrottlerUsername() { $manager = $this->createMock(Manager::class); $session = $this->createMock(ISession::class); $request = $this->createMock(IRequest::class); $userSession = $this->getMockBuilder(Session::class)->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->setMethods(array("\x69\163\x54\x6f\153\x65\x6e\120\141\163\x73\x77\x6f\x72\x64", "\x6c\157\x67\x69\x6e", "\x73\x75\x70\x70\157\162\x74\163\103\157\x6f\153\x69\x65\x73", "\x63\x72\145\x61\x74\x65\123\x65\x73\163\151\x6f\156\124\157\153\x65\x6e", "\x67\x65\x74\x55\x73\x65\162"))->getMock(); $userSession->expects($this->once())->method("\151\x73\124\x6f\153\x65\156\x50\x61\163\x73\x77\157\x72\144")->willReturn(true); $userSession->expects($this->once())->method("\x6c\157\x67\151\x6e")->with("\152\157\150\156", "\111\x2d\101\115\55\x41\116\x2d\x50\101\123\x53\x57\117\x52\104")->willReturn(false); $session->expects($this->never())->method("\163\145\164"); $request->method("\x67\145\164\x52\x65\155\157\164\x65\x41\x64\144\x72\145\163\163")->willReturn("\61\x39\62\56\x31\66\x38\x2e\60\x2e\x31"); $this->throttler->expects($this->exactly(2))->method("\x73\154\145\x65\160\x44\145\x6c\x61\x79\x4f\162\124\x68\162\x6f\x77\x4f\156\x4d\141\170")->with("\61\x39\62\56\x31\66\70\56\60\56\61"); $this->throttler->expects($this->any())->method("\x67\145\164\104\x65\x6c\141\171")->with("\x31\x39\62\56\x31\x36\x38\56\60\56\x31")->willReturn(0); $this->throttler->expects($this->once())->method("\162\145\147\151\x73\164\145\x72\101\x74\x74\145\x6d\x70\164")->with("\x6c\157\x67\151\x6e", "\61\71\x32\56\x31\66\x38\x2e\60\56\61", array("\x75\163\145\162" => "\152\157\x68\x6e")); $this->dispatcher->expects($this->once())->method("\144\x69\x73\160\141\x74\143\x68\x54\x79\160\145\144")->with(new LoginFailed("\152\x6f\x68\156", "\x49\55\101\x4d\55\101\116\x2d\x50\101\123\123\x57\117\122\x44")); $this->assertFalse($userSession->logClientIn("\x6a\157\x68\156", "\x49\x2d\x41\115\55\x41\116\55\x50\101\x53\x53\x57\117\122\x44", $request, $this->throttler)); } public function testLogClientInThrottlerEmail() { $manager = $this->createMock(Manager::class); $session = $this->createMock(ISession::class); $request = $this->createMock(IRequest::class); $userSession = $this->getMockBuilder(Session::class)->setConstructorArgs(array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher))->setMethods(array("\x69\x73\124\157\153\145\156\x50\x61\163\163\x77\157\162\144", "\154\x6f\147\151\156", "\x73\165\x70\x70\x6f\x72\x74\163\103\x6f\157\153\x69\145\163", "\x63\x72\145\141\164\x65\x53\145\x73\x73\151\x6f\x6e\124\157\153\145\156", "\147\145\x74\125\163\x65\x72"))->getMock(); $userSession->expects($this->once())->method("\151\x73\x54\x6f\153\x65\156\x50\141\163\x73\x77\157\162\144")->willReturn(false); $userSession->expects($this->once())->method("\154\157\147\151\156")->with("\x6a\x6f\x68\x6e\100\146\157\157\56\x62\x61\162", "\x49\x2d\101\115\x2d\101\116\55\x50\101\x53\x53\x57\117\122\x44")->willReturn(false); $manager->method("\x67\x65\164\x42\171\105\155\141\x69\x6c")->with("\x6a\157\x68\156\100\146\x6f\157\56\x62\141\x72")->willReturn(array()); $session->expects($this->never())->method("\163\145\164"); $request->method("\x67\145\164\x52\145\155\157\164\x65\x41\144\x64\x72\145\x73\163")->willReturn("\x31\71\x32\56\61\x36\x38\x2e\x30\x2e\x31"); $this->throttler->expects($this->exactly(2))->method("\x73\x6c\x65\x65\x70\x44\x65\154\141\171\x4f\162\x54\150\162\x6f\167\x4f\x6e\x4d\141\x78")->with("\61\71\62\56\x31\66\70\56\x30\56\61"); $this->throttler->expects($this->any())->method("\147\x65\x74\x44\145\x6c\x61\171")->with("\x31\71\62\x2e\61\x36\70\x2e\x30\x2e\61")->willReturn(0); $this->throttler->expects($this->once())->method("\x72\x65\147\x69\x73\x74\145\162\x41\x74\x74\145\155\x70\x74")->with("\x6c\x6f\147\151\x6e", "\61\71\x32\x2e\61\x36\x38\56\x30\x2e\61", array("\165\x73\x65\x72" => "\152\x6f\150\x6e\x40\x66\x6f\x6f\56\142\x61\162")); $this->dispatcher->expects($this->once())->method("\144\151\163\x70\141\164\143\x68\124\171\x70\145\x64")->with(new LoginFailed("\x6a\x6f\150\156\x40\146\x6f\157\x2e\x62\141\x72", "\x49\55\x41\115\x2d\x41\x4e\x2d\120\101\x53\123\x57\117\122\x44")); $this->assertFalse($userSession->logClientIn("\x6a\x6f\150\x6e\100\146\157\x6f\56\142\141\x72", "\111\55\x41\x4d\55\x41\116\x2d\120\x41\123\x53\127\x4f\122\104", $request, $this->throttler)); } }

Function Calls

None

Variables

None

Stats

MD5 94e687f1d32e1aa31c44deef7e2c02ca
Eval Count 0
Decode Time 136 ms