Skip to content

[python] Fix basic auth always being used #12212

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

Merged
merged 3 commits into from
Nov 24, 2023

Conversation

corytodd
Copy link

PR checklist

  • Read the contribution guidelines.
  • Ran the shell script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh and ./bin/security/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in .\bin\windows\.
  • Filed the PR against the correct branch: 3.0.0 branch for changes related to OpenAPI spec 3.0. Default: master.
  • Copied the technical committee to review the pull request if your PR is targeting a particular programming language.
    @kenjones-cisco

Description of the PR

The Python client configuration will always generate a basic auth header regarless of username and password actually being set. This is a problem if the consumer is trying to use another authorization method that uses the same header name, e.g. Authorization. This bug is only triggered when the api spec's security definitions list BasicAuth after an auth type that uses a conflicting header.

Unfortunately, RFC 2617 1 says that both the username and password are allowed to be empty. This is how golang implements basic auth 2 which means golang codegen is also affected. Java codegen requires both username and password to be set 3. C# requires only one of them to be set 4. Java and C# are therefore not affected. Change python codegen to require either the username or password to be set as a compromise between being broken and rfc compliant.

fixes #9430

Cory Todd added 3 commits August 15, 2023 15:33
The Python client configuration will always generate a basic auth header
regarless of username and password actually being set. This is a problem
if the consumer is trying to use another authorization method that uses
the same header name, e.g. Authorization. This bug is only triggered
when the api spec's security definitions list BasicAuth after an auth
type that uses a conflicting header.

Unfortunately, RFC 2617 [1] says that _both_ the username and password are
allowed to be empty. This is how golang implements basic auth [2] which
means golang codegen is also affected. Java codegen requires both
username and password to be set [3]. C# requires only one of them to be
set [4]. Java and C# are therefore not affected. Change python codegen
to require either the username or password to be set as a compromise
between being broken and rfc compliant.

[1]: https://datatracker.ietf.org/doc/html/rfc2617#section-2
[2]: https://github.com/golang/go/blob/master/src/net/http/client.go#L426
[3]: https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/Java/auth/HttpBasicAuth.mustache#L45
[4]: https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/csharp/api.mustache#L407

Signed-off-by: Cory Todd <[email protected]>
Run ./bin/python-petstore.py to pick up basic auth fixes.

Signed-off-by: Cory Todd <[email protected]>
Run ./bin/security/python-petstore.sh to pick up basic auth fixes.

Note that there have been other template changes in python but
security/python-petstore.sh was not run so the sample is out of date. I
have intentionally excluded the changes that are not related to the
basic auth fix.

Signed-off-by: Cory Todd <[email protected]>
@corytodd corytodd force-pushed the fix-python-config-basic-auth branch from 430949d to b1ca1b5 Compare August 15, 2023 22:33
@HugoMario HugoMario merged commit b36bcc9 into swagger-api:master Nov 24, 2023
@HugoMario
Copy link
Contributor

thanks @corytodd for this PR!

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

Successfully merging this pull request may close these issues.

[Python] Generated client uses basic auth when username and password are not set
2 participants