-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
183 lines (155 loc) · 5.5 KB
/
index.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const jwt = require('jsonwebtoken');
const jwksClient = require('jwks-rsa');
const msal = require('@azure/msal-node');
const fetch = require('node-fetch');
// Before running the sample, you will need to replace the values in the .env file,
const config = {
auth: {
clientId: process.env['CLIENT_ID'],
authority: `https://#.microsoftonline.com/${process.env['TENANT_INFO']}`,
clientSecret: process.env['CLIENT_SECRET'],
}
};
// Create msal application object
const cca = new msal.ConfidentialClientApplication(config);
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
const ssoToken = (req.body && req.body.ssoToken);
try {
const isAuthorized = await validateAccessToken(ssoToken);
if (isAuthorized) {
const oboRequest = {
oboAssertion: ssoToken,
scopes: ['User.Read'],
}
try {
let response = await cca.acquireTokenOnBehalfOf(oboRequest);
if (response.accessToken) {
try {
let apiResponse = await callResourceAPI(response.accessToken, 'https://graph.microsoft.com/v1.0/me');
return context.res = {
status: 200,
body: {
response: apiResponse,
},
headers: {
'Content-Type': 'application/json'
}
};
} catch (error) {
context.log(error);
return context.res = {
status: 401,
body: {
response: "No access token"
}
};
}
}
} catch (error) {
context.log(error);
return context.res = {
status: 500,
body: {
response: JSON.stringify(error),
}
};
}
} else {
context.res = {
status: 401,
body: {
response: "Invalid token"
}
};
}
} catch (error) {
context.log(error);
context.res = {
status: 500,
body: {
response: JSON.stringify(error),
}
};
}
}
/**
* Makes an authorization bearer token request
* to given resource endpoint.
*/
callResourceAPI = async (newTokenValue, resourceURI) => {
let options = {
method: 'GET',
headers: {
'Authorization': `Bearer ${newTokenValue}`,
'Content-type': 'application/json',
},
};
let response = await fetch(resourceURI, options);
let json = await response.json();
return json;
}
/**
* Validates the access token for signature
* and against a predefined set of claims
*/
validateAccessToken = async (accessToken) => {
if (!accessToken || accessToken === "" || accessToken === "undefined") {
console.log('No tokens found');
return false;
}
// we will first decode to get kid parameter in header
let decodedToken;
try {
decodedToken = jwt.decode(accessToken, { complete: true });
} catch (error) {
console.log('Token cannot be decoded');
console.log(error);
return false;
}
// obtains signing keys from discovery endpoint
let keys;
try {
keys = await getSigningKeys(decodedToken.header);
} catch (error) {
console.log('Signing keys cannot be obtained');
console.log(error);
return false;
}
// verify the signature at header section using keys
let verifiedToken;
try {
verifiedToken = jwt.verify(accessToken, keys);
} catch (error) {
console.log('Token cannot be verified');
console.log(error);
return false;
}
/**
* Validates the token against issuer, audience, scope
* and timestamp, though implementation and extent vary. For more information, visit:
* https://docs.microsoft.com/azure/active-directory/develop/access-tokens#validating-tokens
*/
const now = Math.round((new Date()).getTime() / 1000); // in UNIX format
const checkTimestamp = verifiedToken["iat"] <= now && verifiedToken["exp"] >= now ? true : false;
const checkAudience = verifiedToken['aud'] === process.env['CLIENT_ID'] || verifiedToken['aud'] === 'api://' + process.env['CLIENT_ID'] ? true : false;
const checkScope = verifiedToken['scp'] === process.env['EXPECTED_SCOPES'] ? true : false;
const checkIssuer = verifiedToken['iss'].includes(process.env['TENANT_INFO']) ? true : false;
if (checkTimestamp && checkAudience && checkScope && checkIssuer) {
return true;
}
return false;
}
/**
* Fetches signing keys of an access token
* from the authority discovery endpoint
*/
getSigningKeys = async (header) => {
// In single-tenant apps, discovery keys endpoint will be specific to your tenant
const jwksUri = `https://#.microsoftonline.com/${process.env['TENANT_INFO']}/discovery/v2.0/keys`
console.log(jwksUri);
const client = jwksClient({
jwksUri: jwksUri
});
return (await client.getSigningKeyAsync(header.kid)).getPublicKey();
};