-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
76 lines (56 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
//Modules
import express from 'express';
import bunyan from 'bunyan';
import bodyParser from 'body-parser';
import fetch from 'node-fetch';
//Load values from .env file
import dotenv from 'dotenv';
dotenv.config();
const app = express();
const log = bunyan.createLogger({ name: 'Client Credentials Flow' });
app.use(express.static('public'));
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
res.render('index');
});
//Step 1: Get the access token
app.get('/get/the/token', (req, res) => {
const Token_Endpoint = `https://login.microsoftonline.com/${process.env.TENANT_ID}/oauth2/v2.0/token`;
const Grant_Type = 'client_credentials';
const Client_Id = process.env.CLIENT_ID;
const Client_Secret = process.env.CLIENT_SECRET;
const Scope = 'https://graph.microsoft.com/.default';
let body = `grant_type=${Grant_Type}&client_id=${Client_Id}&client_secret=${encodeURIComponent(Client_Secret)}&scope=${encodeURIComponent(Scope)}`;
log.info(`Endpoint: ${Token_Endpoint}`);
log.info(`Body: ${body}`);
fetch(Token_Endpoint, {
method: 'POST',
body: body,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(async response => {
let json = await response.json();
res.render('access-token', { token: JSON.stringify(json, undefined, 2) }); //you shouldn't share the access token with the client-side
}).catch(error => {
log.error(error.message);
});
});
//Step 2: Call the protected API
app.post('/call/ms/graph', (req, res) => {
let access_token = JSON.parse(req.body.token).access_token;
const Microsoft_Graph_Endpoint = 'https://graph.microsoft.com/beta';
const Acction_That_I_Have_Access_Because_Of_My_Scope = '/users';
//Call Microsoft Graph with your access token
fetch(`${Microsoft_Graph_Endpoint}${Acction_That_I_Have_Access_Because_Of_My_Scope}`, {
headers: {
'Authorization': `Bearer ${access_token}`
}
}).then(async response => {
let json = await response.json();
res.render('calling-ms-graph', { response: JSON.stringify(json, undefined, 2) });
});
});
app.listen(8000);