github.com/greenpau/go-authcrunch@v1.1.4/pkg/messaging/email.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 // EmailProvider represents email messaging provider. 22 type EmailProvider struct { 23 Name string `json:"name,omitempty" xml:"name,omitempty" yaml:"name,omitempty"` 24 Address string `json:"address,omitempty" xml:"address,omitempty" yaml:"address,omitempty"` 25 Protocol string `json:"protocol,omitempty" xml:"protocol,omitempty" yaml:"protocol,omitempty"` 26 Credentials string `json:"credentials,omitempty" xml:"credentials,omitempty" yaml:"credentials,omitempty"` 27 SenderEmail string `json:"sender_email,omitempty" xml:"sender_email,omitempty" yaml:"sender_email,omitempty"` 28 SenderName string `json:"sender_name,omitempty" xml:"sender_name,omitempty" yaml:"sender_name,omitempty"` 29 Templates map[string]string `json:"templates,omitempty" xml:"templates,omitempty" yaml:"templates,omitempty"` 30 Passwordless bool `json:"passwordless,omitempty" xml:"passwordless,omitempty" yaml:"passwordless,omitempty"` 31 BlindCarbonCopy []string `json:"blind_carbon_copy,omitempty" xml:"blind_carbon_copy,omitempty" yaml:"blind_carbon_copy,omitempty"` 32 } 33 34 // Validate validates EmailProvider configuration. 35 func (e *EmailProvider) Validate() error { 36 if e.Name == "" { 37 return errors.ErrMessagingProviderKeyValueEmpty.WithArgs("name") 38 } 39 40 switch { 41 case e.Credentials != "" && e.Passwordless: 42 return errors.ErrMessagingProviderCredentialsWithPasswordless 43 case e.Credentials == "" && !e.Passwordless: 44 return errors.ErrMessagingProviderKeyValueEmpty.WithArgs("credentials") 45 } 46 47 if e.Address == "" { 48 return errors.ErrMessagingProviderKeyValueEmpty.WithArgs("address") 49 } 50 51 switch e.Protocol { 52 case "smtp": 53 case "smtps": 54 case "": 55 return errors.ErrMessagingProviderKeyValueEmpty.WithArgs("protocol") 56 default: 57 return errors.ErrMessagingProviderProtocolUnsupported.WithArgs(e.Protocol) 58 } 59 60 if e.SenderEmail == "" { 61 return errors.ErrMessagingProviderKeyValueEmpty.WithArgs("sender_email") 62 } 63 if e.Templates != nil { 64 for k := range e.Templates { 65 switch k { 66 case "password_recovery": 67 case "registration_confirmation": 68 case "registration_ready": 69 case "registration_verdict": 70 case "mfa_otp": 71 default: 72 return errors.ErrMessagingProviderInvalidTemplate.WithArgs(k) 73 } 74 } 75 } 76 return nil 77 }