forked from lelylan/simple-oauth2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (54 loc) · 1.86 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use strict';
const Joi = require('joi');
const authCodeModule = require('./lib/client/auth-code');
const passwordModule = require('./lib/client/password');
const accessTokenModule = require('./lib/client/access-token');
const clientCredentialsModule = require('./lib/client/client');
const optionsSchema = Joi
.object()
.keys({
client: Joi.object().keys({
id: Joi.string().required(),
secret: Joi.string().required(),
secretParamName: Joi.string().default('client_secret'),
idParamName: Joi.string().default('client_id'),
}).required(),
auth: Joi.object().keys({
tokenHost: Joi.string().required().uri(['http', 'https']),
tokenPath: Joi.string().default('/oauth/token'),
revokePath: Joi.string().default('/oauth/revoke'),
authorizeHost: Joi.string().default(Joi.ref('tokenHost')),
authorizePath: Joi.string().default('/oauth/authorize'),
}).required(),
http: Joi.object().keys({
headers: Joi.object().default({
Accept: 'application/json',
}),
}).default().unknown(true),
options: Joi.object().keys({
bodyFormat: Joi.any().valid('form', 'json').default('form'),
useBasicAuthorizationHeader: Joi.boolean().default(true),
useBodyAuth: Joi.boolean().default(true),
}).default(),
});
module.exports = {
/**
* Creates a new simple-oauth2 client
* with the passed configuration
*
* @param {Object} options Module options as defined in schema
*/
create(options) {
const moduleOptions = Joi.attempt(
options || {},
optionsSchema,
'Invalid options provided to simple-oauth2'
);
return {
authorizationCode: authCodeModule(moduleOptions),
ownerPassword: passwordModule(moduleOptions),
clientCredentials: clientCredentialsModule(moduleOptions),
accessToken: accessTokenModule(moduleOptions),
};
},
};