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

Add a Basic Access Authentication Provider #766

Merged
merged 4 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
andreaTP marked this conversation as resolved.
Show resolved Hide resolved
this.password = password;
encoded = Base64.getEncoder().encodeToString((username + ":" + password).getBytes(StandardCharsets.UTF_8));
}

/** {@inheritDoc} */
@Override
@Nonnull
public CompletableFuture<Void> authenticateRequest(@Nonnull final RequestInformation request, @Nullable final Map<String, Object> additionalAuthenticationContext) {
andreaTP marked this conversation as resolved.
Show resolved Hide resolved
request.headers.add(AUTHORIZATION_HEADER_KEY, BASIC + encoded);
return CompletableFuture.completedFuture(null);
}
}