github.com/greenpau/go-authcrunch@v1.1.4/pkg/registry/user_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 registry
    16  
    17  import (
    18  	"github.com/greenpau/go-authcrunch/pkg/credentials"
    19  	"github.com/greenpau/go-authcrunch/pkg/errors"
    20  	"github.com/greenpau/go-authcrunch/pkg/messaging"
    21  )
    22  
    23  // UserRegistryConfig represents a common set of configuration settings for user registration
    24  type UserRegistryConfig struct {
    25  	Name string `json:"name,omitempty" xml:"name,omitempty" yaml:"name,omitempty"`
    26  	// The switch determining whether the registration is enabled/disabled.
    27  	Disabled bool `json:"disabled,omitempty" xml:"disabled,omitempty" yaml:"disabled,omitempty"`
    28  	// The title of the registration page
    29  	Title string `json:"title,omitempty" xml:"title,omitempty" yaml:"title,omitempty"`
    30  	// The mandatory registration code. It is possible adding multiple
    31  	// codes, comma separated.
    32  	Code string `json:"code,omitempty" xml:"code,omitempty" yaml:"code,omitempty"`
    33  	// The file path to registration database.
    34  	Dropbox string `json:"dropbox,omitempty" xml:"dropbox,omitempty" yaml:"dropbox,omitempty"`
    35  	// The switch determining whether a user must accept terms and conditions
    36  	RequireAcceptTerms bool `json:"require_accept_terms,omitempty" xml:"require_accept_terms,omitempty" yaml:"require_accept_terms,omitempty"`
    37  	// The switch determining whether the domain associated with an email has
    38  	// a valid MX DNS record.
    39  	RequireDomainMailRecord bool `json:"require_domain_mx,omitempty" xml:"require_domain_mx,omitempty" yaml:"require_domain_mx,omitempty"`
    40  	// The link to terms and conditions document.
    41  	TermsConditionsLink string `json:"terms_conditions_link,omitempty" xml:"terms_conditions_link,omitempty" yaml:"terms_conditions_link,omitempty"`
    42  	// The link to privacy policy document.
    43  	PrivacyPolicyLink string `json:"privacy_policy_link,omitempty" xml:"privacy_policy_link,omitempty" yaml:"privacy_policy_link,omitempty"`
    44  	// The email provider used for the notifications.
    45  	EmailProvider string `json:"email_provider,omitempty" xml:"email_provider,omitempty" yaml:"email_provider,omitempty"`
    46  	// The email address(es) of portal administrators.
    47  	AdminEmails []string `json:"admin_emails,omitempty" xml:"admin_emails,omitempty" yaml:"admin_emails,omitempty"`
    48  	// The name of the identity store associated with the Config.
    49  	IdentityStore string `json:"identity_store,omitempty" xml:"identity_store,omitempty" yaml:"identity_store,omitempty"`
    50  
    51  	credentials *credentials.Config `json:"credentials,omitempty" xml:"credentials,omitempty" yaml:"credentials,omitempty"`
    52  	messaging   *messaging.Config   `json:"messaging,omitempty" xml:"messaging,omitempty" yaml:"messaging,omitempty"`
    53  }
    54  
    55  // Validate validates user registration configuration.
    56  func (cfg *UserRegistryConfig) Validate() error {
    57  	if cfg.Name == "" {
    58  		return errors.ErrUserRegistrationConfig.WithArgs(cfg.Name, "name is not set")
    59  	}
    60  	if cfg.Dropbox == "" {
    61  		return errors.ErrUserRegistrationConfig.WithArgs(cfg.Name, "dropbox is not set")
    62  	}
    63  	if cfg.EmailProvider == "" {
    64  		return errors.ErrUserRegistrationConfig.WithArgs(cfg.Name, "email provider is not set")
    65  	}
    66  	if len(cfg.AdminEmails) < 1 {
    67  		return errors.ErrUserRegistrationConfig.WithArgs(cfg.Name, "admin email address is not set")
    68  	}
    69  	if cfg.Title == "" {
    70  		cfg.Title = "Sign Up"
    71  	}
    72  	if cfg.IdentityStore == "" {
    73  		return errors.ErrUserRegistrationConfig.WithArgs(cfg.Name, "identity store name is not set")
    74  	}
    75  	return nil
    76  }
    77  
    78  // SetCredentials binds to shared credentials.
    79  func (cfg *UserRegistryConfig) SetCredentials(c *credentials.Config) {
    80  	cfg.credentials = c
    81  	return
    82  }
    83  
    84  // SetMessaging binds to messaging config.
    85  func (cfg *UserRegistryConfig) SetMessaging(c *messaging.Config) {
    86  	cfg.messaging = c
    87  	return
    88  }
    89  
    90  // ValidateMessaging validates messaging provider and credentials used for
    91  // the user registration.
    92  func (cfg *UserRegistryConfig) ValidateMessaging() error {
    93  	if cfg.messaging == nil {
    94  		return errors.ErrUserRegistryConfigMessagingNil.WithArgs(cfg.Name)
    95  	}
    96  	if found := cfg.messaging.FindProvider(cfg.EmailProvider); !found {
    97  		return errors.ErrUserRegistryConfigMessagingProviderNotFound.WithArgs(cfg.Name)
    98  	}
    99  
   100  	providerType := cfg.messaging.GetProviderType(cfg.EmailProvider)
   101  
   102  	if providerType == "email" {
   103  		providerCreds := cfg.messaging.FindProviderCredentials(cfg.EmailProvider)
   104  		if providerCreds == "" {
   105  			return errors.ErrUserRegistryConfigMessagingProviderCredentialsNotFound.WithArgs(cfg.Name, cfg.EmailProvider)
   106  		}
   107  
   108  		if providerCreds != "passwordless" {
   109  			if cfg.credentials == nil {
   110  				return errors.ErrUserRegistryConfigCredentialsNil.WithArgs(cfg.Name)
   111  			}
   112  			if found := cfg.credentials.FindCredential(providerCreds); !found {
   113  				return errors.ErrUserRegistryConfigCredentialsNotFound.WithArgs(cfg.Name, providerCreds)
   114  			}
   115  		}
   116  	}
   117  	return nil
   118  }