-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
81 lines (67 loc) · 2.74 KB
/
server.js
File metadata and controls
81 lines (67 loc) · 2.74 KB
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
const Koa = require('koa');
const Router = require('@koa/router');
const jwt = require('jsonwebtoken')
const bodyParser = require('koa-bodyparser');
var basicAuth = require('basic-auth')
const authRoutes = require('./routes/auth')
const staticRoutes = require('./routes/static')
const applicationRoutes = require('./routes/apps')
const configAsCodeRoutes = require('./routes/config-as-code');
const config = require('./config');
const app = new Koa();
app.use(bodyParser());
const publicRoutes = new Router()
const publicRoutesToUse = [authRoutes, staticRoutes]
publicRoutesToUse.forEach(router => {
publicRoutes.use(router.routes())
publicRoutes.use(router.allowedMethods())
})
const privateRoutes = new Router()
// Populate args based on global parameters
privateRoutes.use(async(ctx, next) => {
try {
const encoded = ctx.cookies.get(config.jwtCookieName) || ctx.headers['x-jwt-token']
const decoded = jwt.decode(encoded, config.jwtSecret)
if (decoded && decoded.harnessAccountId) {
ctx.state.user = decoded
console.log(decoded)
}
} catch (ex) {
console.log(ex)
}
const harnessConfig = {}
const jwtUser = ctx.state.user || {}
let basicCreds = basicAuth(ctx) || {}
if (basicCreds.name && basicCreds.pass === '') {
basicCreds.apiKey = basicCreds.name
basicCreds.name = undefined
}
harnessConfig.username = basicCreds.name || ctx.query.harnessUsername || jwtUser.harnessUsername || ctx.headers['x-harness-username'] || process.env.HARNESS_USERNAME
harnessConfig.password = basicCreds.pass || ctx.query.harnessPassword || jwtUser.harnessPassword || ctx.headers['x-harness-password'] || process.env.HARNESS_PASSWORD
harnessConfig.accountId = ctx.query.harnessAccountId || jwtUser.harnessAccountId || ctx.headers['x-harness-account-id'] || process.env.HARNESS_ACCOUNT_ID
harnessConfig.apiKey = basicCreds.apiKey || ctx.query.harnessApiKey || jwtUser.harnessApiKey || ctx.headers['x-harness-api-key'] || process.env.HARNESS_API_KEY
harnessConfig.managerUrl = 'https://app.harness.io'
harnessConfig.globals = {
harnessAccountId: harnessConfig.accountId,
harnessApiKey: harnessConfig.apiKey,
managerUrl: harnessConfig.managerUrl
}
ctx.state.harness = harnessConfig
return next()
})
const privateRoutesToUse = [applicationRoutes, configAsCodeRoutes]
privateRoutesToUse.forEach(router => {
privateRoutes.use(router.routes())
privateRoutes.use(router.allowedMethods())
})
app
.use(publicRoutes.routes())
.use(publicRoutes.allowedMethods())
.use(privateRoutes.routes())
.use(privateRoutes.allowedMethods())
module.exports = {
start: (port) => {
app.listen(port);
return app
}
}