2017-04-24 22:11:48 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Core\Controller;
|
|
|
|
|
|
|
|
use OC\Authentication\Exceptions\InvalidTokenException;
|
|
|
|
use OC\Authentication\Exceptions\PasswordlessTokenException;
|
|
|
|
use OC\Authentication\Token\IProvider;
|
|
|
|
use OC\Authentication\Token\IToken;
|
|
|
|
use OC\Core\Controller\ClientFlowLoginController;
|
2017-05-18 17:31:14 +03:00
|
|
|
use OCA\OAuth2\Db\AccessTokenMapper;
|
|
|
|
use OCA\OAuth2\Db\Client;
|
|
|
|
use OCA\OAuth2\Db\ClientMapper;
|
2017-04-24 22:11:48 +03:00
|
|
|
use OCP\AppFramework\Http;
|
2019-02-04 10:54:56 +03:00
|
|
|
use OCP\AppFramework\Http\StandaloneTemplateResponse;
|
2017-04-24 22:11:48 +03:00
|
|
|
use OCP\Defaults;
|
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\ISession;
|
|
|
|
use OCP\IURLGenerator;
|
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserSession;
|
2017-05-18 17:31:14 +03:00
|
|
|
use OCP\Security\ICrypto;
|
2017-04-24 22:11:48 +03:00
|
|
|
use OCP\Security\ISecureRandom;
|
|
|
|
use OCP\Session\Exceptions\SessionNotAvailableException;
|
2019-02-18 19:38:38 +03:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2017-04-24 22:11:48 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class ClientFlowLoginControllerTest extends TestCase {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
|
2017-04-24 22:11:48 +03:00
|
|
|
private $request;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
|
2017-04-24 22:11:48 +03:00
|
|
|
private $userSession;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
|
2017-04-24 22:11:48 +03:00
|
|
|
private $l10n;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var Defaults|\PHPUnit\Framework\MockObject\MockObject */
|
2017-04-24 22:11:48 +03:00
|
|
|
private $defaults;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ISession|\PHPUnit\Framework\MockObject\MockObject */
|
2017-04-24 22:11:48 +03:00
|
|
|
private $session;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IProvider|\PHPUnit\Framework\MockObject\MockObject */
|
2017-04-24 22:11:48 +03:00
|
|
|
private $tokenProvider;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ISecureRandom|\PHPUnit\Framework\MockObject\MockObject */
|
2017-04-24 22:11:48 +03:00
|
|
|
private $random;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
|
2017-04-24 22:11:48 +03:00
|
|
|
private $urlGenerator;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ClientMapper|\PHPUnit\Framework\MockObject\MockObject */
|
2017-05-18 17:31:14 +03:00
|
|
|
private $clientMapper;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var AccessTokenMapper|\PHPUnit\Framework\MockObject\MockObject */
|
2017-05-18 17:31:14 +03:00
|
|
|
private $accessTokenMapper;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ICrypto|\PHPUnit\Framework\MockObject\MockObject */
|
2017-05-18 17:31:14 +03:00
|
|
|
private $crypto;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var EventDispatcherInterface|\PHPUnit\Framework\MockObject\MockObject */
|
2019-02-18 19:38:38 +03:00
|
|
|
private $eventDispatcher;
|
|
|
|
|
2017-05-18 17:31:14 +03:00
|
|
|
|
2017-04-24 22:11:48 +03:00
|
|
|
/** @var ClientFlowLoginController */
|
|
|
|
private $clientFlowLoginController;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2017-04-24 22:11:48 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->request = $this->createMock(IRequest::class);
|
|
|
|
$this->userSession = $this->createMock(IUserSession::class);
|
|
|
|
$this->l10n = $this->createMock(IL10N::class);
|
|
|
|
$this->l10n
|
|
|
|
->expects($this->any())
|
|
|
|
->method('t')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($text, $parameters = []) {
|
2017-04-24 22:11:48 +03:00
|
|
|
return vsprintf($text, $parameters);
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2017-04-24 22:11:48 +03:00
|
|
|
$this->defaults = $this->createMock(Defaults::class);
|
|
|
|
$this->session = $this->createMock(ISession::class);
|
|
|
|
$this->tokenProvider = $this->createMock(IProvider::class);
|
|
|
|
$this->random = $this->createMock(ISecureRandom::class);
|
|
|
|
$this->urlGenerator = $this->createMock(IURLGenerator::class);
|
2017-05-18 17:31:14 +03:00
|
|
|
$this->clientMapper = $this->createMock(ClientMapper::class);
|
|
|
|
$this->accessTokenMapper = $this->createMock(AccessTokenMapper::class);
|
|
|
|
$this->crypto = $this->createMock(ICrypto::class);
|
2019-02-18 19:38:38 +03:00
|
|
|
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
|
2017-04-24 22:11:48 +03:00
|
|
|
|
|
|
|
$this->clientFlowLoginController = new ClientFlowLoginController(
|
|
|
|
'core',
|
|
|
|
$this->request,
|
|
|
|
$this->userSession,
|
|
|
|
$this->l10n,
|
|
|
|
$this->defaults,
|
|
|
|
$this->session,
|
|
|
|
$this->tokenProvider,
|
|
|
|
$this->random,
|
2017-05-18 17:31:14 +03:00
|
|
|
$this->urlGenerator,
|
|
|
|
$this->clientMapper,
|
|
|
|
$this->accessTokenMapper,
|
2019-02-18 01:49:54 +03:00
|
|
|
$this->crypto,
|
2019-02-18 19:38:38 +03:00
|
|
|
$this->eventDispatcher
|
2017-04-24 22:11:48 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-05-18 17:31:14 +03:00
|
|
|
public function testShowAuthPickerPageNoClientOrOauthRequest() {
|
2019-02-04 10:54:56 +03:00
|
|
|
$expected = new StandaloneTemplateResponse(
|
2017-04-24 22:11:48 +03:00
|
|
|
'core',
|
2017-05-18 17:31:14 +03:00
|
|
|
'error',
|
2017-04-24 22:11:48 +03:00
|
|
|
[
|
2017-05-18 17:31:14 +03:00
|
|
|
'errors' =>
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'error' => 'Access Forbidden',
|
|
|
|
'hint' => 'Invalid request',
|
|
|
|
],
|
|
|
|
],
|
2017-04-24 22:11:48 +03:00
|
|
|
],
|
|
|
|
'guest'
|
|
|
|
);
|
2017-05-18 17:31:14 +03:00
|
|
|
|
2017-04-24 22:11:48 +03:00
|
|
|
$this->assertEquals($expected, $this->clientFlowLoginController->showAuthPickerPage());
|
|
|
|
}
|
|
|
|
|
2017-05-18 17:31:14 +03:00
|
|
|
public function testShowAuthPickerPageWithOcsHeader() {
|
|
|
|
$this->request
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getHeader')
|
|
|
|
->with('USER_AGENT')
|
|
|
|
->willReturn('Mac OS X Sync Client');
|
|
|
|
$this->request
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('getHeader')
|
|
|
|
->with('OCS-APIREQUEST')
|
|
|
|
->willReturn('true');
|
2017-04-24 22:11:48 +03:00
|
|
|
$this->random
|
|
|
|
->expects($this->once())
|
|
|
|
->method('generate')
|
|
|
|
->with(
|
|
|
|
64,
|
|
|
|
ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_DIGITS
|
|
|
|
)
|
|
|
|
->willReturn('StateToken');
|
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('set')
|
|
|
|
->with('client.flow.state.token', 'StateToken');
|
2017-09-06 12:25:19 +03:00
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('oauth.state')
|
|
|
|
->willReturn('OauthStateToken');
|
2017-05-18 17:31:14 +03:00
|
|
|
$this->defaults
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getName')
|
|
|
|
->willReturn('ExampleCloud');
|
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getServerHost')
|
|
|
|
->willReturn('example.com');
|
2018-11-01 01:06:08 +03:00
|
|
|
$this->request
|
|
|
|
->method('getServerProtocol')
|
|
|
|
->willReturn('https');
|
2017-05-18 17:31:14 +03:00
|
|
|
|
2019-02-04 10:54:56 +03:00
|
|
|
$expected = new StandaloneTemplateResponse(
|
2017-05-18 17:31:14 +03:00
|
|
|
'core',
|
|
|
|
'loginflow/authpicker',
|
|
|
|
[
|
|
|
|
'client' => 'Mac OS X Sync Client',
|
|
|
|
'clientIdentifier' => '',
|
|
|
|
'instanceName' => 'ExampleCloud',
|
|
|
|
'urlGenerator' => $this->urlGenerator,
|
|
|
|
'stateToken' => 'StateToken',
|
2018-11-01 01:06:08 +03:00
|
|
|
'serverHost' => 'https://example.com',
|
2017-09-06 12:25:19 +03:00
|
|
|
'oauthState' => 'OauthStateToken',
|
2017-05-18 17:31:14 +03:00
|
|
|
],
|
|
|
|
'guest'
|
|
|
|
);
|
2019-08-10 20:27:01 +03:00
|
|
|
$csp = new Http\ContentSecurityPolicy();
|
|
|
|
$csp->addAllowedFormActionDomain('nc://*');
|
|
|
|
$expected->setContentSecurityPolicy($csp);
|
2017-05-18 17:31:14 +03:00
|
|
|
$this->assertEquals($expected, $this->clientFlowLoginController->showAuthPickerPage());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testShowAuthPickerPageWithOauth() {
|
2017-04-24 22:11:48 +03:00
|
|
|
$this->request
|
2017-05-18 17:31:14 +03:00
|
|
|
->expects($this->at(0))
|
2017-04-24 22:11:48 +03:00
|
|
|
->method('getHeader')
|
|
|
|
->with('USER_AGENT')
|
|
|
|
->willReturn('Mac OS X Sync Client');
|
2017-05-18 17:31:14 +03:00
|
|
|
$client = new Client();
|
|
|
|
$client->setName('My external service');
|
2019-10-04 19:56:30 +03:00
|
|
|
$client->setRedirectUri('https://example.com/redirect.php');
|
2017-05-18 17:31:14 +03:00
|
|
|
$this->clientMapper
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getByIdentifier')
|
|
|
|
->with('MyClientIdentifier')
|
|
|
|
->willReturn($client);
|
|
|
|
$this->random
|
|
|
|
->expects($this->once())
|
|
|
|
->method('generate')
|
|
|
|
->with(
|
|
|
|
64,
|
|
|
|
ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_DIGITS
|
|
|
|
)
|
|
|
|
->willReturn('StateToken');
|
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('set')
|
|
|
|
->with('client.flow.state.token', 'StateToken');
|
2017-09-06 12:25:19 +03:00
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('oauth.state')
|
|
|
|
->willReturn('OauthStateToken');
|
2017-04-24 22:11:48 +03:00
|
|
|
$this->defaults
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getName')
|
|
|
|
->willReturn('ExampleCloud');
|
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getServerHost')
|
|
|
|
->willReturn('example.com');
|
2018-11-01 01:06:08 +03:00
|
|
|
$this->request
|
|
|
|
->method('getServerProtocol')
|
|
|
|
->willReturn('https');
|
2017-04-24 22:11:48 +03:00
|
|
|
|
2019-02-04 10:54:56 +03:00
|
|
|
$expected = new StandaloneTemplateResponse(
|
2017-04-24 22:11:48 +03:00
|
|
|
'core',
|
|
|
|
'loginflow/authpicker',
|
|
|
|
[
|
2017-05-18 17:31:14 +03:00
|
|
|
'client' => 'My external service',
|
|
|
|
'clientIdentifier' => 'MyClientIdentifier',
|
2017-04-24 22:11:48 +03:00
|
|
|
'instanceName' => 'ExampleCloud',
|
|
|
|
'urlGenerator' => $this->urlGenerator,
|
|
|
|
'stateToken' => 'StateToken',
|
2018-11-01 01:06:08 +03:00
|
|
|
'serverHost' => 'https://example.com',
|
2017-09-06 12:25:19 +03:00
|
|
|
'oauthState' => 'OauthStateToken',
|
2017-04-24 22:11:48 +03:00
|
|
|
],
|
|
|
|
'guest'
|
|
|
|
);
|
2019-08-10 20:27:01 +03:00
|
|
|
$csp = new Http\ContentSecurityPolicy();
|
2019-10-04 19:56:30 +03:00
|
|
|
$csp->addAllowedFormActionDomain('https://example.com/redirect.php');
|
2019-08-10 20:27:01 +03:00
|
|
|
$expected->setContentSecurityPolicy($csp);
|
2017-05-18 17:31:14 +03:00
|
|
|
$this->assertEquals($expected, $this->clientFlowLoginController->showAuthPickerPage('MyClientIdentifier'));
|
2017-04-24 22:11:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGenerateAppPasswordWithInvalidToken() {
|
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('client.flow.state.token')
|
|
|
|
->willReturn('OtherToken');
|
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('remove')
|
|
|
|
->with('client.flow.state.token');
|
|
|
|
|
2019-02-04 10:54:56 +03:00
|
|
|
$expected = new StandaloneTemplateResponse(
|
2017-04-24 22:11:48 +03:00
|
|
|
'core',
|
|
|
|
'403',
|
|
|
|
[
|
2019-01-18 11:17:26 +03:00
|
|
|
'message' => 'State token does not match',
|
2017-04-24 22:11:48 +03:00
|
|
|
],
|
|
|
|
'guest'
|
|
|
|
);
|
|
|
|
$expected->setStatus(Http::STATUS_FORBIDDEN);
|
|
|
|
$this->assertEquals($expected, $this->clientFlowLoginController->generateAppPassword('MyStateToken'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGenerateAppPasswordWithSessionNotAvailableException() {
|
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('client.flow.state.token')
|
|
|
|
->willReturn('MyStateToken');
|
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('remove')
|
|
|
|
->with('client.flow.state.token');
|
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getId')
|
|
|
|
->willThrowException(new SessionNotAvailableException());
|
|
|
|
|
|
|
|
$expected = new Http\Response();
|
|
|
|
$expected->setStatus(Http::STATUS_FORBIDDEN);
|
|
|
|
$this->assertEquals($expected, $this->clientFlowLoginController->generateAppPassword('MyStateToken'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGenerateAppPasswordWithInvalidTokenException() {
|
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('client.flow.state.token')
|
|
|
|
->willReturn('MyStateToken');
|
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('remove')
|
|
|
|
->with('client.flow.state.token');
|
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getId')
|
|
|
|
->willReturn('SessionId');
|
|
|
|
$this->tokenProvider
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getToken')
|
|
|
|
->with('SessionId')
|
|
|
|
->willThrowException(new InvalidTokenException());
|
|
|
|
|
|
|
|
$expected = new Http\Response();
|
|
|
|
$expected->setStatus(Http::STATUS_FORBIDDEN);
|
|
|
|
$this->assertEquals($expected, $this->clientFlowLoginController->generateAppPassword('MyStateToken'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGeneratePasswordWithPassword() {
|
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('client.flow.state.token')
|
|
|
|
->willReturn('MyStateToken');
|
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('remove')
|
|
|
|
->with('client.flow.state.token');
|
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getId')
|
|
|
|
->willReturn('SessionId');
|
|
|
|
$myToken = $this->createMock(IToken::class);
|
|
|
|
$myToken
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getLoginName')
|
|
|
|
->willReturn('MyLoginName');
|
|
|
|
$this->tokenProvider
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getToken')
|
|
|
|
->with('SessionId')
|
|
|
|
->willReturn($myToken);
|
|
|
|
$this->tokenProvider
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getPassword')
|
|
|
|
->with($myToken, 'SessionId')
|
|
|
|
->willReturn('MyPassword');
|
|
|
|
$this->random
|
|
|
|
->expects($this->once())
|
|
|
|
->method('generate')
|
2017-04-25 14:12:24 +03:00
|
|
|
->with(72)
|
2017-04-24 22:11:48 +03:00
|
|
|
->willReturn('MyGeneratedToken');
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$user
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('MyUid');
|
|
|
|
$this->userSession
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->willReturn($user);
|
|
|
|
$this->tokenProvider
|
|
|
|
->expects($this->once())
|
|
|
|
->method('generateToken')
|
|
|
|
->with(
|
|
|
|
'MyGeneratedToken',
|
|
|
|
'MyUid',
|
|
|
|
'MyLoginName',
|
|
|
|
'MyPassword',
|
|
|
|
'unknown',
|
|
|
|
IToken::PERMANENT_TOKEN,
|
|
|
|
IToken::DO_NOT_REMEMBER
|
|
|
|
);
|
2017-11-09 02:29:34 +03:00
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getServerProtocol')
|
|
|
|
->willReturn('http');
|
2017-04-24 22:11:48 +03:00
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getServerHost')
|
|
|
|
->willReturn('example.com');
|
2018-01-12 16:15:12 +03:00
|
|
|
$this->request
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getHeader')
|
|
|
|
->willReturn('');
|
2017-04-24 22:11:48 +03:00
|
|
|
|
2019-02-18 19:38:38 +03:00
|
|
|
$this->eventDispatcher->expects($this->once())
|
|
|
|
->method('dispatch');
|
2019-02-18 01:49:54 +03:00
|
|
|
|
2017-11-09 02:29:34 +03:00
|
|
|
$expected = new Http\RedirectResponse('nc://login/server:http://example.com&user:MyLoginName&password:MyGeneratedToken');
|
2017-04-24 22:11:48 +03:00
|
|
|
$this->assertEquals($expected, $this->clientFlowLoginController->generateAppPassword('MyStateToken'));
|
|
|
|
}
|
|
|
|
|
2019-11-11 01:49:57 +03:00
|
|
|
/**
|
|
|
|
* @param string $redirectUri
|
|
|
|
* @param string $redirectUrl
|
|
|
|
*
|
|
|
|
* @testWith
|
|
|
|
* ["https://example.com/redirect.php", "https://example.com/redirect.php?state=MyOauthState&code=MyAccessCode"]
|
|
|
|
* ["https://example.com/redirect.php?hello=world", "https://example.com/redirect.php?hello=world&state=MyOauthState&code=MyAccessCode"]
|
|
|
|
*
|
|
|
|
*/
|
2020-04-09 17:07:47 +03:00
|
|
|
public function testGeneratePasswordWithPasswordForOauthClient($redirectUri, $redirectUrl) {
|
2017-05-18 17:31:14 +03:00
|
|
|
$this->session
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('get')
|
|
|
|
->with('client.flow.state.token')
|
|
|
|
->willReturn('MyStateToken');
|
|
|
|
$this->session
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('remove')
|
|
|
|
->with('client.flow.state.token');
|
|
|
|
$this->session
|
|
|
|
->expects($this->at(3))
|
|
|
|
->method('get')
|
|
|
|
->with('oauth.state')
|
|
|
|
->willReturn('MyOauthState');
|
|
|
|
$this->session
|
|
|
|
->expects($this->at(4))
|
|
|
|
->method('remove')
|
|
|
|
->with('oauth.state');
|
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getId')
|
|
|
|
->willReturn('SessionId');
|
|
|
|
$myToken = $this->createMock(IToken::class);
|
|
|
|
$myToken
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getLoginName')
|
|
|
|
->willReturn('MyLoginName');
|
|
|
|
$this->tokenProvider
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getToken')
|
|
|
|
->with('SessionId')
|
|
|
|
->willReturn($myToken);
|
|
|
|
$this->tokenProvider
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getPassword')
|
|
|
|
->with($myToken, 'SessionId')
|
|
|
|
->willReturn('MyPassword');
|
|
|
|
$this->random
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('generate')
|
|
|
|
->with(72)
|
|
|
|
->willReturn('MyGeneratedToken');
|
|
|
|
$this->random
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('generate')
|
|
|
|
->with(128)
|
|
|
|
->willReturn('MyAccessCode');
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$user
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('MyUid');
|
|
|
|
$this->userSession
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->willReturn($user);
|
|
|
|
$token = $this->createMock(IToken::class);
|
|
|
|
$this->tokenProvider
|
|
|
|
->expects($this->once())
|
|
|
|
->method('generateToken')
|
|
|
|
->with(
|
|
|
|
'MyGeneratedToken',
|
|
|
|
'MyUid',
|
|
|
|
'MyLoginName',
|
|
|
|
'MyPassword',
|
|
|
|
'My OAuth client',
|
|
|
|
IToken::PERMANENT_TOKEN,
|
|
|
|
IToken::DO_NOT_REMEMBER
|
|
|
|
)
|
|
|
|
->willReturn($token);
|
|
|
|
$client = new Client();
|
|
|
|
$client->setName('My OAuth client');
|
2019-11-11 01:49:57 +03:00
|
|
|
$client->setRedirectUri($redirectUri);
|
2017-05-18 17:31:14 +03:00
|
|
|
$this->clientMapper
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getByIdentifier')
|
|
|
|
->with('MyClientIdentifier')
|
|
|
|
->willReturn($client);
|
|
|
|
|
2019-02-18 19:38:38 +03:00
|
|
|
$this->eventDispatcher->expects($this->once())
|
|
|
|
->method('dispatch');
|
2019-02-18 01:49:54 +03:00
|
|
|
|
2019-11-11 01:49:57 +03:00
|
|
|
$expected = new Http\RedirectResponse($redirectUrl);
|
2017-05-18 17:31:14 +03:00
|
|
|
$this->assertEquals($expected, $this->clientFlowLoginController->generateAppPassword('MyStateToken', 'MyClientIdentifier'));
|
|
|
|
}
|
|
|
|
|
2017-04-24 22:11:48 +03:00
|
|
|
public function testGeneratePasswordWithoutPassword() {
|
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('client.flow.state.token')
|
|
|
|
->willReturn('MyStateToken');
|
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('remove')
|
|
|
|
->with('client.flow.state.token');
|
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getId')
|
|
|
|
->willReturn('SessionId');
|
|
|
|
$myToken = $this->createMock(IToken::class);
|
|
|
|
$myToken
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getLoginName')
|
|
|
|
->willReturn('MyLoginName');
|
|
|
|
$this->tokenProvider
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getToken')
|
|
|
|
->with('SessionId')
|
|
|
|
->willReturn($myToken);
|
|
|
|
$this->tokenProvider
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getPassword')
|
|
|
|
->with($myToken, 'SessionId')
|
|
|
|
->willThrowException(new PasswordlessTokenException());
|
|
|
|
$this->random
|
|
|
|
->expects($this->once())
|
|
|
|
->method('generate')
|
2017-04-25 14:12:24 +03:00
|
|
|
->with(72)
|
2017-04-24 22:11:48 +03:00
|
|
|
->willReturn('MyGeneratedToken');
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$user
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('MyUid');
|
|
|
|
$this->userSession
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->willReturn($user);
|
|
|
|
$this->tokenProvider
|
|
|
|
->expects($this->once())
|
|
|
|
->method('generateToken')
|
|
|
|
->with(
|
|
|
|
'MyGeneratedToken',
|
|
|
|
'MyUid',
|
|
|
|
'MyLoginName',
|
|
|
|
null,
|
|
|
|
'unknown',
|
|
|
|
IToken::PERMANENT_TOKEN,
|
|
|
|
IToken::DO_NOT_REMEMBER
|
|
|
|
);
|
2017-11-09 02:29:34 +03:00
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getServerProtocol')
|
|
|
|
->willReturn('http');
|
2017-04-24 22:11:48 +03:00
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getServerHost')
|
|
|
|
->willReturn('example.com');
|
2018-01-12 16:15:12 +03:00
|
|
|
$this->request
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getHeader')
|
|
|
|
->willReturn('');
|
2017-04-24 22:11:48 +03:00
|
|
|
|
2019-02-18 19:38:38 +03:00
|
|
|
$this->eventDispatcher->expects($this->once())
|
|
|
|
->method('dispatch');
|
2019-02-18 01:49:54 +03:00
|
|
|
|
2017-11-09 02:29:34 +03:00
|
|
|
$expected = new Http\RedirectResponse('nc://login/server:http://example.com&user:MyLoginName&password:MyGeneratedToken');
|
2017-04-24 22:11:48 +03:00
|
|
|
$this->assertEquals($expected, $this->clientFlowLoginController->generateAppPassword('MyStateToken'));
|
|
|
|
}
|
2018-01-12 11:39:21 +03:00
|
|
|
|
|
|
|
public function dataGeneratePasswordWithHttpsProxy() {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
[
|
|
|
|
['X-Forwarded-Proto', 'http'],
|
|
|
|
['X-Forwarded-Ssl', 'off'],
|
2018-01-12 16:15:12 +03:00
|
|
|
['USER_AGENT', ''],
|
2018-01-12 11:39:21 +03:00
|
|
|
],
|
|
|
|
'http',
|
|
|
|
'http',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
['X-Forwarded-Proto', 'http'],
|
|
|
|
['X-Forwarded-Ssl', 'off'],
|
2018-01-12 16:15:12 +03:00
|
|
|
['USER_AGENT', ''],
|
2018-01-12 11:39:21 +03:00
|
|
|
],
|
|
|
|
'https',
|
|
|
|
'https',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
['X-Forwarded-Proto', 'https'],
|
|
|
|
['X-Forwarded-Ssl', 'off'],
|
2018-01-12 16:15:12 +03:00
|
|
|
['USER_AGENT', ''],
|
2018-01-12 11:39:21 +03:00
|
|
|
],
|
|
|
|
'http',
|
|
|
|
'https',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
['X-Forwarded-Proto', 'https'],
|
|
|
|
['X-Forwarded-Ssl', 'on'],
|
2018-01-12 16:15:12 +03:00
|
|
|
['USER_AGENT', ''],
|
2018-01-12 11:39:21 +03:00
|
|
|
],
|
|
|
|
'http',
|
|
|
|
'https',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
['X-Forwarded-Proto', 'http'],
|
|
|
|
['X-Forwarded-Ssl', 'on'],
|
2018-01-12 16:15:12 +03:00
|
|
|
['USER_AGENT', ''],
|
2018-01-12 11:39:21 +03:00
|
|
|
],
|
|
|
|
'http',
|
|
|
|
'https',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataGeneratePasswordWithHttpsProxy
|
|
|
|
* @param array $headers
|
|
|
|
* @param string $protocol
|
|
|
|
* @param string $expected
|
|
|
|
*/
|
|
|
|
public function testGeneratePasswordWithHttpsProxy(array $headers, $protocol, $expected) {
|
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('client.flow.state.token')
|
|
|
|
->willReturn('MyStateToken');
|
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('remove')
|
|
|
|
->with('client.flow.state.token');
|
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getId')
|
|
|
|
->willReturn('SessionId');
|
|
|
|
$myToken = $this->createMock(IToken::class);
|
|
|
|
$myToken
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getLoginName')
|
|
|
|
->willReturn('MyLoginName');
|
|
|
|
$this->tokenProvider
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getToken')
|
|
|
|
->with('SessionId')
|
|
|
|
->willReturn($myToken);
|
|
|
|
$this->tokenProvider
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getPassword')
|
|
|
|
->with($myToken, 'SessionId')
|
|
|
|
->willReturn('MyPassword');
|
|
|
|
$this->random
|
|
|
|
->expects($this->once())
|
|
|
|
->method('generate')
|
|
|
|
->with(72)
|
|
|
|
->willReturn('MyGeneratedToken');
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$user
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('MyUid');
|
|
|
|
$this->userSession
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->willReturn($user);
|
|
|
|
$this->tokenProvider
|
|
|
|
->expects($this->once())
|
|
|
|
->method('generateToken')
|
|
|
|
->with(
|
|
|
|
'MyGeneratedToken',
|
|
|
|
'MyUid',
|
|
|
|
'MyLoginName',
|
|
|
|
'MyPassword',
|
|
|
|
'unknown',
|
|
|
|
IToken::PERMANENT_TOKEN,
|
|
|
|
IToken::DO_NOT_REMEMBER
|
|
|
|
);
|
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getServerProtocol')
|
|
|
|
->willReturn($protocol);
|
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getServerHost')
|
|
|
|
->willReturn('example.com');
|
|
|
|
$this->request
|
|
|
|
->expects($this->atLeastOnce())
|
|
|
|
->method('getHeader')
|
|
|
|
->willReturnMap($headers);
|
|
|
|
|
2019-02-18 19:38:38 +03:00
|
|
|
$this->eventDispatcher->expects($this->once())
|
|
|
|
->method('dispatch');
|
2019-02-18 01:49:54 +03:00
|
|
|
|
2018-01-12 11:39:21 +03:00
|
|
|
$expected = new Http\RedirectResponse('nc://login/server:' . $expected . '://example.com&user:MyLoginName&password:MyGeneratedToken');
|
|
|
|
$this->assertEquals($expected, $this->clientFlowLoginController->generateAppPassword('MyStateToken'));
|
|
|
|
}
|
2017-04-24 22:11:48 +03:00
|
|
|
}
|