pactum icon indicating copy to clipboard operation
pactum copied to clipboard

Auth0 authentication

Open larryg01 opened this issue 4 years ago • 16 comments

How can I use OAuth / ("Bearer " + token) authentication with Pactumjs, I can use basic username and password authentication just not find a way to use Auth0. Any help would be much appreciated.

larryg01 avatar Aug 13 '21 10:08 larryg01

We can use withHeaders method to pass Bearer token.

const token = 'abc';

await pactum.spec()
  .get('<url>')
  .withHeaders('Authorization', `Bearer ${token}`)
  .expectStatus(200)

ASaiAnudeep avatar Aug 13 '21 13:08 ASaiAnudeep

Would it be the same process for all these other auth methods as well: Digest, NTLM, API Keys, OAuth, HAWK etc

klassijs avatar Aug 13 '21 13:08 klassijs

Would it be the same process for all these other auth methods as well: Digest, NTLM, API Keys, OAuth, HAWK etc

Yes, for now. Currently the library doesn't have dedicated methods for handling different authentication methods. But we are open to add custom capabilities for the same.

ASaiAnudeep avatar Aug 14 '21 03:08 ASaiAnudeep

I am having problems and getting lots of errors trying to implement this:

await pactum.spec() .get('url') .withHeaders('Authorization', 'username=john + password=test') .expectStatus(200)

Is that the correct implementation i have tried different variations but still no luck. Any help would be greatly appreciated

larryg01 avatar Aug 16 '21 10:08 larryg01

Are you able to make the same request in Postman? If you can, please capture the header value & use it in pactum headers.

ASaiAnudeep avatar Aug 17 '21 03:08 ASaiAnudeep

I ca get it working with .withHeaders('Authorization', Bearer ${token}) but not with .withAuth(username, password)

larryg01 avatar Aug 17 '21 10:08 larryg01

withAuth is for basic authentication. It won't work for bearer.

Examples

Using Headers

await pactum.spec()
  .get('<url>')
  .withHeaders('Authorization', 'Basic ' + Buffer.from(username + ':' + password).toString('base64'))
  .expectStatus(200)

Using withAuth

await pactum.spec()
  .get('<url>')
  .withAuth(username, password)
  .expectStatus(200)

ASaiAnudeep avatar Aug 17 '21 13:08 ASaiAnudeep

This worked for Bearer Token
expected.reportData_body is a String formatted as JSON and bearerToken is a String too.

it("Check status code for TimePeriod.", async () => {
    const DTBody = expected.reportData_body;
    await pactum
        .spec()
        .post("http://localhost:3000/timeperiod")
        .withHeaders("Authorization", `Bearer ${bearerToken}`)
        .withBody(DTBody)
        .expectStatus(200);
});

vijay-jaisankar avatar Sep 10 '21 13:09 vijay-jaisankar

Thanks for posting

larryg01 avatar Sep 10 '21 14:09 larryg01

I am fighting with getting it to work where the auth is part of the url? i.e https://username:[email protected]

larryg01 avatar Sep 10 '21 16:09 larryg01

Not sure how to use basic auth as part of the URL. Raised an issue for the same in phin.

As a workaround we can use withAuth method.

Working

await pactum.spec()
  .get('https://the-internet.herokuapp.com/basic_auth')
  .withAuth('admin', 'admin')

Not Working

await pactum.spec()
  .get('https://admin:[email protected]/basic_auth')

ASaiAnudeep avatar Sep 12 '21 07:09 ASaiAnudeep

Alternatively, if you're using JWT, you may send the username and password as JSON to your "verifier" route.

withAuth() seems too overpowered though :)

vijay-jaisankar avatar Sep 12 '21 07:09 vijay-jaisankar

As a workaround we can use withAuth method.

await pactum.spec()
  .get('https://the-internet.herokuapp.com')
  .withAuth('admin', 'admin')

This worked for me Thank you very much. Its a great tool but I have to admit the Doc needs some serious updating to avoid the 10000 questions.

larryg01 avatar Sep 13 '21 07:09 larryg01

Do you have any updates with reference to using different types of Auth methods (i.e. Digest in particular) ?

larryg01 avatar Nov 10 '22 11:11 larryg01

I have never worked on the Digest authentication method. It would be great, if you can give us a sample working code written with phin.

ASaiAnudeep avatar Nov 11 '22 06:11 ASaiAnudeep

I am struggling to get it to work with aws Signature its working in postman but not in Pactum is there something am missing? Any help would be greatly appreciated

larryg01 avatar Jul 25 '23 12:07 larryg01