From 1d8f881cfdcaf462421d2f07208bdcace81327bf Mon Sep 17 00:00:00 2001 From: "U-NEXDOM\\s.moreau" Date: Wed, 2 Aug 2017 10:37:48 +0200 Subject: [PATCH 1/5] Add option to send client credentials (client_id and client_secret) in the header --- README.md | 6 ++- dist/angular-oauth2.js | 90 ++++++++++++++++++++++++++++-------------- 2 files changed, 65 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 758e055..4899232 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,8 @@ OAuthProvider.configure({ clientId: null, clientSecret: null, grantPath: '/oauth2/token', - revokePath: '/oauth2/revoke' + revokePath: '/oauth2/revoke', + clientCredentials: 'body' }); ``` @@ -117,7 +118,8 @@ OAuth.configure({ clientId: null, clientSecret: null, grantPath: '/oauth2/token', - revokePath: '/oauth2/revoke' + revokePath: '/oauth2/revoke', + clientCredentials: 'body' }); ``` diff --git a/dist/angular-oauth2.js b/dist/angular-oauth2.js index ab9f75a..d0a59cf 100644 --- a/dist/angular-oauth2.js +++ b/dist/angular-oauth2.js @@ -95,6 +95,32 @@ } return config; }; + var addCredentialsInHeader = function addCredentialsInHeader(config, options) { + credentials = config.clientId + ":"; + if (null !== config.clientSecret) { + credentials += config.clientSecret; + } + credentials = "Basic " + btoa(credentials); + + options = angular.extend({ + headers: { + Authorization: credentials, + "Content-Type": "application/x-www-form-urlencoded" + } + }, options); + + return options; + }; + var addCredentialsInBody = function addCredentialsInBody(config, data) { + data = angular.extend({ + client_id: config.clientId + }, data); + if (null !== config.clientSecret) { + data.client_secret = config.clientSecret; + } + + return data; + }; this.configure = function(params) { _this.defaultConfig = sanitizeConfigParams(params); }; @@ -117,20 +143,22 @@ }, { key: "getAccessToken", value: function getAccessToken(data, options) { + if ("header" === this.config.clientCredentials) { + options = addCredentialsInHeader(this.config, options); + } else { + data = addCredentialsInBody(this.config, data); + options = angular.extend({ + headers: { + Authorization: undefined, + "Content-Type": "application/x-www-form-urlencoded" + } + }, options); + } + data = angular.extend({ - client_id: this.config.clientId, grant_type: "password" }, data); - if (null !== this.config.clientSecret) { - data.client_secret = this.config.clientSecret; - } data = queryString.stringify(data); - options = angular.extend({ - headers: { - Authorization: undefined, - "Content-Type": "application/x-www-form-urlencoded" - } - }, options); return $http.post("" + this.config.baseUrl + this.config.grantPath, data, options).then(function(response) { OAuthToken.setToken(response.data); return response; @@ -139,21 +167,23 @@ }, { key: "getRefreshToken", value: function getRefreshToken(data, options) { + if ("header" === this.config.clientCredentials) { + options = addCredentialsInHeader(this.config, options); + } else { + data = addCredentialsInBody(this.config, data); + options = angular.extend({ + headers: { + Authorization: undefined, + "Content-Type": "application/x-www-form-urlencoded" + } + }, options); + } + data = angular.extend({ - client_id: this.config.clientId, grant_type: "refresh_token", refresh_token: OAuthToken.getRefreshToken() }, data); - if (null !== this.config.clientSecret) { - data.client_secret = this.config.clientSecret; - } data = queryString.stringify(data); - options = angular.extend({ - headers: { - Authorization: undefined, - "Content-Type": "application/x-www-form-urlencoded" - } - }, options); return $http.post("" + this.config.baseUrl + this.config.grantPath, data, options).then(function(response) { OAuthToken.setToken(response.data); return response; @@ -162,21 +192,23 @@ }, { key: "revokeToken", value: function revokeToken(data, options) { + if ("header" === this.config.clientCredentials) { + options = addCredentialsInHeader(this.config, options); + } else { + data = addCredentialsInBody(this.config, data); + options = angular.extend({ + headers: { + "Content-Type": "application/x-www-form-urlencoded" + } + }, options); + } + var refreshToken = OAuthToken.getRefreshToken(); data = angular.extend({ - client_id: this.config.clientId, token: refreshToken ? refreshToken : OAuthToken.getAccessToken(), token_type_hint: refreshToken ? "refresh_token" : "access_token" }, data); - if (null !== this.config.clientSecret) { - data.client_secret = this.config.clientSecret; - } data = queryString.stringify(data); - options = angular.extend({ - headers: { - "Content-Type": "application/x-www-form-urlencoded" - } - }, options); return $http.post("" + this.config.baseUrl + this.config.revokePath, data, options).then(function(response) { OAuthToken.removeToken(); return response; From 7ad5e3f33d2af83e3ff31451d5a6ff747d662322 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Moreau?= Date: Sat, 9 Dec 2017 17:57:53 +0100 Subject: [PATCH 2/5] Add option to send client credentials (client_id and client_secret) in the header --- src/providers/oauth-provider.js | 115 ++++++++++++++++++++++---------- 1 file changed, 80 insertions(+), 35 deletions(-) diff --git a/src/providers/oauth-provider.js b/src/providers/oauth-provider.js index 2de0045..51698c6 100644 --- a/src/providers/oauth-provider.js +++ b/src/providers/oauth-provider.js @@ -65,6 +65,50 @@ function OAuthProvider() { return config; }; + + /** + * @private + * add credentials in header + * @param {object} config - Object containing the clientId and the clientSecret + * @param {object} options - Optional configuration. + * @return {object} Modified optional configuration. + */ + const addCredentialsInHeader = (config, options) => { + // Create the credentials string + credentials = config.clientId + ":"; + if (null !== config.clientSecret) { + credentials += config.clientSecret; + } + credentials = "Basic " + btoa(credentials); + + // Add the credentials in the header + options = angular.extend({ + headers: { + Authorization: credentials, + "Content-Type": "application/x-www-form-urlencoded" + } + }, options); + + return options; + }; + + /** + * @private + * add credentials in body + * @param {object} config - Object containing the clientId and the clientSecret + * @param {object} data - Request content, e.g., `username` and `password`. + * @return {object} Modified request content. + */ + const addCredentialsInBody = (config, data) => { + data = angular.extend({ + client_id: config.clientId + }, data); + if (null !== config.clientSecret) { + data.client_secret = config.clientSecret; + } + + return data; +}; /** * Configure. @@ -121,24 +165,24 @@ function OAuthProvider() { */ getAccessToken(data, options) { + if ("header" === this.config.clientCredentials) { + options = addCredentialsInHeader(this.config, options); + } else { + data = addCredentialsInBody(this.config, data); + options = angular.extend({ + headers: { + Authorization: undefined, + "Content-Type": "application/x-www-form-urlencoded" + } + }, options); + } + data = angular.extend({ - client_id: this.config.clientId, grant_type: 'password' }, data); - if (null !== this.config.clientSecret) { - data.client_secret = this.config.clientSecret; - } - data = queryString.stringify(data); - options = angular.extend({ - headers: { - 'Authorization': undefined, - 'Content-Type': 'application/x-www-form-urlencoded' - } - }, options); - return $http.post(`${this.config.baseUrl}${this.config.grantPath}`, data, options).then((response) => { OAuthToken.setToken(response.data); @@ -156,25 +200,25 @@ function OAuthProvider() { */ getRefreshToken(data, options) { + if ("header" === this.config.clientCredentials) { + options = addCredentialsInHeader(this.config, options); + } else { + data = addCredentialsInBody(this.config, data); + options = angular.extend({ + headers: { + Authorization: undefined, + "Content-Type": "application/x-www-form-urlencoded" + } + }, options); + } + data = angular.extend({ - client_id: this.config.clientId, grant_type: 'refresh_token', refresh_token: OAuthToken.getRefreshToken(), }, data); - if (null !== this.config.clientSecret) { - data.client_secret = this.config.clientSecret; - } - data = queryString.stringify(data); - options = angular.extend({ - headers: { - 'Authorization': undefined, - 'Content-Type': 'application/x-www-form-urlencoded' - } - }, options); - return $http.post(`${this.config.baseUrl}${this.config.grantPath}`, data, options).then((response) => { OAuthToken.setToken(response.data); @@ -192,26 +236,27 @@ function OAuthProvider() { */ revokeToken(data, options) { + if ("header" === this.config.clientCredentials) { + options = addCredentialsInHeader(this.config, options); + } else { + data = addCredentialsInBody(this.config, data); + options = angular.extend({ + headers: { + Authorization: undefined, + "Content-Type": "application/x-www-form-urlencoded" + } + }, options); + } + var refreshToken = OAuthToken.getRefreshToken(); data = angular.extend({ - client_id: this.config.clientId, token: refreshToken ? refreshToken : OAuthToken.getAccessToken(), token_type_hint: refreshToken ? 'refresh_token' : 'access_token' }, data); - if (null !== this.config.clientSecret) { - data.client_secret = this.config.clientSecret; - } - data = queryString.stringify(data); - options = angular.extend({ - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - } - }, options); - return $http.post(`${this.config.baseUrl}${this.config.revokePath}`, data, options).then((response) => { OAuthToken.removeToken(); From c5328495cb80a5c3e41fa13c8a848b67525a357f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Moreau?= Date: Sat, 9 Dec 2017 18:03:02 +0100 Subject: [PATCH 3/5] Revert "Add option to send client credentials (client_id and client_secret) in the header" This reverts commit 1d8f881cfdcaf462421d2f07208bdcace81327bf. --- README.md | 6 +-- dist/angular-oauth2.js | 90 ++++++++++++++---------------------------- 2 files changed, 31 insertions(+), 65 deletions(-) diff --git a/README.md b/README.md index 4899232..758e055 100644 --- a/README.md +++ b/README.md @@ -103,8 +103,7 @@ OAuthProvider.configure({ clientId: null, clientSecret: null, grantPath: '/oauth2/token', - revokePath: '/oauth2/revoke', - clientCredentials: 'body' + revokePath: '/oauth2/revoke' }); ``` @@ -118,8 +117,7 @@ OAuth.configure({ clientId: null, clientSecret: null, grantPath: '/oauth2/token', - revokePath: '/oauth2/revoke', - clientCredentials: 'body' + revokePath: '/oauth2/revoke' }); ``` diff --git a/dist/angular-oauth2.js b/dist/angular-oauth2.js index d0a59cf..ab9f75a 100644 --- a/dist/angular-oauth2.js +++ b/dist/angular-oauth2.js @@ -95,32 +95,6 @@ } return config; }; - var addCredentialsInHeader = function addCredentialsInHeader(config, options) { - credentials = config.clientId + ":"; - if (null !== config.clientSecret) { - credentials += config.clientSecret; - } - credentials = "Basic " + btoa(credentials); - - options = angular.extend({ - headers: { - Authorization: credentials, - "Content-Type": "application/x-www-form-urlencoded" - } - }, options); - - return options; - }; - var addCredentialsInBody = function addCredentialsInBody(config, data) { - data = angular.extend({ - client_id: config.clientId - }, data); - if (null !== config.clientSecret) { - data.client_secret = config.clientSecret; - } - - return data; - }; this.configure = function(params) { _this.defaultConfig = sanitizeConfigParams(params); }; @@ -143,22 +117,20 @@ }, { key: "getAccessToken", value: function getAccessToken(data, options) { - if ("header" === this.config.clientCredentials) { - options = addCredentialsInHeader(this.config, options); - } else { - data = addCredentialsInBody(this.config, data); - options = angular.extend({ - headers: { - Authorization: undefined, - "Content-Type": "application/x-www-form-urlencoded" - } - }, options); - } - data = angular.extend({ + client_id: this.config.clientId, grant_type: "password" }, data); + if (null !== this.config.clientSecret) { + data.client_secret = this.config.clientSecret; + } data = queryString.stringify(data); + options = angular.extend({ + headers: { + Authorization: undefined, + "Content-Type": "application/x-www-form-urlencoded" + } + }, options); return $http.post("" + this.config.baseUrl + this.config.grantPath, data, options).then(function(response) { OAuthToken.setToken(response.data); return response; @@ -167,23 +139,21 @@ }, { key: "getRefreshToken", value: function getRefreshToken(data, options) { - if ("header" === this.config.clientCredentials) { - options = addCredentialsInHeader(this.config, options); - } else { - data = addCredentialsInBody(this.config, data); - options = angular.extend({ - headers: { - Authorization: undefined, - "Content-Type": "application/x-www-form-urlencoded" - } - }, options); - } - data = angular.extend({ + client_id: this.config.clientId, grant_type: "refresh_token", refresh_token: OAuthToken.getRefreshToken() }, data); + if (null !== this.config.clientSecret) { + data.client_secret = this.config.clientSecret; + } data = queryString.stringify(data); + options = angular.extend({ + headers: { + Authorization: undefined, + "Content-Type": "application/x-www-form-urlencoded" + } + }, options); return $http.post("" + this.config.baseUrl + this.config.grantPath, data, options).then(function(response) { OAuthToken.setToken(response.data); return response; @@ -192,23 +162,21 @@ }, { key: "revokeToken", value: function revokeToken(data, options) { - if ("header" === this.config.clientCredentials) { - options = addCredentialsInHeader(this.config, options); - } else { - data = addCredentialsInBody(this.config, data); - options = angular.extend({ - headers: { - "Content-Type": "application/x-www-form-urlencoded" - } - }, options); - } - var refreshToken = OAuthToken.getRefreshToken(); data = angular.extend({ + client_id: this.config.clientId, token: refreshToken ? refreshToken : OAuthToken.getAccessToken(), token_type_hint: refreshToken ? "refresh_token" : "access_token" }, data); + if (null !== this.config.clientSecret) { + data.client_secret = this.config.clientSecret; + } data = queryString.stringify(data); + options = angular.extend({ + headers: { + "Content-Type": "application/x-www-form-urlencoded" + } + }, options); return $http.post("" + this.config.baseUrl + this.config.revokePath, data, options).then(function(response) { OAuthToken.removeToken(); return response; From 715a259462f92eba8cf15a25b7f54f7c1902c8d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Moreau?= Date: Sat, 9 Dec 2017 18:05:13 +0100 Subject: [PATCH 4/5] Modify the README file to add the information about 'clientCredentials' option --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 758e055..4899232 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,8 @@ OAuthProvider.configure({ clientId: null, clientSecret: null, grantPath: '/oauth2/token', - revokePath: '/oauth2/revoke' + revokePath: '/oauth2/revoke', + clientCredentials: 'body' }); ``` @@ -117,7 +118,8 @@ OAuth.configure({ clientId: null, clientSecret: null, grantPath: '/oauth2/token', - revokePath: '/oauth2/revoke' + revokePath: '/oauth2/revoke', + clientCredentials: 'body' }); ``` From 362003514cd890533ea3bbfbaf5fb310670873b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Moreau?= Date: Wed, 28 Mar 2018 17:30:34 +0200 Subject: [PATCH 5/5] Fixing JSHint warnings --- src/providers/oauth-provider.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/providers/oauth-provider.js b/src/providers/oauth-provider.js index 51698c6..1a6c134 100644 --- a/src/providers/oauth-provider.js +++ b/src/providers/oauth-provider.js @@ -75,17 +75,17 @@ function OAuthProvider() { */ const addCredentialsInHeader = (config, options) => { // Create the credentials string - credentials = config.clientId + ":"; + let credentials = config.clientId + ':'; if (null !== config.clientSecret) { credentials += config.clientSecret; } - credentials = "Basic " + btoa(credentials); + credentials = 'Basic ' + btoa(credentials); // Add the credentials in the header options = angular.extend({ headers: { Authorization: credentials, - "Content-Type": "application/x-www-form-urlencoded" + 'Content-Type': 'application/x-www-form-urlencoded' } }, options); @@ -165,14 +165,14 @@ function OAuthProvider() { */ getAccessToken(data, options) { - if ("header" === this.config.clientCredentials) { + if ('header' === this.config.clientCredentials) { options = addCredentialsInHeader(this.config, options); } else { data = addCredentialsInBody(this.config, data); options = angular.extend({ headers: { Authorization: undefined, - "Content-Type": "application/x-www-form-urlencoded" + 'Content-Type': 'application/x-www-form-urlencoded' } }, options); } @@ -200,14 +200,14 @@ function OAuthProvider() { */ getRefreshToken(data, options) { - if ("header" === this.config.clientCredentials) { + if ('header' === this.config.clientCredentials) { options = addCredentialsInHeader(this.config, options); } else { data = addCredentialsInBody(this.config, data); options = angular.extend({ headers: { Authorization: undefined, - "Content-Type": "application/x-www-form-urlencoded" + 'Content-Type': 'application/x-www-form-urlencoded' } }, options); } @@ -236,14 +236,14 @@ function OAuthProvider() { */ revokeToken(data, options) { - if ("header" === this.config.clientCredentials) { + if ('header' === this.config.clientCredentials) { options = addCredentialsInHeader(this.config, options); } else { data = addCredentialsInBody(this.config, data); options = angular.extend({ headers: { Authorization: undefined, - "Content-Type": "application/x-www-form-urlencoded" + 'Content-Type': 'application/x-www-form-urlencoded' } }, options); }