Skip to content

Commit 4f6564c

Browse files
[reorganize-internal-module-structure] Reorganize module internally for better cohesion between modules
1 parent efb15e2 commit 4f6564c

15 files changed

+49
-36
lines changed

API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Simple OAuth2 grant classes accept an object with the following params.
3535
### URL resolution
3636
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.
3737

38-
## Grants
38+
## Grant Types
3939
### new AuthorizationCode(options)
4040
This submodule provides support for the OAuth2 [Authorization Code](https://oauth.net/2/grant-types/authorization-code/) grant type.
4141

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Next
44
### Maintainance
55
- Documentation updates for persistent access token refresh
6+
- Internal module reorganization
67

78
## 4.1.0
89
### Improvements

index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
22

3-
const Client = require('./lib/client');
43
const Config = require('./lib/config');
5-
const AuthorizationCodeGrant = require('./lib/grants/authorization-code');
6-
const ResourceOwnerPasswordGrant = require('./lib/grants/resource-owner-password');
7-
const ClientCredentialsGrant = require('./lib/grants/client-credentials');
4+
const { Client } = require('./lib/client');
5+
const AuthorizationCodeGrantType = require('./lib/authorization-code-grant-type');
6+
const ResourceOwnerPasswordGrantType = require('./lib/resource-owner-password-grant-type');
7+
const ClientCredentialsGrantType = require('./lib/client-credentials-grant-type');
88

9-
class AuthorizationCode extends AuthorizationCodeGrant {
9+
class AuthorizationCode extends AuthorizationCodeGrantType {
1010
constructor(options) {
1111
const config = Config.apply(options);
1212
const client = new Client(config);
@@ -15,7 +15,7 @@ class AuthorizationCode extends AuthorizationCodeGrant {
1515
}
1616
}
1717

18-
class ClientCredentials extends ClientCredentialsGrant {
18+
class ClientCredentials extends ClientCredentialsGrantType {
1919
constructor(options) {
2020
const config = Config.apply(options);
2121
const client = new Client(config);
@@ -24,7 +24,7 @@ class ClientCredentials extends ClientCredentialsGrant {
2424
}
2525
}
2626

27-
class ResourceOwnerPassword extends ResourceOwnerPasswordGrant {
27+
class ResourceOwnerPassword extends ResourceOwnerPasswordGrantType {
2828
constructor(options) {
2929
const config = Config.apply(options);
3030
const client = new Client(config);
File renamed without changes.

lib/access-token/index.js renamed to lib/access-token.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

33
const Hoek = require('@hapi/hoek');
4-
const GrantParams = require('../grant-params');
5-
const { parseToken } = require('./token-parser');
4+
const GrantTypeParams = require('./grant-type-params');
5+
const { parseToken } = require('./access-token-parser');
66

77
const ACCESS_TOKEN_PROPERTY_NAME = 'access_token';
88
const REFRESH_TOKEN_PROPERTY_NAME = 'refresh_token';
@@ -44,7 +44,7 @@ module.exports = class AccessToken {
4444
refresh_token: this.token.refresh_token,
4545
};
4646

47-
const parameters = GrantParams.forGrant(REFRESH_TOKEN_PROPERTY_NAME, this.#config.options, refreshParams);
47+
const parameters = GrantTypeParams.forGrantType(REFRESH_TOKEN_PROPERTY_NAME, this.#config.options, refreshParams);
4848
const response = await this.#client.request(this.#config.auth.tokenPath, parameters.toObject());
4949

5050
return new AccessToken(this.#config, this.#client, response);

lib/grants/authorization-code.js renamed to lib/authorization-code-grant-type.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
const { URL } = require('url');
44
const querystring = require('querystring');
5-
const AccessToken = require('../access-token');
6-
const GrantParams = require('../grant-params');
5+
const AccessToken = require('./access-token');
6+
const GrantTypeParams = require('./grant-type-params');
77

88
module.exports = class AuthorizationCode {
99
#config = null;
@@ -31,7 +31,7 @@ module.exports = class AuthorizationCode {
3131
};
3232

3333
const url = new URL(this.#config.auth.authorizePath, this.#config.auth.authorizeHost);
34-
const parameters = new GrantParams(this.#config.options, baseParams, params);
34+
const parameters = new GrantTypeParams(this.#config.options, baseParams, params);
3535

3636
return `${url}?${querystring.stringify(parameters.toObject())}`;
3737
}
@@ -46,7 +46,7 @@ module.exports = class AuthorizationCode {
4646
* @return {Promise<AccessToken>}
4747
*/
4848
async getToken(params, httpOptions) {
49-
const parameters = GrantParams.forGrant('authorization_code', this.#config.options, params);
49+
const parameters = GrantTypeParams.forGrantType('authorization_code', this.#config.options, params);
5050
const response = await this.#client.request(this.#config.auth.tokenPath, parameters.toObject(), httpOptions);
5151

5252
return this.createToken(response);

lib/grants/client-credentials.js renamed to lib/client-credentials-grant-type.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

3-
const GrantParams = require('../grant-params');
4-
const AccessToken = require('../access-token');
3+
const AccessToken = require('./access-token');
4+
const GrantTypeParams = require('./grant-type-params');
55

66
module.exports = class ClientCredentials {
77
#config = null;
@@ -21,7 +21,7 @@ module.exports = class ClientCredentials {
2121
* @return {Promise<AccessToken>}
2222
*/
2323
async getToken(params, httpOptions) {
24-
const parameters = GrantParams.forGrant('client_credentials', this.#config.options, params);
24+
const parameters = GrantTypeParams.forGrantType('client_credentials', this.#config.options, params);
2525
const response = await this.#client.request(this.#config.auth.tokenPath, parameters.toObject(), httpOptions);
2626

2727
return this.createToken(response);
File renamed without changes.

lib/request-options/encoding.js renamed to lib/client/credentials-encoding.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const HEADER_ENCODING_FORMAT = 'base64';
44

5-
const encodingModeEnum = {
5+
const credentialsEncodingModeEnum = {
66
STRICT: 'strict',
77
LOOSE: 'loose',
88
};
@@ -31,7 +31,7 @@ function getCredentialsString(clientID, clientSecret) {
3131
return `${clientID}:${clientSecret}`;
3232
}
3333

34-
class Encoding {
34+
class CredentialsEncoding {
3535
constructor(encodingMode) {
3636
this.encodingMode = encodingMode;
3737
}
@@ -45,7 +45,7 @@ class Encoding {
4545
getAuthorizationHeaderToken(clientID, clientSecret) {
4646
let encodedCredentials;
4747

48-
if (this.encodingMode === encodingModeEnum.STRICT) {
48+
if (this.encodingMode === credentialsEncodingModeEnum.STRICT) {
4949
encodedCredentials = getCredentialsString(useFormURLEncode(clientID), useFormURLEncode(clientSecret));
5050
} else {
5151
encodedCredentials = getCredentialsString(clientID, clientSecret);
@@ -56,6 +56,6 @@ class Encoding {
5656
}
5757

5858
module.exports = {
59-
Encoding,
60-
encodingModeEnum,
59+
CredentialsEncoding,
60+
credentialsEncodingModeEnum,
6161
};

lib/client/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
const Client = require('./client');
4+
const { credentialsEncodingModeEnum } = require('./credentials-encoding');
5+
const { RequestOptions, authorizationMethodEnum, bodyFormatEnum } = require('./request-options');
6+
7+
module.exports = {
8+
Client,
9+
RequestOptions,
10+
credentialsEncodingModeEnum,
11+
authorizationMethodEnum,
12+
bodyFormatEnum,
13+
};

0 commit comments

Comments
 (0)