com.azure.core.credential.BasicAuthenticationCredential Maven / Gradle / Ivy
Show all versions of azure-core Show documentation
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.core.credential;
import com.azure.core.util.Base64Util;
import reactor.core.publisher.Mono;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
/**
*
* The {@link BasicAuthenticationCredential} is used to authenticate and authorize requests made to
* Azure services using the Basic authentication scheme. Basic Authentication is a simple authentication scheme
* that uses a combination of a username and password.
*
*
*
* Note that Basic Authentication is generally considered less secure than other authentication methods,
* such as Azure Active Directory (AAD) authentication. It is recommended to use
* Azure Active Directory (Azure AD)
* authentication via {@link TokenCredential} whenever possible, especially for production environments.
*
*
*
* Sample: Azure SAS Authentication
*
*
*
* The following code sample demonstrates the creation of a
* {@link com.azure.core.credential.BasicAuthenticationCredential}, using username and password
*
*
*
*
* BasicAuthenticationCredential basicAuthenticationCredential =
* new BasicAuthenticationCredential("<username>", "<password>");
*
*
*
* @see com.azure.core.credential
* @see com.azure.core.credential.TokenCredential
*/
public class BasicAuthenticationCredential implements TokenCredential {
/**
* Base64 encoded username-password credential.
*/
private final String encodedCredential;
/**
* Creates a basic authentication credential.
*
* @param username basic auth user name
* @param password basic auth password
*/
public BasicAuthenticationCredential(String username, String password) {
String credential = username + ":" + password;
this.encodedCredential = Base64Util.encodeToString(credential.getBytes(StandardCharsets.UTF_8));
}
/**
* @throws RuntimeException If the UTF-8 encoding isn't supported.
*/
@Override
public Mono getToken(TokenRequestContext request) {
return Mono.fromCallable(() -> new AccessToken(encodedCredential, OffsetDateTime.MAX));
}
}