forked from lelylan/simple-oauth2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (55 loc) · 1.91 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
60
'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) {
let moduleOptions = Object.assign({}, options || {});
moduleOptions = joi.attempt(
moduleOptions,
optionsSchema,
'Invalid options provided to simple-oauth2.'
);
return {
authorizationCode: authCodeModule(moduleOptions),
ownerPassword: passwordModule(moduleOptions),
clientCredentials: clientCredentialsModule(moduleOptions),
accessToken: accessTokenModule(moduleOptions),
};
},
};