Skip to content

Commit

Permalink
Merge pull request #766 from andreaTP/basic-auth
Browse files Browse the repository at this point in the history
Add a Basic Access Authentication Provider
  • Loading branch information
baywet authored Oct 31, 2023
2 parents 9099bd8 + 46fd0da commit abd2209
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
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;
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) {
request.headers.add(AUTHORIZATION_HEADER_KEY, BASIC + encoded);
return CompletableFuture.completedFuture(null);
}
}

0 comments on commit abd2209

Please sign in to comment.