-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
85 lines (77 loc) · 2.25 KB
/
server.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
"use strict";
const express = require("express")
const bodyParser = require('body-parser')
const expressValidator = require('express-validator')
const github = require('octonode')
const logger = require('./modules/logger')
const port = 3000
let app = express();
// Setup various middlewares
app.use(bodyParser.urlencoded({ extended: false}))
app.use(bodyParser.json())
app.use(expressValidator({
errorFormatter: function(param, msg, value) {
var namespace = param.split('.')
, root = namespace.shift()
, formParam = root
while(namespace.length) {
formParam += '[' + namespace.shift() + ']'
}
return {
param : formParam,
msg : msg,
value: value
}
}
}))
// Setup application routes
app.get('/', (req, res) => {
logger.info('ping')
res.sendStatus(200)
})
app.post('/authenticate', (req, res) => {
let postData = req.body
if (postData === null || postData === 'undefined') {
let responseJson = {
'apiVersion': 'authentication.k8s.io/v1beta1',
'kind': 'TokenReview',
'status': {
'Authenticated': false
}
}
res.status(401).send(responseJson)
} else {
let token = postData.spec.token
let client = github.client(token)
client.get('/user', {}, function (err, status, body, headers) {
if (err) {
logger.error('could not retrieve user with the token passed in.', err)
let responseJson = {
'apiVersion': 'authentication.k8s.io/v1beta1',
'kind': 'TokenReview',
'status': {
'Authenticated': false
}
}
res.status(401).send(responseJson)
} else {
logger.info('authenticated OK with github for user: ' + body.login)
let responseJson = {
'apiVersion': 'authentication.k8s.io/v1beta1',
'kind': 'TokenReview',
'status': {
'Authenticated': true,
'User': {
'Username': body.login,
'UID': body.login
// Potentially in the future get user team membership from github, and pass into Groups[] here...
}
}
}
res.status(200).send(responseJson)
}
})
}
})
app.listen(port);
logger.info('Application started and listening on port ' + port)