Skip to content

Commit

Permalink
feat: updates UserAuthorizer to support retrieving token response dir…
Browse files Browse the repository at this point in the history
…ectly 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
  • Loading branch information
lsirac authored Sep 5, 2024
1 parent 242a082 commit 1651006
Show file tree
Hide file tree
Showing 6 changed files with 995 additions and 254 deletions.
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

0 comments on commit 1651006

Please sign in to comment.