-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
70 lines (56 loc) · 1.78 KB
/
Copy pathapp.js
File metadata and controls
70 lines (56 loc) · 1.78 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
const express = require('express');
const msal = require('@azure/msal-node');
const jwt = require('jsonwebtoken');
const jwks = require('jwks-rsa');
const config = {
auth: {
authority: "https://login.microsoftonline.com/<tenant-id>",
clientId: "<client-id>",
clientSecret: "<client-secret>",
scopes: ["user.read"]
}
};
const validateJwt = (req, res, next) => {
const token = req.headers.authorization.split(" ")[1];
if (token) {
const validationOptions = {
audience: config.auth.clientId,
issuer: config.auth.authority + "/v2.0"
}
jwt.verify(token, getSigningKeys, validationOptions, (err, payload) => {
if (err) {
return res.sendStatus(403);
}
next();
});
} else {
res.sendStatus(401);
}
};
const getSigningKeys = (header, callback) => {
const jwksClient = jwks({
jwksUri: 'https://login.microsoftonline.com/common/discovery/keys'
});
jwksClient.getSigningKey(header.kid, function (err, key) {
const signingKey = key.publicKey || key.rsaPublicKey;
callback(null, signingKey);
});
};
const cca = new msal.ConfidentialClientApplication(config);
const app = express();
const server_port = 3000;
app.get('/auth', validateJwt, (req, res) => {
const authHeader = req.headers.authorization.split(" ")[1];
const oboRequest = {
oboAssertion: authHeader,
scopes: config.scopes
};
cca.acquireTokenOnBehalfOf(oboRequest).then((data) => {
res.status(200).send(data.accessToken);
}).catch((error) => {
res.status(500).send(error);
});
});
app.listen(server_port, () => {
console.log(`app listening at http://localhost:${server_port}/auth`);
});