github.com/livekit/protocol@v1.16.1-0.20240517185851-47e4c6bba773/auth/provider.go (about) 1 // Copyright 2023 LiveKit, Inc. 2 // 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 package auth 16 17 import ( 18 "io" 19 20 "gopkg.in/yaml.v3" 21 ) 22 23 type FileBasedKeyProvider struct { 24 keys map[string]string 25 } 26 27 func NewFileBasedKeyProviderFromReader(r io.Reader) (p *FileBasedKeyProvider, err error) { 28 keys := make(map[string]string) 29 decoder := yaml.NewDecoder(r) 30 if err = decoder.Decode(&keys); err != nil { 31 return 32 } 33 p = &FileBasedKeyProvider{ 34 keys: keys, 35 } 36 37 return 38 } 39 40 func NewFileBasedKeyProviderFromMap(keys map[string]string) *FileBasedKeyProvider { 41 return &FileBasedKeyProvider{ 42 keys: keys, 43 } 44 } 45 46 func (p *FileBasedKeyProvider) GetSecret(key string) string { 47 return p.keys[key] 48 } 49 50 func (p *FileBasedKeyProvider) NumKeys() int { 51 return len(p.keys) 52 } 53 54 type SimpleKeyProvider struct { 55 apiKey string 56 apiSecret string 57 } 58 59 func NewSimpleKeyProvider(apiKey, apiSecret string) *SimpleKeyProvider { 60 return &SimpleKeyProvider{ 61 apiKey: apiKey, 62 apiSecret: apiSecret, 63 } 64 } 65 66 func (p *SimpleKeyProvider) GetSecret(key string) string { 67 if key == p.apiKey { 68 return p.apiSecret 69 } 70 return "" 71 } 72 73 func (p *SimpleKeyProvider) NumKeys() int { 74 return 1 75 }