Skip to content

Commit 1651006

Browse files
authoredSep 5, 2024··
feat: updates UserAuthorizer to support retrieving token response directly with different client auth types (#1486)
* feat: updates UserAuthorizer to support retrieving token response directly * fix: cleanup * fix: review * fix: review suggestions * fix: incorrect import * fix: adds missing check
1 parent 242a082 commit 1651006

File tree

6 files changed

+995
-254
lines changed

6 files changed

+995
-254
lines changed
 

‎oauth2_http/java/com/google/auth/oauth2/OAuth2Utils.java

+24
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import com.google.api.client.util.SecurityUtils;
4444
import com.google.auth.http.AuthHttpConstants;
4545
import com.google.auth.http.HttpTransportFactory;
46+
import com.google.common.base.Strings;
47+
import com.google.common.io.BaseEncoding;
4648
import com.google.common.io.ByteStreams;
4749
import java.io.ByteArrayInputStream;
4850
import java.io.File;
@@ -80,6 +82,7 @@ class OAuth2Utils {
8082
"https://iamcredentials.%s/v1/projects/-/serviceAccounts/%s:generateIdToken";
8183

8284
static final URI TOKEN_SERVER_URI = URI.create("https://oauth2.googleapis.com/token");
85+
8386
static final URI TOKEN_REVOKE_URI = URI.create("https://oauth2.googleapis.com/revoke");
8487
static final URI USER_AUTH_URI = URI.create("https://accounts.google.com/o/oauth2/auth");
8588

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

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+
264288
private OAuth2Utils() {}
265289
}

0 commit comments

Comments
 (0)
Please sign in to comment.