github.com/greenpau/go-authcrunch@v1.0.50/pkg/messaging/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 messaging
    16  
    17  import (
    18  	"github.com/greenpau/go-authcrunch/pkg/errors"
    19  )
    20  
    21  // Config represents a collection of various messaging providers.
    22  type Config struct {
    23  	EmailProviders []*EmailProvider `json:"email_providers,omitempty" xml:"email_providers,omitempty" yaml:"email_providers,omitempty"`
    24  	FileProviders  []*FileProvider  `json:"file_providers,omitempty" xml:"file_providers,omitempty" yaml:"file_providers,omitempty"`
    25  }
    26  
    27  // Provider is an interface to work with messaging providers.
    28  type Provider interface {
    29  	Validate() error
    30  }
    31  
    32  // Add adds a messaging provider to Config.
    33  func (cfg *Config) Add(c Provider) error {
    34  	switch v := c.(type) {
    35  	case *EmailProvider:
    36  	case *FileProvider:
    37  	default:
    38  		return errors.ErrMessagingAddProviderConfigType.WithArgs(v)
    39  	}
    40  
    41  	if err := c.Validate(); err != nil {
    42  		return err
    43  	}
    44  
    45  	switch v := c.(type) {
    46  	case *EmailProvider:
    47  		cfg.EmailProviders = append(cfg.EmailProviders, v)
    48  	case *FileProvider:
    49  		cfg.FileProviders = append(cfg.FileProviders, v)
    50  	}
    51  	return nil
    52  }
    53  
    54  // FindProvider search for Provider by name.
    55  func (cfg *Config) FindProvider(s string) bool {
    56  	for _, p := range cfg.EmailProviders {
    57  		if p.Name == s {
    58  			return true
    59  		}
    60  	}
    61  	for _, p := range cfg.FileProviders {
    62  		if p.Name == s {
    63  			return true
    64  		}
    65  	}
    66  	return false
    67  }
    68  
    69  // FindProviderCredentials search for Provider by name and then identifies
    70  // the credentials used by the provider.
    71  func (cfg *Config) FindProviderCredentials(s string) string {
    72  	for _, p := range cfg.EmailProviders {
    73  		if p.Name == s {
    74  			if p.Passwordless {
    75  				return "passwordless"
    76  			}
    77  			return p.Credentials
    78  		}
    79  	}
    80  	return ""
    81  }
    82  
    83  // GetProviderType returns type of a messaging provider.
    84  func (cfg *Config) GetProviderType(s string) string {
    85  	for _, p := range cfg.EmailProviders {
    86  		if p.Name == s {
    87  			return "email"
    88  		}
    89  	}
    90  	for _, p := range cfg.FileProviders {
    91  		if p.Name == s {
    92  			return "file"
    93  		}
    94  	}
    95  
    96  	return "unknown"
    97  }
    98  
    99  // ExtractEmailProvider returns EmailProvider by name.
   100  func (cfg *Config) ExtractEmailProvider(s string) *EmailProvider {
   101  	for _, p := range cfg.EmailProviders {
   102  		if p.Name == s {
   103  			return p
   104  		}
   105  	}
   106  	return nil
   107  }
   108  
   109  // ExtractFileProvider returns FileProvider by name.
   110  func (cfg *Config) ExtractFileProvider(s string) *FileProvider {
   111  	for _, p := range cfg.FileProviders {
   112  		if p.Name == s {
   113  			return p
   114  		}
   115  	}
   116  	return nil
   117  }