-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSample.js
69 lines (57 loc) · 2.01 KB
/
Sample.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
const { URLSearchParams } = require('url');
var appsettings = require('./appsettings.json');
var assert = require('assert');
var axios = require('axios');
// Step 1: get needed variables
var resource = appsettings.Resource;
var clientId = appsettings.ClientId;
var clientSecret = appsettings.ClientSecret;
var tenantId = appsettings.TenantId;
var apiVersion = appsettings.ApiVersion;
var app = async function (request1, response) {
// Step 2: get the authentication endpoint from the discovery URL
var wellknown_information = await axios({
url: resource + '/identity/.well-known/openid-configuration',
method: 'GET',
headers: {
Accept: 'application/json',
'Accept-Encoding': 'gzip',
},
})
authority = new URL(wellknown_information.data.token_endpoint);
// Step 3: use the client ID and Secret to get the needed bearer token
var body = new URLSearchParams({
grant_type: 'client_credentials',
client_id: clientId,
client_secret: clientSecret,
});
var token_information = await axios({
url: authority.href,
method: 'POST',
data: body.toString(),
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
});
var token = token_information.data.access_token;
// Step 4: test token by calling the base tenant endpoint
var tenant = await axios({
url: resource + '/api/' + apiVersion + '/Tenants/' + tenantId,
method: 'GET',
headers: {
Authorization: 'bearer ' + token,
'Content-type': 'application/json',
},
});
// test it by making sure we got a valid http status code
assert.equal(tenant.status, 200);
};
var toRun = function () {
//This server is hosted over HTTP. This is not secure and should not be used beyond local testing.
http.createServer(app).listen(8080);
};
process.argv = process.argv.slice(2);
if (require.main === module) {
app.apply(process.argv);
}
module.exports = app;