pact-foundation / pact-ruby

Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service provider project.
https://pact.io
MIT License
2.17k stars 216 forks source link

Need Guidance on Injecting Dynamic Authentication Headers in Provider Verification #306

Closed kheraankit closed 3 months ago

kheraankit commented 9 months ago

Description: We're using Pact for contract testing in a Rails application with token-based API authentication. Our setup involves dynamically creating users and generating tokens in provider states, but we're facing challenges in injecting these tokens as authentication headers into Pact's verification requests.

Challenges:

No clear method for modifying Pact verification requests to include dynamic authentication headers. Lack of documentation on handling authentication scenarios where tokens are generated during provider state setup. Key Questions:

How can we inject dynamic authentication headers, based on tokens from provider states, into Pact verification requests? Are there best practices or alternative approaches within Pact for authenticated requests, especially with dynamically generated user tokens? Our goal is to ensure verification requests are authenticated similarly to real-world API calls. Any guidance or feature consideration to facilitate this would greatly benefit users facing similar authentication setups.

Thank you for your support and the ongoing development of Pact.

YOU54F commented 5 months ago

we have a page on auth in the docs

https://docs.pact.io/provider/handling_auth

ruby doesn’t have a native request filter but there are a couple of options

custom header

https://github.com/pact-foundation/pact-provider-verifier?tab=readme-ov-file#setting-a-custom-authentication-header

you can also provide custom middleware and create your own request filter, see the pact provider proxy project for an example

https://github.com/pact-foundation/pact-provider-proxy

https://github.com/pact-foundation/pact-provider-proxy/blob/master/spec/support/middleware_pact_helper.rb