Skip to content

Commit

Permalink
allow OAuth configuration at run time
Browse files Browse the repository at this point in the history
  • Loading branch information
lionelB committed Jul 17, 2016
1 parent 9152090 commit 304aeb0
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 120 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ angular.module('myApp', ['angular-oauth2'])
}]);
```

ps: You can also configure service at runtime, in case you retrieve oauth server configuration from a ajax request.

###### 4. Catch `OAuth` errors and do something with them (optional):

```js
Expand Down Expand Up @@ -84,6 +86,18 @@ OAuthProvider.configure({

#### OAuth

Update configuration defaults

```js
OAuth.configure({
baseUrl: null,
clientId: null,
clientSecret: null,
grantPath: '/oauth2/token',
revokePath: '/oauth2/revoke'
});

```
Check authentication status:

```js
Expand Down
93 changes: 50 additions & 43 deletions src/providers/oauth-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,46 +29,51 @@ function OAuthProvider() {
var defaultConfig;

/**
* Configure.
*
* @param {object} params - An `object` of params to extend.
* @private
* sanitize configuration parameters
* @param {object} an `object` of params to sanitize
* @return {object} an sanitize version of the params
*/

this.configure = function(params) {
// Can only be configured once.
if (defaultConfig) {
throw new Error('Already configured.');
}

// Check if is an `object`.
const sanitizeConfigParams = (params) => {
if (!(params instanceof Object)) {
throw new TypeError('Invalid argument: `config` must be an `Object`.');
}

// Extend default configuration.
defaultConfig = angular.extend({}, defaults, params);
const config = angular.extend({}, defaults, params);

// Check if all required keys are set.
angular.forEach(requiredKeys, (key) => {
if (!defaultConfig[key]) {
if (!config[key]) {
throw new Error(`Missing parameter: ${key}.`);
}
});

// Remove `baseUrl` trailing slash.
if('/' === defaultConfig.baseUrl.substr(-1)) {
defaultConfig.baseUrl = defaultConfig.baseUrl.slice(0, -1);
if ('/' === config.baseUrl.substr(-1)) {
config.baseUrl = config.baseUrl.slice(0, -1);
}

// Add `grantPath` facing slash.
if('/' !== defaultConfig.grantPath[0]) {
defaultConfig.grantPath = `/${defaultConfig.grantPath}`;
if ('/' !== config.grantPath[0]) {
config.grantPath = `/${config.grantPath}`;
}

// Add `revokePath` facing slash.
if('/' !== defaultConfig.revokePath[0]) {
defaultConfig.revokePath = `/${defaultConfig.revokePath}`;
if ('/' !== config.revokePath[0]) {
config.revokePath = `/${config.revokePath}`;
}

return config;
};

/**
* Configure.
*
* @param {object} params - An `object` of params to extend.
*/
this.configure = function(params) {
defaultConfig = sanitizeConfigParams(params);
return defaultConfig;
};

Expand All @@ -88,6 +93,17 @@ function OAuthProvider() {
throw new Error('`OAuthProvider` must be configured first.');
}
}

/**
* Configure OAuth service during runtime
*
* @param {Object} params - An object of params to extend
*/
configure(params) {
defaultConfig = sanitizeConfigParams(params);
return defaultConfig;
}


/**
* Verifies if the `user` is authenticated or not based on the `token`
Expand All @@ -109,17 +125,14 @@ function OAuthProvider() {
* @return {promise} A response promise.
*/

getAccessToken(data, options, config) {
//Override default Oauth config
config = angular.extend({}, defaultConfig, config);

getAccessToken(data, options) {
data = angular.extend({
client_id: config.clientId,
client_id: defaultConfig.clientId,
grant_type: 'password'
}, data);

if (null !== config.clientSecret) {
data.client_secret = config.clientSecret;
if (null !== defaultConfig.clientSecret) {
data.client_secret = defaultConfig.clientSecret;
}

data = queryString.stringify(data);
Expand All @@ -131,7 +144,7 @@ function OAuthProvider() {
}
}, options);

return $http.post(`${config.baseUrl}${config.grantPath}`, data, options).then((response) => {
return $http.post(`${defaultConfig.baseUrl}${defaultConfig.grantPath}`, data, options).then((response) => {
OAuthToken.setToken(response.data);

return response;
Expand All @@ -147,18 +160,15 @@ function OAuthProvider() {
* @return {promise} A response promise.
*/

getRefreshToken(data, options, config) {
//Override default Oauth config
config = angular.extend({}, defaultConfig, config);

getRefreshToken(data, options) {
data = angular.extend({
client_id: config.clientId,
client_id: defaultConfig.clientId,
grant_type: 'refresh_token',
refresh_token: OAuthToken.getRefreshToken(),
}, data);

if (null !== config.clientSecret) {
data.client_secret = config.clientSecret;
if (null !== defaultConfig.clientSecret) {
data.client_secret = defaultConfig.clientSecret;
}

data = queryString.stringify(data);
Expand All @@ -170,7 +180,7 @@ function OAuthProvider() {
}
}, options);

return $http.post(`${config.baseUrl}${config.grantPath}`, data, options).then((response) => {
return $http.post(`${defaultConfig.baseUrl}${defaultConfig.grantPath}`, data, options).then((response) => {
OAuthToken.setToken(response.data);

return response;
Expand All @@ -186,20 +196,17 @@ function OAuthProvider() {
* @return {promise} A response promise.
*/

revokeToken(data, options, config) {
//Override default Oauth config
config = angular.extend(defaultConfig, config);

revokeToken(data, options) {
var refreshToken = OAuthToken.getRefreshToken();

data = angular.extend({
client_id: config.clientId,
client_id: defaultConfig.clientId,
token: refreshToken ? refreshToken : OAuthToken.getAccessToken(),
token_type_hint: refreshToken ? 'refresh_token' : 'access_token'
}, data);

if (null !== config.clientSecret) {
data.client_secret = config.clientSecret;
if (null !== defaultConfig.clientSecret) {
data.client_secret = defaultConfig.clientSecret;
}

data = queryString.stringify(data);
Expand All @@ -210,7 +217,7 @@ function OAuthProvider() {
}
}, options);

return $http.post(`${config.baseUrl}${config.revokePath}`, data, options).then((response) => {
return $http.post(`${defaultConfig.baseUrl}${defaultConfig.revokePath}`, data, options).then((response) => {
OAuthToken.removeToken();

return response;
Expand Down
Loading

0 comments on commit 304aeb0

Please sign in to comment.