Skip to content

Commit

Permalink
Immediately redirect upon login() with OIDC.
Browse files Browse the repository at this point in the history
Fixes #39.
  • Loading branch information
RubenVerborgh committed Apr 28, 2018
1 parent cfbda1e commit 917f92c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 26 deletions.
15 changes: 6 additions & 9 deletions README.md
Expand Up @@ -42,7 +42,7 @@ functions. You don't have to know anything about flow.*
login (idp: string, {
callbackUri?: string,
storage?: Storage
}): Promise<?session | ?redirectFn>
}): Promise<?session>
```

Authenticates the user with their IDP (identity provider) and promises an object
Expand All @@ -51,15 +51,12 @@ containing the user's session.
When the user is successfully authenticated, the session will be non-null. When
the user is not authenticated by the IDP, the session will be `null`.

Auth flows like OIDC require the user to give consent on their identity
provider. In such cases, this function will return _a function which
redirects the user to their auth provider_, so as not to break the promise.
All you have to do is call that function in order to send the user on their
way. Then, call `currentSession` when the user gives consent and lands back
in your app.
Auth flows like OIDC require the user to give consent on their identity provider.
In such cases, this function will _redirect the user to their auth provider_.
Then, call `currentSession` when the user gives consent and lands back in your app.

If you're using an auth flow with redirections, and don't want to take the
user away from your app, consider using the [popup workflow](#Logging-in-via-the-popup-app).
If don't want to take the user away from your app,
consider using the [popup workflow](#Logging-in-via-the-popup-app).

If there's an error during the auth handshake, the Promise will reject.

Expand Down
8 changes: 3 additions & 5 deletions src/api.js
Expand Up @@ -46,19 +46,17 @@ async function firstSession(
return firstSession(storage, authFns.slice(1))
}

type redirectFn = () => any

export async function login(
idp: string,
options: loginOptions
): Promise<?Session | ?redirectFn> {
): Promise<?Session> {
options = { ...defaultLoginOptions(), ...options }
const webIdTlsSession = await WebIdTls.login(idp)
if (webIdTlsSession) {
return saveSession(options.storage)(webIdTlsSession)
}
const webIdOidcLoginRedirectFn = await WebIdOidc.login(idp, options)
return webIdOidcLoginRedirectFn
const webIdOidcLogin = await WebIdOidc.login(idp, options)
return webIdOidcLogin
}

export async function popupLogin(options: loginOptions): Promise<?Session> {
Expand Down
15 changes: 5 additions & 10 deletions src/api.spec.js
Expand Up @@ -132,8 +132,7 @@ describe('login', () => {
.post('/register')
.reply(200, oidcRegistration)

const redirectFn = await login('https://localhost')
await redirectFn()
await login('https://localhost')
const location = new window.URL(window.location.href)
expect(location.origin).toEqual('https://localhost')
expect(location.pathname).toEqual('/authorize')
Expand Down Expand Up @@ -161,10 +160,9 @@ describe('login', () => {
.post('/register')
.reply(200, oidcRegistration)

const redirectFn = await login('https://localhost', {
await login('https://localhost', {
callbackUri: 'https://app.biz/welcome/'
})
await redirectFn()
const location = new window.URL(window.location.href)
expect(location.origin).toEqual('https://localhost')
expect(location.pathname).toEqual('/authorize')
Expand Down Expand Up @@ -194,8 +192,7 @@ describe('login', () => {

window.location.href += '#foo-bar'

const redirectFn = await login('https://localhost')
await redirectFn()
await login('https://localhost')
const location = new window.URL(window.location.href)
expect(location.origin).toEqual('https://localhost')
expect(location.pathname).toEqual('/authorize')
Expand Down Expand Up @@ -263,8 +260,7 @@ describe('currentSession', () => {

let expectedIdToken, expectedAccessToken

const redirectFn = await login('https://localhost')
await redirectFn()
await login('https://localhost')
// generate the auth response
const location = new window.URL(window.location.href)
const state = location.searchParams.get('state')
Expand Down Expand Up @@ -342,8 +338,7 @@ describe('logout', () => {

let expectedIdToken, expectedAccessToken

const redirectFn = await login('https://localhost')
await redirectFn()
await login('https://localhost')
// generate the auth response
const location = new window.URL(window.location.href)
const state = location.searchParams.get('state')
Expand Down
4 changes: 2 additions & 2 deletions src/webid-oidc.js
Expand Up @@ -14,11 +14,11 @@ import { defaultStorage, getData, updateStorage } from './storage'
export const login = async (
idp: string,
options: loginOptions
): Promise<null | (() => any)> => {
): Promise<?null> => {
try {
const rp = await getRegisteredRp(idp, options)
await saveAppHashFragment(options.storage)
return () => sendAuthRequest(rp, options)
return sendAuthRequest(rp, options)
} catch (err) {
console.warn('Error logging in with WebID-OIDC')
console.error(err)
Expand Down

0 comments on commit 917f92c

Please sign in to comment.