Search code examples
automationoauth-2.0postmanidentityserver4pkce

Automate getting access token in Postman using Auth2.0 AuthorizationFlow + PKCE


So I currently have all my requests set up in postman and to be able to make the request to my api I first need to go to the Authorization tab and click "Get Access Token" then a new window appears showing the log in screen from the Identity Server where I can enter the correct username and password to obtain the access token.

I am now wanting to use Postman to test my api however when the access token expires the tests of course fail. What I want to know is the best way to approach setting up a script that could run the authorization before that request/tests are run.

I can see lots of examples if you only use ClientId and Secret however our Identity Server also requires the user to log in with username and password as that information is needed so that the Identity Info can be included in the returned token as the api called in the request uses this to determine which user to return data for.

Really hitting a brick wall here as I cannot see a way to automate the identity login.

How do others deal with running automated tests against an api protected with IdentityServer login?


Solution

  • You would have to mimic the set of requests and responses that would normally go through the browser. For example, you first make an authorization request. The server responds with a 302 to a login page. You can grab the location header and call the login page. Then you would post the username and password to the login form's action, etc.

    You can have a look at this example: https://github.com/curityio/token-handler-node-express/blob/master/test/login.sh this is a set of curl commands which perform such login to an instance of the Curity Identity Server.

    You should be able to script it as a series of requests in Postman.