Skip to content
This repository was archived by the owner on May 12, 2026. It is now read-only.
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
24 changes: 24 additions & 0 deletions oauth2_http/java/com/google/auth/oauth2/OAuth2Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import com.google.api.client.util.SecurityUtils;
import com.google.auth.http.AuthHttpConstants;
import com.google.auth.http.HttpTransportFactory;
import com.google.common.base.Strings;
import com.google.common.io.BaseEncoding;
import com.google.common.io.ByteStreams;
import java.io.ByteArrayInputStream;
import java.io.File;
Expand Down Expand Up @@ -80,6 +82,7 @@ class OAuth2Utils {
"https://iamcredentials.%s/v1/projects/-/serviceAccounts/%s:generateIdToken";

static final URI TOKEN_SERVER_URI = URI.create("https://oauth2.googleapis.com/token");

static final URI TOKEN_REVOKE_URI = URI.create("https://oauth2.googleapis.com/revoke");
static final URI USER_AUTH_URI = URI.create("https://accounts.google.com/o/oauth2/auth");

Expand Down Expand Up @@ -261,5 +264,26 @@ static PrivateKey privateKeyFromPkcs8(String privateKeyPkcs8) throws IOException
throw new IOException("Unexpected exception reading PKCS#8 data", unexpectedException);
}

/**
* Generates a Basic Authentication header string for the provided username and password.
*
* <p>This method constructs a Basic Authentication string using the provided username and
* password. The credentials are encoded in Base64 format and prefixed with the "Basic " scheme
* identifier.
*
* @param username The username for authentication.
* @param password The password for authentication.
* @return The Basic Authentication header value.
* @throws IllegalArgumentException if either username or password is null or empty.
*/
static String generateBasicAuthHeader(String username, String password) {
if (Strings.isNullOrEmpty(username) || Strings.isNullOrEmpty(password)) {
throw new IllegalArgumentException("Username and password cannot be null or empty.");
}
String credentials = username + ":" + password;
String encodedCredentials = BaseEncoding.base64().encode(credentials.getBytes());
return "Basic " + encodedCredentials;
}

private OAuth2Utils() {}
}
Loading