I am using a node action. It needs to call another service I have that caches some claim information…
const axios = require('axios').default;
const getToken = async (secret, audience, scope)=>{
const tokenResult = await axios.post('https://.../oauth/token', {
"grant_type": "...",
"client_id" : "...",
"client_secret": secret,
audience,
"scope":"service:identity"
});
return tokenResult.data["access_token"]
}
const getCorp = async (email, domain, secret)=>{
const token = await getToken(secret, "...", "...")
const corpResult = await axios.get(`${domain}/corporate?email=${email}`, {
headers: {
"Authorization": "Bearer "+token
}
});
console.log("What we get back is ", corpResult.data);
return corpResult.data;
}
exports.onExecutePostLogin = async (event, api) => {
console.log("Running...");
if (event.authorization && event.user.email != null){
...
corp = await getCorp(event.user.email, event.secrets.API_DOMAIN, event.secrets.CLIENT_SECRET);
...
}
};
This works but since the action can’t cache the value it requests a new M2M token every time. Is there a way to cache the token so that I am only getting a new one when I need to (expiry, etc)?