OAuth 2.0 Possibility to reset token e.g. after environment change or per script
I have checked the following:
- [x] I've searched existing issues and found nothing related to my issue.
This feature
- [ ] blocks me from using Bruno
- [x] would improve my quality of life in Bruno
- [ ] is something I've never seen an API client do before
Describe the feature you want to add, and how it would change your usage of Bruno
I use the same requests in different environments. Each environment requires a different authorization token (e.g. access token with a different audience). Currently, I need to restart Bruno to clear the token internally stored in {{$oauth2.credentials.access_token}}. It would be amazing if either the would be an option to reset the token when the environment is change or the possibility to access and delete this variable in the script section - in my case of the collection - so that a new token with the variables from the newly selected environment is fetched.
Mockups or Images of the feature
No response
The token can be forcibly retrieved or cleared using the buttons at the bottom of the auth config, but yes, this is annoying to have to do when switching environments (and is technically a regression from the manually-scripted behaviour I had from before the new oauth2 functionality was rolled out in V2).
Screenshot
Yes, I agree! I find it not only annoying but it can also lead to errors if you think you send a request to one system while in reality you send the data to a different system ☹️ I see two possibilities for a fix:
- Force the fetch of a new token after each change of environment
- Store the tokens on the environment level (One token per environment)
For me, this is a critical feature. The token can only be used once, so every time I log in, I have to manually click 'reset'. With the previous version, I didn't have this issue
I stumbled upon this situation, and I thought that I could probably implement an automation using pre-request scripts. However, it seems that even removing the access_token from the bru namespace (using delete bru.oauth2CredentialVariables["$oauth2.credentials.access_token"], for lack of an appropriate function) won't do a thing, the request will still be made using an old token.
My use case is just as OP's: I have a collection with APIs that can be used by different types of user, and I have one Environment for each user, but changing the environment will still use an old user's Access Token until it expires. Only then it will (automatically) request a new one
This is also a critical feature for us. When working with non-productive and productive environments together, we now have to be very careful to manually reset the token after switching envs. This action alone, which changes the parameters in the Auth tab, should be enough for Bruno to automatically understand that the token is not relevant anymore.
I think this PR would solve this issue to an extend. This allows variables in tokenID, This means you can make the Token ID environment-specific so that switching environments naturally selects a different cached token (or triggers a fresh fetch), without having to manually reset.
Sounds like a simple solution to the problem. I would appreciate it if that were merged.
@anusree-bruno, it's also important to fix bug #5543 in order to make that feature usable
This issue really should be added to the roadmap. I'm a bit surprised this slipped through.
We have the same requirement and found a working solution using environnement variable and credentials_id variable (named "Token ID" in the GUI). Example:
-
in
environments/prod.bru:vars { clientId: {{process.env.client-id-prod}} clientSecret: {{process.env.client-secret-prod}} api1_env_id: api-partner1-v1-prod api2_env_id: api-partner2-v1-prod } -
in a
api1/folder.bru:auth { mode: oauth2 } auth:oauth2 { grant_type: client_credentials client_id: {{clientId}} client_secret: {{clientSecret}} ... credentials_id: {{api1_env_id}} }
Token are now environment dependent.
This is scriptable too, see the docs:
You may optionally assign a Token ID to your token. If you do, you can reference this token in requests using the format
{{$oauth2.<tokenId>.access_token}}(replace<tokenId>with your Token ID).
If you use the same Token ID in multiple OAuth requests, Bruno will overwrite the existing token data. Make sure to use unique Token IDs for different OAuth configurations.
Thanks @nioc your instructions worked. Thank you very much!
@nioc That worked, thanks! credentials_id should just be called something like {{ENV}}-credentials by default, instead of having the same string value for all environments.