Skip to content

Support multiple OAuth2 setups #7537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
thoffmann-fms opened this issue Oct 25, 2019 · 8 comments
Closed

Support multiple OAuth2 setups #7537

thoffmann-fms opened this issue Oct 25, 2019 · 8 comments

Comments

@thoffmann-fms
Copy link

I tend to switch between several different OAuth2 controlled APIs so it would be great if the current Get New Access Token screen was just a dropdown list of Settings (with an add/edit button) which would contain all the fields (except possibly Token Name) so I could swap quickly between different setups by selecting the setup name from the dropdown rather than having to copy/paste in the 7 fields from a Notepad to change which API I need a token from.

e.g. Something like this:
image

image

@dirkesquire
Copy link

Yes please, this is quite an important feature. I've filled in all 7 fields, and it works, but when I switch to a different collection or even a different workspace, and want to get an OAuth2.0 token from a different provider, I notice that all the 7 fields still keep all the same values as from before. So it means it is unable to record collection specific settings for GET NEW ACCESS TOKEN

@dirkesquire
Copy link

At the moment if you have multiple OAuth2 projects, you are kinda blocked by Postman, unless you want to copy and paste the token manually each time the session expires.

@dirkesquire
Copy link

I think the values should be stored on the Workspace, Collection or Request, just like the Authorization Bearer token is specific to the Workspace, Collection and Request.

@marshallr12
Copy link

I think this is a bug rather than a feature request. Other forms of authorization (like basic) seem to remain distinct between collections, whereas oAuth2 settings seem to overwrite each other across collections.

@thoffmann-fms
Copy link
Author

The workaround I'm now using is to add a pre-request script to grab a token to the Collection and then use Environment variables to keep track of what's available. Changing the Environment will grab the correct token.
e.g.

image

// Pre-request Script to get OAuth2 token

// Resuse the existing token if there is a valid one

var token = pm.environment.get("accessToken");
if (token !== undefined) {
  var expiresAt = pm.environment.get("tokenExpires");
  if (expiresAt !== undefined) {
    var exp = new Date(expiresAt);
    if (exp > new Date()) {
      console.info("Reuse existing token until " + exp);
      return;
    }
  }
}

console.info("Request new OAuth token");

let getTokenRequest = {
  method: "POST",
  url: pm.variables.get("apiUrl") + "/token",
  body: {
    mode: "raw",
    raw:
      "grant_type=password&username=" +
      pm.variables.get("username") +
      "&password=" +
      pm.variables.get("password"),
  },
};

pm.sendRequest(getTokenRequest, (err, response) => {
  if (err !== null) {
    console.error(err);
  } else {
    let jsonResponse = response.json();
    if (jsonResponse.error !== undefined) {
      console.error(response.code + "; " + jsonResponse.error);
      throw new Error("OAuth error, check un/pw");
    } else {
      // Save the token and it's expiration
      pm.environment.set("accessToken", jsonResponse.access_token);

      // And log when it expires
      let expires = new Date();
      expires.setSeconds(expires.getSeconds() + jsonResponse.expires_in);
      console.info("Token expires at " + expires);
      pm.environment.set("tokenExpires", expires.toString());
    }
  }
});

@vikiCoder
Copy link

Hi everyone,

We have made a few changes in OAuth 2.0 helper in the Postman app to address the problems discussed here. These changes are available in the Postman Canary. Please download the latest Postman Canary app and check it out.

See the issue #9056 to know more about the changes. Feel free to leave your feedback at #9056.

@vvs11
Copy link

vvs11 commented Sep 23, 2020

@timothyhoffmann @dirkesquire @marshallr12 Could you try our Canary build and see if the solution meets your requirements?

@giridharvc7
Copy link

Can you check if the recent changes to Auth fixes your problem? Please check out app version 8.1.
Closing this issue, will open it back again if your problem persists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants