-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
59 lines (52 loc) · 1.99 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
const apiRouter = require('./lib/auth.router');
const passportService = require('./lib/services/passport.service');
const passport = require('koa-passport');
const debug = require('debug')('oauth-plugin');
const mongoose = require('mongoose');
const jwt = require('koa-jwt');
const views = require('koa-views');
const Promise = require('bluebird');
const JWT = Promise.promisifyAll(require('jsonwebtoken'));
const authServiceFunc = require('./lib/services/auth.service');
function init() {
}
function middleware(app, plugin, generalConfig) {
debug('Loading oauth-plugin');
const connection = mongoose.createConnection(`${generalConfig.mongoUri}`);
const AuthService = authServiceFunc(plugin, connection);
app.use(views(`${__dirname}/lib/views`, { extension: 'ejs' }));
passportService(plugin, connection);
app.use(passport.initialize());
app.use(passport.session());
if (plugin.config.jwt.active) {
debug('JWT active');
app.use(jwt({
secret: plugin.config.jwt.secret,
passthrough: plugin.config.jwt.passthrough,
isRevoked: AuthService.checkRevokedToken
}));
app.use(async (ctx, next) => {
if (ctx.headers && ctx.headers.authentication) {
debug('Authenticated microservice with token: ', ctx.headers.authentication);
try {
const service = await JWT.verify(ctx.headers.authentication, plugin.config.jwt.secret);
if (service) {
ctx.state.microservice = {
id: service.id,
name: service.name,
url: service.url,
};
}
} catch (err) {
debug('Token invalid', err);
}
}
await next();
});
}
app.use(apiRouter(plugin, connection, generalConfig).middleware());
}
module.exports = {
middleware,
init,
};