Visitar URL original
fix(authentication-oauth): keep the OAuth token in the redirect fragment · feathersjs/feathers@4c8650c · GitHub
Skip to content

Commit 4c8650c

Browse files
committed
fix(authentication-oauth): keep the OAuth token in the redirect fragment
1 parent f300461 commit 4c8650c

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

‎docs/api/authentication/oauth.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ A successful oAuth flow will redirect to the origin or redirect URL in the form
205205

206206
<BlockQuote type="info" label="Note">
207207

208-
The redirect is using a hash instead of a query string by default because it is not logged server side and can be easily read on the client. You can force query based redirect by adding a `?` to the end of the `redirect` option.
208+
The redirect is using a hash instead of a query string by default because it is not logged server side and can be easily read on the client. You can force query based redirect by adding a `?` to the end of the `redirect` option. This only applies to the configured option, a `redirect` query parameter always gets the hash.
209209

210210
</BlockQuote>
211211

‎packages/authentication-oauth/src/strategy.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ export class OAuthStrategy extends AuthenticationBaseStrategy {
237237
}
238238

239239
const redirectUrl = `${redirect}${queryRedirect}`
240-
const separator = redirectUrl.endsWith('?') ? '' : redirect.indexOf('#') !== -1 ? '?' : '#'
240+
// Only the configured redirect can put the token in the query string, a client supplied redirect can not
241+
const separator = redirect.endsWith('?') && !queryRedirect ? '' : redirect.indexOf('#') !== -1 ? '?' : '#'
241242
const authResult: AuthenticationResult = data
242243
const query = authResult.accessToken
243244
? { access_token: authResult.accessToken }

‎packages/authentication-oauth/test/strategy.test.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,13 @@ describe('@feathersjs/authentication-oauth/strategy', () => {
446446
redirect: '/hi-there?'
447447
}
448448
)
449-
assert.equal(redirect, '/home/hi-there?access_token=testing')
449+
assert.equal(redirect, '/home/hi-there?#access_token=testing')
450+
451+
redirect = await strategy.getRedirect({ accessToken: 'testing' }, { redirect: '?' })
452+
assert.equal(redirect, '/home?#access_token=testing')
453+
454+
redirect = await strategy.getRedirect(new Error('something went wrong'), { redirect: '/hi-there?' })
455+
assert.equal(redirect, '/home/hi-there?#error=something%20went%20wrong')
450456

451457
redirect = await strategy.getRedirect(new Error('something went wrong'))
452458
assert.equal(redirect, '/home#error=something%20went%20wrong')
@@ -459,6 +465,9 @@ describe('@feathersjs/authentication-oauth/strategy', () => {
459465
redirect = await strategy.getRedirect({ accessToken: 'testing' })
460466
assert.equal(redirect, '/home?access_token=testing')
461467

468+
redirect = await strategy.getRedirect({ accessToken: 'testing' }, { redirect: '/hi-there' })
469+
assert.equal(redirect, '/home?/hi-there#access_token=testing')
470+
462471
delete app.get('authentication').oauth.redirect
463472

464473
redirect = await strategy.getRedirect({ accessToken: 'testing' })
@@ -507,6 +516,17 @@ describe('@feathersjs/authentication-oauth/strategy', () => {
507516
)
508517
assert.equal(redirect, 'https://feathersjs.com/home#access_token=testing')
509518

519+
redirect = await strategy.getRedirect(
520+
{ accessToken: 'testing' },
521+
{
522+
redirect: '/home?',
523+
headers: {
524+
referer: 'https://feathersjs.com/somewhere'
525+
}
526+
}
527+
)
528+
assert.equal(redirect, 'https://feathersjs.com/home?#access_token=testing')
529+
510530
await assert.rejects(
511531
() =>
512532
strategy.getRedirect(

0 commit comments

Comments
 (0)