@@ -27,6 +27,7 @@ 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"
3132 "oras.land/oras-go/v2/registry/remote/retry"
3233)
@@ -58,29 +59,6 @@ var maxResponseBytes int64 = 128 * 1024 // 128 KiB
5859// See also ClientID.
5960var defaultClientID = "oras-go"
6061
61- // CredentialFunc represents a function that resolves the credential for the
62- // given registry (i.e. host:port).
63- //
64- // [EmptyCredential] is a valid return value and should not be considered as
65- // an error.
66- type CredentialFunc func (ctx context.Context , hostport string ) (Credential , error )
67-
68- // StaticCredential specifies static credentials for the given host.
69- func StaticCredential (registry string , cred Credential ) CredentialFunc {
70- if registry == "docker.io" {
71- // it is expected that traffic targeting "docker.io" will be redirected
72- // to "registry-1.docker.io"
73- // reference: https://github.com/moby/moby/blob/v24.0.0-beta.2/registry/config.go#L25-L48
74- registry = "registry-1.docker.io"
75- }
76- return func (_ context.Context , hostport string ) (Credential , error ) {
77- if hostport == registry {
78- return cred , nil
79- }
80- return EmptyCredential , nil
81- }
82- }
83-
8462// Client is an auth-decorated HTTP client.
8563// Its zero value is a usable client that uses http.DefaultClient with no cache.
8664type Client struct {
@@ -102,7 +80,7 @@ type Client struct {
10280 // EmptyCredential is a valid return value and should not be considered as
10381 // an error.
10482 // If nil, the credential is always resolved to EmptyCredential.
105- Credential CredentialFunc
83+ Credential credentials. CredentialFunc
10684
10785 // Cache caches credentials for direct accessing the remote registry.
10886 // If nil, no cache is used.
@@ -140,9 +118,9 @@ func (c *Client) send(req *http.Request) (*http.Response, error) {
140118}
141119
142120// credential resolves the credential for the given registry.
143- func (c * Client ) credential (ctx context.Context , reg string ) (Credential , error ) {
121+ func (c * Client ) credential (ctx context.Context , reg string ) (credentials. Credential , error ) {
144122 if c .Credential == nil {
145- return EmptyCredential , nil
123+ return credentials . EmptyCredential , nil
146124 }
147125 return c .Credential (ctx , reg )
148126}
@@ -283,7 +261,7 @@ func (c *Client) fetchBasicAuth(ctx context.Context, registry string) (string, e
283261 if err != nil {
284262 return "" , fmt .Errorf ("failed to resolve credential: %w" , err )
285263 }
286- if cred == EmptyCredential {
264+ if cred == credentials . EmptyCredential {
287265 return "" , ErrBasicCredentialNotFound
288266 }
289267 if cred .Username == "" || cred .Password == "" {
@@ -302,7 +280,7 @@ func (c *Client) fetchBearerToken(ctx context.Context, registry, realm, service
302280 if cred .AccessToken != "" {
303281 return cred .AccessToken , nil
304282 }
305- if cred == EmptyCredential || (cred .RefreshToken == "" && ! c .ForceAttemptOAuth2 ) {
283+ if cred == credentials . EmptyCredential || (cred .RefreshToken == "" && ! c .ForceAttemptOAuth2 ) {
306284 return c .fetchDistributionToken (ctx , realm , service , scopes , cred .Username , cred .Password )
307285 }
308286 return c .fetchOAuth2Token (ctx , realm , service , scopes , cred )
@@ -362,7 +340,7 @@ func (c *Client) fetchDistributionToken(ctx context.Context, realm, service stri
362340
363341// fetchOAuth2Token fetches an OAuth2 access token.
364342// Reference: https://distribution.github.io/distribution/spec/auth/oauth/
365- func (c * Client ) fetchOAuth2Token (ctx context.Context , realm , service string , scopes []string , cred Credential ) (string , error ) {
343+ func (c * Client ) fetchOAuth2Token (ctx context.Context , realm , service string , scopes []string , cred credentials. Credential ) (string , error ) {
366344 form := url.Values {}
367345 if cred .RefreshToken != "" {
368346 form .Set ("grant_type" , "refresh_token" )
0 commit comments