torii
Version:
A set of clean abstractions for authentication in Ember.js
46 lines (38 loc) • 1.49 kB
JavaScript
import Provider from 'torii/providers/oauth2-code';
var Oauth2Bearer = Provider.extend({
responseType: 'token',
/**
* @method open
* @return {Promise<object>} If the authorization attempt is a success,
* the promise will resolve an object containing the following keys:
* - authorizationToken: The `token` from the 3rd-party provider
* - provider: The name of the provider (i.e., google-oauth2)
* - redirectUri: The redirect uri (some server-side exchange flows require this)
* If there was an error or the user either canceled the authorization or
* closed the popup window, the promise rejects.
*/
open(options) {
var name = this.get('name'),
url = this.buildUrl(),
redirectUri = this.get('redirectUri'),
responseParams = this.get('responseParams');
return this.get('popup').open(url, responseParams, options).then(function(authData){
var missingResponseParams = [];
responseParams.forEach(function(param){
if (authData[param] === undefined) {
missingResponseParams.push(param);
}
});
if (missingResponseParams.length){
throw new Error("The response from the provider is missing " +
"these required response params: " + missingResponseParams.join(', '));
}
return {
authorizationToken: authData,
provider: name,
redirectUri: redirectUri
};
});
}
});
export default Oauth2Bearer;