| 1 | /* |
| 2 | Copyright The ORAS Authors. |
| 3 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | you may not use this file except in compliance with the License. |
| 5 | You may obtain a copy of the License at |
| 6 | |
| 7 | http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | |
| 9 | Unless required by applicable law or agreed to in writing, software |
| 10 | distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | See the License for the specific language governing permissions and |
| 13 | limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | package authscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/ |
| 17 | |
| 18 | import ( |
| 19 | "contextscip-go gomod github.com/golang/go/src go1.23.0 context/" |
| 20 | "stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/" |
| 21 | "syncscip-go gomod github.com/golang/go/src go1.23.0 sync/" |
| 22 | |
| 23 | "oras.land/oras-go/v2/errdefscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/" |
| 24 | "oras.land/oras-go/v2/internal/syncutilscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/syncutil`/" |
| 25 | ) |
| 26 | |
| 27 | // DefaultCache is the sharable cache used by DefaultClient. |
| 28 | var DefaultCachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/DefaultCache. Cachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Cache# = NewCachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/NewCache().() |
| 29 | |
| 30 | // Cache caches the auth-scheme and auth-token for the "Authorization" header in |
| 31 | // accessing the remote registry. |
| 32 | // Precisely, the header is `Authorization: auth-scheme auth-token`. |
| 33 | // The `auth-token` is a generic term as `token68` in RFC 7235 section 2.1. |
| 34 | type Cachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Cache# interface { |
| 35 | // GetScheme returns the auth-scheme part cached for the given registry. |
| 36 | // A single registry is assumed to have a consistent scheme. |
| 37 | // If a registry has different schemes per path, the auth client is still |
| 38 | // workable. However, the cache may not be effective as the cache cannot |
| 39 | // correctly guess the scheme. |
| 40 | GetSchemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Cache#GetScheme.(ctx contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Contextscip-go gomod github.com/golang/go/src go1.23.0 context/Context#, registry string) (Schemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Scheme#, error) |
| 41 | |
| 42 | // GetToken returns the auth-token part cached for the given registry of a |
| 43 | // given scheme. |
| 44 | // The underlying implementation MAY cache the token for all schemes for the |
| 45 | // given registry. |
| 46 | GetTokenscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Cache#GetToken.(ctx contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Contextscip-go gomod github.com/golang/go/src go1.23.0 context/Context#, registry string, scheme Schemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Scheme#, key string) (string, error) |
| 47 | |
| 48 | // Set fetches the token using the given fetch function and caches the token |
| 49 | // for the given scheme with the given key for the given registry. |
| 50 | // The return values of the fetch function is returned by this function. |
| 51 | // The underlying implementation MAY combine the fetch operation if the Set |
| 52 | // function is invoked multiple times at the same time. |
| 53 | Setscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Cache#Set.(ctx contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Contextscip-go gomod github.com/golang/go/src go1.23.0 context/Context#, registry string, scheme Schemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Scheme#, key string, fetch func(contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Contextscip-go gomod github.com/golang/go/src go1.23.0 context/Context#) (string, error)) (string, error) |
| 54 | } |
| 55 | |
| 56 | // cacheEntry is a cache entry for a single registry. |
| 57 | type cacheEntryscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/cacheEntry# struct { |
| 58 | schemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/cacheEntry#scheme. Schemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Scheme# |
| 59 | tokensscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/cacheEntry#tokens. syncscip-go gomod github.com/golang/go/src go1.23.0 sync/.Mapscip-go gomod github.com/golang/go/src go1.23.0 sync/Map# // map[string]string |
| 60 | } |
| 61 | |
| 62 | // concurrentCache is a cache suitable for concurrent invocation. |
| 63 | type concurrentCachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/concurrentCache# struct { |
| 64 | statusscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/concurrentCache#status. syncscip-go gomod github.com/golang/go/src go1.23.0 sync/.Mapscip-go gomod github.com/golang/go/src go1.23.0 sync/Map# // map[string]*syncutil.Once |
| 65 | cachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/concurrentCache#cache. syncscip-go gomod github.com/golang/go/src go1.23.0 sync/.Mapscip-go gomod github.com/golang/go/src go1.23.0 sync/Map# // map[string]*cacheEntry |
| 66 | } |
| 67 | |
| 68 | // NewCache creates a new go-routine safe cache instance. |
| 69 | func NewCachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/NewCache().() Cachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Cache# { |
| 70 | return &concurrentCachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/concurrentCache#{} |
| 71 | } |
| 72 | |
| 73 | // GetScheme returns the auth-scheme part cached for the given registry. |
| 74 | func (cc *concurrentCachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/concurrentCache#) GetSchemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/concurrentCache#GetScheme().(ctx contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Contextscip-go gomod github.com/golang/go/src go1.23.0 context/Context#, registry string) (Schemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Scheme#, error) { |
| 75 | entry, ok := cc.cachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/concurrentCache#cache..Loadscip-go gomod github.com/golang/go/src go1.23.0 sync/Map#Load().(registry) |
| 76 | if !ok { |
| 77 | return SchemeUnknownscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/SchemeUnknown., errdefscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/.ErrNotFoundscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/ErrNotFound. |
| 78 | } |
| 79 | return entry.(*cacheEntryscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/cacheEntry#).schemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/cacheEntry#scheme., nil |
| 80 | } |
| 81 | |
| 82 | // GetToken returns the auth-token part cached for the given registry of a given |
| 83 | // scheme. |
| 84 | func (cc *concurrentCachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/concurrentCache#) GetTokenscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/concurrentCache#GetToken().(ctx contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Contextscip-go gomod github.com/golang/go/src go1.23.0 context/Context#, registry string, scheme Schemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Scheme#, key string) (string, error) { |
| 85 | entryValue, ok := cc.cachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/concurrentCache#cache..Loadscip-go gomod github.com/golang/go/src go1.23.0 sync/Map#Load().(registry) |
| 86 | if !ok { |
| 87 | return "", errdefscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/.ErrNotFoundscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/ErrNotFound. |
| 88 | } |
| 89 | entry := entryValue.(*cacheEntryscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/cacheEntry#) |
| 90 | if entry.schemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/cacheEntry#scheme. != scheme { |
| 91 | return "", errdefscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/.ErrNotFoundscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/ErrNotFound. |
| 92 | } |
| 93 | if token, ok := entry.tokensscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/cacheEntry#tokens..Loadscip-go gomod github.com/golang/go/src go1.23.0 sync/Map#Load().(key); ok { |
| 94 | return token.(string), nil |
| 95 | } |
| 96 | return "", errdefscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/.ErrNotFoundscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/ErrNotFound. |
| 97 | } |
| 98 | |
| 99 | // Set fetches the token using the given fetch function and caches the token |
| 100 | // for the given scheme with the given key for the given registry. |
| 101 | // Set combines the fetch operation if the Set is invoked multiple times at the |
| 102 | // same time. |
| 103 | func (cc *concurrentCachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/concurrentCache#) Setscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/concurrentCache#Set().(ctx contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Contextscip-go gomod github.com/golang/go/src go1.23.0 context/Context#, registry string, scheme Schemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Scheme#, key string, fetch func(contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Contextscip-go gomod github.com/golang/go/src go1.23.0 context/Context#) (string, error)) (string, error) { |
| 104 | // fetch token |
| 105 | statusKey := stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.23.0 strings/Join().([]string{ |
| 106 | registry, |
| 107 | scheme.Stringscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Scheme#String().(), |
| 108 | key, |
| 109 | }, " ") |
| 110 | statusValue, _ := cc.statusscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/concurrentCache#status..LoadOrStorescip-go gomod github.com/golang/go/src go1.23.0 sync/Map#LoadOrStore().(statusKey, syncutilscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/syncutil`/.NewOncescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/syncutil`/NewOnce().()) |
| 111 | fetchOnce := statusValue.(*syncutilscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/syncutil`/.Oncescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/syncutil`/Once#) |
| 112 | fetchedFirst, result, err := fetchOnce.Doscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/syncutil`/Once#Do().(ctx, func() (interface{}, error) { |
| 113 | return fetch(ctx) |
| 114 | }) |
| 115 | if fetchedFirst { |
| 116 | cc.statusscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/concurrentCache#status..Deletescip-go gomod github.com/golang/go/src go1.23.0 sync/Map#Delete().(statusKey) |
| 117 | } |
| 118 | if err != nil { |
| 119 | return "", err |
| 120 | } |
| 121 | token := result.(string) |
| 122 | if !fetchedFirst { |
| 123 | return token, nil |
| 124 | } |
| 125 | |
| 126 | // cache token |
| 127 | newEntry := &cacheEntryscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/cacheEntry#{ |
| 128 | schemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/cacheEntry#scheme.: scheme, |
| 129 | } |
| 130 | entryValue, exists := cc.cachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/concurrentCache#cache..LoadOrStorescip-go gomod github.com/golang/go/src go1.23.0 sync/Map#LoadOrStore().(registry, newEntry) |
| 131 | entry := entryValue.(*cacheEntryscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/cacheEntry#) |
| 132 | if exists && entry.schemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/cacheEntry#scheme. != scheme { |
| 133 | // there is a scheme change, which is not expected in most scenarios. |
| 134 | // force invalidating all previous cache. |
| 135 | entry = newEntry |
| 136 | cc.cachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/concurrentCache#cache..Storescip-go gomod github.com/golang/go/src go1.23.0 sync/Map#Store().(registry, entry) |
| 137 | } |
| 138 | entry.tokensscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/cacheEntry#tokens..Storescip-go gomod github.com/golang/go/src go1.23.0 sync/Map#Store().(key, token) |
| 139 | |
| 140 | return token, nil |
| 141 | } |
| 142 | |
| 143 | // noCache is a cache implementation that does not do cache at all. |
| 144 | type noCachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/noCache# struct{} |
| 145 | |
| 146 | // GetScheme always returns not found error as it has no cache. |
| 147 | func (noCachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/noCache#) GetSchemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/noCache#GetScheme().(ctx contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Contextscip-go gomod github.com/golang/go/src go1.23.0 context/Context#, registry string) (Schemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Scheme#, error) { |
| 148 | return SchemeUnknownscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/SchemeUnknown., errdefscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/.ErrNotFoundscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/ErrNotFound. |
| 149 | } |
| 150 | |
| 151 | // GetToken always returns not found error as it has no cache. |
| 152 | func (noCachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/noCache#) GetTokenscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/noCache#GetToken().(ctx contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Contextscip-go gomod github.com/golang/go/src go1.23.0 context/Context#, registry string, scheme Schemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Scheme#, key string) (string, error) { |
| 153 | return "", errdefscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/.ErrNotFoundscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/errdef`/ErrNotFound. |
| 154 | } |
| 155 | |
| 156 | // Set calls fetch directly without caching. |
| 157 | func (noCachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/noCache#) Setscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/noCache#Set().(ctx contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Contextscip-go gomod github.com/golang/go/src go1.23.0 context/Context#, registry string, scheme Schemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Scheme#, key string, fetch func(contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Contextscip-go gomod github.com/golang/go/src go1.23.0 context/Context#) (string, error)) (string, error) { |
| 158 | return fetch(ctx) |
| 159 | } |
| 160 | |
| 161 | // hostCache is an auth cache that ignores scopes. Uses only the registry's hostname to find a token. |
| 162 | type hostCachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/hostCache# struct { |
| 163 | Cachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/hostCache#Cache. |
| 164 | } |
| 165 | |
| 166 | // GetToken implements Cache. |
| 167 | func (c *hostCachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/hostCache#) GetTokenscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/hostCache#GetToken().(ctx contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Contextscip-go gomod github.com/golang/go/src go1.23.0 context/Context#, registry string, scheme Schemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Scheme#, key string) (string, error) { |
| 168 | return c.Cachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/hostCache#Cache..GetTokenscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Cache#GetToken.(ctx, registry, scheme, "") |
| 169 | } |
| 170 | |
| 171 | // Set implements Cache. |
| 172 | func (c *hostCachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/hostCache#) Setscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/hostCache#Set().(ctx contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Contextscip-go gomod github.com/golang/go/src go1.23.0 context/Context#, registry string, scheme Schemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Scheme#, key string, fetch func(contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Contextscip-go gomod github.com/golang/go/src go1.23.0 context/Context#) (string, error)) (string, error) { |
| 173 | return c.Cachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/hostCache#Cache..Setscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Cache#Set.(ctx, registry, scheme, "", fetch) |
| 174 | } |
| 175 | |
| 176 | // fallbackCache tries the primary cache then falls back to the secondary cache. |
| 177 | type fallbackCachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/fallbackCache# struct { |
| 178 | primaryscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/fallbackCache#primary. Cachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Cache# |
| 179 | secondaryscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/fallbackCache#secondary. Cachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Cache# |
| 180 | } |
| 181 | |
| 182 | // GetScheme implements Cache. |
| 183 | func (fc *fallbackCachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/fallbackCache#) GetSchemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/fallbackCache#GetScheme().(ctx contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Contextscip-go gomod github.com/golang/go/src go1.23.0 context/Context#, registry string) (Schemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Scheme#, error) { |
| 184 | scheme, err := fc.primaryscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/fallbackCache#primary..GetSchemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Cache#GetScheme.(ctx, registry) |
| 185 | if err == nil { |
| 186 | return scheme, nil |
| 187 | } |
| 188 | |
| 189 | // fallback |
| 190 | return fc.secondaryscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/fallbackCache#secondary..GetSchemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Cache#GetScheme.(ctx, registry) |
| 191 | } |
| 192 | |
| 193 | // GetToken implements Cache. |
| 194 | func (fc *fallbackCachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/fallbackCache#) GetTokenscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/fallbackCache#GetToken().(ctx contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Contextscip-go gomod github.com/golang/go/src go1.23.0 context/Context#, registry string, scheme Schemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Scheme#, key string) (string, error) { |
| 195 | token, err := fc.primaryscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/fallbackCache#primary..GetTokenscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Cache#GetToken.(ctx, registry, scheme, key) |
| 196 | if err == nil { |
| 197 | return token, nil |
| 198 | } |
| 199 | |
| 200 | // fallback |
| 201 | return fc.secondaryscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/fallbackCache#secondary..GetTokenscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Cache#GetToken.(ctx, registry, scheme, key) |
| 202 | } |
| 203 | |
| 204 | // Set implements Cache. |
| 205 | func (fc *fallbackCachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/fallbackCache#) Setscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/fallbackCache#Set().(ctx contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Contextscip-go gomod github.com/golang/go/src go1.23.0 context/Context#, registry string, scheme Schemescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Scheme#, key string, fetch func(contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Contextscip-go gomod github.com/golang/go/src go1.23.0 context/Context#) (string, error)) (string, error) { |
| 206 | token, err := fc.primaryscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/fallbackCache#primary..Setscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Cache#Set.(ctx, registry, scheme, key, fetch) |
| 207 | if err != nil { |
| 208 | return "", err |
| 209 | } |
| 210 | |
| 211 | return fc.secondaryscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/fallbackCache#secondary..Setscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Cache#Set.(ctx, registry, scheme, key, func(ctx contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Contextscip-go gomod github.com/golang/go/src go1.23.0 context/Context#) (string, error) { |
| 212 | return token, nil |
| 213 | }) |
| 214 | } |
| 215 | |
| 216 | // NewSingleContextCache creates a host-based cache for optimizing the auth flow for non-compliant registries. |
| 217 | // It is intended to be used in a single context, such as pulling from a single repository. |
| 218 | // This cache should not be shared. |
| 219 | // |
| 220 | // Note: [NewCache] should be used for compliant registries as it can be shared |
| 221 | // across context and will generally make less re-authentication requests. |
| 222 | func NewSingleContextCachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/NewSingleContextCache().() Cachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/Cache# { |
| 223 | cache := NewCachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/NewCache().() |
| 224 | return &fallbackCachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/fallbackCache#{ |
| 225 | primaryscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/fallbackCache#primary.: cache, |
| 226 | // We can re-use the came concurrentCache here because the key space is different |
| 227 | // (keys are always empty for the hostCache) so there is no collision. |
| 228 | // Even if there is a collision it is not an issue. |
| 229 | // Re-using saves a little memory. |
| 230 | secondaryscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/fallbackCache#secondary.: &hostCachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/registry/remote/auth`/hostCache#{cache}, |
| 231 | } |
| 232 | } |