From 7075833606df6e717b5f8e03b4c2e7a2088b954d Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Tue, 17 Dec 2024 07:45:04 +1300 Subject: [PATCH] [http-client] Add BasicAuthIntercept.header() helper method Provided as a helper method for code that wants the header value to then apply it explicitly [typically as a http Authorization header] rather than using this as a request intercept. --- .../avaje/http/client/BasicAuthIntercept.java | 29 ++++++++++++++++--- .../http/client/BasicAuthInterceptTest.java | 6 ++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/http-client/src/main/java/io/avaje/http/client/BasicAuthIntercept.java b/http-client/src/main/java/io/avaje/http/client/BasicAuthIntercept.java index 4c18a33ed..5c5076a36 100644 --- a/http-client/src/main/java/io/avaje/http/client/BasicAuthIntercept.java +++ b/http-client/src/main/java/io/avaje/http/client/BasicAuthIntercept.java @@ -4,17 +4,38 @@ import java.util.Base64; -/** Adds Basic Authorization header to requests. */ +/** + * Adds Basic Authorization header to requests. + */ public final class BasicAuthIntercept implements RequestIntercept { private final String headerValue; - /** Construct with the username and password. */ + /** + * Construct with the username and password. + */ public BasicAuthIntercept(String username, String password) { - this.headerValue = "Basic " + encode(username, password); + this.headerValue = header(username, password); } - /** Return Base64 encoding of {@literal username:password} */ + /** + * Return the Basic header value with the encoding of {@literal username:password} + *

+ * Provided as a helper method for code that wants the header value to then + * apply explicitly rather than using this as a request intercept. + * + * @return {@code "Basic " + encode(username, password)} + */ + public static String header(String username, String password) { + return "Basic " + encode(username, password); + } + + /** + * Return Base64 encoding of {@literal username:password} + *

+ * Provided as a helper method for code that wants to encode the username + * password pair and use that explicitly rather than using this as a request intercept. + */ public static String encode(String username, String password) { return Base64.getEncoder().encodeToString((username + ":" + password).getBytes(UTF_8)); } diff --git a/http-client/src/test/java/io/avaje/http/client/BasicAuthInterceptTest.java b/http-client/src/test/java/io/avaje/http/client/BasicAuthInterceptTest.java index 74d7755be..6f374acd7 100644 --- a/http-client/src/test/java/io/avaje/http/client/BasicAuthInterceptTest.java +++ b/http-client/src/test/java/io/avaje/http/client/BasicAuthInterceptTest.java @@ -14,6 +14,12 @@ void encode() { assertThat(encode).isEqualTo("QWxhZGRpbjpvcGVuIHNlc2FtZQ=="); } + @Test + void header() { + final String encode = BasicAuthIntercept.header("Aladdin", "open sesame"); + assertThat(encode).isEqualTo("Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="); + } + @Test void beforeRequest() { // setup