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

Implement HasAuthentication to be able to use authentication #138

Open
TareqK opened this issue Nov 8, 2023 · 1 comment
Open

Implement HasAuthentication to be able to use authentication #138

TareqK opened this issue Nov 8, 2023 · 1 comment

Comments

@TareqK
Copy link

TareqK commented Nov 8, 2023

So after some debugging on a project, i found out that the driver doesn't implement HasAuthentication, which means that using basic authentication with selenide does not work - and might not work with mainline selenium(didn't test that). In all cases, implementing HasAuthentication solves this problem. I have a really dumb internal implementation, but having this built in would be nice.

@TareqK
Copy link
Author

TareqK commented Nov 8, 2023

Dumb workaround for basic authentication if someone is looking

public  class HtmlUnitDriverWithAuth extends HtmlUnitDriver implements HasAuthentication {

        public HtmlUnitDriverWithAuth() {
            super();
        }

        public HtmlUnitDriverWithAuth(BrowserVersion version) {
            super(version);
        }

        public HtmlUnitDriverWithAuth(boolean enableJavascript) {
            super(enableJavascript);
        }

        public HtmlUnitDriverWithAuth(BrowserVersion version, boolean enableJavascript) {
            super(version, enableJavascript);
        }

        public HtmlUnitDriverWithAuth(Capabilities capabilities) {
            super(capabilities);
        }

        public HtmlUnitDriverWithAuth(Capabilities desiredCapabilities, Capabilities requiredCapabilities) {
            super(desiredCapabilities, requiredCapabilities);
        }

        @Override
        public void register(Predicate<URI> whenThisMatches, Supplier<Credentials> useTheseCredentials) {
            Require.nonNull("Check to use to see how we should authenticate", whenThisMatches);
            Require.nonNull("Credentials to use when authenticating", useTheseCredentials);
            Credentials credentials = useTheseCredentials.get();
            if (credentials instanceof UsernameAndPassword usernameAndPassword) {
                String username = usernameAndPassword.username();
                String password = usernameAndPassword.password();
                String credsString = new StringBuilder(username).append(":").append(password).toString();
                String base64encodedUsernameAndPassword = new String(Base64.getEncoder().encode(credsString.getBytes()));
                this.getWebClient().addRequestHeader("Authorization", "Basic " + base64encodedUsernameAndPassword);
            }
        }

    }

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

No branches or pull requests

1 participant