-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtoken.js
More file actions
84 lines (80 loc) · 2.24 KB
/
token.js
File metadata and controls
84 lines (80 loc) · 2.24 KB
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
/*!
* oauth2-token-manager-for-nodeJS v1.0.0
* Copyright 2020 Orange
* Licensed under MIT (https://github.com/orange-opensource/oauth2-token-manager-for-nodeJS/blob/master/LICENSE)
*/
/**
* Create a new issuer to manage access_token
*/
const { Issuer, TokenSet } = require('openid-client')
let tokenSet = null
let expiresInTime = 1000 // Set default expiration time to 1 second
let client = null
let issuer
// if discover endpoint exist, use it
if(process.env.DISCOVER_URL) {
Issuer.discover(process.env.DISCOVER_URL) // => Promise
.then(function (issuerDisco) {
issuer = issuerDisco
clientConfig()
},(error) => {
// Error when retrieving token
console.log(error)
});
} else {
// if no discover endpoint use TOKEN_ENDPOINT value
issuer = new Issuer({
issuer: 'odiIssuer',
token_endpoint: process.env.TOKEN_ENDPOINT
})
clientConfig()
}
/**
* Configure the client with our client_id and client_secret
* CLIENT_ID and CLIENT_SECRET need to be set in .env file
*/
function clientConfig() {
client = new issuer.Client({
client_id:process.env.CLIENT_ID,
client_secret:process.env.CLIENT_SECRET,
token_endpoint_auth_method:'client_secret_post'
})
}
/**
* get current access token if it's still valid or ask a new one if needed
*/
function getAccessToken() {
return new Promise((resolve, reject) => {
// check if token exist and not expired
if(tokenSet && (tokenSet.expires_at - Date.now() / 1000 >= expiresInTime)) {
resolve(tokenSet.access_token)
} else {
// No token or token expired then request a new token
let body = {grant_type:'client_credentials'}
if(process.env.SCOPES) {
body = {
grant_type:'client_credentials',
scope:process.env.SCOPES}
}
client.grant(body)
.then((token) => {
// Save tokenSet in memory and return the access_token
tokenSet = token
resolve(tokenSet.access_token)
},(error) => {
// Error when retrieving token
reject(error)
})
}
})
}
/**
* drop accesstoken, usefull in case of 401(invalid token)
*/
function cleanAccessToken() {
tokenSet = null
}
module.exports = {
getAccessToken: getAccessToken,
cleanAccessToken: cleanAccessToken
}