Permalink
Browse files

feat(auth0lock): Upgrade code to work with Auth0 Lock 10.x (no backwa…

…rds compat)
  • Loading branch information...
pfurini committed Sep 28, 2016
1 parent a48518e commit fe3adc77e8f82d4dbc751d8c18d1bc525bf21cf8
@@ -252,6 +252,7 @@ define(["exports", "./authFilterValueConverter", "./authenticatedValueConverter"
this.autoUpdateToken = true;
this.clientId = false;
this.refreshTokenProp = 'refresh_token';
this.refreshTokenSubmitProp = 'refresh_token';
this.refreshTokenName = 'token';
this.refreshTokenRoot = false;
this.idTokenProp = 'id_token';
@@ -386,9 +387,7 @@ define(["exports", "./authFilterValueConverter", "./authenticatedValueConverter"
clientId: 'your_client_id',
clientDomain: 'your_domain_url',
display: 'popup',
lockOptions: {
popup: true
},
lockOptions: {},
responseType: 'token',
state: randomState
}
@@ -530,14 +529,12 @@ define(["exports", "./authFilterValueConverter", "./authenticatedValueConverter"
name: null,
state: null,
scope: null,
scopeDelimiter: null,
scopeDelimiter: ' ',
redirectUri: null,
clientId: null,
clientDomain: null,
display: 'popup',
lockOptions: {
popup: true
},
lockOptions: {},
popupOptions: null,
responseType: 'token'
};
@@ -558,26 +555,46 @@ define(["exports", "./authFilterValueConverter", "./authenticatedValueConverter"
this.storage.set(stateName, provider.state);
}
this.lock = this.lock || new _aureliaPal.PLATFORM.global.Auth0Lock(provider.clientId, provider.clientDomain);
var opts = {
auth: {
params: {}
}
};
if (Array.isArray(provider.scope) && provider.scope.length) {
opts.auth.params.scope = provider.scope.join(provider.scopeDelimiter);
}
if (provider.state) {
opts.auth.params.state = this.storage.get(provider.name + '_state');
}
if (provider.display === 'popup') {
opts.auth.redirect = false;
} else if (typeof provider.redirectUri === 'string') {
opts.auth.redirect = true;
opts.auth.redirectUrl = provider.redirectUri;
}
if (_typeof(provider.popupOptions) === 'object') {
opts.popupOptions = provider.popupOptions;
}
if (typeof provider.responseType === 'string') {
opts.auth.responseType = provider.responseType;
}
var lockOptions = (0, _extend2.default)(true, {}, provider.lockOptions, opts);
this.lock = this.lock || new _aureliaPal.PLATFORM.global.Auth0Lock(provider.clientId, provider.clientDomain, lockOptions);
var openPopup = new Promise(function (resolve, reject) {
var 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 = _this3.storage.get(provider.name + '_state');
_this3.lock.show(provider.lockOptions, function (err, profile, tokenOrCode) {
if (err) {
reject(err);
} else {
resolve({
access_token: tokenOrCode
});
_this3.lock.on('authenticated', function (authResponse) {
if (!lockOptions.auth.redirect) {
_this3.lock.hide();
}
resolve({
access_token: authResponse.idToken
});
});
_this3.lock.on('authorization_error', function (err) {
reject(err);
});
_this3.lock.show();
});
return openPopup.then(function (lockResponse) {
@@ -1207,10 +1224,11 @@ define(["exports", "./authFilterValueConverter", "./authenticatedValueConverter"
if (this.authentication.updateTokenCallstack.length === 0) {
var content = {
grant_type: 'refresh_token',
refresh_token: this.authentication.getRefreshToken(),
client_id: this.config.clientId ? this.config.clientId : undefined
};
content[this.config.refreshTokenSubmitProp] = this.authentication.getRefreshToken();
this.client.post(this.config.joinBase(this.config.refreshTokenUrl ? this.config.refreshTokenUrl : this.config.loginUrl), content).then(function (response) {
_this10.setResponseObject(response);
_this10.authentication.resolveUpdateTokenCallstack(_this10.isAuthenticated());
@@ -132,6 +132,9 @@ export declare class BaseConfig {
// The the property from which to get the refresh token after a successful token refresh. Can also be dotted eg "refreshTokenProp.refreshTokenProp"
refreshTokenProp: any;
// The proprety name used to send the existing token when refreshing `{ "refreshTokenSubmitProp": '...' }`
refreshTokenSubmitProp: any;
// If the property defined by `refreshTokenProp` is an object:
// -----------------------------------------------------------
// This is the property from which to get the token `{ "refreshTokenProp": { "refreshTokenName" : '...' } }`
@@ -541,6 +544,16 @@ export declare class FetchConfig {
* @param {{}|Function} config
*/
export declare function configure(aurelia?: any, config?: any): any;
export declare class AuthFilterValueConverter {
/**
* route toView predictator on route.config.auth === isAuthenticated
* @param {RouteConfig} routes the routes array to convert
* @param {Boolean} isAuthenticated authentication status
* @return {Boolean} show/hide element
*/
toView(routes?: any, isAuthenticated?: any): any;
}
export declare class AuthenticatedFilterValueConverter {
constructor(authService?: any);
@@ -560,14 +573,4 @@ export declare class AuthenticatedValueConverter {
* @return {Boolean} show/hide element
*/
toView(): any;
}
export declare class AuthFilterValueConverter {
/**
* route toView predictator on route.config.auth === isAuthenticated
* @param {RouteConfig} routes the routes array to convert
* @param {Boolean} isAuthenticated authentication status
* @return {Boolean} show/hide element
*/
toView(routes?: any, isAuthenticated?: any): any;
}
@@ -248,6 +248,8 @@ export class BaseConfig {
clientId = false;
// The the property from which to get the refresh token after a successful token refresh. Can also be dotted eg "refreshTokenProp.refreshTokenProp"
refreshTokenProp = 'refresh_token';
// The proprety name used to send the existing token when refreshing `{ "refreshTokenSubmitProp": '...' }`
refreshTokenSubmitProp = 'refresh_token';
// If the property defined by `refreshTokenProp` is an object:
// -----------------------------------------------------------
@@ -413,9 +415,7 @@ export class BaseConfig {
clientId: 'your_client_id',
clientDomain: 'your_domain_url',
display: 'popup',
lockOptions: {
popup: true
},
lockOptions: {},
responseType: 'token',
state: randomState
}
@@ -564,14 +564,12 @@ export class AuthLock {
name: null,
state: null,
scope: null,
scopeDelimiter: null,
scopeDelimiter: ' ',
redirectUri: null,
clientId: null,
clientDomain: null,
display: 'popup',
lockOptions: {
popup: true
},
lockOptions: {},
popupOptions: null,
responseType: 'token'
};
@@ -591,27 +589,48 @@ export class AuthLock {
this.storage.set(stateName, provider.state);
}
this.lock = this.lock || new PLATFORM.global.Auth0Lock(provider.clientId, provider.clientDomain);
// transform provider options into auth0-lock options
let opts = {
auth: {
params: {}
}
};
if (Array.isArray(provider.scope) && provider.scope.length) {
opts.auth.params.scope = provider.scope.join(provider.scopeDelimiter);
}
if (provider.state) {
opts.auth.params.state = this.storage.get(provider.name + '_state');
}
if (provider.display === 'popup') {
opts.auth.redirect = false;
} else if (typeof provider.redirectUri === 'string') {
opts.auth.redirect = true;
opts.auth.redirectUrl = provider.redirectUri;
}
if (typeof provider.popupOptions === 'object') {
opts.popupOptions = provider.popupOptions;
}
if (typeof provider.responseType === 'string') {
opts.auth.responseType = provider.responseType;
}
let lockOptions = extend(true, {}, provider.lockOptions, opts);
this.lock = this.lock || new PLATFORM.global.Auth0Lock(provider.clientId, provider.clientDomain, lockOptions);
const openPopup = new Promise((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
});
this.lock.on('authenticated', authResponse => {
if (!lockOptions.auth.redirect) {
// hides the lock popup, as it doesn't do so automatically
this.lock.hide();
}
resolve({
access_token: authResponse.idToken
});
});
this.lock.on('authorization_error', err => {
reject(err);
});
this.lock.show();
});
return openPopup
@@ -1410,12 +1429,13 @@ export class AuthService {
}
if (this.authentication.updateTokenCallstack.length === 0) {
const content = {
let content = {
grant_type: 'refresh_token',
refresh_token: this.authentication.getRefreshToken(),
client_id: this.config.clientId ? this.config.clientId : undefined
};
content[this.config.refreshTokenSubmitProp] = this.authentication.getRefreshToken();
this.client.post(this.config.joinBase(this.config.refreshTokenUrl
? this.config.refreshTokenUrl
: this.config.loginUrl), content)
@@ -243,6 +243,7 @@ var BaseConfig = exports.BaseConfig = function () {
this.autoUpdateToken = true;
this.clientId = false;
this.refreshTokenProp = 'refresh_token';
this.refreshTokenSubmitProp = 'refresh_token';
this.refreshTokenName = 'token';
this.refreshTokenRoot = false;
this.idTokenProp = 'id_token';
@@ -377,9 +378,7 @@ var BaseConfig = exports.BaseConfig = function () {
clientId: 'your_client_id',
clientDomain: 'your_domain_url',
display: 'popup',
lockOptions: {
popup: true
},
lockOptions: {},
responseType: 'token',
state: randomState
}
@@ -521,14 +520,12 @@ var AuthLock = exports.AuthLock = (_dec2 = (0, _aureliaDependencyInjection.injec
name: null,
state: null,
scope: null,
scopeDelimiter: null,
scopeDelimiter: ' ',
redirectUri: null,
clientId: null,
clientDomain: null,
display: 'popup',
lockOptions: {
popup: true
},
lockOptions: {},
popupOptions: null,
responseType: 'token'
};
@@ -549,26 +546,46 @@ var AuthLock = exports.AuthLock = (_dec2 = (0, _aureliaDependencyInjection.injec
this.storage.set(stateName, provider.state);
}
this.lock = this.lock || new _aureliaPal.PLATFORM.global.Auth0Lock(provider.clientId, provider.clientDomain);
var opts = {
auth: {
params: {}
}
};
if (Array.isArray(provider.scope) && provider.scope.length) {
opts.auth.params.scope = provider.scope.join(provider.scopeDelimiter);
}
if (provider.state) {
opts.auth.params.state = this.storage.get(provider.name + '_state');
}
if (provider.display === 'popup') {
opts.auth.redirect = false;
} else if (typeof provider.redirectUri === 'string') {
opts.auth.redirect = true;
opts.auth.redirectUrl = provider.redirectUri;
}
if (_typeof(provider.popupOptions) === 'object') {
opts.popupOptions = provider.popupOptions;
}
if (typeof provider.responseType === 'string') {
opts.auth.responseType = provider.responseType;
}
var lockOptions = (0, _extend2.default)(true, {}, provider.lockOptions, opts);
this.lock = this.lock || new _aureliaPal.PLATFORM.global.Auth0Lock(provider.clientId, provider.clientDomain, lockOptions);
var openPopup = new Promise(function (resolve, reject) {
var 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 = _this3.storage.get(provider.name + '_state');
_this3.lock.show(provider.lockOptions, function (err, profile, tokenOrCode) {
if (err) {
reject(err);
} else {
resolve({
access_token: tokenOrCode
});
_this3.lock.on('authenticated', function (authResponse) {
if (!lockOptions.auth.redirect) {
_this3.lock.hide();
}
resolve({
access_token: authResponse.idToken
});
});
_this3.lock.on('authorization_error', function (err) {
reject(err);
});
_this3.lock.show();
});
return openPopup.then(function (lockResponse) {
@@ -1198,10 +1215,11 @@ var AuthService = exports.AuthService = (_dec12 = (0, _aureliaDependencyInjectio
if (this.authentication.updateTokenCallstack.length === 0) {
var content = {
grant_type: 'refresh_token',
refresh_token: this.authentication.getRefreshToken(),
client_id: this.config.clientId ? this.config.clientId : undefined
};
content[this.config.refreshTokenSubmitProp] = this.authentication.getRefreshToken();
this.client.post(this.config.joinBase(this.config.refreshTokenUrl ? this.config.refreshTokenUrl : this.config.loginUrl), content).then(function (response) {
_this10.setResponseObject(response);
_this10.authentication.resolveUpdateTokenCallstack(_this10.isAuthenticated());
Oops, something went wrong.

0 comments on commit fe3adc7

Please sign in to comment.