github.com/greenpau/go-authcrunch@v1.1.4/pkg/registry/user_registry.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  	"encoding/json"
    19  	"github.com/greenpau/go-authcrunch/pkg/errors"
    20  	"github.com/greenpau/go-authcrunch/pkg/identity"
    21  	"github.com/greenpau/go-authcrunch/pkg/requests"
    22  	"go.uber.org/zap"
    23  )
    24  
    25  // LocaUserRegistry is a local registry.
    26  type LocaUserRegistry struct {
    27  	db     *identity.Database
    28  	config *UserRegistryConfig
    29  	cache  *RegistrationCache
    30  	logger *zap.Logger
    31  }
    32  
    33  // UserRegistry represents user registry.
    34  type UserRegistry interface {
    35  	// GetRealm() string
    36  	GetName() string
    37  	GetConfig() map[string]interface{}
    38  	// Configure() error
    39  	// Configured() bool
    40  	AddUser(*requests.Request) error
    41  	GetRegistrationEntry(string) (map[string]string, error)
    42  	DeleteRegistrationEntry(string) error
    43  	AddRegistrationEntry(string, map[string]string) error
    44  
    45  	GetUsernamePolicyRegex() string
    46  	GetUsernamePolicySummary() string
    47  	GetPasswordPolicyRegex() string
    48  	GetPasswordPolicySummary() string
    49  
    50  	GetTitle() string
    51  	GetCode() string
    52  	GetRequireAcceptTerms() bool
    53  	GetTermsConditionsLink() string
    54  	GetPrivacyPolicyLink() string
    55  
    56  	GetEmailProvider() string
    57  	GetRequireDomainMailRecord() bool
    58  	GetAdminEmails() []string
    59  
    60  	Notify(map[string]string) error
    61  	GetIdentityStoreName() string
    62  }
    63  
    64  // NewUserRegistry returns UserRegistry instance.
    65  func NewUserRegistry(cfg *UserRegistryConfig, logger *zap.Logger) (UserRegistry, error) {
    66  	var r UserRegistry
    67  	var err error
    68  
    69  	if err := cfg.Validate(); err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	if logger == nil {
    74  		return nil, errors.ErrUserRegistrationConfig.WithArgs(cfg.Name, errors.ErrUserRegistryConfigureLoggerNotFound)
    75  	}
    76  
    77  	db, err := identity.NewDatabase(cfg.Dropbox)
    78  	if err != nil {
    79  		return nil, errors.ErrUserRegistrationConfig.WithArgs(cfg.Name, err)
    80  	}
    81  
    82  	localRegistry := &LocaUserRegistry{
    83  		db:     db,
    84  		config: cfg,
    85  		logger: logger,
    86  		cache:  NewRegistrationCache(),
    87  	}
    88  
    89  	localRegistry.cache.Run()
    90  
    91  	r = localRegistry
    92  	return r, nil
    93  }
    94  
    95  // GetConfig returns user registry configuration.
    96  func (r *LocaUserRegistry) GetConfig() map[string]interface{} {
    97  	var m map[string]interface{}
    98  	j, _ := json.Marshal(r.config)
    99  	json.Unmarshal(j, &m)
   100  	return m
   101  }
   102  
   103  // GetName returns the uid of the user registry.
   104  func (r *LocaUserRegistry) GetName() string {
   105  	return r.config.Name
   106  }
   107  
   108  // AddUser adds user to the user registry.
   109  func (r *LocaUserRegistry) AddUser(rr *requests.Request) error {
   110  	return r.db.AddUser(rr)
   111  }
   112  
   113  // GetRegistrationEntry returns a registration entry by id.
   114  func (r *LocaUserRegistry) GetRegistrationEntry(s string) (map[string]string, error) {
   115  	return r.cache.Get(s)
   116  }
   117  
   118  // DeleteRegistrationEntry deleted a registration entry by id.
   119  func (r *LocaUserRegistry) DeleteRegistrationEntry(s string) error {
   120  	return r.cache.Delete(s)
   121  }
   122  
   123  // AddRegistrationEntry adds a registration entry.
   124  func (r *LocaUserRegistry) AddRegistrationEntry(s string, entry map[string]string) error {
   125  	return r.cache.Add(s, entry)
   126  }
   127  
   128  // GetUsernamePolicyRegex returns username policy regular expression.
   129  func (r *LocaUserRegistry) GetUsernamePolicyRegex() string {
   130  	return r.db.GetUsernamePolicyRegex()
   131  }
   132  
   133  // GetUsernamePolicySummary returns username policy summary.
   134  func (r *LocaUserRegistry) GetUsernamePolicySummary() string {
   135  	return r.db.GetUsernamePolicySummary()
   136  }
   137  
   138  // GetPasswordPolicyRegex returns password policy regular expression.
   139  func (r *LocaUserRegistry) GetPasswordPolicyRegex() string {
   140  	return r.db.GetPasswordPolicyRegex()
   141  }
   142  
   143  // GetPasswordPolicySummary returns password policy summary.
   144  func (r *LocaUserRegistry) GetPasswordPolicySummary() string {
   145  	return r.db.GetPasswordPolicySummary()
   146  }
   147  
   148  // GetTitle returns the title of signup page.
   149  func (r *LocaUserRegistry) GetTitle() string {
   150  	return r.config.Title
   151  }
   152  
   153  // GetCode returns authorization code.
   154  func (r *LocaUserRegistry) GetCode() string {
   155  	return r.config.Code
   156  }
   157  
   158  // GetRequireAcceptTerms returns true if the acceptance of terms is required.
   159  func (r *LocaUserRegistry) GetRequireAcceptTerms() bool {
   160  	return r.config.RequireAcceptTerms
   161  }
   162  
   163  // GetTermsConditionsLink returns the terms and conditions link.
   164  func (r *LocaUserRegistry) GetTermsConditionsLink() string {
   165  	return r.config.TermsConditionsLink
   166  }
   167  
   168  // GetPrivacyPolicyLink returns the privacy policy link.
   169  func (r *LocaUserRegistry) GetPrivacyPolicyLink() string {
   170  	return r.config.PrivacyPolicyLink
   171  }
   172  
   173  // GetAdminEmails returns admin email addresses.
   174  func (r *LocaUserRegistry) GetAdminEmails() []string {
   175  	return r.config.AdminEmails
   176  }
   177  
   178  // GetEmailProvider returns email provider name.
   179  func (r *LocaUserRegistry) GetEmailProvider() string {
   180  	return r.config.EmailProvider
   181  }
   182  
   183  // GetRequireDomainMailRecord returns true if MX record requires validation.
   184  func (r *LocaUserRegistry) GetRequireDomainMailRecord() bool {
   185  	return r.config.RequireDomainMailRecord
   186  }
   187  
   188  // GetIdentityStoreName returns associated identity store name.
   189  func (r *LocaUserRegistry) GetIdentityStoreName() string {
   190  	return r.config.IdentityStore
   191  }