Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using swagger-codegen with username and password basic auth

I have a django rest_framework API, Swagger and a Swagger UI. When I am not logged in I can see a very limited view of "login" and "docs". When I am logged in I can see lots of stuff.

I am trying to use the swagger-codegen to generate a client:

java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar \
generate -i http://127.0.0.1:8080/api/docs/ -l python -o myclient

However, it only generates a very limited client that provides the "login" and "docs" functionality.

How do I let swagger-codegen know how to login using http basic authentication, in order for it to generate a more complete client?

The docs says I should do the following, but I do not know what it expects:

    -a <authorization>, --auth <authorization>
        adds authorization headers when fetching the swagger definitions
        remotely. Pass in a URL-encoded string of name:header with a comma
        separating multiple values
like image 541
tomsv Avatar asked Feb 08 '17 17:02

tomsv


People also ask

How do you pass basic authentication in swagger UI?

Basic authentication is easy to define. In the global securityDefinitions section, add an entry with type: basic and an arbitrary name (in this example - basicAuth). Then, apply security to the whole API or specific operations by using the security section.

How do you send basic authentication in header with username and password?

Basic Auth:The client sends HTTP requests with the Authorization header that contains the word Basic, followed by a space and a base64-encoded(non-encrypted) string username: password. For example, to authorize as username / Pa$$w0rd the client would send.


1 Answers

Since you are using http basic authentication the command should be:

java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar \
generate -i http://127.0.0.1:8080/api/docs/ -l python -o myclient -a "Authorization:Basic QWxhZGRpbjpPcGVuU2VzYW1l"

Where QWxhZGRpbjpPcGVuU2VzYW1l is your username:password encoded in base64.

Here you can have a look at the code that will parse this option.

like image 80
moondaisy Avatar answered Sep 28 '22 19:09

moondaisy