Skip to content

Commit 6084d69

Browse files
Merge pull request #32375 from nextcloud/bugfix/noid/show-user-account-on-grant-loginflow-step
Show user account on grant loginflow step
2 parents ad405e9 + 40b9769 commit 6084d69

6 files changed

Lines changed: 82 additions & 36 deletions

File tree

core/Controller/ClientFlowLoginController.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
use OCP\IRequest;
5050
use OCP\ISession;
5151
use OCP\IURLGenerator;
52+
use OCP\IUser;
5253
use OCP\IUserSession;
5354
use OCP\Security\ICrypto;
5455
use OCP\Security\ISecureRandom;
@@ -251,10 +252,15 @@ public function grantPage(string $stateToken = '',
251252
$csp->addAllowedFormActionDomain('nc://*');
252253
}
253254

255+
/** @var IUser $user */
256+
$user = $this->userSession->getUser();
257+
254258
$response = new StandaloneTemplateResponse(
255259
$this->appName,
256260
'loginflow/grant',
257261
[
262+
'userId' => $user->getUID(),
263+
'userDisplayName' => $user->getDisplayName(),
258264
'client' => $clientName,
259265
'clientIdentifier' => $clientIdentifier,
260266
'instanceName' => $this->defaults->getName(),

core/Controller/ClientFlowLoginV2Controller.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
use OCP\IRequest;
4343
use OCP\ISession;
4444
use OCP\IURLGenerator;
45+
use OCP\IUser;
46+
use OCP\IUserSession;
4547
use OCP\Security\ISecureRandom;
4648

4749
class ClientFlowLoginV2Controller extends Controller {
@@ -54,6 +56,8 @@ class ClientFlowLoginV2Controller extends Controller {
5456
private $urlGenerator;
5557
/** @var ISession */
5658
private $session;
59+
/** @var IUserSession */
60+
private $userSession;
5761
/** @var ISecureRandom */
5862
private $random;
5963
/** @var Defaults */
@@ -68,6 +72,7 @@ public function __construct(string $appName,
6872
LoginFlowV2Service $loginFlowV2Service,
6973
IURLGenerator $urlGenerator,
7074
ISession $session,
75+
IUserSession $userSession,
7176
ISecureRandom $random,
7277
Defaults $defaults,
7378
?string $userId,
@@ -76,6 +81,7 @@ public function __construct(string $appName,
7681
$this->loginFlowV2Service = $loginFlowV2Service;
7782
$this->urlGenerator = $urlGenerator;
7883
$this->session = $session;
84+
$this->userSession = $userSession;
7985
$this->random = $random;
8086
$this->defaults = $defaults;
8187
$this->userId = $userId;
@@ -162,10 +168,15 @@ public function grantPage(string $stateToken): StandaloneTemplateResponse {
162168
return $this->loginTokenForbiddenResponse();
163169
}
164170

171+
/** @var IUser $user */
172+
$user = $this->userSession->getUser();
173+
165174
return new StandaloneTemplateResponse(
166175
$this->appName,
167176
'loginflowv2/grant',
168177
[
178+
'userId' => $user->getUID(),
179+
'userDisplayName' => $user->getDisplayName(),
169180
'client' => $flow->getClientName(),
170181
'instanceName' => $this->defaults->getName(),
171182
'urlGenerator' => $this->urlGenerator,

core/templates/loginflow/grant.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929

3030
<div class="picker-window">
3131
<h2><?php p($l->t('Account access')) ?></h2>
32+
<p class="info">
33+
<?php p($l->t('Currently logged in as %1$s (%2$s).', [
34+
$_['userDisplayName'],
35+
$_['userId'],
36+
])) ?>
37+
</p>
3238
<p class="info">
3339
<?php print_unescaped($l->t('You are about to grant %1$s access to your %2$s account.', [
3440
'<strong>' . \OCP\Util::sanitizeHTML($_['client']) . '</strong>',
@@ -44,7 +50,7 @@
4450
<input type="hidden" name="requesttoken" value="<?php p($_['requesttoken']) ?>" />
4551
<input type="hidden" name="stateToken" value="<?php p($_['stateToken']) ?>" />
4652
<input type="hidden" name="oauthState" value="<?php p($_['oauthState']) ?>" />
47-
<?php if (p($_['direct'])) { ?>
53+
<?php if ($_['direct']) { ?>
4854
<input type="hidden" name="direct" value="1" />
4955
<?php } ?>
5056
<div id="submit-wrapper">

core/templates/loginflowv2/grant.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929

3030
<div class="picker-window">
3131
<h2><?php p($l->t('Account access')) ?></h2>
32+
<p class="info">
33+
<?php p($l->t('Currently logged in as %1$s (%2$s).', [
34+
$_['userDisplayName'],
35+
$_['userId'],
36+
])) ?>
37+
</p>
3238
<p class="info">
3339
<?php print_unescaped($l->t('You are about to grant %1$s access to your %2$s account.', [
3440
'<strong>' . \OCP\Util::sanitizeHTML($_['client']) . '</strong>',
@@ -41,10 +47,10 @@
4147
<p id="redirect-link">
4248
<form method="POST" action="<?php p($urlGenerator->linkToRouteAbsolute('core.ClientFlowLoginV2.generateAppPassword')) ?>">
4349
<input type="hidden" name="requesttoken" value="<?php p($_['requesttoken']) ?>" />
44-
<input type="hidden" name="stateToken" value="<?php p($_['stateToken']) ?>" />
50+
<input type="hidden" name="stateToken" value="<?php p($_['stateToken']) ?>" />
4551
<div id="submit-wrapper">
4652
<input type="submit" class="login primary icon-confirm-white" title="" value="<?php p($l->t('Grant access')); ?>" />
47-
</div>
53+
</div>
4854
</form>
4955
</p>
5056
</div>

tests/Core/Controller/ClientFlowLoginControllerTest.php

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,15 @@ public function testShowAuthPickerPageNoClientOrOauthRequest() {
134134

135135
public function testShowAuthPickerPageWithOcsHeader() {
136136
$this->request
137-
->expects($this->at(0))
138137
->method('getHeader')
139-
->with('USER_AGENT')
140-
->willReturn('Mac OS X Sync Client');
141-
$this->request
142-
->expects($this->at(1))
143-
->method('getHeader')
144-
->with('OCS-APIREQUEST')
145-
->willReturn('true');
138+
->withConsecutive(
139+
['USER_AGENT'],
140+
['OCS-APIREQUEST']
141+
)
142+
->willReturnMap([
143+
['USER_AGENT', 'Mac OS X Sync Client'],
144+
['OCS-APIREQUEST', 'true'],
145+
]);
146146
$this->random
147147
->expects($this->once())
148148
->method('generate')
@@ -196,10 +196,15 @@ public function testShowAuthPickerPageWithOcsHeader() {
196196

197197
public function testShowAuthPickerPageWithOauth() {
198198
$this->request
199-
->expects($this->at(0))
200199
->method('getHeader')
201-
->with('USER_AGENT')
202-
->willReturn('Mac OS X Sync Client');
200+
->withConsecutive(
201+
['USER_AGENT'],
202+
['OCS-APIREQUEST']
203+
)
204+
->willReturnMap([
205+
['USER_AGENT', 'Mac OS X Sync Client'],
206+
['OCS-APIREQUEST', 'false'],
207+
]);
203208
$client = new Client();
204209
$client->setName('My external service');
205210
$client->setRedirectUri('https://example.com/redirect.php');
@@ -413,23 +418,21 @@ public function testGeneratePasswordWithPassword() {
413418
*/
414419
public function testGeneratePasswordWithPasswordForOauthClient($redirectUri, $redirectUrl) {
415420
$this->session
416-
->expects($this->at(0))
417421
->method('get')
418-
->with('client.flow.state.token')
419-
->willReturn('MyStateToken');
420-
$this->session
421-
->expects($this->at(1))
422-
->method('remove')
423-
->with('client.flow.state.token');
424-
$this->session
425-
->expects($this->at(3))
426-
->method('get')
427-
->with('oauth.state')
428-
->willReturn('MyOauthState');
422+
->withConsecutive(
423+
['client.flow.state.token'],
424+
['oauth.state']
425+
)
426+
->willReturnMap([
427+
['client.flow.state.token', 'MyStateToken'],
428+
['oauth.state', 'MyOauthState'],
429+
]);
429430
$this->session
430-
->expects($this->at(4))
431431
->method('remove')
432-
->with('oauth.state');
432+
->withConsecutive(
433+
['client.flow.state.token'],
434+
['oauth.state']
435+
);
433436
$this->session
434437
->expects($this->once())
435438
->method('getId')
@@ -450,15 +453,15 @@ public function testGeneratePasswordWithPasswordForOauthClient($redirectUri, $re
450453
->with($myToken, 'SessionId')
451454
->willReturn('MyPassword');
452455
$this->random
453-
->expects($this->at(0))
454456
->method('generate')
455-
->with(72)
456-
->willReturn('MyGeneratedToken');
457-
$this->random
458-
->expects($this->at(1))
459-
->method('generate')
460-
->with(128)
461-
->willReturn('MyAccessCode');
457+
->withConsecutive(
458+
[72],
459+
[128]
460+
)
461+
->willReturnMap([
462+
[72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS, 'MyGeneratedToken'],
463+
[128, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS, 'MyAccessCode'],
464+
]);
462465
$user = $this->createMock(IUser::class);
463466
$user
464467
->expects($this->once())

tests/Core/Controller/ClientFlowLoginV2ControllerTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
use OCP\IRequest;
3737
use OCP\ISession;
3838
use OCP\IURLGenerator;
39+
use OCP\IUser;
40+
use OCP\IUserSession;
3941
use OCP\Security\ISecureRandom;
4042
use PHPUnit\Framework\MockObject\MockObject;
4143
use Test\TestCase;
@@ -50,6 +52,8 @@ class ClientFlowLoginV2ControllerTest extends TestCase {
5052
private $urlGenerator;
5153
/** @var ISession|MockObject */
5254
private $session;
55+
/** @var IUserSession|MockObject */
56+
private $userSession;
5357
/** @var ISecureRandom|MockObject */
5458
private $random;
5559
/** @var Defaults|MockObject */
@@ -66,6 +70,7 @@ protected function setUp(): void {
6670
$this->loginFlowV2Service = $this->createMock(LoginFlowV2Service::class);
6771
$this->urlGenerator = $this->createMock(IURLGenerator::class);
6872
$this->session = $this->createMock(ISession::class);
73+
$this->userSession = $this->createMock(IUserSession::class);
6974
$this->random = $this->createMock(ISecureRandom::class);
7075
$this->defaults = $this->createMock(Defaults::class);
7176
$this->l = $this->createMock(IL10N::class);
@@ -75,6 +80,7 @@ protected function setUp(): void {
7580
$this->loginFlowV2Service,
7681
$this->urlGenerator,
7782
$this->session,
83+
$this->userSession,
7884
$this->random,
7985
$this->defaults,
8086
'user',
@@ -224,6 +230,14 @@ public function testGrantPageValid() {
224230
return null;
225231
});
226232

233+
$user = $this->createMock(IUser::class);
234+
$user->method('getUID')
235+
->willReturn('uid');
236+
$user->method('getDisplayName')
237+
->willReturn('display name');
238+
$this->userSession->method('getUser')
239+
->willReturn($user);
240+
227241
$flow = new LoginFlowV2();
228242
$this->loginFlowV2Service->method('getByLoginToken')
229243
->with('loginToken')

0 commit comments

Comments
 (0)