From 4f6564c4fd9c927bfd774868428bf6a5004778cd Mon Sep 17 00:00:00 2001 From: Jonathan Samines Date: Sat, 13 Jun 2020 19:40:17 -0600 Subject: [PATCH 1/2] [reorganize-internal-module-structure] Reorganize module internally for better cohesion between modules --- API.md | 2 +- CHANGELOG.md | 1 + index.js | 14 +++++++------- .../token-parser.js => access-token-parser.js} | 0 lib/{access-token/index.js => access-token.js} | 6 +++--- ...on-code.js => authorization-code-grant-type.js} | 8 ++++---- ...entials.js => client-credentials-grant-type.js} | 6 +++--- lib/{ => client}/client.js | 0 .../encoding.js => client/credentials-encoding.js} | 10 +++++----- lib/client/index.js | 13 +++++++++++++ .../index.js => client/request-options.js} | 5 ++--- lib/config.js | 6 +++--- lib/{grant-params.js => grant-type-params.js} | 6 +++--- ...rd.js => resource-owner-password-grant-type.js} | 6 +++--- test/access-token.js | 2 +- 15 files changed, 49 insertions(+), 36 deletions(-) rename lib/{access-token/token-parser.js => access-token-parser.js} (100%) rename lib/{access-token/index.js => access-token.js} (91%) rename lib/{grants/authorization-code.js => authorization-code-grant-type.js} (88%) rename lib/{grants/client-credentials.js => client-credentials-grant-type.js} (83%) rename lib/{ => client}/client.js (100%) rename lib/{request-options/encoding.js => client/credentials-encoding.js} (88%) create mode 100644 lib/client/index.js rename lib/{request-options/index.js => client/request-options.js} (92%) rename lib/{grant-params.js => grant-type-params.js} (82%) rename lib/{grants/resource-owner-password.js => resource-owner-password-grant-type.js} (85%) diff --git a/API.md b/API.md index c0c233a9..f7bd8b23 100644 --- a/API.md +++ b/API.md @@ -35,7 +35,7 @@ Simple OAuth2 grant classes accept an object with the following params. ### URL resolution URL paths are relatively resolved to their corresponding host property using the [Node WHATWG URL](https://nodejs.org/dist/latest-v12.x/docs/api/url.html#url_constructor_new_url_input_base) resolution algorithm. -## Grants +## Grant Types ### new AuthorizationCode(options) This submodule provides support for the OAuth2 [Authorization Code](https://oauth.net/2/grant-types/authorization-code/) grant type. diff --git a/CHANGELOG.md b/CHANGELOG.md index ec5598c5..f9e0f078 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Next ### Maintainance - Documentation updates for persistent access token refresh +- Internal module reorganization ## 4.1.0 ### Improvements diff --git a/index.js b/index.js index 9e1dcca3..51838787 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,12 @@ 'use strict'; -const Client = require('./lib/client'); const Config = require('./lib/config'); -const AuthorizationCodeGrant = require('./lib/grants/authorization-code'); -const ResourceOwnerPasswordGrant = require('./lib/grants/resource-owner-password'); -const ClientCredentialsGrant = require('./lib/grants/client-credentials'); +const { Client } = require('./lib/client'); +const AuthorizationCodeGrantType = require('./lib/authorization-code-grant-type'); +const ResourceOwnerPasswordGrantType = require('./lib/resource-owner-password-grant-type'); +const ClientCredentialsGrantType = require('./lib/client-credentials-grant-type'); -class AuthorizationCode extends AuthorizationCodeGrant { +class AuthorizationCode extends AuthorizationCodeGrantType { constructor(options) { const config = Config.apply(options); const client = new Client(config); @@ -15,7 +15,7 @@ class AuthorizationCode extends AuthorizationCodeGrant { } } -class ClientCredentials extends ClientCredentialsGrant { +class ClientCredentials extends ClientCredentialsGrantType { constructor(options) { const config = Config.apply(options); const client = new Client(config); @@ -24,7 +24,7 @@ class ClientCredentials extends ClientCredentialsGrant { } } -class ResourceOwnerPassword extends ResourceOwnerPasswordGrant { +class ResourceOwnerPassword extends ResourceOwnerPasswordGrantType { constructor(options) { const config = Config.apply(options); const client = new Client(config); diff --git a/lib/access-token/token-parser.js b/lib/access-token-parser.js similarity index 100% rename from lib/access-token/token-parser.js rename to lib/access-token-parser.js diff --git a/lib/access-token/index.js b/lib/access-token.js similarity index 91% rename from lib/access-token/index.js rename to lib/access-token.js index 26777c6e..5a32540d 100644 --- a/lib/access-token/index.js +++ b/lib/access-token.js @@ -1,8 +1,8 @@ 'use strict'; const Hoek = require('@hapi/hoek'); -const GrantParams = require('../grant-params'); -const { parseToken } = require('./token-parser'); +const GrantTypeParams = require('./grant-type-params'); +const { parseToken } = require('./access-token-parser'); const ACCESS_TOKEN_PROPERTY_NAME = 'access_token'; const REFRESH_TOKEN_PROPERTY_NAME = 'refresh_token'; @@ -44,7 +44,7 @@ module.exports = class AccessToken { refresh_token: this.token.refresh_token, }; - const parameters = GrantParams.forGrant(REFRESH_TOKEN_PROPERTY_NAME, this.#config.options, refreshParams); + const parameters = GrantTypeParams.forGrantType(REFRESH_TOKEN_PROPERTY_NAME, this.#config.options, refreshParams); const response = await this.#client.request(this.#config.auth.tokenPath, parameters.toObject()); return new AccessToken(this.#config, this.#client, response); diff --git a/lib/grants/authorization-code.js b/lib/authorization-code-grant-type.js similarity index 88% rename from lib/grants/authorization-code.js rename to lib/authorization-code-grant-type.js index 278fbcbe..5f7a75ae 100644 --- a/lib/grants/authorization-code.js +++ b/lib/authorization-code-grant-type.js @@ -2,8 +2,8 @@ const { URL } = require('url'); const querystring = require('querystring'); -const AccessToken = require('../access-token'); -const GrantParams = require('../grant-params'); +const AccessToken = require('./access-token'); +const GrantTypeParams = require('./grant-type-params'); module.exports = class AuthorizationCode { #config = null; @@ -31,7 +31,7 @@ module.exports = class AuthorizationCode { }; const url = new URL(this.#config.auth.authorizePath, this.#config.auth.authorizeHost); - const parameters = new GrantParams(this.#config.options, baseParams, params); + const parameters = new GrantTypeParams(this.#config.options, baseParams, params); return `${url}?${querystring.stringify(parameters.toObject())}`; } @@ -46,7 +46,7 @@ module.exports = class AuthorizationCode { * @return {Promise} */ async getToken(params, httpOptions) { - const parameters = GrantParams.forGrant('authorization_code', this.#config.options, params); + const parameters = GrantTypeParams.forGrantType('authorization_code', this.#config.options, params); const response = await this.#client.request(this.#config.auth.tokenPath, parameters.toObject(), httpOptions); return this.createToken(response); diff --git a/lib/grants/client-credentials.js b/lib/client-credentials-grant-type.js similarity index 83% rename from lib/grants/client-credentials.js rename to lib/client-credentials-grant-type.js index 4dc1779f..635573d1 100644 --- a/lib/grants/client-credentials.js +++ b/lib/client-credentials-grant-type.js @@ -1,7 +1,7 @@ 'use strict'; -const GrantParams = require('../grant-params'); -const AccessToken = require('../access-token'); +const AccessToken = require('./access-token'); +const GrantTypeParams = require('./grant-type-params'); module.exports = class ClientCredentials { #config = null; @@ -21,7 +21,7 @@ module.exports = class ClientCredentials { * @return {Promise} */ async getToken(params, httpOptions) { - const parameters = GrantParams.forGrant('client_credentials', this.#config.options, params); + const parameters = GrantTypeParams.forGrantType('client_credentials', this.#config.options, params); const response = await this.#client.request(this.#config.auth.tokenPath, parameters.toObject(), httpOptions); return this.createToken(response); diff --git a/lib/client.js b/lib/client/client.js similarity index 100% rename from lib/client.js rename to lib/client/client.js diff --git a/lib/request-options/encoding.js b/lib/client/credentials-encoding.js similarity index 88% rename from lib/request-options/encoding.js rename to lib/client/credentials-encoding.js index 9dcada0f..4ee7e00b 100644 --- a/lib/request-options/encoding.js +++ b/lib/client/credentials-encoding.js @@ -2,7 +2,7 @@ const HEADER_ENCODING_FORMAT = 'base64'; -const encodingModeEnum = { +const credentialsEncodingModeEnum = { STRICT: 'strict', LOOSE: 'loose', }; @@ -31,7 +31,7 @@ function getCredentialsString(clientID, clientSecret) { return `${clientID}:${clientSecret}`; } -class Encoding { +class CredentialsEncoding { constructor(encodingMode) { this.encodingMode = encodingMode; } @@ -45,7 +45,7 @@ class Encoding { getAuthorizationHeaderToken(clientID, clientSecret) { let encodedCredentials; - if (this.encodingMode === encodingModeEnum.STRICT) { + if (this.encodingMode === credentialsEncodingModeEnum.STRICT) { encodedCredentials = getCredentialsString(useFormURLEncode(clientID), useFormURLEncode(clientSecret)); } else { encodedCredentials = getCredentialsString(clientID, clientSecret); @@ -56,6 +56,6 @@ class Encoding { } module.exports = { - Encoding, - encodingModeEnum, + CredentialsEncoding, + credentialsEncodingModeEnum, }; diff --git a/lib/client/index.js b/lib/client/index.js new file mode 100644 index 00000000..a9e8cf8d --- /dev/null +++ b/lib/client/index.js @@ -0,0 +1,13 @@ +'use strict'; + +const Client = require('./client'); +const { credentialsEncodingModeEnum } = require('./credentials-encoding'); +const { RequestOptions, authorizationMethodEnum, bodyFormatEnum } = require('./request-options'); + +module.exports = { + Client, + RequestOptions, + credentialsEncodingModeEnum, + authorizationMethodEnum, + bodyFormatEnum, +}; diff --git a/lib/request-options/index.js b/lib/client/request-options.js similarity index 92% rename from lib/request-options/index.js rename to lib/client/request-options.js index e7012228..64bf7eba 100644 --- a/lib/request-options/index.js +++ b/lib/client/request-options.js @@ -3,7 +3,7 @@ const Hoek = require('@hapi/hoek'); const querystring = require('querystring'); const debug = require('debug')('simple-oauth2:request-options'); -const { Encoding, encodingModeEnum } = require('./encoding'); +const { CredentialsEncoding } = require('./credentials-encoding'); const JSON_CONTENT_TYPE = 'application/json'; const FORM_CONTENT_TYPE = 'application/x-www-form-urlencoded'; @@ -38,7 +38,7 @@ class RequestOptions { const requestOptions = getDefaultRequestOptions(); if (this.#config.options.authorizationMethod === authorizationMethodEnum.HEADER) { - const encoding = new Encoding(this.#config.options.credentialsEncodingMode); + const encoding = new CredentialsEncoding(this.#config.options.credentialsEncodingMode); const credentials = encoding.getAuthorizationHeaderToken(this.#config.client.id, this.#config.client.secret); debug('Using header authentication. Authorization header set to %s', credentials); @@ -75,5 +75,4 @@ module.exports = { RequestOptions, authorizationMethodEnum, bodyFormatEnum, - encodingModeEnum, }; diff --git a/lib/config.js b/lib/config.js index a5a8fcf6..34135275 100644 --- a/lib/config.js +++ b/lib/config.js @@ -1,7 +1,7 @@ 'use strict'; const Joi = require('@hapi/joi'); -const { authorizationMethodEnum, bodyFormatEnum, encodingModeEnum } = require('./request-options'); +const { authorizationMethodEnum, bodyFormatEnum, credentialsEncodingModeEnum } = require('./client'); // https://tools.ietf.org/html/draft-ietf-oauth-v2-31#appendix-A.1 const vsCharRegEx = /^[\x20-\x7E]*$/; @@ -25,8 +25,8 @@ const optionsSchema = Joi.object().keys({ scopeSeparator: Joi.string().default(' '), credentialsEncodingMode: Joi .string() - .valid(...Object.values(encodingModeEnum)) - .default(encodingModeEnum.STRICT), + .valid(...Object.values(credentialsEncodingModeEnum)) + .default(credentialsEncodingModeEnum.STRICT), bodyFormat: Joi .string() .valid(...Object.values(bodyFormatEnum)) diff --git a/lib/grant-params.js b/lib/grant-type-params.js similarity index 82% rename from lib/grant-params.js rename to lib/grant-type-params.js index 5a463138..4dcb5ff0 100644 --- a/lib/grant-params.js +++ b/lib/grant-type-params.js @@ -16,17 +16,17 @@ function getScopeParam(scope, scopeSeparator) { }; } -module.exports = class GrantParams { +module.exports = class GrantTypeParams { #params = null; #baseParams = null; #options = null; - static forGrant(grantType, options, params) { + static forGrantType(grantType, options, params) { const baseParams = { grant_type: grantType, }; - return new GrantParams(options, baseParams, params); + return new GrantTypeParams(options, baseParams, params); } constructor(options, baseParams, params) { diff --git a/lib/grants/resource-owner-password.js b/lib/resource-owner-password-grant-type.js similarity index 85% rename from lib/grants/resource-owner-password.js rename to lib/resource-owner-password-grant-type.js index 7162c85a..df6f4bed 100644 --- a/lib/grants/resource-owner-password.js +++ b/lib/resource-owner-password-grant-type.js @@ -1,7 +1,7 @@ 'use strict'; -const GrantParams = require('../grant-params'); -const AccessToken = require('../access-token'); +const AccessToken = require('./access-token'); +const GrantTypeParams = require('./grant-type-params'); module.exports = class ResourceOwnerPassword { #config = null; @@ -23,7 +23,7 @@ module.exports = class ResourceOwnerPassword { * @return {Promise} */ async getToken(params, httpOptions) { - const parameters = GrantParams.forGrant('password', this.#config.options, params); + const parameters = GrantTypeParams.forGrantType('password', this.#config.options, params); const response = await this.#client.request(this.#config.auth.tokenPath, parameters.toObject(), httpOptions); return this.createToken(response); diff --git a/test/access-token.js b/test/access-token.js index cdfddece..6aa10e80 100644 --- a/test/access-token.js +++ b/test/access-token.js @@ -11,7 +11,7 @@ const { } = require('date-fns'); const AccessToken = require('../lib/access-token'); -const Client = require('../lib/client'); +const { Client } = require('../lib/client'); const { has, hasIn } = require('./_property'); const { createModuleConfigWithDefaults: createModuleConfig } = require('./_module-config'); const { createAuthorizationServer } = require('./_authorization-server-mock'); From 72fca2fd806a2088908fdac0d6f94668d8a34a06 Mon Sep 17 00:00:00 2001 From: Jonathan Samines Date: Sat, 13 Jun 2020 19:41:41 -0600 Subject: [PATCH 2/2] [reorganize-internal-module-structure] Rename test modules for grant-types --- test/{authorization-code.js => authorization-code-grant-type.js} | 0 test/{client-credentials.js => client-credentials-grant-type.js} | 0 test/{password-owner.js => resource-owner-password-grant-type.js} | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename test/{authorization-code.js => authorization-code-grant-type.js} (100%) rename test/{client-credentials.js => client-credentials-grant-type.js} (100%) rename test/{password-owner.js => resource-owner-password-grant-type.js} (100%) diff --git a/test/authorization-code.js b/test/authorization-code-grant-type.js similarity index 100% rename from test/authorization-code.js rename to test/authorization-code-grant-type.js diff --git a/test/client-credentials.js b/test/client-credentials-grant-type.js similarity index 100% rename from test/client-credentials.js rename to test/client-credentials-grant-type.js diff --git a/test/password-owner.js b/test/resource-owner-password-grant-type.js similarity index 100% rename from test/password-owner.js rename to test/resource-owner-password-grant-type.js