@@ -27,7 +27,9 @@ import (
2727 "net/url"
2828 "strings"
2929
30+ "oras.land/oras-go/v2/registry/remote/credentials"
3031 "oras.land/oras-go/v2/registry/remote/internal/errutil"
32+ "oras.land/oras-go/v2/registry/remote/properties"
3133 "oras.land/oras-go/v2/registry/remote/retry"
3234)
3335
@@ -66,29 +68,6 @@ var maxResponseBytes int64 = 128 * 1024 // 128 KiB
6668// See also ClientID.
6769var defaultClientID = "oras-go"
6870
69- // CredentialFunc represents a function that resolves the credential for the
70- // given registry (i.e. host:port).
71- //
72- // [EmptyCredential] is a valid return value and should not be considered as
73- // an error.
74- type CredentialFunc func (ctx context.Context , hostport string ) (Credential , error )
75-
76- // StaticCredential specifies static credentials for the given host.
77- func StaticCredential (registry string , cred Credential ) CredentialFunc {
78- if registry == "docker.io" {
79- // it is expected that traffic targeting "docker.io" will be redirected
80- // to "registry-1.docker.io"
81- // reference: https://github.com/moby/moby/blob/v24.0.0-beta.2/registry/config.go#L25-L48
82- registry = "registry-1.docker.io"
83- }
84- return func (_ context.Context , hostport string ) (Credential , error ) {
85- if hostport == registry {
86- return cred , nil
87- }
88- return EmptyCredential , nil
89- }
90- }
91-
9271// Client is an auth-decorated HTTP client.
9372// Its zero value is a usable client that uses http.DefaultClient with no cache.
9473type Client struct {
@@ -105,12 +84,12 @@ type Client struct {
10584 // Header contains the custom headers to be added to each request.
10685 Header http.Header
10786
108- // Credential specifies the function for resolving the credential for the
87+ // CredentialFunc specifies the function for resolving the credential for the
10988 // given registry (i.e. host:port).
11089 // EmptyCredential is a valid return value and should not be considered as
11190 // an error.
11291 // If nil, the credential is always resolved to EmptyCredential.
113- Credential CredentialFunc
92+ CredentialFunc credentials. CredentialFunc
11493
11594 // Cache caches credentials for direct accessing the remote registry.
11695 // If nil, no cache is used.
@@ -148,11 +127,11 @@ func (c *Client) send(req *http.Request) (*http.Response, error) {
148127}
149128
150129// credential resolves the credential for the given registry.
151- func (c * Client ) credential (ctx context.Context , reg string ) (Credential , error ) {
152- if c .Credential == nil {
153- return EmptyCredential , nil
130+ func (c * Client ) credential (ctx context.Context , reg string ) (properties. Credential , error ) {
131+ if c .CredentialFunc == nil {
132+ return properties . EmptyCredential , nil
154133 }
155- return c .Credential (ctx , reg )
134+ return c .CredentialFunc (ctx , reg )
156135}
157136
158137// cache resolves the cache.
@@ -291,7 +270,7 @@ func (c *Client) fetchBasicAuth(ctx context.Context, registry string) (string, e
291270 if err != nil {
292271 return "" , fmt .Errorf ("failed to resolve credential: %w" , err )
293272 }
294- if cred == EmptyCredential {
273+ if cred == properties . EmptyCredential {
295274 return "" , ErrBasicCredentialNotFound
296275 }
297276 if cred .Username == "" || cred .Password == "" {
@@ -310,7 +289,7 @@ func (c *Client) fetchBearerToken(ctx context.Context, registry, realm, service
310289 if cred .AccessToken != "" {
311290 return cred .AccessToken , nil
312291 }
313- if cred == EmptyCredential || (cred .RefreshToken == "" && ! c .ForceAttemptOAuth2 ) {
292+ if cred == properties . EmptyCredential || (cred .RefreshToken == "" && ! c .ForceAttemptOAuth2 ) {
314293 return c .fetchDistributionToken (ctx , realm , service , scopes , cred .Username , cred .Password )
315294 }
316295 return c .fetchOAuth2Token (ctx , realm , service , scopes , cred )
@@ -370,7 +349,7 @@ func (c *Client) fetchDistributionToken(ctx context.Context, realm, service stri
370349
371350// fetchOAuth2Token fetches an OAuth2 access token.
372351// Reference: https://distribution.github.io/distribution/spec/auth/oauth/
373- func (c * Client ) fetchOAuth2Token (ctx context.Context , realm , service string , scopes []string , cred Credential ) (string , error ) {
352+ func (c * Client ) fetchOAuth2Token (ctx context.Context , realm , service string , scopes []string , cred properties. Credential ) (string , error ) {
374353 form := url.Values {}
375354 if cred .RefreshToken != "" {
376355 form .Set ("grant_type" , "refresh_token" )
0 commit comments