Skip to content

Commit 46adb7d

Browse files
committed
AuthPicker: redirect oauth client to grant page
Signed-off-by: Sergej Nikolaev <kinolaev@gmail.com>
1 parent 3ffd09d commit 46adb7d

3 files changed

Lines changed: 28 additions & 34 deletions

File tree

core/Controller/ClientFlowLoginController.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use OCA\OAuth2\Db\ClientMapper;
4242
use OCP\AppFramework\Controller;
4343
use OCP\AppFramework\Http;
44+
use OCP\AppFramework\Http\RedirectResponse;
4445
use OCP\AppFramework\Http\Response;
4546
use OCP\AppFramework\Http\StandaloneTemplateResponse;
4647
use OCP\Defaults;
@@ -166,7 +167,7 @@ private function stateTokenForbiddenResponse() {
166167
*
167168
* @param string $clientIdentifier
168169
*
169-
* @return StandaloneTemplateResponse
170+
* @return StandaloneTemplateResponse|RedirectResponse
170171
*/
171172
public function showAuthPickerPage($clientIdentifier = '') {
172173
$clientName = $this->getClientName();
@@ -201,6 +202,19 @@ public function showAuthPickerPage($clientIdentifier = '') {
201202
);
202203
$this->session->set(self::STATE_NAME, $stateToken);
203204

205+
$oauthState = $this->session->get('oauth.state');
206+
if (!empty($oauthState)) {
207+
$targetUrl = $this->urlGenerator->linkToRoute(
208+
'core.ClientFlowLogin.grantPage',
209+
[
210+
'stateToken' => $stateToken,
211+
'clientIdentifier' => $clientIdentifier,
212+
'oauthState' => $oauthState
213+
]
214+
);
215+
return new RedirectResponse($targetUrl);
216+
}
217+
204218
$csp = new Http\ContentSecurityPolicy();
205219
if ($client) {
206220
$csp->addAllowedFormActionDomain($client->getRedirectUri());
@@ -218,7 +232,6 @@ public function showAuthPickerPage($clientIdentifier = '') {
218232
'urlGenerator' => $this->urlGenerator,
219233
'stateToken' => $stateToken,
220234
'serverHost' => $this->getServerPath(),
221-
'oauthState' => $this->session->get('oauth.state'),
222235
],
223236
'guest'
224237
);

core/templates/loginflow/authpicker.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<br/>
4444

4545
<p id="redirect-link">
46-
<a href="<?php p($urlGenerator->linkToRoute('core.ClientFlowLogin.grantPage', ['stateToken' => $_['stateToken'], 'clientIdentifier' => $_['clientIdentifier'], 'oauthState' => $_['oauthState']])) ?>">
46+
<a href="<?php p($urlGenerator->linkToRoute('core.ClientFlowLogin.grantPage', ['stateToken' => $_['stateToken'], 'clientIdentifier' => $_['clientIdentifier']])) ?>">
4747
<input type="submit" class="login primary icon-confirm-white" value="<?php p($l->t('Log in')) ?>">
4848
</a>
4949
</p>
@@ -63,6 +63,4 @@
6363
</form>
6464
</div>
6565

66-
<?php if (empty($_['oauthState'])): ?>
6766
<a id="app-token-login" class="warning" href="#"><?php p($l->t('Alternative log in using app token')) ?></a>
68-
<?php endif; ?>

tests/Core/Controller/ClientFlowLoginControllerTest.php

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public function testShowAuthPickerPageWithOcsHeader() {
159159
->expects($this->once())
160160
->method('get')
161161
->with('oauth.state')
162-
->willReturn('OauthStateToken');
162+
->willReturn(null);
163163
$this->defaults
164164
->expects($this->once())
165165
->method('getName')
@@ -182,7 +182,6 @@ public function testShowAuthPickerPageWithOcsHeader() {
182182
'urlGenerator' => $this->urlGenerator,
183183
'stateToken' => 'StateToken',
184184
'serverHost' => 'https://example.com',
185-
'oauthState' => 'OauthStateToken',
186185
],
187186
'guest'
188187
);
@@ -223,35 +222,19 @@ public function testShowAuthPickerPageWithOauth() {
223222
->method('get')
224223
->with('oauth.state')
225224
->willReturn('OauthStateToken');
226-
$this->defaults
225+
$this->urlGenerator
227226
->expects($this->once())
228-
->method('getName')
229-
->willReturn('ExampleCloud');
230-
$this->request
231-
->expects($this->once())
232-
->method('getServerHost')
233-
->willReturn('example.com');
234-
$this->request
235-
->method('getServerProtocol')
236-
->willReturn('https');
227+
->method('linkToRoute')
228+
->with(
229+
'core.ClientFlowLogin.grantPage',
230+
[
231+
'stateToken' => 'StateToken',
232+
'clientIdentifier' => 'MyClientIdentifier',
233+
'oauthState' => 'OauthStateToken'
234+
])
235+
->willReturn('grantURL');
237236

238-
$expected = new StandaloneTemplateResponse(
239-
'core',
240-
'loginflow/authpicker',
241-
[
242-
'client' => 'My external service',
243-
'clientIdentifier' => 'MyClientIdentifier',
244-
'instanceName' => 'ExampleCloud',
245-
'urlGenerator' => $this->urlGenerator,
246-
'stateToken' => 'StateToken',
247-
'serverHost' => 'https://example.com',
248-
'oauthState' => 'OauthStateToken',
249-
],
250-
'guest'
251-
);
252-
$csp = new Http\ContentSecurityPolicy();
253-
$csp->addAllowedFormActionDomain('https://example.com/redirect.php');
254-
$expected->setContentSecurityPolicy($csp);
237+
$expected = new Http\RedirectResponse('grantURL');
255238
$this->assertEquals($expected, $this->clientFlowLoginController->showAuthPickerPage('MyClientIdentifier'));
256239
}
257240

0 commit comments

Comments
 (0)