@@ -27,6 +27,7 @@ import (
2727 "net/url"
2828 "strings"
2929
30+ "github.com/oras-project/oras-go/v3/registry/remote/credentials"
3031 "github.com/oras-project/oras-go/v3/registry/remote/internal/errutil"
3132 "github.com/oras-project/oras-go/v3/registry/remote/retry"
3233)
@@ -66,29 +67,6 @@ var maxResponseBytes int64 = 128 * 1024 // 128 KiB
6667// See also ClientID.
6768var defaultClientID = "oras-go"
6869
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-
9270// Client is an auth-decorated HTTP client.
9371// Its zero value is a usable client that uses http.DefaultClient with no cache.
9472type Client struct {
@@ -105,12 +83,12 @@ type Client struct {
10583 // Header contains the custom headers to be added to each request.
10684 Header http.Header
10785
108- // Credential specifies the function for resolving the credential for the
86+ // CredentialFunc specifies the function for resolving the credential for the
10987 // given registry (i.e. host:port).
11088 // EmptyCredential is a valid return value and should not be considered as
11189 // an error.
11290 // If nil, the credential is always resolved to EmptyCredential.
113- Credential CredentialFunc
91+ CredentialFunc credentials. CredentialFunc
11492
11593 // Cache caches credentials for direct accessing the remote registry.
11694 // If nil, no cache is used.
@@ -148,11 +126,11 @@ func (c *Client) send(req *http.Request) (*http.Response, error) {
148126}
149127
150128// 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
129+ func (c * Client ) credential (ctx context.Context , reg string ) (credentials. Credential , error ) {
130+ if c .CredentialFunc == nil {
131+ return credentials . EmptyCredential , nil
154132 }
155- return c .Credential (ctx , reg )
133+ return c .CredentialFunc (ctx , reg )
156134}
157135
158136// cache resolves the cache.
@@ -291,7 +269,7 @@ func (c *Client) fetchBasicAuth(ctx context.Context, registry string) (string, e
291269 if err != nil {
292270 return "" , fmt .Errorf ("failed to resolve credential: %w" , err )
293271 }
294- if cred == EmptyCredential {
272+ if cred == credentials . EmptyCredential {
295273 return "" , ErrBasicCredentialNotFound
296274 }
297275 if cred .Username == "" || cred .Password == "" {
@@ -310,7 +288,7 @@ func (c *Client) fetchBearerToken(ctx context.Context, registry, realm, service
310288 if cred .AccessToken != "" {
311289 return cred .AccessToken , nil
312290 }
313- if cred == EmptyCredential || (cred .RefreshToken == "" && ! c .ForceAttemptOAuth2 ) {
291+ if cred == credentials . EmptyCredential || (cred .RefreshToken == "" && ! c .ForceAttemptOAuth2 ) {
314292 return c .fetchDistributionToken (ctx , realm , service , scopes , cred .Username , cred .Password )
315293 }
316294 return c .fetchOAuth2Token (ctx , realm , service , scopes , cred )
@@ -370,7 +348,7 @@ func (c *Client) fetchDistributionToken(ctx context.Context, realm, service stri
370348
371349// fetchOAuth2Token fetches an OAuth2 access token.
372350// 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 ) {
351+ func (c * Client ) fetchOAuth2Token (ctx context.Context , realm , service string , scopes []string , cred credentials. Credential ) (string , error ) {
374352 form := url.Values {}
375353 if cred .RefreshToken != "" {
376354 form .Set ("grant_type" , "refresh_token" )
0 commit comments