-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #318 from lelylan/feature/new-module-api
[new module api] New module public API
- Loading branch information
Showing
16 changed files
with
355 additions
and
325 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,40 @@ | ||
'use strict'; | ||
|
||
const Joi = require('@hapi/joi'); | ||
const Client = require('./lib/client'); | ||
const AuthorizationCode = require('./lib/grants/authorization-code'); | ||
const PasswordOwner = require('./lib/grants/password-owner'); | ||
const ClientCredentials = require('./lib/grants/client-credentials'); | ||
const AccessToken = require('./lib/access-token'); | ||
const { authorizationMethodEnum, bodyFormatEnum, encodingModeEnum } = require('./lib/request-options'); | ||
|
||
// https://tools.ietf.org/html/draft-ietf-oauth-v2-31#appendix-A.1 | ||
const vsCharRegEx = /^[\x20-\x7E]*$/; | ||
|
||
const clientSchema = Joi.object().keys({ | ||
id: Joi.string().pattern(vsCharRegEx).allow(''), | ||
secret: Joi.string().pattern(vsCharRegEx).allow(''), | ||
secretParamName: Joi.string().default('client_secret'), | ||
idParamName: Joi.string().default('client_id'), | ||
}).required(); | ||
|
||
const authSchema = Joi.object().keys({ | ||
tokenHost: Joi.string().required().uri({ scheme: ['http', 'https'] }), | ||
tokenPath: Joi.string().default('/oauth/token'), | ||
revokePath: Joi.string().default('/oauth/revoke'), | ||
authorizeHost: Joi.string().uri({ scheme: ['http', 'https'] }).default(Joi.ref('tokenHost')), | ||
authorizePath: Joi.string().default('/oauth/authorize'), | ||
}).required(); | ||
|
||
const optionsSchema = Joi.object().keys({ | ||
scopeSeparator: Joi.string().default(' '), | ||
credentialsEncodingMode: Joi | ||
.string() | ||
.valid(...Object.values(encodingModeEnum)) | ||
.default(encodingModeEnum.STRICT), | ||
bodyFormat: Joi | ||
.string() | ||
.valid(...Object.values(bodyFormatEnum)) | ||
.default(bodyFormatEnum.FORM), | ||
authorizationMethod: Joi | ||
.string() | ||
.valid(...Object.values(authorizationMethodEnum)) | ||
.default(authorizationMethodEnum.HEADER), | ||
}).default(); | ||
|
||
const moduleOptionsSchema = Joi.object().keys({ | ||
client: clientSchema, | ||
auth: authSchema, | ||
http: Joi.object().unknown(true), | ||
options: optionsSchema, | ||
}); | ||
const Config = require('./lib/config'); | ||
const AuthorizationCodeGrant = require('./lib/grants/authorization-code'); | ||
const PasswordOwnerGrant = require('./lib/grants/password-owner'); | ||
const ClientCredentialsGrant = require('./lib/grants/client-credentials'); | ||
|
||
class AuthorizationCode extends AuthorizationCodeGrant { | ||
constructor(options) { | ||
const config = Config.apply(options); | ||
const client = new Client(config); | ||
|
||
super(config, client); | ||
} | ||
} | ||
|
||
class ClientCredentials extends ClientCredentialsGrant { | ||
constructor(options) { | ||
const config = Config.apply(options); | ||
const client = new Client(config); | ||
|
||
super(config, client); | ||
} | ||
} | ||
|
||
class PasswordOwner extends PasswordOwnerGrant { | ||
constructor(options) { | ||
const config = Config.apply(options); | ||
const client = new Client(config); | ||
|
||
super(config, client); | ||
} | ||
} | ||
|
||
module.exports = { | ||
|
||
/** | ||
* Creates a new simple-oauth2 client with the provided configuration | ||
* @param {Object} opts Module options as defined in schema | ||
* @returns {Object} The simple-oauth2 client | ||
*/ | ||
create(opts = {}) { | ||
const options = Joi.attempt(opts, moduleOptionsSchema, 'Invalid options provided to simple-oauth2'); | ||
const client = new Client(options); | ||
|
||
return Object.freeze({ | ||
accessToken: { | ||
create: AccessToken.factory(options, client), | ||
}, | ||
ownerPassword: new PasswordOwner(options, client), | ||
authorizationCode: new AuthorizationCode(options, client), | ||
clientCredentials: new ClientCredentials(options, client), | ||
}); | ||
}, | ||
PasswordOwner, | ||
ClientCredentials, | ||
AuthorizationCode, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict'; | ||
|
||
const Joi = require('@hapi/joi'); | ||
const { authorizationMethodEnum, bodyFormatEnum, encodingModeEnum } = require('./request-options'); | ||
|
||
// https://tools.ietf.org/html/draft-ietf-oauth-v2-31#appendix-A.1 | ||
const vsCharRegEx = /^[\x20-\x7E]*$/; | ||
|
||
const clientSchema = Joi.object().keys({ | ||
id: Joi.string().pattern(vsCharRegEx).allow(''), | ||
secret: Joi.string().pattern(vsCharRegEx).allow(''), | ||
secretParamName: Joi.string().default('client_secret'), | ||
idParamName: Joi.string().default('client_id'), | ||
}).required(); | ||
|
||
const authSchema = Joi.object().keys({ | ||
tokenHost: Joi.string().required().uri({ scheme: ['http', 'https'] }), | ||
tokenPath: Joi.string().default('/oauth/token'), | ||
revokePath: Joi.string().default('/oauth/revoke'), | ||
authorizeHost: Joi.string().uri({ scheme: ['http', 'https'] }).default(Joi.ref('tokenHost')), | ||
authorizePath: Joi.string().default('/oauth/authorize'), | ||
}).required(); | ||
|
||
const optionsSchema = Joi.object().keys({ | ||
scopeSeparator: Joi.string().default(' '), | ||
credentialsEncodingMode: Joi | ||
.string() | ||
.valid(...Object.values(encodingModeEnum)) | ||
.default(encodingModeEnum.STRICT), | ||
bodyFormat: Joi | ||
.string() | ||
.valid(...Object.values(bodyFormatEnum)) | ||
.default(bodyFormatEnum.FORM), | ||
authorizationMethod: Joi | ||
.string() | ||
.valid(...Object.values(authorizationMethodEnum)) | ||
.default(authorizationMethodEnum.HEADER), | ||
}).default(); | ||
|
||
const ModuleSchema = Joi.object().keys({ | ||
client: clientSchema, | ||
auth: authSchema, | ||
http: Joi.object().unknown(true), | ||
options: optionsSchema, | ||
}); | ||
|
||
const Config = { | ||
apply(options) { | ||
return Joi.attempt(options, ModuleSchema, 'Invalid options provided to simple-oauth2'); | ||
}, | ||
}; | ||
|
||
module.exports = Config; |
Oops, something went wrong.