oras.land/oras-go/v2@v2.5.1-0.20240520045656-aef90e4d04c4/registry/remote/credentials/memory_store.go (about)

     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 credentials
    17  
    18  import (
    19  	"context"
    20  	"sync"
    21  
    22  	"oras.land/oras-go/v2/registry/remote/auth"
    23  )
    24  
    25  // memoryStore is a store that keeps credentials in memory.
    26  type memoryStore struct {
    27  	store sync.Map
    28  }
    29  
    30  // NewMemoryStore creates a new in-memory credentials store.
    31  func NewMemoryStore() Store {
    32  	return &memoryStore{}
    33  }
    34  
    35  // Get retrieves credentials from the store for the given server address.
    36  func (ms *memoryStore) Get(_ context.Context, serverAddress string) (auth.Credential, error) {
    37  	cred, found := ms.store.Load(serverAddress)
    38  	if !found {
    39  		return auth.EmptyCredential, nil
    40  	}
    41  	return cred.(auth.Credential), nil
    42  }
    43  
    44  // Put saves credentials into the store for the given server address.
    45  func (ms *memoryStore) Put(_ context.Context, serverAddress string, cred auth.Credential) error {
    46  	ms.store.Store(serverAddress, cred)
    47  	return nil
    48  }
    49  
    50  // Delete removes credentials from the store for the given server address.
    51  func (ms *memoryStore) Delete(_ context.Context, serverAddress string) error {
    52  	ms.store.Delete(serverAddress)
    53  	return nil
    54  }