Skip to content

Commit

Permalink
Merge pull request #32422 from nextcloud/backport/32375/stable23
Browse files Browse the repository at this point in the history
[stable23] Show user account on grant loginflow step
  • Loading branch information
nickvergessen authored May 16, 2022
2 parents 596f8fd + e432abd commit 33c21e1
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 36 deletions.
6 changes: 6 additions & 0 deletions core/Controller/ClientFlowLoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
use OCP\IRequest;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Security\ICrypto;
use OCP\Security\ISecureRandom;
Expand Down Expand Up @@ -250,10 +251,15 @@ public function grantPage(string $stateToken = '',
$csp->addAllowedFormActionDomain('nc://*');
}

/** @var IUser $user */
$user = $this->userSession->getUser();

$response = new StandaloneTemplateResponse(
$this->appName,
'loginflow/grant',
[
'userId' => $user->getUID(),
'userDisplayName' => $user->getDisplayName(),
'client' => $clientName,
'clientIdentifier' => $clientIdentifier,
'instanceName' => $this->defaults->getName(),
Expand Down
11 changes: 11 additions & 0 deletions core/Controller/ClientFlowLoginV2Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
use OCP\IRequest;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Security\ISecureRandom;

class ClientFlowLoginV2Controller extends Controller {
Expand All @@ -53,6 +55,8 @@ class ClientFlowLoginV2Controller extends Controller {
private $urlGenerator;
/** @var ISession */
private $session;
/** @var IUserSession */
private $userSession;
/** @var ISecureRandom */
private $random;
/** @var Defaults */
Expand All @@ -67,6 +71,7 @@ public function __construct(string $appName,
LoginFlowV2Service $loginFlowV2Service,
IURLGenerator $urlGenerator,
ISession $session,
IUserSession $userSession,
ISecureRandom $random,
Defaults $defaults,
?string $userId,
Expand All @@ -75,6 +80,7 @@ public function __construct(string $appName,
$this->loginFlowV2Service = $loginFlowV2Service;
$this->urlGenerator = $urlGenerator;
$this->session = $session;
$this->userSession = $userSession;
$this->random = $random;
$this->defaults = $defaults;
$this->userId = $userId;
Expand Down Expand Up @@ -160,10 +166,15 @@ public function grantPage(string $stateToken): StandaloneTemplateResponse {
return $this->loginTokenForbiddenResponse();
}

/** @var IUser $user */
$user = $this->userSession->getUser();

return new StandaloneTemplateResponse(
$this->appName,
'loginflowv2/grant',
[
'userId' => $user->getUID(),
'userDisplayName' => $user->getDisplayName(),
'client' => $flow->getClientName(),
'instanceName' => $this->defaults->getName(),
'urlGenerator' => $this->urlGenerator,
Expand Down
8 changes: 7 additions & 1 deletion core/templates/loginflow/grant.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@

<div class="picker-window">
<h2><?php p($l->t('Account access')) ?></h2>
<p class="info">
<?php p($l->t('Currently logged in as %1$s (%2$s).', [
$_['userDisplayName'],
$_['userId'],
])) ?>
</p>
<p class="info">
<?php print_unescaped($l->t('You are about to grant %1$s access to your %2$s account.', [
'<strong>' . \OCP\Util::sanitizeHTML($_['client']) . '</strong>',
Expand All @@ -44,7 +50,7 @@
<input type="hidden" name="requesttoken" value="<?php p($_['requesttoken']) ?>" />
<input type="hidden" name="stateToken" value="<?php p($_['stateToken']) ?>" />
<input type="hidden" name="oauthState" value="<?php p($_['oauthState']) ?>" />
<?php if (p($_['direct'])) { ?>
<?php if ($_['direct']) { ?>
<input type="hidden" name="direct" value="1" />
<?php } ?>
<div id="submit-wrapper">
Expand Down
10 changes: 8 additions & 2 deletions core/templates/loginflowv2/grant.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@

<div class="picker-window">
<h2><?php p($l->t('Account access')) ?></h2>
<p class="info">
<?php p($l->t('Currently logged in as %1$s (%2$s).', [
$_['userDisplayName'],
$_['userId'],
])) ?>
</p>
<p class="info">
<?php print_unescaped($l->t('You are about to grant %1$s access to your %2$s account.', [
'<strong>' . \OCP\Util::sanitizeHTML($_['client']) . '</strong>',
Expand All @@ -41,10 +47,10 @@
<p id="redirect-link">
<form method="POST" action="<?php p($urlGenerator->linkToRouteAbsolute('core.ClientFlowLoginV2.generateAppPassword')) ?>">
<input type="hidden" name="requesttoken" value="<?php p($_['requesttoken']) ?>" />
<input type="hidden" name="stateToken" value="<?php p($_['stateToken']) ?>" />
<input type="hidden" name="stateToken" value="<?php p($_['stateToken']) ?>" />
<div id="submit-wrapper">
<input type="submit" class="login primary icon-confirm-white" title="" value="<?php p($l->t('Grant access')); ?>" />
</div>
</div>
</form>
</p>
</div>
69 changes: 36 additions & 33 deletions tests/Core/Controller/ClientFlowLoginControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ public function testShowAuthPickerPageNoClientOrOauthRequest() {

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');
->withConsecutive(
['USER_AGENT'],
['OCS-APIREQUEST']
)
->willReturnMap([
['USER_AGENT', 'Mac OS X Sync Client'],
['OCS-APIREQUEST', 'true'],
]);
$this->random
->expects($this->once())
->method('generate')
Expand Down Expand Up @@ -195,10 +195,15 @@ public function testShowAuthPickerPageWithOcsHeader() {

public function testShowAuthPickerPageWithOauth() {
$this->request
->expects($this->at(0))
->method('getHeader')
->with('USER_AGENT')
->willReturn('Mac OS X Sync Client');
->withConsecutive(
['USER_AGENT'],
['OCS-APIREQUEST']
)
->willReturnMap([
['USER_AGENT', 'Mac OS X Sync Client'],
['OCS-APIREQUEST', 'false'],
]);
$client = new Client();
$client->setName('My external service');
$client->setRedirectUri('https://example.com/redirect.php');
Expand Down Expand Up @@ -411,23 +416,21 @@ public function testGeneratePasswordWithPassword() {
*/
public function testGeneratePasswordWithPasswordForOauthClient($redirectUri, $redirectUrl) {
$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');
->withConsecutive(
['client.flow.state.token'],
['oauth.state']
)
->willReturnMap([
['client.flow.state.token', 'MyStateToken'],
['oauth.state', 'MyOauthState'],
]);
$this->session
->expects($this->at(4))
->method('remove')
->with('oauth.state');
->withConsecutive(
['client.flow.state.token'],
['oauth.state']
);
$this->session
->expects($this->once())
->method('getId')
Expand All @@ -448,15 +451,15 @@ public function testGeneratePasswordWithPasswordForOauthClient($redirectUri, $re
->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');
->withConsecutive(
[72],
[128]
)
->willReturnMap([
[72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS, 'MyGeneratedToken'],
[128, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS, 'MyAccessCode'],
]);
$user = $this->createMock(IUser::class);
$user
->expects($this->once())
Expand Down
14 changes: 14 additions & 0 deletions tests/Core/Controller/ClientFlowLoginV2ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
use OCP\IRequest;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Security\ISecureRandom;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
Expand All @@ -50,6 +52,8 @@ class ClientFlowLoginV2ControllerTest extends TestCase {
private $urlGenerator;
/** @var ISession|MockObject */
private $session;
/** @var IUserSession|MockObject */
private $userSession;
/** @var ISecureRandom|MockObject */
private $random;
/** @var Defaults|MockObject */
Expand All @@ -66,6 +70,7 @@ protected function setUp(): void {
$this->loginFlowV2Service = $this->createMock(LoginFlowV2Service::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->session = $this->createMock(ISession::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->random = $this->createMock(ISecureRandom::class);
$this->defaults = $this->createMock(Defaults::class);
$this->l = $this->createMock(IL10N::class);
Expand All @@ -75,6 +80,7 @@ protected function setUp(): void {
$this->loginFlowV2Service,
$this->urlGenerator,
$this->session,
$this->userSession,
$this->random,
$this->defaults,
'user',
Expand Down Expand Up @@ -224,6 +230,14 @@ public function testGrantPageValid() {
return null;
});

$user = $this->createMock(IUser::class);
$user->method('getUID')
->willReturn('uid');
$user->method('getDisplayName')
->willReturn('display name');
$this->userSession->method('getUser')
->willReturn($user);

$flow = new LoginFlowV2();
$this->loginFlowV2Service->method('getByLoginToken')
->with('loginToken')
Expand Down

0 comments on commit 33c21e1

Please sign in to comment.