swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

Basic auth shouldn't require a password change reverted?

Open RedwoodForest opened this issue 7 years ago • 6 comments

I am using swagger-ui version 3.19.5 (via Swashbuckle.AspNetCore.SwaggerUI v4.0.1) with an API that uses basic auth with a username but no password.

(One example of a high traffic and well-designed API that uses this approach for authentication is the Stripe API.)

It looks like in #2114 swagger-ui was changed to not require a password, but in the authorize dialog the password seems to always be required.

Was reverting this change intentional? Is there a workaround I can use to make it optional?

RedwoodForest avatar Jun 18 '19 16:06 RedwoodForest

I think all that needs to be done is to remove the required flag here: https://github.com/swagger-api/swagger-ui/blob/a86fcf312a0d9b04d99982bf88a3d95884eddb9a/src/core/components/auth/basic-auth.jsx#L76

heldersepu avatar Jun 23 '19 16:06 heldersepu

@heldersepu

I agree.

We found that removing this attribute manually via the browser inspector fixes the issue so we wrote a Swagger plugin as a temporary workaround until this is fixed. (There is almost certainly a more idiomatic way to do this in React, but it's not a framework I'm familiar with.)

// Workaround for https://github.com/swagger-api/swagger-ui/issues/5410
// Based on https://stackoverflow.com/questions/52775462/how-to-insert-minor-customization-into-swagger-inside-its-markup
const BasicAuthPasswordOptionalPlugin = function () {
    return {
        wrapComponents: {
            // We're wrapping the Auths component used to display the authentication prompt:
            // https://github.com/swagger-api/swagger-ui/blob/v3.19.5/src/core/components/auth/auths.jsx
            auths: (Original, system) => (props) => {
                var wrapperClass = system.React.createClass({
                    componentDidMount: function () {
                        var passwordElement = document.querySelector(".auth-container input[type='password']");
                        // The password input field is only present if credentials have not already been entered yet
                        if (passwordElement !== null) {
                            passwordElement.removeAttribute("required");
                        }
                    },
                    render: function () {
                        return system.React.createElement(Original, props);
                    }
                });
                return system.React.createElement(wrapperClass, null);
            }
        }
    }
}

configObject.plugins = [BasicAuthPasswordOptionalPlugin];

RedwoodForest avatar Jun 27 '19 18:06 RedwoodForest

@bodnia, @webron, @fehguy You guys worked on #2114 should we just remove the required="required" on: https://github.com/swagger-api/swagger-ui/blob/a86fcf312a0d9b04d99982bf88a3d95884eddb9a/src/core/components/auth/basic-auth.jsx#L76

heldersepu avatar Jun 27 '19 18:06 heldersepu

Hi, I also need this issue to be solved. Thanks!

leseb83 avatar Aug 26 '19 13:08 leseb83

PR is welcome to remove the required attribute 😄

shockey avatar Aug 26 '19 23:08 shockey

Just ran into this issue and since a solution was proposed but no PR yet, here is one: https://github.com/swagger-api/swagger-ui/pull/5812

Simran-B avatar Jan 29 '20 17:01 Simran-B