-
Notifications
You must be signed in to change notification settings - Fork 1
/
get-bearer-token.js
34 lines (30 loc) · 1.23 KB
/
get-bearer-token.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
const axios = require('axios');
const fs = require('fs');
const jwt = require('njwt');
const uuid = require('uuid');
const clientId = process.argv[2];
const keyPath = process.argv[3];
const lpbHost = process.argv[4];
const oktaTokenAud = process.argv[5];
const claims = { aud: oktaTokenAud, iss: clientId, sub: clientId, jti: uuid.v1() }
const privateKey = fs.readFileSync(keyPath, 'utf8');
const token = jwt.create(claims, privateKey, 'RS256')
token.setExpiration(new Date().getTime() + 60*1000)
console.log('OKTA Token: ', token.compact());
const oktaToken = token.compact();
const bearerEndpoint = `https://${lpbHost}/oauth2/consumer-management/system/v1/token`;
const headers = {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
};
const data = {
'grant_type': 'client_credentials',
'client_assertion_type': 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
'client_assertion': oktaToken,
'scope': 'provider.read provider.write',
};
axios.post(bearerEndpoint, new URLSearchParams(data).toString(), {headers: headers})
.then(response => {
console.log('Bearer token: ', response.data.access_token);
fs.writeFileSync('./bearer.token', response.data.access_token);
});