Skip to content

Commit

Permalink
Merge pull request #87 from lionelB/allow-oauth-override
Browse files Browse the repository at this point in the history
allow overriding oauth base config using options
  • Loading branch information
João Granado authored Nov 3, 2016
2 parents b2a878f + 2a71da7 commit 0f56aed
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 56 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ If you're using `bower` they will be automatically downloaded upon installing th
<script src="<VENDOR_FOLDER>/angular-oauth2/dist/angular-oauth2.min.js"></script>
```

###### 3. Configure `OAuth` (required) and `OAuthToken` (optional):
###### 3. Configure `OAuth` (optional) and `OAuthToken` (optional):

```js
angular.module('myApp', ['angular-oauth2'])
Expand All @@ -56,6 +56,19 @@ angular.module('myApp', ['angular-oauth2'])
}]);
```

You can also configure `OAuth` service in a `.run()`block, in case you retrieve the Oauth server configuration from a ajax request.

```js
angular.module('myApp', ['angular-oauth2'])
.run(['OAuth', function(OAuth) {
OAuth.configure({
baseUrl: 'https://api.website.com',
clientId: 'CLIENT_ID',
clientSecret: 'CLIENT_SECRET' // optional
});
}]);
```

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

```js
Expand Down Expand Up @@ -96,6 +109,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
76 changes: 43 additions & 33 deletions src/providers/oauth-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,20 @@ var requiredKeys = [
*/

function OAuthProvider() {
var config;

/**
* 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 (config) {
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.
config = angular.extend({}, defaults, params);
const config = angular.extend({}, defaults, params);

// Check if all required keys are set.
angular.forEach(requiredKeys, (key) => {
Expand All @@ -56,22 +49,31 @@ function OAuthProvider() {
});

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

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

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

return config;
};

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

/**
* OAuth service.
Expand All @@ -84,11 +86,19 @@ function OAuthProvider() {
* Check if `OAuthProvider` is configured.
*/

constructor() {
if (!config) {
throw new Error('`OAuthProvider` must be configured first.');
}
constructor(config) {
this.config = config;
}

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


/**
* Verifies if the `user` is authenticated or not based on the `token`
Expand All @@ -112,12 +122,12 @@ function OAuthProvider() {

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

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

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

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

return response;
Expand All @@ -147,13 +157,13 @@ function OAuthProvider() {

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

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

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

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

return response;
Expand All @@ -185,13 +195,13 @@ function OAuthProvider() {
var refreshToken = OAuthToken.getRefreshToken();

data = angular.extend({
client_id: config.clientId,
client_id: this.config.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 !== this.config.clientSecret) {
data.client_secret = this.config.clientSecret;
}

data = queryString.stringify(data);
Expand All @@ -202,15 +212,15 @@ function OAuthProvider() {
}
}, options);

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

return response;
});
}
}

return new OAuth();
return new OAuth(this.defaultConfig);
};

this.$get.$inject = ['$http', 'OAuthToken'];
Expand Down
Loading

0 comments on commit 0f56aed

Please sign in to comment.