Skip to content

Commit 127a1e6

Browse files
committed
AuthPicker: redirect oauth client to grant page
Signed-off-by: Sergej Nikolaev <[email protected]>
1 parent a02a626 commit 127a1e6

3 files changed

Lines changed: 27 additions & 33 deletions

File tree

core/Controller/ClientFlowLoginController.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use OCA\OAuth2\Db\ClientMapper;
3636
use OCP\AppFramework\Controller;
3737
use OCP\AppFramework\Http;
38+
use OCP\AppFramework\Http\RedirectResponse;
3839
use OCP\AppFramework\Http\Response;
3940
use OCP\AppFramework\Http\StandaloneTemplateResponse;
4041
use OCP\Defaults;
@@ -195,6 +196,19 @@ public function showAuthPickerPage($clientIdentifier = '') {
195196
);
196197
$this->session->set(self::stateName, $stateToken);
197198

199+
$oauthState = $this->session->get('oauth.state');
200+
if (!empty($oauthState)) {
201+
$targetUrl = $this->urlGenerator->linkToRouteAbsolute(
202+
'core.ClientFlowLogin.grantPage',
203+
[
204+
'stateToken' => $stateToken,
205+
'clientIdentifier' => $clientIdentifier,
206+
'oauthState' => $oauthState
207+
]
208+
);
209+
return new RedirectResponse($targetUrl);
210+
}
211+
198212
$csp = new Http\ContentSecurityPolicy();
199213
if ($client) {
200214
$csp->addAllowedFormActionDomain($client->getRedirectUri());
@@ -212,7 +226,6 @@ public function showAuthPickerPage($clientIdentifier = '') {
212226
'urlGenerator' => $this->urlGenerator,
213227
'stateToken' => $stateToken,
214228
'serverHost' => $this->getServerPath(),
215-
'oauthState' => $this->session->get('oauth.state'),
216229
],
217230
'guest'
218231
);

core/templates/loginflow/authpicker.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<br/>
4040

4141
<p id="redirect-link">
42-
<a href="<?php p($urlGenerator->linkToRouteAbsolute('core.ClientFlowLogin.grantPage', ['stateToken' => $_['stateToken'], 'clientIdentifier' => $_['clientIdentifier'], 'oauthState' => $_['oauthState']])) ?>">
42+
<a href="<?php p($urlGenerator->linkToRouteAbsolute('core.ClientFlowLogin.grantPage', ['stateToken' => $_['stateToken'], 'clientIdentifier' => $_['clientIdentifier']])) ?>">
4343
<input type="submit" class="login primary icon-confirm-white" value="<?php p($l->t('Log in')) ?>">
4444
</a>
4545
</p>
@@ -59,6 +59,4 @@
5959
</form>
6060
</div>
6161

62-
<?php if(empty($_['oauthState'])): ?>
6362
<a id="app-token-login" class="warning" href="#"><?php p($l->t('Alternative log in using app token')) ?></a>
64-
<?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('linkToRouteAbsolute')
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)