github.com/whoyao/protocol@v0.0.0-20230519045905-2d8ace718ca5/auth/provider.go (about)

     1  package auth
     2  
     3  import (
     4  	"io"
     5  
     6  	"gopkg.in/yaml.v3"
     7  )
     8  
     9  type FileBasedKeyProvider struct {
    10  	keys map[string]string
    11  }
    12  
    13  func NewFileBasedKeyProviderFromReader(r io.Reader) (p *FileBasedKeyProvider, err error) {
    14  	keys := make(map[string]string)
    15  	decoder := yaml.NewDecoder(r)
    16  	if err = decoder.Decode(&keys); err != nil {
    17  		return
    18  	}
    19  	p = &FileBasedKeyProvider{
    20  		keys: keys,
    21  	}
    22  
    23  	return
    24  }
    25  
    26  func NewFileBasedKeyProviderFromMap(keys map[string]string) *FileBasedKeyProvider {
    27  	return &FileBasedKeyProvider{
    28  		keys: keys,
    29  	}
    30  }
    31  
    32  func (p *FileBasedKeyProvider) GetSecret(key string) string {
    33  	return p.keys[key]
    34  }
    35  
    36  func (p *FileBasedKeyProvider) NumKeys() int {
    37  	return len(p.keys)
    38  }
    39  
    40  type SimpleKeyProvider struct {
    41  	apiKey    string
    42  	apiSecret string
    43  }
    44  
    45  func NewSimpleKeyProvider(apiKey, apiSecret string) *SimpleKeyProvider {
    46  	return &SimpleKeyProvider{
    47  		apiKey:    apiKey,
    48  		apiSecret: apiSecret,
    49  	}
    50  }
    51  
    52  func (p *SimpleKeyProvider) GetSecret(key string) string {
    53  	if key == p.apiKey {
    54  		return p.apiSecret
    55  	}
    56  	return ""
    57  }
    58  
    59  func (p *SimpleKeyProvider) NumKeys() int {
    60  	return 1
    61  }