forked from zamd/introspection-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetupResourceServer.js
55 lines (49 loc) · 1.58 KB
/
setupResourceServer.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
/**
Update resource server with verificationKey
**/
const jwt = require('jsonwebtoken');
const request = require('superagent-bluebird-promise');
const signingKey = require('./signingKey.json');
require('dotenv').config();
// used for local testing, never do this in a production env
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
let accessToken;
// authenticate
request
.post(`${process.env.AUTH0_TENANT}/oauth/token`)
.send({
grant_type: 'client_credentials',
client_id: process.env.AUTH0_MGMTAPI_CLIENTID,
client_secret: process.env.AUTH0_MGMTAPI_CLIENTSECRET,
audience: process.env.AUTH0_MGMTAPI_IDENTIFIER
}).then(res => {
accessToken = res.body.access_token;
if (!accessToken) {
throw new Error('Unable to obtain access token');
}
// now we have a token, patch the resource server
return request
.get(`${process.env.AUTH0_MGMTAPI_IDENTIFIER}resource-servers`)
.set('Authorization', 'Bearer ' + accessToken)
.send();
}).then(res => {
let selectedResourceServer = res.body.filter(function(resourceServer) {
return resourceServer.identifier === process.env.RESOURCE_SERVER_IDENTIFIER;
}).reduce(function (acc, obj) {
return obj;
});
return request
.patch(`${process.env.AUTH0_MGMTAPI_IDENTIFIER}resource-servers/${selectedResourceServer.id}`)
.set('Authorization', 'Bearer ' + accessToken)
.send({
verificationKeys: [{kid: '123', pem: signingKey.cert}]
});
}).then(res => {
if (res.statusCode !== 200) {
throw new Error(res);
}
console.log('Resource Server updated');
})
.catch(err => {
console.log(err.message);
});