github.com/greenpau/go-authcrunch@v1.1.4/pkg/credentials/config.go (about) 1 // Copyright 2022 Paul Greenberg greenpau@outlook.com 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 credentials 16 17 import ( 18 "github.com/greenpau/go-authcrunch/pkg/errors" 19 ) 20 21 // Config represents a collection of various credentials. 22 type Config struct { 23 Generic []*Generic `json:"generic,omitempty" xml:"generic,omitempty" yaml:"generic,omitempty"` 24 } 25 26 // Credential is an interface to work with credentials. 27 type Credential interface { 28 Validate() error 29 } 30 31 // Add adds a credential to Config. 32 func (cfg *Config) Add(c Credential) error { 33 switch v := c.(type) { 34 case *Generic: 35 default: 36 return errors.ErrCredAddConfigType.WithArgs(v) 37 } 38 39 if err := c.Validate(); err != nil { 40 return err 41 } 42 43 switch v := c.(type) { 44 case *Generic: 45 cfg.Generic = append(cfg.Generic, v) 46 } 47 return nil 48 } 49 50 // FindCredential search for Credential by name. 51 func (cfg *Config) FindCredential(s string) bool { 52 for _, c := range cfg.Generic { 53 if c.Name == s { 54 return true 55 } 56 } 57 return false 58 } 59 60 // ExtractGeneric returns Generic credentials by name. 61 func (cfg *Config) ExtractGeneric(s string) *Generic { 62 for _, c := range cfg.Generic { 63 if c.Name == s { 64 return c 65 } 66 } 67 return nil 68 }