storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/config/identity/openid/validators.go (about) 1 /* 2 * MinIO Cloud Storage, (C) 2018-2019 MinIO, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package openid 18 19 import ( 20 "errors" 21 "fmt" 22 "sync" 23 ) 24 25 // ID - holds identification name authentication validator target. 26 type ID string 27 28 // Validator interface describes basic implementation 29 // requirements of various authentication providers. 30 type Validator interface { 31 // Validate is a custom validator function for this provider, 32 // each validation is authenticationType or provider specific. 33 Validate(token string, duration string) (map[string]interface{}, error) 34 35 // ID returns provider name of this provider. 36 ID() ID 37 } 38 39 // ErrTokenExpired - error token expired 40 var ( 41 ErrTokenExpired = errors.New("token expired") 42 ) 43 44 // Validators - holds list of providers indexed by provider id. 45 type Validators struct { 46 sync.RWMutex 47 providers map[ID]Validator 48 } 49 50 // Add - adds unique provider to provider list. 51 func (list *Validators) Add(provider Validator) error { 52 list.Lock() 53 defer list.Unlock() 54 55 if _, ok := list.providers[provider.ID()]; ok { 56 return fmt.Errorf("provider %v already exists", provider.ID()) 57 } 58 59 list.providers[provider.ID()] = provider 60 return nil 61 } 62 63 // List - returns available provider IDs. 64 func (list *Validators) List() []ID { 65 list.RLock() 66 defer list.RUnlock() 67 68 keys := []ID{} 69 for k := range list.providers { 70 keys = append(keys, k) 71 } 72 73 return keys 74 } 75 76 // Get - returns the provider for the given providerID, if not found 77 // returns an error. 78 func (list *Validators) Get(id ID) (p Validator, err error) { 79 list.RLock() 80 defer list.RUnlock() 81 var ok bool 82 if p, ok = list.providers[id]; !ok { 83 return nil, fmt.Errorf("provider %v doesn't exist", id) 84 } 85 return p, nil 86 } 87 88 // NewValidators - creates Validators. 89 func NewValidators() *Validators { 90 return &Validators{providers: make(map[ID]Validator)} 91 }