|
| 1 | +package com.salesforce.multicloudj.registry.driver; |
| 2 | + |
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import com.google.gson.JsonParser; |
| 5 | +import com.google.gson.JsonSyntaxException; |
| 6 | +import org.apache.commons.lang3.StringUtils; |
| 7 | +import org.apache.http.HttpHeaders; |
| 8 | +import org.apache.http.HttpStatus; |
| 9 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 10 | +import org.apache.http.client.methods.HttpGet; |
| 11 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 12 | +import org.apache.http.util.EntityUtils; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.net.URI; |
| 16 | +import java.net.URISyntaxException; |
| 17 | +import java.nio.charset.StandardCharsets; |
| 18 | + |
| 19 | +import org.apache.http.client.utils.URIBuilder; |
| 20 | + |
| 21 | +/** |
| 22 | + * Handles OAuth2 Bearer Token exchange for OCI registries. |
| 23 | + * Exchanges identity tokens for registry-scoped bearer tokens. |
| 24 | + */ |
| 25 | +public class BearerTokenExchange { |
| 26 | + |
| 27 | + private final CloseableHttpClient httpClient; |
| 28 | + |
| 29 | + /** |
| 30 | + * Creates a new BearerTokenExchange instance. |
| 31 | + * |
| 32 | + * @param httpClient the HTTP client to use for requests |
| 33 | + */ |
| 34 | + public BearerTokenExchange(CloseableHttpClient httpClient) { |
| 35 | + this.httpClient = httpClient; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Exchanges an identity token for a registry-scoped bearer token. |
| 40 | + * |
| 41 | + * @param challenge the authentication challenge from AuthChallenge.discover() |
| 42 | + * @param identityToken the identity token (e.g., OAuth2 access token from GCP) |
| 43 | + * @param repository the repository to request access for |
| 44 | + * @param actions the actions to request (e.g., "pull", "push") |
| 45 | + * @return the bearer token for use in Authorization header |
| 46 | + * @throws IOException if the token exchange fails |
| 47 | + */ |
| 48 | + public String getBearerToken(AuthChallenge challenge, String identityToken, |
| 49 | + String repository, String... actions) throws IOException { |
| 50 | + if (challenge == null || !challenge.isBearer()) { |
| 51 | + throw new IllegalArgumentException("Bearer token exchange requires a Bearer challenge"); |
| 52 | + } |
| 53 | + |
| 54 | + String realm = challenge.getRealm(); |
| 55 | + if (StringUtils.isBlank(realm)) { |
| 56 | + throw new IOException("Bearer challenge missing realm"); |
| 57 | + } |
| 58 | + |
| 59 | + // Build token request URL using URIBuilder for proper encoding |
| 60 | + URI tokenUri = buildTokenUri(realm, challenge, repository, actions); |
| 61 | + |
| 62 | + HttpGet request = new HttpGet(tokenUri); |
| 63 | + // Use identity token as Bearer auth for the token endpoint |
| 64 | + request.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + identityToken); |
| 65 | + |
| 66 | + try (CloseableHttpResponse response = httpClient.execute(request)) { |
| 67 | + int statusCode = response.getStatusLine().getStatusCode(); |
| 68 | + if (statusCode != HttpStatus.SC_OK) { |
| 69 | + String errorBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); |
| 70 | + throw new IOException("Token exchange failed: HTTP " + statusCode + " - " + errorBody); |
| 71 | + } |
| 72 | + |
| 73 | + String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); |
| 74 | + |
| 75 | + JsonObject json; |
| 76 | + try { |
| 77 | + json = JsonParser.parseString(responseBody).getAsJsonObject(); |
| 78 | + } catch (JsonSyntaxException e) { |
| 79 | + throw new IOException("Invalid JSON response from token endpoint: " + responseBody, e); |
| 80 | + } |
| 81 | + |
| 82 | + // Token can be in "token" (Docker Hub, AWS ECR) or "access_token" (GCP Artifact Registry) field |
| 83 | + if (json.has("token") && !json.get("token").isJsonNull()) { |
| 84 | + return json.get("token").getAsString(); |
| 85 | + } else if (json.has("access_token") && !json.get("access_token").isJsonNull()) { |
| 86 | + return json.get("access_token").getAsString(); |
| 87 | + } |
| 88 | + |
| 89 | + throw new IOException("Token response missing token field"); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private URI buildTokenUri(String realm, AuthChallenge challenge, String repository, String[] actions) |
| 94 | + throws IOException { |
| 95 | + try { |
| 96 | + URIBuilder uriBuilder = new URIBuilder(realm); |
| 97 | + |
| 98 | + if (challenge.getService() != null) { |
| 99 | + uriBuilder.addParameter("service", challenge.getService()); |
| 100 | + } |
| 101 | + |
| 102 | + // Build scope: repository:<name>:<actions> |
| 103 | + if (repository != null && actions.length > 0) { |
| 104 | + String scope = "repository:" + repository + ":" + String.join(",", actions); |
| 105 | + uriBuilder.addParameter("scope", scope); |
| 106 | + } else if (challenge.getScope() != null) { |
| 107 | + // Fallback: use scope from the original WWW-Authenticate header |
| 108 | + uriBuilder.addParameter("scope", challenge.getScope()); |
| 109 | + } |
| 110 | + |
| 111 | + return uriBuilder.build(); |
| 112 | + } catch (URISyntaxException e) { |
| 113 | + throw new IOException("Invalid token endpoint URL: " + realm, e); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments