forked from integr8ly/tutorial-web-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_middleware.js
79 lines (71 loc) · 1.98 KB
/
server_middleware.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
const axios = require('axios');
const https = require('https');
const { OPENSHIFT_HOST } = process.env;
const insecureAgent = new https.Agent({
rejectUnauthorized: false
});
exports.fetchOpenshiftUser = (req, res, next) => {
// Not much we can do if the openshift host is not set. This probably
// means we are running in a mock environment
if (!OPENSHIFT_HOST) {
return next();
}
const token = req.get('X-Forwarded-Access-Token');
if (!token) {
return res.sendStatus(403);
}
return axios({
httpsAgent: insecureAgent,
url: `https://${OPENSHIFT_HOST}/oapi/v1/users/~`,
headers: {
authorization: `Bearer ${token}`,
Accept: 'application/json'
}
})
.then(response => {
const {
kind,
metadata: { name }
} = response.data;
if (kind === 'User') {
req.body.openshiftUser = name;
return next();
}
return res.sendStatus(403);
})
.catch(err => next(err));
};
exports.fetchMiddlewareVersions = (req, res) => {
// Not much we can do if the openshift host is not set. This probably
// means we are running in a mock environment
if (!OPENSHIFT_HOST) {
console.log('No openshift host found, returning false.');
return false;
}
const token = req.get('X-Forwarded-Access-Token');
if (!token) {
console.log('No token found, returning 403 error.');
return res.sendStatus(403);
}
return axios({
httpsAgent: insecureAgent,
url: `https://${OPENSHIFT_HOST}//console/project/webapp/browse/secrets/manifest/`,
headers: {
authorization: `Bearer ${token}`,
Accept: 'application/json'
}
})
.then(response => {
console.log(`Manifest value is:${response.data}`);
// const {
// kind,
// metadata: { name }
// } = response.data;
// if (kind === 'User') {
// req.body.openshiftUser = name;
// return next();
// }
return res.sendStatus(403);
})
.catch(error => console.log(error));
};