|
43 | 43 | import com.google.api.client.util.SecurityUtils;
|
44 | 44 | import com.google.auth.http.AuthHttpConstants;
|
45 | 45 | import com.google.auth.http.HttpTransportFactory;
|
| 46 | +import com.google.common.base.Strings; |
| 47 | +import com.google.common.io.BaseEncoding; |
46 | 48 | import com.google.common.io.ByteStreams;
|
47 | 49 | import java.io.ByteArrayInputStream;
|
48 | 50 | import java.io.File;
|
@@ -80,6 +82,7 @@ class OAuth2Utils {
|
80 | 82 | "https://iamcredentials.%s/v1/projects/-/serviceAccounts/%s:generateIdToken";
|
81 | 83 |
|
82 | 84 | static final URI TOKEN_SERVER_URI = URI.create("https://oauth2.googleapis.com/token");
|
| 85 | + |
83 | 86 | static final URI TOKEN_REVOKE_URI = URI.create("https://oauth2.googleapis.com/revoke");
|
84 | 87 | static final URI USER_AUTH_URI = URI.create("https://accounts.google.com/o/oauth2/auth");
|
85 | 88 |
|
@@ -261,5 +264,26 @@ static PrivateKey privateKeyFromPkcs8(String privateKeyPkcs8) throws IOException
|
261 | 264 | throw new IOException("Unexpected exception reading PKCS#8 data", unexpectedException);
|
262 | 265 | }
|
263 | 266 |
|
| 267 | + /** |
| 268 | + * Generates a Basic Authentication header string for the provided username and password. |
| 269 | + * |
| 270 | + * <p>This method constructs a Basic Authentication string using the provided username and |
| 271 | + * password. The credentials are encoded in Base64 format and prefixed with the "Basic " scheme |
| 272 | + * identifier. |
| 273 | + * |
| 274 | + * @param username The username for authentication. |
| 275 | + * @param password The password for authentication. |
| 276 | + * @return The Basic Authentication header value. |
| 277 | + * @throws IllegalArgumentException if either username or password is null or empty. |
| 278 | + */ |
| 279 | + static String generateBasicAuthHeader(String username, String password) { |
| 280 | + if (Strings.isNullOrEmpty(username) || Strings.isNullOrEmpty(password)) { |
| 281 | + throw new IllegalArgumentException("Username and password cannot be null or empty."); |
| 282 | + } |
| 283 | + String credentials = username + ":" + password; |
| 284 | + String encodedCredentials = BaseEncoding.base64().encode(credentials.getBytes()); |
| 285 | + return "Basic " + encodedCredentials; |
| 286 | + } |
| 287 | + |
264 | 288 | private OAuth2Utils() {}
|
265 | 289 | }
|
0 commit comments