-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (75 loc) · 2.13 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'use strict';
/**
* @name express-oauth
* @description minimal oauth implementation for express
* @author lally elias <lallyelias87@mail.com>
* @since 0.1.0
* @version 0.1.0
* @license MIT
* @example
*
* const { app } = require('@lykmapipo/express-oauth');
*
* ...
*
* app.start()
*
*/
/* dependencies */
const path = require('path');
const _ = require('lodash');
const app = require('@lykmapipo/express-common');
/* local constants */
const libPath = path.join(__dirname, 'lib');
/* declarations */
const pkg = require(path.join(__dirname, 'package.json'));
const fields = [
'name',
'description',
'version',
'license',
'homepage',
'repository',
'bugs',
'sandbox',
'contributors'
];
const info = _.merge({}, _.pick(pkg, fields));
/* ensure api version */
process.env.API_VERSION = (process.env.API_VERSION || info.version);
/* lifetimes */
process.env.REFRESH_TOKEN_LIFETIME =
(process.env.REFRESH_TOKEN_LIFETIME || (60 * 60 * 24 * 14)); //2 weeks
process.env.ACCESS_TOKEN_LIFETIME =
(process.env.ACCESS_TOKEN_LIFETIME || (60 * 60)); // 1 hour
process.env.AUTHORIZATION_CODE_LIFETIME =
(process.env.AUTHORIZATION_CODE_LIFETIME || (60 * 5)); // 5 minutes
/* import models */
const Client = require(path.join(libPath, 'client.model'));
const User = require(path.join(libPath, 'user.model'));
const Token = require(path.join(libPath, 'token.model'));
/* import routers */
const clientRouter = require(path.join(libPath, 'client.router'));
const userRouter = require(path.join(libPath, 'user.router'));
const tokenRouter = require(path.join(libPath, 'token.router'));
/* export package(module) info */
exports.info = info;
/* export models */
exports.Client = Client;
exports.User = User;
exports.Token = Token;
/* export routers */
exports.userRouter = userRouter;
exports.clientRouter = clientRouter;
exports.tokenRouter = tokenRouter;
/* export app */
Object.defineProperty(exports, 'app', {
get() {
/* TODO bind oauth middlewares authenticate, token, authorize */
/* bind oauth routes */
app.mount(clientRouter);
app.mount(userRouter);
app.mount(tokenRouter);
return app;
}
});