This repository was archived by the owner on Nov 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(authentication): add support for auth0 login using lock
Adds an Auth0Lock provider class that wraps the Auth0-Lock library. It activates when the oauthType property is equal to 'auth0-lock', and expects some peculiar properties in its configuration block (see the sample config in baseConfig.js). TODO: proper testing; better way to load it (provider extensibility api)
- Loading branch information
Showing
5 changed files
with
106 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import {inject} from 'aurelia-dependency-injection'; | ||
import {PLATFORM} from 'aurelia-pal'; | ||
import extend from 'extend'; | ||
|
||
import {Storage} from './storage'; | ||
import {BaseConfig} from './baseConfig'; | ||
|
||
@inject(Storage, BaseConfig) | ||
export class Auth0Lock { | ||
constructor(storage, config) { | ||
this.storage = storage; | ||
this.config = config; | ||
this.defaults = { | ||
name: null, | ||
state: null, | ||
scope: null, | ||
scopeDelimiter: null, | ||
redirectUri: null, | ||
clientId: null, | ||
clientDomain: null, | ||
display: 'popup', | ||
lockOptions: { | ||
popup: true | ||
}, | ||
popupOptions: null, | ||
responseType: 'token' | ||
}; | ||
} | ||
|
||
open(options, userData) { | ||
// check pre-conditions | ||
if (typeof PLATFORM.global.Auth0Lock !== 'function') { | ||
throw new Error('Auth0Lock was not found in global scope. Please load it before using this provider.'); | ||
} | ||
const provider = extend(true, {}, this.defaults, options); | ||
const stateName = provider.name + '_state'; | ||
|
||
if (typeof provider.state === 'function') { | ||
this.storage.set(stateName, provider.state()); | ||
} else if (typeof provider.state === 'string') { | ||
this.storage.set(stateName, provider.state); | ||
} | ||
|
||
this.lock = this.lock || new PLATFORM.global.Auth0Lock(provider.clientId, provider.clientDomain); | ||
|
||
const openPopup = new Promise(function(resolve, reject) { | ||
let opts = provider.lockOptions; | ||
opts.popupOptions = provider.popupOptions; | ||
opts.responseType = provider.responseType; | ||
opts.callbackURL = provider.redirectUri; | ||
opts.authParams = opts.authParams || {}; | ||
if (provider.scope) opts.authParams.scope = provider.scope; | ||
if (provider.state) opts.authParams.state = this.storage.get(provider.name + '_state'); | ||
|
||
this.lock.show(provider.lockOptions, (err, profile, tokenOrCode) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve({ | ||
//NOTE: this is an id token (JWT) and it shouldn't be named access_token | ||
access_token: tokenOrCode | ||
}); | ||
} | ||
}); | ||
}.bind(this)); | ||
|
||
return openPopup | ||
.then(lockResponse => { | ||
if (provider.responseType === 'token' || | ||
provider.responseType === 'id_token%20token' || | ||
provider.responseType === 'token%20id_token' | ||
) { | ||
return lockResponse; | ||
} | ||
//NOTE: 'code' responseType is not supported, this is an OpenID response (JWT token) | ||
// and code flow is not secure client-side | ||
throw new Error('Only `token` responseType is supported'); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters