Skip to content

Commit 1869a78

Browse files
committed
refactor user registration
1 parent df36737 commit 1869a78

21 files changed

+705
-336
lines changed

Makefile

+3-2
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,11 @@ clean:
7979
@rm -rf bin/
8080
@echo "$@: complete"
8181

82-
qtest:
82+
qtest: covdir
8383
@echo "Perform quick tests ..."
8484
@#time richgo test $(VERBOSE) $(TEST) -coverprofile=.coverage/coverage.out -run TestNewConfig ./*.go
85-
@time richgo test $(VERBOSE) $(TEST) -coverprofile=.coverage/coverage.out -run TestNewServer ./*.go
85+
@#time richgo test $(VERBOSE) $(TEST) -coverprofile=.coverage/coverage.out -run TestNewServer ./*.go
86+
@time richgo test $(VERBOSE) $(TEST) -coverprofile=.coverage/coverage.out ./pkg/registry/...
8687
@#time richgo test -v -coverprofile=.coverage/coverage.out internal/tag/*.go
8788
@### time richgo test $(VERBOSE) $(TEST) -coverprofile=.coverage/coverage.out -run TestAuthorize ./pkg/authz/validator/...
8889
@#time richgo test $(VERBOSE) $(TEST) -coverprofile=.coverage/coverage.out -run TestAddProviders ./pkg/messaging/...

config.go

+38-5
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,20 @@ import (
2323
"github.com/greenpau/go-authcrunch/pkg/idp"
2424
"github.com/greenpau/go-authcrunch/pkg/ids"
2525
"github.com/greenpau/go-authcrunch/pkg/messaging"
26+
"github.com/greenpau/go-authcrunch/pkg/registry"
2627
)
2728

2829
// Config is a configuration of Server.
2930
type Config struct {
3031
Credentials *credentials.Config `json:"credentials,omitempty" xml:"credentials,omitempty" yaml:"credentials,omitempty"`
32+
Messaging *messaging.Config `json:"messaging,omitempty" xml:"messaging,omitempty" yaml:"messaging,omitempty"`
3133
AuthenticationPortals []*authn.PortalConfig `json:"authentication_portals,omitempty" xml:"authentication_portals,omitempty" yaml:"authentication_portals,omitempty"`
3234
AuthorizationPolicies []*authz.PolicyConfig `json:"authorization_policies,omitempty" xml:"authorization_policies,omitempty" yaml:"authorization_policies,omitempty"`
33-
Messaging *messaging.Config `json:"messaging,omitempty" xml:"messaging,omitempty" yaml:"messaging,omitempty"`
3435
IdentityStores []*ids.IdentityStoreConfig `json:"identity_stores,omitempty" xml:"identity_stores,omitempty" yaml:"identity_stores,omitempty"`
3536
IdentityProviders []*idp.IdentityProviderConfig `json:"identity_providers,omitempty" xml:"identity_providers,omitempty" yaml:"identity_providers,omitempty"`
3637
disabledIdentityStores map[string]interface{}
3738
disabledIdentityProviders map[string]interface{}
39+
UserRegistries []*registry.UserRegistryConfig `json:"user_registries,omitempty" xml:"user_registries,omitempty" yaml:"user_registries,omitempty"`
3840
}
3941

4042
// NewConfig returns an instance of Config.
@@ -102,13 +104,31 @@ func (cfg *Config) Validate() error {
102104
return fmt.Errorf("no portals and gatekeepers found")
103105
}
104106

105-
for _, portalCfg := range cfg.AuthenticationPortals {
106-
portalCfg.SetCredentials(cfg.Credentials)
107-
portalCfg.SetMessaging(cfg.Messaging)
108-
if err := portalCfg.ValidateCredentials(); err != nil {
107+
identityStoreUserRegistry := make(map[string]string)
108+
for _, userRegistry := range cfg.UserRegistries {
109+
userRegistry.SetCredentials(cfg.Credentials)
110+
userRegistry.SetMessaging(cfg.Messaging)
111+
if err := userRegistry.ValidateMessaging(); err != nil {
109112
return err
110113
}
114+
var identityStoreFound bool
115+
for _, identityStore := range cfg.IdentityStores {
116+
if identityStore.Name == userRegistry.IdentityStore {
117+
identityStoreFound = true
118+
identityStoreUserRegistry[identityStore.Name] = userRegistry.IdentityStore
119+
break
120+
}
121+
}
122+
if !identityStoreFound {
123+
return fmt.Errorf(
124+
"identity store %q referenced in %q user registry not found",
125+
userRegistry.IdentityStore, userRegistry.Name,
126+
)
127+
}
128+
}
111129

130+
// Validate auth portal configurations.
131+
for _, portalCfg := range cfg.AuthenticationPortals {
112132
// If there are no excplicitly specified identity stores and providers in a portal, add all of them.
113133
if len(portalCfg.IdentityStores) == 0 && len(portalCfg.IdentityProviders) == 0 {
114134
for _, entry := range cfg.IdentityStores {
@@ -165,6 +185,10 @@ func (cfg *Config) Validate() error {
165185
authByName[storeName] = "identity store in " + realmName + " realm"
166186
}
167187

188+
// Add regustry store if configured.
189+
if v, exists := identityStoreUserRegistry[storeName]; exists {
190+
portalCfg.UserRegistries = append(portalCfg.UserRegistries, v)
191+
}
168192
}
169193

170194
// Filter out disabled identity store names.
@@ -259,3 +283,12 @@ func (cfg *Config) filterDisabledIdentityProviders(arr []string) []string {
259283
}
260284
return output
261285
}
286+
287+
// AddUserRegistry adds a user registry configuration.
288+
func (cfg *Config) AddUserRegistry(r *registry.UserRegistryConfig) error {
289+
if err := r.Validate(); err != nil {
290+
return err
291+
}
292+
cfg.UserRegistries = append(cfg.UserRegistries, r)
293+
return nil
294+
}

config_test.go

-28
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"github.com/greenpau/go-authcrunch/internal/testutils"
2121
"github.com/greenpau/go-authcrunch/pkg/acl"
2222
"github.com/greenpau/go-authcrunch/pkg/authn"
23-
"github.com/greenpau/go-authcrunch/pkg/authn/registration"
2423
"github.com/greenpau/go-authcrunch/pkg/authz"
2524
"github.com/greenpau/go-authcrunch/pkg/credentials"
2625
"github.com/greenpau/go-authcrunch/pkg/errors"
@@ -226,33 +225,6 @@ func TestNewConfig(t *testing.T) {
226225
errPhase: "AddAuthorizationPolicy",
227226
err: errors.ErrInvalidConfiguration.WithArgs("mygatekeeper", "access list rule config not found"),
228227
},
229-
{
230-
name: "test local auth config with invalid credentials",
231-
identityStores: []*ids.IdentityStoreConfig{
232-
{
233-
Name: "localdb",
234-
Kind: "local",
235-
Params: map[string]interface{}{
236-
"realm": "local",
237-
"path": dbPath,
238-
},
239-
},
240-
},
241-
portals: []*authn.PortalConfig{
242-
{
243-
Name: "myportal",
244-
IdentityStores: []string{
245-
"localdb",
246-
},
247-
UserRegistrationConfig: &registration.Config{
248-
EmailProvider: "default",
249-
},
250-
},
251-
},
252-
shouldErr: true,
253-
errPhase: "Validate",
254-
err: errors.ErrPortalConfigMessagingNil,
255-
},
256228
{
257229
name: "test valid local auth config",
258230
credentials: []credentials.Credential{

internal/tag/tag_test.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/greenpau/go-authcrunch/pkg/authn"
2525
authncache "github.com/greenpau/go-authcrunch/pkg/authn/cache"
2626
"github.com/greenpau/go-authcrunch/pkg/authn/cookie"
27-
"github.com/greenpau/go-authcrunch/pkg/authn/registration"
2827
"github.com/greenpau/go-authcrunch/pkg/authn/transformer"
2928
"github.com/greenpau/go-authcrunch/pkg/authn/ui"
3029
"github.com/greenpau/go-authcrunch/pkg/authproxy"
@@ -45,6 +44,7 @@ import (
4544
"github.com/greenpau/go-authcrunch/pkg/ids/local"
4645
"github.com/greenpau/go-authcrunch/pkg/kms"
4746
"github.com/greenpau/go-authcrunch/pkg/messaging"
47+
"github.com/greenpau/go-authcrunch/pkg/registry"
4848
"github.com/greenpau/go-authcrunch/pkg/requests"
4949
"github.com/greenpau/go-authcrunch/pkg/user"
5050
"github.com/greenpau/go-authcrunch/pkg/util"
@@ -65,6 +65,11 @@ func TestTagCompliance(t *testing.T) {
6565
shouldErr bool
6666
err error
6767
}{
68+
{
69+
name: "test registry.LocaUserRegistry struct",
70+
entry: &registry.LocaUserRegistry{},
71+
opts: &Options{},
72+
},
6873
{
6974
name: "test messaging.FileProvider struct",
7075
entry: &messaging.FileProvider{},
@@ -111,13 +116,13 @@ func TestTagCompliance(t *testing.T) {
111116
opts: &Options{},
112117
},
113118
{
114-
name: "test cache.RegistrationCache struct",
115-
entry: &authncache.RegistrationCache{},
119+
name: "test registry.RegistrationCache struct",
120+
entry: &registry.RegistrationCache{},
116121
opts: &Options{},
117122
},
118123
{
119-
name: "test cache.RegistrationCacheEntry struct",
120-
entry: &authncache.RegistrationCacheEntry{},
124+
name: "test registry.RegistrationCacheEntry struct",
125+
entry: &registry.RegistrationCacheEntry{},
121126
opts: &Options{},
122127
},
123128
{
@@ -442,8 +447,8 @@ func TestTagCompliance(t *testing.T) {
442447
opts: &Options{},
443448
},
444449
{
445-
name: "test registration.Config struct",
446-
entry: &registration.Config{},
450+
name: "test registry.UserRegistryConfig struct",
451+
entry: &registry.UserRegistryConfig{},
447452
opts: &Options{
448453
AllowFieldMismatch: true,
449454
AllowedFields: map[string]interface{}{

pkg/authn/config.go

+2-63
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,14 @@
1515
package authn
1616

1717
import (
18-
// "time"
19-
2018
"github.com/greenpau/go-authcrunch/pkg/acl"
21-
// "github.com/greenpau/go-authcrunch/pkg/authn/cache"
2219
"github.com/greenpau/go-authcrunch/pkg/authn/cookie"
23-
"github.com/greenpau/go-authcrunch/pkg/authn/registration"
2420
"github.com/greenpau/go-authcrunch/pkg/authn/transformer"
2521
"github.com/greenpau/go-authcrunch/pkg/authn/ui"
2622
"github.com/greenpau/go-authcrunch/pkg/authz/options"
27-
// "github.com/greenpau/go-authcrunch/pkg/authz/validator"
28-
"github.com/greenpau/go-authcrunch/pkg/credentials"
2923
"github.com/greenpau/go-authcrunch/pkg/errors"
3024
"github.com/greenpau/go-authcrunch/pkg/kms"
31-
"github.com/greenpau/go-authcrunch/pkg/messaging"
3225
cfgutil "github.com/greenpau/go-authcrunch/pkg/util/cfg"
33-
// "go.uber.org/zap"
3426
"strings"
3527
)
3628

@@ -39,8 +31,6 @@ type PortalConfig struct {
3931
Name string `json:"name,omitempty" xml:"name,omitempty" yaml:"name,omitempty"`
4032
// UI holds the configuration for the user interface.
4133
UI *ui.Parameters `json:"ui,omitempty" xml:"ui,omitempty" yaml:"ui,omitempty"`
42-
// UserRegistrationConfig holds the configuration for the user registration.
43-
UserRegistrationConfig *registration.Config `json:"user_registration_config,omitempty" xml:"user_registration_config,omitempty" yaml:"user_registration_config,omitempty"`
4434
// UserTransformerConfig holds the configuration for the user transformer.
4535
UserTransformerConfigs []*transformer.Config `json:"user_transformer_configs,omitempty" xml:"user_transformer_configs,omitempty" yaml:"user_transformer_configs,omitempty"`
4636
// CookieConfig holds the configuration for the cookies issues by Authenticator.
@@ -49,6 +39,8 @@ type PortalConfig struct {
4939
IdentityStores []string `json:"identity_stores,omitempty" xml:"identity_stores,omitempty" yaml:"identity_stores,omitempty"`
5040
// The names of identity providers.
5141
IdentityProviders []string `json:"identity_providers,omitempty" xml:"identity_providers,omitempty" yaml:"identity_providers,omitempty"`
42+
// The names of user registries.
43+
UserRegistries []string `json:"user_registries,omitempty" xml:"user_registries,omitempty" yaml:"user_registries,omitempty"`
5244
// AccessListConfigs hold the configurations for the ACL of the token validator.
5345
AccessListConfigs []*acl.RuleConfiguration `json:"access_list_configs,omitempty" xml:"access_list_configs,omitempty" yaml:"access_list_configs,omitempty"`
5446
// TokenValidatorOptions holds the configuration for the token validator.
@@ -68,11 +60,6 @@ type PortalConfig struct {
6860

6961
// Indicated that the config was successfully validated.
7062
validated bool
71-
72-
// Shared credentials.
73-
credentials *credentials.Config `json:"credentials,omitempty" xml:"credentials,omitempty" yaml:"credentials,omitempty"`
74-
// Shared messaging.
75-
messaging *messaging.Config `json:"messaging,omitempty" xml:"messaging,omitempty" yaml:"messaging,omitempty"`
7663
}
7764

7865
// AddRawCryptoConfigs adds raw crypto configs.
@@ -123,54 +110,6 @@ func (cfg *PortalConfig) parseRawCryptoConfigs() error {
123110
return nil
124111
}
125112

126-
// SetCredentials binds to shared credentials.
127-
func (cfg *PortalConfig) SetCredentials(c *credentials.Config) {
128-
cfg.credentials = c
129-
return
130-
}
131-
132-
// SetMessaging binds to messaging config.
133-
func (cfg *PortalConfig) SetMessaging(c *messaging.Config) {
134-
cfg.messaging = c
135-
return
136-
}
137-
138-
// ValidateCredentials validates messaging provider and credentials used for
139-
// the user registration.
140-
func (cfg *PortalConfig) ValidateCredentials() error {
141-
if cfg.UserRegistrationConfig == nil {
142-
return nil
143-
}
144-
145-
if cfg.UserRegistrationConfig.EmailProvider == "" {
146-
return nil
147-
}
148-
149-
if cfg.messaging == nil {
150-
return errors.ErrPortalConfigMessagingNil
151-
}
152-
if found := cfg.messaging.FindProvider(cfg.UserRegistrationConfig.EmailProvider); !found {
153-
return errors.ErrPortalConfigMessagingProviderNotFound.WithArgs(cfg.UserRegistrationConfig.EmailProvider)
154-
}
155-
providerCreds := cfg.messaging.FindProviderCredentials(cfg.UserRegistrationConfig.EmailProvider)
156-
if providerCreds == "" {
157-
return errors.ErrPortalConfigMessagingProviderCredentialsNotFound.WithArgs(cfg.UserRegistrationConfig.EmailProvider)
158-
}
159-
if providerCreds != "passwordless" {
160-
if cfg.credentials == nil {
161-
return errors.ErrPortalConfigCredentialsNil
162-
}
163-
if found := cfg.credentials.FindCredential(providerCreds); !found {
164-
return errors.ErrPortalConfigCredentialsNotFound.WithArgs(providerCreds)
165-
}
166-
}
167-
168-
if len(cfg.UserRegistrationConfig.AdminEmails) < 1 {
169-
return errors.ErrPortalConfigAdminEmailNotFound
170-
}
171-
return nil
172-
}
173-
174113
// Validate validates PortalConfig.
175114
func (cfg *PortalConfig) Validate() error {
176115
if cfg.validated {

0 commit comments

Comments
 (0)