Skip to content

Commit be53b90

Browse files
authored
Pass scopes to HTTP client (#273)
2 parents 81eaea8 + a1d71d0 commit be53b90

12 files changed

Lines changed: 365 additions & 112 deletions

File tree

src/main/java/land/oras/ContainerRef.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,27 @@ public String getRegistry() {
9292
return registry;
9393
}
9494

95+
/**
96+
* Get the full repository name including the namespace if any
97+
* @param registry The registry
98+
* @return The full repository name
99+
*/
100+
public String getFullRepository(@Nullable Registry registry) {
101+
String namespace = getNamespace(registry);
102+
if (namespace != null) {
103+
return "%s/%s".formatted(namespace, repository);
104+
}
105+
return repository;
106+
}
107+
108+
/**
109+
* Get the full repository name including the namespace if any
110+
* @return The full repository name
111+
*/
112+
public String getFullRepository() {
113+
return getFullRepository(null);
114+
}
115+
95116
/**
96117
* Get the API registry
97118
* @param target The target registry

src/main/java/land/oras/Registry.java

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@
3232
import java.util.Map;
3333
import land.oras.auth.AuthProvider;
3434
import land.oras.auth.AuthStoreAuthenticationProvider;
35+
import land.oras.auth.HttpClient;
3536
import land.oras.auth.NoAuthProvider;
37+
import land.oras.auth.Scopes;
3638
import land.oras.auth.UsernamePasswordProvider;
3739
import land.oras.exception.OrasException;
3840
import land.oras.utils.ArchiveUtils;
3941
import land.oras.utils.Const;
40-
import land.oras.utils.HttpClient;
4142
import land.oras.utils.JsonUtils;
4243
import land.oras.utils.SupportedAlgorithm;
4344
import org.jspecify.annotations.NullMarked;
@@ -151,8 +152,11 @@ public String getScheme() {
151152
public Tags getTags(ContainerRef containerRef) {
152153
URI uri = URI.create(
153154
"%s://%s".formatted(getScheme(), containerRef.forRegistry(this).getTagsPath(this)));
154-
HttpClient.ResponseWrapper<String> response =
155-
client.get(uri, Map.of(Const.ACCEPT_HEADER, Const.DEFAULT_JSON_MEDIA_TYPE), containerRef, authProvider);
155+
HttpClient.ResponseWrapper<String> response = client.get(
156+
uri,
157+
Map.of(Const.ACCEPT_HEADER, Const.DEFAULT_JSON_MEDIA_TYPE),
158+
Scopes.of(this, containerRef),
159+
authProvider);
156160
handleError(response);
157161
return JsonUtils.fromJson(response.response(), Tags.class);
158162
}
@@ -165,7 +169,10 @@ public Referrers getReferrers(ContainerRef containerRef, @Nullable ArtifactType
165169
URI uri = URI.create(
166170
"%s://%s".formatted(getScheme(), containerRef.forRegistry(this).getReferrersPath(this, artifactType)));
167171
HttpClient.ResponseWrapper<String> response = client.get(
168-
uri, Map.of(Const.ACCEPT_HEADER, Const.DEFAULT_INDEX_MEDIA_TYPE), containerRef, authProvider);
172+
uri,
173+
Map.of(Const.ACCEPT_HEADER, Const.DEFAULT_INDEX_MEDIA_TYPE),
174+
Scopes.of(this, containerRef),
175+
authProvider);
169176
handleError(response);
170177
return JsonUtils.fromJson(response.response(), Referrers.class);
171178
}
@@ -177,7 +184,8 @@ public Referrers getReferrers(ContainerRef containerRef, @Nullable ArtifactType
177184
public void deleteManifest(ContainerRef containerRef) {
178185
URI uri = URI.create(
179186
"%s://%s".formatted(getScheme(), containerRef.forRegistry(this).getManifestsPath(this)));
180-
HttpClient.ResponseWrapper<String> response = client.delete(uri, Map.of(), containerRef, authProvider);
187+
HttpClient.ResponseWrapper<String> response =
188+
client.delete(uri, Map.of(), Scopes.of(this, containerRef), authProvider);
181189
logResponse(response);
182190
handleError(response);
183191
}
@@ -201,7 +209,7 @@ public Manifest pushManifest(ContainerRef containerRef, Manifest manifest) {
201209
uri,
202210
manifestData,
203211
Map.of(Const.CONTENT_TYPE_HEADER, Const.DEFAULT_MANIFEST_MEDIA_TYPE),
204-
containerRef,
212+
Scopes.of(this, containerRef),
205213
authProvider);
206214
logResponse(response);
207215
handleError(response);
@@ -225,7 +233,7 @@ public Index pushIndex(ContainerRef containerRef, Index index) {
225233
uri,
226234
indexData,
227235
Map.of(Const.CONTENT_TYPE_HEADER, Const.DEFAULT_INDEX_MEDIA_TYPE),
228-
containerRef,
236+
Scopes.of(this, containerRef),
229237
authProvider);
230238
logResponse(response);
231239
handleError(response);
@@ -239,7 +247,8 @@ public Index pushIndex(ContainerRef containerRef, Index index) {
239247
public void deleteBlob(ContainerRef containerRef) {
240248
URI uri = URI.create(
241249
"%s://%s".formatted(getScheme(), containerRef.forRegistry(this).getBlobsPath(this)));
242-
HttpClient.ResponseWrapper<String> response = client.delete(uri, Map.of(), containerRef, authProvider);
250+
HttpClient.ResponseWrapper<String> response =
251+
client.delete(uri, Map.of(), Scopes.of(this, containerRef), authProvider);
243252
logResponse(response);
244253
handleError(response);
245254
}
@@ -344,7 +353,7 @@ public Layer pushBlob(ContainerRef containerRef, Path blob, Map<String, String>
344353
uri,
345354
Map.of(Const.CONTENT_TYPE_HEADER, Const.APPLICATION_OCTET_STREAM_HEADER_VALUE),
346355
blob,
347-
containerRef,
356+
Scopes.of(this, containerRef),
348357
authProvider);
349358
logResponse(response);
350359

@@ -367,7 +376,7 @@ public Layer pushBlob(ContainerRef containerRef, Path blob, Map<String, String>
367376
URI.create("%s&digest=%s".formatted(location, digest)),
368377
Map.of(Const.CONTENT_TYPE_HEADER, Const.APPLICATION_OCTET_STREAM_HEADER_VALUE),
369378
blob,
370-
containerRef,
379+
Scopes.of(this, containerRef),
371380
authProvider);
372381
if (response.statusCode() == 201) {
373382
LOG.debug("Successful push: {}", response.response());
@@ -398,7 +407,7 @@ public Layer pushBlob(ContainerRef containerRef, byte[] data) {
398407
uri,
399408
data,
400409
Map.of(Const.CONTENT_TYPE_HEADER, Const.APPLICATION_OCTET_STREAM_HEADER_VALUE),
401-
containerRef,
410+
Scopes.of(this, containerRef),
402411
authProvider);
403412
logResponse(response);
404413

@@ -420,7 +429,7 @@ public Layer pushBlob(ContainerRef containerRef, byte[] data) {
420429
URI.create("%s&digest=%s".formatted(location, digest)),
421430
data,
422431
Map.of(Const.CONTENT_TYPE_HEADER, Const.APPLICATION_OCTET_STREAM_HEADER_VALUE),
423-
containerRef,
432+
Scopes.of(this, containerRef),
424433
authProvider);
425434
if (response.statusCode() == 201) {
426435
LOG.debug("Successful push: {}", response.response());
@@ -449,7 +458,7 @@ private HttpClient.ResponseWrapper<String> headBlob(ContainerRef containerRef) {
449458
HttpClient.ResponseWrapper<String> response = client.head(
450459
uri,
451460
Map.of(Const.ACCEPT_HEADER, Const.APPLICATION_OCTET_STREAM_HEADER_VALUE),
452-
containerRef,
461+
Scopes.of(this, containerRef),
453462
authProvider);
454463
logResponse(response);
455464
return response;
@@ -480,7 +489,7 @@ public void fetchBlob(ContainerRef containerRef, Path path) {
480489
uri,
481490
Map.of(Const.ACCEPT_HEADER, Const.APPLICATION_OCTET_STREAM_HEADER_VALUE),
482491
path,
483-
containerRef,
492+
Scopes.of(this, containerRef),
484493
authProvider);
485494
logResponse(response);
486495
handleError(response);
@@ -496,7 +505,7 @@ public InputStream fetchBlob(ContainerRef containerRef) {
496505
HttpClient.ResponseWrapper<InputStream> response = client.download(
497506
uri,
498507
Map.of(Const.ACCEPT_HEADER, Const.APPLICATION_OCTET_STREAM_HEADER_VALUE),
499-
containerRef,
508+
Scopes.of(this, containerRef),
500509
authProvider);
501510
logResponse(response);
502511
handleError(response);
@@ -561,11 +570,15 @@ public Descriptor probeDescriptor(ContainerRef ref) {
561570
private HttpClient.ResponseWrapper<String> getManifestResponse(ContainerRef containerRef) {
562571
URI uri = URI.create(
563572
"%s://%s".formatted(getScheme(), containerRef.forRegistry(this).getManifestsPath(this)));
564-
HttpClient.ResponseWrapper<String> response =
565-
client.head(uri, Map.of(Const.ACCEPT_HEADER, Const.MANIFEST_ACCEPT_TYPE), containerRef, authProvider);
573+
HttpClient.ResponseWrapper<String> response = client.head(
574+
uri,
575+
Map.of(Const.ACCEPT_HEADER, Const.MANIFEST_ACCEPT_TYPE),
576+
Scopes.of(this, containerRef),
577+
authProvider);
566578
logResponse(response);
567579
handleError(response);
568-
return client.get(uri, Map.of("Accept", Const.MANIFEST_ACCEPT_TYPE), containerRef, authProvider);
580+
return client.get(
581+
uri, Map.of("Accept", Const.MANIFEST_ACCEPT_TYPE), Scopes.of(this, containerRef), authProvider);
569582
}
570583

571584
private byte[] ensureDigest(ContainerRef ref, byte[] data) {
@@ -635,8 +648,11 @@ String getContentType(ContainerRef containerRef) {
635648
Map<String, String> getHeaders(ContainerRef containerRef) {
636649
URI uri = URI.create(
637650
"%s://%s".formatted(getScheme(), containerRef.forRegistry(this).getManifestsPath(this)));
638-
HttpClient.ResponseWrapper<String> response =
639-
client.head(uri, Map.of(Const.ACCEPT_HEADER, Const.MANIFEST_ACCEPT_TYPE), containerRef, authProvider);
651+
HttpClient.ResponseWrapper<String> response = client.head(
652+
uri,
653+
Map.of(Const.ACCEPT_HEADER, Const.MANIFEST_ACCEPT_TYPE),
654+
Scopes.of(this, containerRef),
655+
authProvider);
640656
logResponse(response);
641657
handleError(response);
642658
return response.headers();

src/main/java/land/oras/auth/BearerTokenProvider.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
package land.oras.auth;
2222

2323
import land.oras.ContainerRef;
24-
import land.oras.utils.HttpClient;
2524
import org.jspecify.annotations.NullMarked;
2625
import org.jspecify.annotations.Nullable;
2726
import org.slf4j.Logger;

0 commit comments

Comments
 (0)