2023-01-09 11:58:34 +00:00
|
|
|
import moment from 'moment';
|
|
|
|
import fetch from 'node-fetch';
|
2023-01-09 12:35:05 +00:00
|
|
|
/**
|
|
|
|
* The Endpoint where the Access Token is requested
|
|
|
|
*/
|
|
|
|
const _accessTokenEndpoint = 'https://idm.staging.floriday.io/oauth2/ausmw6b47z1BnlHkw0h7/v1/token';
|
2023-01-09 11:58:34 +00:00
|
|
|
|
2023-01-09 12:35:05 +00:00
|
|
|
/**
|
|
|
|
* Gets the Access Token from the client config table
|
|
|
|
*
|
|
|
|
* @param {sequelize.models} models
|
|
|
|
* @returns {Date} tokenExpirationDate formated as YYYY-MM-DD HH:mm:ss
|
|
|
|
*/
|
2023-01-09 11:58:34 +00:00
|
|
|
async function getClientToken(models) {
|
|
|
|
const clientConfigData = await models.clientConfig.findAll();
|
|
|
|
|
|
|
|
const now = moment().format('YYYY-MM-DD HH:mm:ss');
|
|
|
|
const tokenExpirationDate = clientConfigData[0].tokenExpiration;
|
|
|
|
|
2023-01-09 12:35:05 +00:00
|
|
|
if (clientConfigData[0].tokenExpiration == null || moment(now).isAfter(tokenExpirationDate)) {
|
2023-01-09 11:58:34 +00:00
|
|
|
let clientId = clientConfigData[0].clientId;
|
|
|
|
let clientSecret = clientConfigData[0].clientSecret;
|
|
|
|
|
|
|
|
const tokenRequest = await fetch(_accessTokenEndpoint, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
},
|
|
|
|
body: `grant_type=client_credentials&client_id=${clientId}&client_secret=${clientSecret}&scope=role:app catalog:read supply:read organization:read network:read`,
|
|
|
|
});
|
|
|
|
|
|
|
|
const tokenResponse = await tokenRequest.json();
|
|
|
|
|
|
|
|
if (tokenRequest.status === 200) {
|
|
|
|
console.log('Token request successful');
|
|
|
|
} else {
|
|
|
|
throw new Error(
|
|
|
|
`Token request failed with status ${tokenRequest.status}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const accessToken = tokenResponse.access_token;
|
|
|
|
let now = moment().format('YYYY-MM-DD HH:mm:ss');
|
|
|
|
|
|
|
|
let tokenExpirationDate = moment(now)
|
|
|
|
.add(tokenResponse.expires_in, 's')
|
|
|
|
.format('YYYY-MM-DD HH:mm:ss');
|
|
|
|
|
|
|
|
updateClientConfig(
|
|
|
|
models,
|
|
|
|
clientId,
|
|
|
|
clientSecret,
|
|
|
|
accessToken,
|
|
|
|
tokenExpirationDate
|
|
|
|
);
|
|
|
|
|
2023-01-09 12:35:05 +00:00
|
|
|
return tokenExpirationDate;
|
2023-01-09 11:58:34 +00:00
|
|
|
} else {
|
|
|
|
console.log('Using the current token...');
|
2023-01-09 12:35:05 +00:00
|
|
|
return tokenExpirationDate;
|
2023-01-09 11:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-09 12:35:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the Access Token in the client config table
|
|
|
|
*
|
|
|
|
* @param {sequelize.models} models
|
|
|
|
* @param {String} clientId
|
|
|
|
* @param {String} clientSecret
|
|
|
|
* @param {String} accessToken
|
|
|
|
* @param {String} tokenExpirationDate
|
|
|
|
*/
|
|
|
|
async function updateClientConfig(models, clientId, clientSecret, accessToken, tokenExpirationDate) {
|
2023-01-09 11:58:34 +00:00
|
|
|
try {
|
|
|
|
console.log('Updating the client config with the new token...');
|
|
|
|
await models.clientConfig.update(
|
|
|
|
{
|
|
|
|
clientId: clientId,
|
|
|
|
clientSecret: clientSecret,
|
|
|
|
currentToken: accessToken,
|
|
|
|
tokenExpiration: tokenExpirationDate,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
where: {
|
|
|
|
id: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
console.log('Client config updated, new Token set');
|
|
|
|
console.log('New token expiration date: ', tokenExpirationDate);
|
|
|
|
} catch (error) {
|
|
|
|
console.log('There was a error while updating the client config');
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-09 12:35:05 +00:00
|
|
|
export { getClientToken, updateClientConfig };
|