gloabl state for oauth 2.0 tokens #3305
anthony-nhs
started this conversation in
Artillery
Replies: 1 comment 1 reply
-
Hey @anthony-nhs 👋 You can probably use a modification of our Refresh Auth Token example. If you do something like this in the processor: const TOKEN_REFRESH_INTERVAL = 1000 * 5; // 5 seconds
let TOKEN_EXPIRY_TIME;
let vuToken;
export async function refreshTokenIfNeeded(requestParams, vuContext, events) {
if (!TOKEN_EXPIRY_TIME || TOKEN_EXPIRY_TIME < Date.now()) {
console.log('Fetching new token');
vuToken = await fetchToken();
TOKEN_EXPIRY_TIME = Date.now() + TOKEN_REFRESH_INTERVAL;
console.log(' expiry time:', TOKEN_EXPIRY_TIME);
console.log(' new token:', vuContext.vars.authToken);
} else {
console.log(' using existing token')
}
vuContext.vars.authToken = vuToken;
}
async function fetchToken() {
// Return a dummy token for the sake of this example. A real-world
// implementation would usually fetch a token from an external endpoint.
return `token-${Date.now()}`;
} Then the |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am trying to set up a load test against a rest API endpoint that uses OAuth 2.0 authentication. The token is valid for 10 minutes and I want to re-use the token as much as possible so I am testing the API rather than the authentication part.
We are running our tests on fargate.
I am using a modified version of the example at https://github.com/artilleryio/artillery/tree/main/examples/generating-vu-tokens to have a before block which gets the token. This works great initially, but the issue I am struggling with is how to get and store a new token when the initial token expires, so that the new token is used by other vusers.
When I initially get the token, I store the expiry time in a variable. Before I make each request I check the expiry time, and if needed I can fetch a new token and then store the new token and expiry time in context.vars
If the initial token has expired and I get a new one, this is only made available to that vuser. As soon as the initial token expires, each call makes a request to get a new token
Is there a way to have some kind of global state that I can store an OAuth 2.0 token in and get a new one and store it so that is available to all subsequent calls. I am aware that there will be a spike of calls to get a new token when an existing one expires, but as long as one of the new tokens is stored that should be fine
Beta Was this translation helpful? Give feedback.
All reactions