diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bf18881..30d8891d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- Added a default implementation of `BasicAccessAuthenticationProvider` + ## [0.7.8] - 2023-10-13 ### Fixed diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/authentication/BasicAccessAuthenticationProvider.java b/components/abstractions/src/main/java/com/microsoft/kiota/authentication/BasicAccessAuthenticationProvider.java new file mode 100644 index 00000000..cb062238 --- /dev/null +++ b/components/abstractions/src/main/java/com/microsoft/kiota/authentication/BasicAccessAuthenticationProvider.java @@ -0,0 +1,43 @@ +package com.microsoft.kiota.authentication; + +import com.microsoft.kiota.RequestInformation; +import jakarta.annotation.Nonnull; +import jakarta.annotation.Nullable; + +import java.nio.charset.StandardCharsets; +import java.util.Base64; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.CompletableFuture; + +/** Provides an implementation of the Basic Access Authentication scheme: https://en.wikipedia.org/wiki/Basic_access_authentication . */ +public class BasicAccessAuthenticationProvider implements AuthenticationProvider { + private final static String AUTHORIZATION_HEADER_KEY = "Authorization"; + private static final String BASIC = "Basic "; + + private final String username; + private final String password; + private final String encoded; + + /** + * Instantiates a new BasicAccessAuthenticationProvider. + * @param username the username to be used. + * @param password the password to be used. + */ + public BasicAccessAuthenticationProvider(@Nonnull final String username, @Nonnull final String password) { + Objects.requireNonNull(username); + Objects.requireNonNull(password); + + this.username = username; + this.password = password; + encoded = Base64.getEncoder().encodeToString((username + ":" + password).getBytes(StandardCharsets.UTF_8)); + } + + /** {@inheritDoc} */ + @Override + @Nonnull + public CompletableFuture authenticateRequest(@Nonnull final RequestInformation request, @Nullable final Map additionalAuthenticationContext) { + request.headers.add(AUTHORIZATION_HEADER_KEY, BASIC + encoded); + return CompletableFuture.completedFuture(null); + } +}