Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorganize internal module structure #331

Merged
merged 2 commits into from
Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Next
### Maintainance
- Documentation updates for persistent access token refresh
- Internal module reorganization

## 4.1.0
### Improvements
Expand Down
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions lib/access-token/index.js → lib/access-token.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())}`;
}
Expand All @@ -46,7 +46,7 @@ module.exports = class AuthorizationCode {
* @return {Promise<AccessToken>}
*/
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,7 +21,7 @@ module.exports = class ClientCredentials {
* @return {Promise<AccessToken>}
*/
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);
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const HEADER_ENCODING_FORMAT = 'base64';

const encodingModeEnum = {
const credentialsEncodingModeEnum = {
STRICT: 'strict',
LOOSE: 'loose',
};
Expand Down Expand Up @@ -31,7 +31,7 @@ function getCredentialsString(clientID, clientSecret) {
return `${clientID}:${clientSecret}`;
}

class Encoding {
class CredentialsEncoding {
constructor(encodingMode) {
this.encodingMode = encodingMode;
}
Expand All @@ -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);
Expand All @@ -56,6 +56,6 @@ class Encoding {
}

module.exports = {
Encoding,
encodingModeEnum,
CredentialsEncoding,
credentialsEncodingModeEnum,
};
13 changes: 13 additions & 0 deletions lib/client/index.js
Original file line number Diff line number Diff line change
@@ -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,
};
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -75,5 +75,4 @@ module.exports = {
RequestOptions,
authorizationMethodEnum,
bodyFormatEnum,
encodingModeEnum,
};
6 changes: 3 additions & 3 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -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]*$/;
Expand All @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions lib/grant-params.js → lib/grant-type-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,7 +23,7 @@ module.exports = class ResourceOwnerPassword {
* @return {Promise<AccessToken>}
*/
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);
Expand Down
2 changes: 1 addition & 1 deletion test/access-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.