swift-openapi-generator icon indicating copy to clipboard operation
swift-openapi-generator copied to clipboard

How to support OAuth2 Password flows?

Open jmg-duarte opened this issue 1 year ago • 7 comments

Question

I'm currently developing an API that makes use of OAuth2 Password Flows and need to be able to authenticate to use it.

Currently, there is no OAuth2 specific code, but for my use case, merely allowing me to declare the Authorization header would already solve my issue. However, there is no way (that I know of) of declaring arbitrary headers on the generated code.

I'm already modifying my OpenAPI schema to handle https://github.com/apple/swift-openapi-generator/pull/558#issuecomment-2037603263 but would appreciate more escape hatches on the Swift side.

To that end, I am keen on helping implementing the generation for either the escape hatch or the Authorization header on the presence of OAuth security schemes. Just tell me where to look into!

jmg-duarte avatar Apr 05 '24 11:04 jmg-duarte

Hi @jmg-duarte,

you're right that there is no special code generated for OAuth2 flows yet, that's a missing feature: https://swiftpackageindex.com/apple/swift-openapi-generator/1.2.1/documentation/swift-openapi-generator/supported-openapi-features#OAuth-Flows-Object

In the short term, we recommend folks implement a ServerMiddleware and add any auth information as a task local, as shown in this example: https://github.com/apple/swift-openapi-generator/tree/main/Examples/auth-server-middleware-example

Long term, it'd be great to see a proposal of how to better support OAuth2 in the generated code. I don't think we've come up with a design yet, and we welcome the community proposing one (using our Proposal process).

czechboy0 avatar Apr 05 '24 12:04 czechboy0

In the short term, we recommend folks implement a ServerMiddleware and add any auth information as a task local, as shown in this example: https://github.com/apple/swift-openapi-generator/tree/main/Examples/auth-server-middleware-example

I'm doing a client though, would that work?

jmg-duarte avatar Apr 05 '24 13:04 jmg-duarte

developing an API

Apologies, I took the above to mean you're writing the server. Yes, just replace ServerMiddleware with ClientMiddleware, and use this example instead: https://github.com/apple/swift-openapi-generator/tree/main/Examples/auth-client-middleware-example

czechboy0 avatar Apr 05 '24 13:04 czechboy0

Related to #37.

czechboy0 avatar Oct 29 '24 09:10 czechboy0