-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #766 from andreaTP/basic-auth
Add a Basic Access Authentication Provider
- Loading branch information
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...s/src/main/java/com/microsoft/kiota/authentication/BasicAccessAuthenticationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |