Skip to content
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

HTTP Basic Auth doesn't work in v2.1.0-M2 #1171

Closed
r-brown opened this issue Apr 16, 2015 · 16 comments
Closed

HTTP Basic Auth doesn't work in v2.1.0-M2 #1171

r-brown opened this issue Apr 16, 2015 · 16 comments
Milestone

Comments

@r-brown
Copy link

r-brown commented Apr 16, 2015

Hello,

I'm trying to configure Swagger to use default demo account with HTTP Basic Auth.

Below is my index.html configuration:

...
            function addAuthorization() {
                var username = $('#input_username').val();
                var password = $('#input_password').val();
                if (username && username.trim() != "" && password && password.trim() != "") {
                    var basicAuth = new SwaggerClient.PasswordAuthorization('basic', username, password);
                    window.swaggerUi.api.clientAuthorizations.add("basicAuth", basicAuth);
                }
            }
...

source: https://github.com/Labs64/NetLicensing-API/blob/gh-pages/index.html

... and JSON definition:

...
                "security": [
                    {
                        "basicAuth": []
                    }
                ]
...
    "securityDefinitions": {
        "basicAuth": {
            "type": "basic",
            "description": "HTTP Basic Authentication. Works over `HTTP` and `HTTPS`"
        }
    },
...

source: https://github.com/Labs64/NetLicensing-API/blob/gh-pages/v2.0/netlicensing.json

With this configuration basicAuth header is not set and I'm always getting browser prompt dialog for credentials entry.
Could you help me with this issue?

Live version can be found here: http://io.labs64.com/NetLicensing-API/

@fehguy
Copy link
Contributor

fehguy commented Apr 16, 2015

We just pushed a fix to the develop_2.0 that should help address this. This affects the index.html as well as the library--can you please look at the updates and test the updates?

@r-brown
Copy link
Author

r-brown commented Apr 17, 2015

I've just updated codebase with 'develop_2.0' and got following error (see screenshot)

screen shot 2015-04-17 at 07 11 25

window.swaggerUi.api is null

@webron webron added this to the v2.1 milestone May 4, 2015
@ponelat
Copy link
Member

ponelat commented May 5, 2015

The line that's throwing you an error has a typo...
You need to change from .api to .apis in the above screenshot.

@webron
Copy link
Contributor

webron commented May 5, 2015

@ponelat
Copy link
Member

ponelat commented May 5, 2015

Apologies, @r-brown I've tested against your spec, it works now :D

@r-brown
Copy link
Author

r-brown commented May 5, 2015

No issues guys - I should be able recognize this on my own :-/
I'll try again the same with the develop_2.0 and report here.

@webron
Copy link
Contributor

webron commented May 5, 2015

keep in mind it's not merged into develop_2.0 yet. and still, if it's a bug, it needs to be handled, so thanks for the report.

@r-brown
Copy link
Author

r-brown commented May 5, 2015

Sure; the change #1240 is trivial and I'll merge this locally before test. Thx!

@r-brown
Copy link
Author

r-brown commented May 6, 2015

I've just updated swagger-ui version at http://io.labs64.com/NetLicensing-API/ to develop_2.0 and tried both .api and .apis (see console output below)

> window.swaggerUi.api
> 3.module.exports {authorizationScheme: null, authorizations: null, basePath: "/core/v2/rest", debug: false, info: Object…}
> window.swaggerUi.apis
> undefined

.api at least contains some definitions but still doesn't work - basicAuth header is not set yet.

@ponelat
Copy link
Member

ponelat commented May 7, 2015

Again I must apologize, my previous comment is erroneous!
I blindly made the change, everything worked - so I carried on...
The real bug is the second addAuthorization() call which happens before swaggerUi has loaded.

The real fix, which is slightly older than my comment above, explains why it was working on my side.

You can return poor little window.swaggerUi.api to his original self and remove the line below.

 // pre-populate on the page using demo account
            $('#input_username').val("demo");
            $('#input_password').val("demo");
            addAuthorization(); // <--- this evil little bugger! Remove.

And if everything is absolutely hunky-dory, I will stop apologizing and fix it!

@r-brown
Copy link
Author

r-brown commented May 7, 2015

I've changed the code as you suggested, but the header still doesn't contain Authorization.
So I'm already getting HTTP401 on OPTIONS /core/v2/rest/licensee HTTP/1.1.

@ponelat
Copy link
Member

ponelat commented May 7, 2015

Hmmm, so you've checked in Chrome(or other browser) and you don't see the request header..
"Authorization:Basic ZGVtbzpkZW1v" Which happens to be demo:demo

If not, I'm going to take a closer look at your code (if you don't mind) I'd like to put some debug info in..
I'll be around soon. Thanks for bearing with us.

@r-brown
Copy link
Author

r-brown commented May 7, 2015

I've checked this with the following browsers:

  • IE11
  • Chrome 42.0.x
  • Safari 8.0.5 (auth prompt window appears)

Sure, feel free to dig into the code; I can also grant access to the repo https://github.com/Labs64/NetLicensing-API if necessary.

@fehguy
Copy link
Contributor

fehguy commented May 8, 2015

OK looks like a chicken & egg problem. You need to return a 200 for the OPTIONS request when calling your server. That is required for seeing IF it can even send the authorizations header. Once that's done, it should work.

@ponelat
Copy link
Member

ponelat commented May 15, 2015

@r-brown did you get a chance to see if you can get a successful(200) response from OPTIONS? Looking to close the issue, but want to make sure all the bugs are fixed :)

@r-brown
Copy link
Author

r-brown commented May 16, 2015

Hi @ponelat,

I've just changed Apache HTTPD configuration for CORS preflight requests and... it works http://io.labs64.com/NetLicensing-API/

Used swagger-ui version: 'develop_2.0' (branch)

Thank you for your support!

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

No branches or pull requests

4 participants