Skip to content

Instantly share code, notes, and snippets.

@SumanaMalkapuram
Created February 6, 2020 16:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save SumanaMalkapuram/fc1c1879754a644592a56390d8821551 to your computer and use it in GitHub Desktop.
Save SumanaMalkapuram/fc1c1879754a644592a56390d8821551 to your computer and use it in GitHub Desktop.
async function getTokenFromRulesConfig(user, context, callback) {
const m2mClientID = configuration.m2mCID;
const m2mClientSecret = configuration.m2mCSecret;
let auth0Domain = '<<your_tenant>>.auth0.com';
const moment = require('moment-timezone');
let axios = require('axios');
const country = context.request.geoip.country_name;
const data = {
user_app_metadata: user.app_metadata,
email: user.email,
email_verified: user.email_verified,
name: user.name
};
const datos = { 'data': data };
const getM2MAuth0AccessToken = async(clientId, clientSecret) => {
if (global.M2MClient) {
console.log("m2mclient from cache");
return global.M2MClient;
} else {
const ManagementClient = require('auth0@2.19.0').ManagementClient;
const mngmntClient = new ManagementClient({
domain: auth0Domain,
clientId: clientId,
clientSecret: clientSecret,
scope: 'update:rules_configs delete:rules_configs'
});
global.M2MClient = mngmntClient;
return mngmntClient;
}
};
async function settingRulesConfig(paramsKey, paramsValue) {
console.log("came into setting Rules config", paramsValue);
let mngmntClient = await getM2MAuth0AccessToken(m2mClientID, m2mClientSecret);
return await mngmntClient.setRulesConfig(paramsKey, paramsValue);
}
async function getNewAPIAccessToken() {
let postBody = {
client_id: m2mClientID,
client_secret: m2mClientSecret,
audience: '<<your backend api identifier>>',
grant_type: 'client_credentials'
};
let options = {
method: 'POST',
url: 'https://' + auth0Domain + '/oauth/token',
headers: { 'content-type': 'application/json' },
data: postBody
};
let token;
await axios(options)
.then(function(response) {
console.log("Successfully generated M2M Client Credentials Key to call for API");
token = {
accessToken: response.data.access_token,
// Forces reissue token 60 seconds before the token actually expires
expirationDate: Date.now() + (response.data.expires_in - 60) * 1000
};
});
return token;
}
const getM2MBackendAccessToken = async() => {
try {
console.log("in try", configuration.api_key);
var APIKey = JSON.parse(configuration.api_key);
console.log("configuration access_token", APIKey.expirationDate);
let isMyAPIKeyValid = APIKey && APIKey.expirationDate > Date.now();
if (isMyAPIKeyValid) {
console.log("token from configuration");
return APIKey;
} else {
var NewAPIKey = await getNewAPIAccessToken();
var params = {
key: "api_key"
};
console.log("token from new M2M call", NewAPIKey);
var data = {
value: JSON.stringify(NewAPIKey)
};
settingRulesConfig(params, data);
console.log("rules set");
return NewAPIKey;
}
} catch (e) {
console.log("excpetion", e);
return callback("no access token");
}
};
try {
const tokenItems = await getM2MBackendAccessToken();
console.log("bearer token", tokenItems.accessToken);
request.post({
url: '<<your backend api>>',
headers: {
Authorization: 'Bearer ' + tokenItems.accessToken,
Accept: 'application/json'
},
json: datos,
});
return callback(null, user, context);
} catch (e) {
return callback(e, null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment