Skip to content

Commit e287f8b

Browse files
author
Cory Todd
committed
[python] Fix basic auth always being used
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]>
1 parent de33c6f commit e287f8b

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

modules/swagger-codegen/src/main/resources/python/configuration.mustache

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,12 @@ class Configuration(object):
216216

217217
:return: The token for basic HTTP authentication.
218218
"""
219-
return urllib3.util.make_headers(
220-
basic_auth=self.username + ':' + self.password
221-
).get('authorization')
219+
token = ""
220+
if self.username or self.password:
221+
token = urllib3.util.make_headers(
222+
basic_auth=self.username + ':' + self.password
223+
).get('authorization')
224+
return token
222225

223226
def auth_settings(self):
224227
"""Gets Auth Settings dict for api client.

0 commit comments

Comments
 (0)