From a22c8fcb47d1f47d631bb32ba1f129e61d16c818 Mon Sep 17 00:00:00 2001 From: Francis PEROT Date: Wed, 26 Feb 2020 21:44:40 +0100 Subject: [PATCH] [CLOUDTRUST-2314] Fix birthDate attribute format. Attributes management enhancement --- Gopkg.lock | 14 +- Gopkg.toml | 2 +- api/account/api.go | 51 +++---- api/account/api_test.go | 52 +++++--- api/kyc/api.go | 17 +-- api/kyc/api_test.go | 2 +- api/management/api.go | 126 ++++++++---------- api/management/api_test.go | 40 ++++-- api/management/swagger-api_management.yaml | 1 + api/register/api.go | 25 ++-- api/validation/api.go | 53 +++----- api/validation/api_test.go | 8 +- .../{messages => constants}/errormessages.go | 2 +- internal/constants/keycloak.go | 23 ++++ internal/constants/regexp.go | 39 ++++++ internal/keycloakb/configdbmodule.go | 2 +- internal/keycloakb/configdbmodule_test.go | 2 +- internal/keycloakb/datehelpers.go | 2 +- internal/keycloakb/eventsdbmodule.go | 2 +- pkg/account/component.go | 28 +--- pkg/account/component_test.go | 21 ++- pkg/account/endpoint.go | 2 +- pkg/event/endpoint.go | 2 +- pkg/event/http.go | 2 +- pkg/events/component.go | 2 +- pkg/export/component.go | 2 +- pkg/export/http.go | 2 +- pkg/export/module.go | 2 +- pkg/export/storage.go | 2 +- pkg/kyc/component.go | 20 +-- pkg/kyc/component_test.go | 29 +--- pkg/kyc/endpoint.go | 2 +- pkg/kyc/http.go | 8 +- .../management}/authorizationutil.go | 2 +- .../management}/authorizationutil_test.go | 2 +- pkg/management/component.go | 27 ++-- pkg/management/component_test.go | 75 ++++++----- pkg/management/endpoint.go | 2 +- pkg/management/http.go | 42 +++--- pkg/register/endpoint.go | 2 +- pkg/statistics/component.go | 2 +- pkg/statistics/endpoint.go | 2 +- pkg/validation/component.go | 9 +- pkg/validation/endpoint.go | 2 +- 44 files changed, 375 insertions(+), 379 deletions(-) rename internal/{messages => constants}/errormessages.go (99%) create mode 100644 internal/constants/keycloak.go create mode 100644 internal/constants/regexp.go rename {internal/keycloakb => pkg/management}/authorizationutil.go (98%) rename {internal/keycloakb => pkg/management}/authorizationutil_test.go (99%) diff --git a/Gopkg.lock b/Gopkg.lock index 79975901f..07e912fcd 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -42,12 +42,12 @@ revision = "b9f14ada4af152b9fc1c81c349422f4ad4f0fd88" [[projects]] - branch = "master" - digest = "1:999dd3f7c6704616c1eb42ff8ccc55e11ed1aaee6e97c50fd8b24e64b0eeaf1b" + branch = "ct-2314" + digest = "1:719b32ee3fea2854b9cb5d64647644c896321935acb0362c41864ca8bc11f1a0" name = "github.com/cloudtrust/keycloak-client" packages = ["."] pruneopts = "UT" - revision = "214ab9f5ed6947638e3964bb081ef562b7f31b78" + revision = "07d73c9ac42b8cb8b538b78387287714c4a4d700" [[projects]] digest = "1:910c5b111ca13127693df80a59be2c952ad97313626116c0c9895e7b588f2a29" @@ -389,7 +389,7 @@ "pbkdf2", ] pruneopts = "UT" - revision = "2aa609cf4a9d7d1126360de73b55b6002f9e052a" + revision = "78000ba7a073cafc0278790f6bce552a0f25850e" [[projects]] branch = "master" @@ -402,7 +402,7 @@ "publicsuffix", ] pruneopts = "UT" - revision = "0de0cce0169b09b364e001f108dc0399ea8630b3" + revision = "244492dfa37ae2ce87222fd06250a03160745faa" [[projects]] branch = "master" @@ -417,11 +417,11 @@ [[projects]] branch = "master" - digest = "1:578046baf093df15df2904bbb0fe06f3f2385bad59987532201007754aa7e1d5" + digest = "1:8d59aa6f03198132e0ca3bf998677f9698e1e27fc67976c85d85086ac3496530" name = "golang.org/x/sys" packages = ["unix"] pruneopts = "UT" - revision = "d5e6a3e2c0ae16fc7480523ebcb7fd4dd3215489" + revision = "5c8b2ff67527cb88b770f693cebf3799036d8bc0" [[projects]] digest = "1:8d8faad6b12a3a4c819a3f9618cb6ee1fa1cfc33253abeeea8b55336721e3405" diff --git a/Gopkg.toml b/Gopkg.toml index aa7227140..a71da5dcc 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -31,7 +31,7 @@ [[constraint]] name = "github.com/cloudtrust/keycloak-client" - branch = "master" + branch = "ct-2314" [[constraint]] name = "github.com/go-kit/kit" diff --git a/api/account/api.go b/api/account/api.go index 686f093c8..89f271b0b 100644 --- a/api/account/api.go +++ b/api/account/api.go @@ -1,10 +1,9 @@ package account import ( - "strconv" - "github.com/cloudtrust/common-service/validation" - msg "github.com/cloudtrust/keycloak-bridge/internal/messages" + "github.com/cloudtrust/keycloak-bridge/internal/constants" + msg "github.com/cloudtrust/keycloak-bridge/internal/constants" kc "github.com/cloudtrust/keycloak-client" ) @@ -82,27 +81,22 @@ func ConvertToAPIAccount(userKc kc.UserRepresentation) AccountRepresentation { userRep.FirstName = userKc.FirstName userRep.LastName = userKc.LastName - if userKc.Attributes != nil { - var m = *userKc.Attributes - - if value, ok := m["phoneNumber"]; ok && len(value) > 0 { - userRep.PhoneNumber = &value[0] - } - if value, ok := m["gender"]; ok && len(value) > 0 { - userRep.Gender = &value[0] - } - if value, ok := m["birthDate"]; ok && len(value) > 0 { - userRep.BirthDate = &value[0] - } - if value, ok := m["locale"]; ok && len(value) > 0 { - userRep.Locale = &value[0] - } - if value, ok := m["phoneNumberVerified"]; ok && len(value) > 0 { - if verified, err := strconv.ParseBool(value[0]); err == nil { - userRep.PhoneNumberVerified = &verified - } - } + if value := userKc.GetAttributeString(constants.AttrbPhoneNumber); value != nil { + userRep.PhoneNumber = value + } + if value := userKc.GetAttributeString(constants.AttrbGender); value != nil { + userRep.Gender = value + } + if value := userKc.GetAttributeDate(constants.AttrbBirthDate, constants.SupportedDateLayouts); value != nil { + userRep.BirthDate = value + } + if value := userKc.GetAttributeString(constants.AttrbLocale); value != nil { + userRep.Locale = value + } + if verified, err := userKc.GetAttributeBool(constants.AttrbPhoneNumberVerified); err == nil && verified != nil { + userRep.PhoneNumberVerified = verified } + return userRep } @@ -115,14 +109,9 @@ func ConvertToKCUser(user AccountRepresentation) kc.UserRepresentation { userRep.FirstName = user.FirstName userRep.LastName = user.LastName - var attributes = make(map[string][]string) - - if user.PhoneNumber != nil { - attributes["phoneNumber"] = []string{*user.PhoneNumber} - } - if user.Locale != nil { - attributes["locale"] = []string{*user.Locale} - } + var attributes = make(kc.Attributes) + attributes.SetStringWhenNotNil(constants.AttrbPhoneNumber, user.PhoneNumber) + attributes.SetStringWhenNotNil(constants.AttrbLocale, user.Locale) if len(attributes) > 0 { userRep.Attributes = &attributes diff --git a/api/account/api_test.go b/api/account/api_test.go index 4b0021a59..e640892eb 100644 --- a/api/account/api_test.go +++ b/api/account/api_test.go @@ -3,6 +3,8 @@ package account import ( "testing" + "github.com/cloudtrust/keycloak-bridge/internal/constants" + kc "github.com/cloudtrust/keycloak-client" "github.com/stretchr/testify/assert" ) @@ -30,27 +32,35 @@ func TestConvertToAPIAccount(t *testing.T) { var kcUser = kc.UserRepresentation{} assert.Nil(t, nil, ConvertToAPIAccount(kcUser)) - var attributes = make(map[string][]string) - kcUser = kc.UserRepresentation{Attributes: &attributes} - assert.Nil(t, nil, ConvertToAPIAccount(kcUser).PhoneNumber) - - attributes["phoneNumber"] = []string{"+41221234567"} - attributes["gender"] = []string{"M"} - attributes["birthDate"] = []string{"15.02.1920"} - attributes["locale"] = []string{"fr"} - attributes["phoneNumberVerified"] = []string{"true"} - kcUser = kc.UserRepresentation{Attributes: &attributes} - - var user = ConvertToAPIAccount(kcUser) - assert.Equal(t, "+41221234567", *user.PhoneNumber) - assert.Equal(t, "M", *user.Gender) - assert.Equal(t, "15.02.1920", *user.BirthDate) - assert.Equal(t, "fr", *user.Locale) - assert.True(t, *user.PhoneNumberVerified) - - attributes["phoneNumberVerified"] = []string{"vielleicht"} - user = ConvertToAPIAccount(kcUser) - assert.Nil(t, user.PhoneNumberVerified) + t.Run("Empty attributes", func(t *testing.T) { + var attributes = make(kc.Attributes) + kcUser = kc.UserRepresentation{Attributes: &attributes} + assert.Nil(t, nil, ConvertToAPIAccount(kcUser).PhoneNumber) + }) + + var attributes = kc.Attributes{ + "phoneNumber": []string{"+41221234567"}, + "gender": []string{"M"}, + "birthDate": []string{"15.02.1920"}, + "locale": []string{"fr"}, + "phoneNumberVerified": []string{"true"}, + } + + t.Run("Check attributes are copied", func(t *testing.T) { + kcUser = kc.UserRepresentation{Attributes: &attributes} + var user = ConvertToAPIAccount(kcUser) + assert.Equal(t, "+41221234567", *user.PhoneNumber) + assert.Equal(t, "M", *user.Gender) + assert.Equal(t, "15.02.1920", *user.BirthDate) + assert.Equal(t, "fr", *user.Locale) + assert.True(t, *user.PhoneNumberVerified) + }) + + t.Run("PhoneNumberVerified is invalid", func(t *testing.T) { + attributes.SetString(constants.AttrbPhoneNumberVerified, "vielleicht") + var user = ConvertToAPIAccount(kcUser) + assert.Nil(t, user.PhoneNumberVerified) + }) } func TestConvertToKCUser(t *testing.T) { diff --git a/api/kyc/api.go b/api/kyc/api.go index 0ab322775..501990d0c 100644 --- a/api/kyc/api.go +++ b/api/kyc/api.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/cloudtrust/common-service/validation" + "github.com/cloudtrust/keycloak-bridge/internal/constants" kc "github.com/cloudtrust/keycloak-client" ) @@ -82,24 +83,20 @@ func (u *UserRepresentation) UserToJSON() string { func (u *UserRepresentation) ExportToKeycloak(kcUser *kc.UserRepresentation) { var bFalse = false var bTrue = true - var attributes = make(map[string][]string) + var attributes = make(kc.Attributes) if kcUser.Attributes != nil { attributes = *kcUser.Attributes } - if u.Gender != nil { - attributes["gender"] = []string{*u.Gender} - } + attributes.SetStringWhenNotNil(constants.AttrbGender, u.Gender) if u.PhoneNumber != nil { - if value, ok := attributes["phoneNumber"]; !ok || (len(value) > 0 && value[0] != *u.PhoneNumber) { - attributes["phoneNumber"] = []string{*u.PhoneNumber} - attributes["phoneNumberVerified"] = []string{"false"} + if value := attributes.GetString(constants.AttrbPhoneNumber); value == nil || *value != *u.PhoneNumber { + attributes.SetString(constants.AttrbPhoneNumber, *u.PhoneNumber) + attributes.SetBool(constants.AttrbPhoneNumberVerified, false) } } - if u.BirthDate != nil { - attributes["birthDate"] = []string{*u.BirthDate} - } + attributes.SetDateWhenNotNil(constants.AttrbBirthDate, u.BirthDate, constants.SupportedDateLayouts) if u.Username != nil { kcUser.Username = u.Username diff --git a/api/kyc/api_test.go b/api/kyc/api_test.go index 21d75584e..1f111032a 100644 --- a/api/kyc/api_test.go +++ b/api/kyc/api_test.go @@ -46,7 +46,7 @@ func createValidKeycloakUser() kc.UserRepresentation { firstName = "Marc" lastName = "El-Bichoun" email = "marcel.bichon@elca.ch" - attributes = map[string][]string{ + attributes = kc.Attributes{ "gender": []string{"M"}, "phoneNumber": []string{"00 33 686 550011"}, "phoneNumberVerified": []string{"true"}, diff --git a/api/management/api.go b/api/management/api.go index 1dccb8c7c..37dfeea4c 100644 --- a/api/management/api.go +++ b/api/management/api.go @@ -8,7 +8,7 @@ import ( "github.com/cloudtrust/common-service/configuration" "github.com/cloudtrust/common-service/validation" - internal "github.com/cloudtrust/keycloak-bridge/internal/messages" + "github.com/cloudtrust/keycloak-bridge/internal/constants" kc "github.com/cloudtrust/keycloak-client" ) @@ -250,31 +250,14 @@ func ConvertToKCUser(user UserRepresentation) kc.UserRepresentation { userRep.Groups = user.Groups userRep.RealmRoles = user.Roles - var attributes = make(map[string][]string) + var attributes = make(kc.Attributes) - if user.PhoneNumber != nil { - attributes["phoneNumber"] = []string{*user.PhoneNumber} - } - - if user.Label != nil { - attributes["label"] = []string{*user.Label} - } - - if user.Gender != nil { - attributes["gender"] = []string{*user.Gender} - } - - if user.BirthDate != nil { - attributes["birthDate"] = []string{*user.BirthDate} - } - - if user.PhoneNumberVerified != nil { - attributes["phoneNumberVerified"] = []string{strconv.FormatBool(*user.PhoneNumberVerified)} - } - - if user.Locale != nil { - attributes["locale"] = []string{*user.Locale} - } + attributes.SetStringWhenNotNil(constants.AttrbPhoneNumber, user.PhoneNumber) + attributes.SetBoolWhenNotNil(constants.AttrbPhoneNumberVerified, user.PhoneNumberVerified) + attributes.SetStringWhenNotNil(constants.AttrbLabel, user.Label) + attributes.SetStringWhenNotNil(constants.AttrbGender, user.Gender) + attributes.SetDateWhenNotNil(constants.AttrbBirthDate, user.BirthDate, constants.SupportedDateLayouts) + attributes.SetStringWhenNotNil(constants.AttrbLocale, user.Locale) if len(attributes) > 0 { userRep.Attributes = &attributes @@ -416,26 +399,26 @@ func NewBackOfficeConfigurationFromJSON(confJSON string) (BackOfficeConfiguratio // Validate is a validator for UserRepresentation func (user UserRepresentation) Validate() error { var v = validation.NewParameterValidator(). - ValidateParameterRegExp(internal.UserID, user.ID, RegExpID, false). - ValidateParameterRegExp(internal.Username, user.Username, RegExpUsername, false). - ValidateParameterRegExp(internal.Email, user.Email, RegExpEmail, false). - ValidateParameterRegExp(internal.Firstname, user.FirstName, RegExpFirstName, false). - ValidateParameterRegExp(internal.Lastname, user.LastName, RegExpLastName, false). - ValidateParameterRegExp(internal.PhoneNumber, user.PhoneNumber, RegExpPhoneNumber, false). - ValidateParameterRegExp(internal.Label, user.Label, RegExpLabel, false). - ValidateParameterRegExp(internal.Gender, user.Gender, RegExpGender, false). - ValidateParameterRegExp(internal.Birthdate, user.BirthDate, RegExpBirthDate, false). - ValidateParameterRegExp(internal.Locale, user.Locale, RegExpLocale, false) + ValidateParameterRegExp(constants.UserID, user.ID, constants.RegExpID, false). + ValidateParameterRegExp(constants.Username, user.Username, constants.RegExpUsername, false). + ValidateParameterRegExp(constants.Email, user.Email, constants.RegExpEmail, false). + ValidateParameterRegExp(constants.Firstname, user.FirstName, constants.RegExpFirstName, false). + ValidateParameterRegExp(constants.Lastname, user.LastName, constants.RegExpLastName, false). + ValidateParameterRegExp(constants.PhoneNumber, user.PhoneNumber, constants.RegExpPhoneNumber, false). + ValidateParameterRegExp(constants.Label, user.Label, constants.RegExpLabel, false). + ValidateParameterRegExp(constants.Gender, user.Gender, constants.RegExpGender, false). + ValidateParameterDateMultipleLayout(constants.Birthdate, user.BirthDate, constants.SupportedDateLayouts, false). + ValidateParameterRegExp(constants.Locale, user.Locale, constants.RegExpLocale, false) if user.Groups != nil { for _, groupID := range *(user.Groups) { - v = v.ValidateParameterRegExp(internal.GroupID, &groupID, RegExpID, true) + v = v.ValidateParameterRegExp(constants.GroupID, &groupID, constants.RegExpID, true) } } if user.Roles != nil { for _, roleID := range *(user.Roles) { - v = v.ValidateParameterRegExp(internal.RoleID, &roleID, RegExpID, true) + v = v.ValidateParameterRegExp(constants.RoleID, &roleID, constants.RegExpID, true) } } @@ -445,35 +428,35 @@ func (user UserRepresentation) Validate() error { // Validate is a validator for RoleRepresentation func (role RoleRepresentation) Validate() error { return validation.NewParameterValidator(). - ValidateParameterRegExp(internal.RoleID, role.ID, RegExpID, false). - ValidateParameterRegExp(internal.Username, role.Name, RegExpName, false). - ValidateParameterRegExp(internal.Description, role.Description, RegExpDescription, false). - ValidateParameterRegExp(internal.ContainerID, role.ContainerID, RegExpID, false). + ValidateParameterRegExp(constants.RoleID, role.ID, constants.RegExpID, false). + ValidateParameterRegExp(constants.Username, role.Name, constants.RegExpName, false). + ValidateParameterRegExp(constants.Description, role.Description, constants.RegExpDescription, false). + ValidateParameterRegExp(constants.ContainerID, role.ContainerID, constants.RegExpID, false). Status() } // Validate is a validator for GroupRepresentation func (group GroupRepresentation) Validate() error { return validation.NewParameterValidator(). - ValidateParameterRegExp(internal.GroupName, group.ID, RegExpID, false). - ValidateParameterRegExp(internal.Name, group.Name, RegExpName, false). + ValidateParameterRegExp(constants.GroupName, group.ID, constants.RegExpID, false). + ValidateParameterRegExp(constants.Name, group.Name, constants.RegExpName, false). Status() } // Validate is a validator for PasswordRepresentation func (password PasswordRepresentation) Validate() error { return validation.NewParameterValidator(). - ValidateParameterRegExp(internal.Password, password.Value, RegExpPassword, false). + ValidateParameterRegExp(constants.Password, password.Value, constants.RegExpPassword, false). Status() } // Validate is a validator for RealmCustomConfiguration func (config RealmCustomConfiguration) Validate() error { return validation.NewParameterValidator(). - ValidateParameterRegExp(internal.DefaultClientID, config.DefaultClientID, RegExpClientID, false). - ValidateParameterRegExp(internal.DefaultRedirectURI, config.DefaultRedirectURI, RegExpRedirectURI, false). - ValidateParameterRegExp(internal.RedirectCancelledRegistrationURL, config.RedirectCancelledRegistrationURL, RegExpRedirectURI, false). - ValidateParameterRegExp(internal.RedirectSuccessfulRegistrationURL, config.RedirectSuccessfulRegistrationURL, RegExpRedirectURI, false). + ValidateParameterRegExp(constants.DefaultClientID, config.DefaultClientID, constants.RegExpClientID, false). + ValidateParameterRegExp(constants.DefaultRedirectURI, config.DefaultRedirectURI, constants.RegExpRedirectURI, false). + ValidateParameterRegExp(constants.RedirectCancelledRegistrationURL, config.RedirectCancelledRegistrationURL, constants.RegExpRedirectURI, false). + ValidateParameterRegExp(constants.RedirectSuccessfulRegistrationURL, config.RedirectSuccessfulRegistrationURL, constants.RegExpRedirectURI, false). Status() } @@ -482,7 +465,7 @@ func (requiredAction RequiredAction) Validate() error { if requiredAction != "" { var value = string(requiredAction) return validation.NewParameterValidator(). - ValidateParameterRegExp(internal.RequiredAction, &value, RegExpRequiredAction, true). + ValidateParameterRegExp(constants.RequiredAction, &value, constants.RegExpRequiredAction, true). Status() } return nil @@ -491,44 +474,43 @@ func (requiredAction RequiredAction) Validate() error { // Validate is a validator for FederatedIdentityRepresentation func (fedID FederatedIdentityRepresentation) Validate() error { return validation.NewParameterValidator(). - ValidateParameterRegExp(internal.UserID, fedID.UserID, RegExpID, true). - ValidateParameterRegExp(internal.Username, fedID.Username, RegExpUsername, true). + ValidateParameterRegExp(constants.UserID, fedID.UserID, constants.RegExpID, true). + ValidateParameterRegExp(constants.Username, fedID.Username, constants.RegExpUsername, true). Status() } // Regular expressions for parameters validation const ( - RegExpID = `^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$` - RegExpName = `^[a-zA-Z0-9-_]{1,128}$` - RegExpDescription = `^.{1,255}$` + RegExpID = constants.RegExpID + RegExpName = constants.RegExpName + RegExpDescription = constants.RegExpDescription // Client - RegExpClientID = `^[a-zA-Z0-9-_.]{1,255}$` + RegExpClientID = constants.RegExpClientID // User - RegExpUsername = `^[a-zA-Z0-9-_.]{1,128}$` - RegExpEmail = `^.+\@.+\..+` - RegExpFirstName = `^.{1,128}$` - RegExpLastName = `^.{1,128}$` - RegExpPhoneNumber = `^\+[1-9]\d{1,14}$` - RegExpLabel = `^.{1,255}$` - RegExpGender = `^[MF]$` - RegExpBirthDate = `^(\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))$` - RegExpLocale = `^[a-z]{2}$` + RegExpUsername = constants.RegExpUsername + RegExpEmail = constants.RegExpEmail + RegExpFirstName = constants.RegExpFirstName + RegExpLastName = constants.RegExpLastName + RegExpPhoneNumber = constants.RegExpPhoneNumber + RegExpLabel = constants.RegExpLabel + RegExpGender = constants.RegExpGender + RegExpLocale = constants.RegExpLocale // Password - RegExpPassword = `^.{1,255}$` + RegExpPassword = constants.RegExpPassword // RealmCustomConfiguration - RegExpRedirectURI = `^\w+:(\/?\/?)[^\s]+$` + RegExpRedirectURI = constants.RegExpRedirectURI // RequiredAction - RegExpRequiredAction = `^[a-zA-Z0-9-_]{1,255}$` + RegExpRequiredAction = constants.RegExpRequiredAction // Others - RegExpRealmName = `^[a-zA-Z0-9_-]{1,36}$` - RegExpSearch = `^.{1,128}$` - RegExpLifespan = `^[0-9]{1,10}$` - RegExpGroupIds = `^([a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12})(,[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}){0,20}$` - RegExpNumber = `^\d+$` + RegExpRealmName = constants.RegExpRealmName + RegExpSearch = constants.RegExpSearch + RegExpLifespan = constants.RegExpLifespan + RegExpGroupIds = constants.RegExpGroupIds + RegExpNumber = constants.RegExpNumber ) diff --git a/api/management/api_test.go b/api/management/api_test.go index 20499d5e2..9a10ff5e5 100644 --- a/api/management/api_test.go +++ b/api/management/api_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/cloudtrust/common-service/configuration" + "github.com/cloudtrust/keycloak-bridge/internal/constants" kc "github.com/cloudtrust/keycloak-client" "github.com/stretchr/testify/assert" ) @@ -31,54 +32,55 @@ func TestConvertCredential(t *testing.T) { func TestConvertToAPIUser(t *testing.T) { var kcUser kc.UserRepresentation - m := make(map[string][]string) + m := make(kc.Attributes) // Phone number assert.Nil(t, ConvertToAPIUser(kcUser).PhoneNumber) kcUser.Attributes = &m - m["phoneNumber"] = []string{"+4122555555"} + m.SetString(constants.AttrbPhoneNumber, "+4122555555") assert.NotNil(t, ConvertToAPIUser(kcUser).PhoneNumber) // Label assert.Nil(t, ConvertToAPIUser(kcUser).Label) kcUser.Attributes = &m - m["label"] = []string{"a label"} + m.SetString(constants.AttrbLabel, "a label") assert.NotNil(t, ConvertToAPIUser(kcUser).Label) // Gender assert.Nil(t, ConvertToAPIUser(kcUser).Gender) kcUser.Attributes = &m - m["gender"] = []string{"a gender"} + m.SetString(constants.AttrbGender, "a gender") assert.NotNil(t, ConvertToAPIUser(kcUser).Gender) // Birthdate assert.Nil(t, ConvertToAPIUser(kcUser).BirthDate) kcUser.Attributes = &m - m["birthDate"] = []string{"25/12/0"} + m.SetString(constants.AttrbBirthDate, "25/12/0") assert.NotNil(t, ConvertToAPIUser(kcUser).BirthDate) // PhoneNumberVerified assert.Nil(t, ConvertToAPIUser(kcUser).PhoneNumberVerified) kcUser.Attributes = &m - m["phoneNumberVerified"] = []string{"true"} + m.SetBool(constants.AttrbPhoneNumberVerified, true) assert.True(t, *ConvertToAPIUser(kcUser).PhoneNumberVerified) // Locale assert.Nil(t, ConvertToAPIUser(kcUser).Locale) kcUser.Attributes = &m - m["locale"] = []string{"en"} + m.SetString(constants.AttrbLocale, "en") assert.NotNil(t, *ConvertToAPIUser(kcUser).Locale) // SmsSent assert.Nil(t, ConvertToAPIUser(kcUser).SmsSent) kcUser.Attributes = &m + m.SetInt(constants.AttrbSmsSent, 0) m["smsSent"] = []string{"0"} assert.NotNil(t, *ConvertToAPIUser(kcUser).SmsSent) // trustID groups assert.Nil(t, ConvertToAPIUser(kcUser).TrustIDGroups) kcUser.Attributes = &m - m["trustIDGroups"] = []string{"en"} + m.SetString(constants.AttrbTrustIDGroups, "en") assert.NotNil(t, *ConvertToAPIUser(kcUser).TrustIDGroups) } @@ -450,6 +452,28 @@ func TestValidateRequiredAction(t *testing.T) { action := RequiredAction("^") assert.NotNil(t, action.Validate()) + + action = RequiredAction("") + assert.Nil(t, action.Validate()) +} + +func TestValidateFederatedIdentityRepresentation(t *testing.T) { + var userID = "abcd1234-abcd-1234-efgh-abcd1234efgh" + var username = "abcdef" + var invalid = "invalid" + var fi FederatedIdentityRepresentation + + fi.UserID = &userID + fi.Username = &username + assert.Nil(t, fi.Validate()) + + fi.UserID = &invalid + fi.Username = &username + assert.NotNil(t, fi.Validate()) + + fi.UserID = &userID + fi.Username = nil + assert.NotNil(t, fi.Validate()) } func createValidUserRepresentation() UserRepresentation { diff --git a/api/management/swagger-api_management.yaml b/api/management/swagger-api_management.yaml index 21a3576f3..8c3b4d671 100644 --- a/api/management/swagger-api_management.yaml +++ b/api/management/swagger-api_management.yaml @@ -1124,6 +1124,7 @@ components: type: string birthDate: type: string + description: format is DD.MM.YYYY createdTimestamp: type: integer format: int64 diff --git a/api/register/api.go b/api/register/api.go index 708d52662..95c50452f 100644 --- a/api/register/api.go +++ b/api/register/api.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/cloudtrust/common-service/validation" + "github.com/cloudtrust/keycloak-bridge/internal/constants" kc "github.com/cloudtrust/keycloak-client" ) @@ -57,8 +58,6 @@ const ( // Multiple values with digits and letters separated by a single separator (space, dash) regExpIDDocumentNumber = `^([\w\d]+([ -][\w\d]+)*){1,50}$` regExpLocale = `^\w{2}(-\w{2})?$` - - dateLayout = "02.01.2006" ) var ( @@ -86,22 +85,16 @@ func (u *UserRepresentation) ConvertToKeycloak() kc.UserRepresentation { var ( bTrue = true bFalse = false - attributes = make(map[string][]string) + attributes = make(kc.Attributes) ) - if u.Gender != nil { - attributes["gender"] = []string{*u.Gender} - } + attributes.SetStringWhenNotNil(constants.AttrbGender, u.Gender) if u.PhoneNumber != nil { - attributes["phoneNumber"] = []string{*u.PhoneNumber} - attributes["phoneNumberVerified"] = []string{"false"} - } - if u.BirthDate != nil { - attributes["birthDate"] = []string{*u.BirthDate} - } - if u.Locale != nil { - attributes["locale"] = []string{*u.Locale} + attributes.SetString(constants.AttrbPhoneNumber, *u.PhoneNumber) + attributes.SetBool(constants.AttrbPhoneNumberVerified, false) } + attributes.SetDateWhenNotNil(constants.AttrbBirthDate, u.BirthDate, constants.SupportedDateLayouts) + attributes.SetStringWhenNotNil(constants.AttrbLocale, u.Locale) return kc.UserRepresentation{ Username: u.Username, @@ -122,11 +115,11 @@ func (u *UserRepresentation) Validate() error { ValidateParameterRegExp(prmUserLastName, u.LastName, regExpLastName, true). ValidateParameterRegExp(prmUserEmail, u.EmailAddress, regExpEmail, true). ValidateParameterPhoneNumber(prmUserPhoneNumber, u.PhoneNumber, true). - ValidateParameterDate(prmUserBirthDate, u.BirthDate, dateLayout, true). + ValidateParameterDateMultipleLayout(prmUserBirthDate, u.BirthDate, constants.SupportedDateLayouts, true). ValidateParameterRegExp(prmUserBirthLocation, u.BirthLocation, regExpBirthLocation, true). ValidateParameterIn(prmUserIDDocumentType, u.IDDocumentType, allowedDocumentType, true). ValidateParameterRegExp(prmUserIDDocumentNumber, u.IDDocumentNumber, regExpIDDocumentNumber, true). - ValidateParameterDate(prmUserIDDocumentExpiration, u.IDDocumentExpiration, dateLayout, true). + ValidateParameterDateMultipleLayout(prmUserIDDocumentExpiration, u.IDDocumentExpiration, constants.SupportedDateLayouts, true). ValidateParameterRegExp(prmUserLocale, u.Locale, regExpLocale, true). Status() } diff --git a/api/validation/api.go b/api/validation/api.go index fb0248c84..e3a9c662a 100644 --- a/api/validation/api.go +++ b/api/validation/api.go @@ -1,10 +1,10 @@ package validation import ( - "strconv" "time" "github.com/cloudtrust/common-service/validation" + "github.com/cloudtrust/keycloak-bridge/internal/constants" "github.com/cloudtrust/keycloak-bridge/internal/dto" kc "github.com/cloudtrust/keycloak-client" ) @@ -60,20 +60,18 @@ const ( prmCheckNature = "check_nature" prmCheckProofType = "check_proof_type" - RegExpID = `^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$` - regExpNames = `^([\wàáâäçèéêëìíîïñòóôöùúûüß]+([ '-][\wàáâäçèéêëìíîïñòóôöùúûüß]+)*){1,50}$` - regExpFirstName = regExpNames - regExpLastName = regExpNames - regExpEmail = `^.+\@.+\..+$` - regExpBirthLocation = regExpNames + RegExpID = constants.RegExpID + regExpNames = constants.RegExpNameSpecialChars + regExpFirstName = constants.RegExpNameSpecialChars + regExpLastName = constants.RegExpNameSpecialChars + regExpEmail = constants.RegExpEmail + regExpBirthLocation = constants.RegExpNameSpecialChars // Multiple values with digits and letters separated by a single separator (space, dash) regExpIDDocumentNumber = `^([\w\d]+([ -][\w\d]+)*){1,50}$` regExpOperator = `[a-zA-Z0-9_-]{1,255}` regExpNature = `[a-zA-Z0-9_-]{1,255}` regExpProofType = `[a-zA-Z0-9_-]{1,255}` - - DateLayout = "02.01.2006" ) var ( @@ -108,25 +106,20 @@ func (c *CheckRepresentation) ConvertToDBCheck() dto.DBCheck { func (u *UserRepresentation) ExportToKeycloak(kcUser *kc.UserRepresentation) { var bFalse = false var bTrue = true - var attributes = make(map[string][]string) + var attributes = make(kc.Attributes) if kcUser.Attributes != nil { attributes = *kcUser.Attributes } - if u.Gender != nil { - attributes["gender"] = []string{*u.Gender} - } + attributes.SetStringWhenNotNil(constants.AttrbGender, u.Gender) if u.PhoneNumber != nil { - if value, ok := attributes["phoneNumber"]; !ok || (len(value) > 0 && value[0] != *u.PhoneNumber) { - attributes["phoneNumber"] = []string{*u.PhoneNumber} - attributes["phoneNumberVerified"] = []string{"false"} + if phoneNumber := attributes.GetString(constants.AttrbPhoneNumber); phoneNumber == nil || *phoneNumber != *u.PhoneNumber { + attributes.SetString(constants.AttrbPhoneNumber, *u.PhoneNumber) + attributes.SetBool(constants.AttrbPhoneNumberVerified, false) } } - if u.BirthDate != nil { - var birthDate = *u.BirthDate - attributes["birthDate"] = []string{birthDate.Format(DateLayout)} - } + attributes.SetTimeWhenNotNil(constants.AttrbBirthDate, u.BirthDate, constants.SupportedDateLayouts[0]) if u.Username != nil { kcUser.Username = u.Username @@ -153,21 +146,17 @@ func (u *UserRepresentation) ImportFromKeycloak(kcUser kc.UserRepresentation) { var birthdate = u.BirthDate if kcUser.Attributes != nil { - var m = *kcUser.Attributes - if value, ok := m["phoneNumber"]; ok && len(value) > 0 { - phoneNumber = &value[0] + if pn := kcUser.GetAttributeString(constants.AttrbPhoneNumber); pn != nil { + phoneNumber = pn } - if value, ok := m["phoneNumberVerified"]; ok && len(value) > 0 { - if verified, err := strconv.ParseBool(value[0]); err == nil { - phoneNumberVerified = &verified - } + if value, err := kcUser.GetAttributeBool(constants.AttrbPhoneNumberVerified); err == nil && value != nil { + phoneNumberVerified = value } - if value, ok := m["gender"]; ok && len(value) > 0 { - gender = &value[0] + if value := kcUser.GetAttributeString(constants.AttrbGender); value != nil { + gender = value } - if value, ok := m["birthDate"]; ok && len(value) > 0 { - date, _ := time.Parse(DateLayout, value[0]) - birthdate = &date + if value, err := kcUser.Attributes.GetTime(constants.AttrbBirthDate, constants.SupportedDateLayouts); err == nil && value != nil { + birthdate = value } } diff --git a/api/validation/api_test.go b/api/validation/api_test.go index 8365e92d7..8a2fa9e8f 100644 --- a/api/validation/api_test.go +++ b/api/validation/api_test.go @@ -4,6 +4,8 @@ import ( "testing" "time" + "github.com/cloudtrust/keycloak-bridge/internal/constants" + kc "github.com/cloudtrust/keycloak-client" "github.com/stretchr/testify/assert" ) @@ -47,7 +49,7 @@ func createValidKeycloakUser() kc.UserRepresentation { firstName = "Marc" lastName = "El-Bichoun" email = "marcel.bichon@elca.ch" - attributes = map[string][]string{ + attributes = kc.Attributes{ "gender": []string{"M"}, "phoneNumber": []string{"00 33 686 550011"}, "phoneNumberVerified": []string{"true"}, @@ -138,6 +140,7 @@ func TestExportToKeycloak(t *testing.T) { } func TestImportFromKeycloak(t *testing.T) { + var dateLayout = constants.SupportedDateLayouts[0] var user = createValidUser() user.BirthLocation = nil user.IDDocumentType = nil @@ -150,12 +153,11 @@ func TestImportFromKeycloak(t *testing.T) { var imported = UserRepresentation{} imported.ImportFromKeycloak(kcUser) - assert.Equal(t, (*user.BirthDate).Format(DateLayout), (*imported.BirthDate).Format(DateLayout)) + assert.Equal(t, (*user.BirthDate).Format(dateLayout), (*imported.BirthDate).Format(dateLayout)) user.BirthDate = nil imported.BirthDate = nil assert.Equal(t, user, imported) - } func TestUserValidate(t *testing.T) { diff --git a/internal/messages/errormessages.go b/internal/constants/errormessages.go similarity index 99% rename from internal/messages/errormessages.go rename to internal/constants/errormessages.go index 1830c14f6..b46958151 100644 --- a/internal/messages/errormessages.go +++ b/internal/constants/errormessages.go @@ -1,4 +1,4 @@ -package keycloakb +package constants // Normalized error messages const ( diff --git a/internal/constants/keycloak.go b/internal/constants/keycloak.go new file mode 100644 index 000000000..f19d31c0e --- /dev/null +++ b/internal/constants/keycloak.go @@ -0,0 +1,23 @@ +package constants + +import ( + kc "github.com/cloudtrust/keycloak-client" +) + +// Date layout management: first date layout is the one used to format dates in Keycloak. +// Following one are other supported format when parsing +var ( + SupportedDateLayouts = []string{"02.01.2006", "2006-01-02"} +) + +// Attribute keys definition +const ( + AttrbBirthDate = kc.AttributeKey("birthDate") + AttrbGender = kc.AttributeKey("gender") + AttrbLabel = kc.AttributeKey("label") + AttrbLocale = kc.AttributeKey("locale") + AttrbPhoneNumber = kc.AttributeKey("phoneNumber") + AttrbPhoneNumberVerified = kc.AttributeKey("phoneNumberVerified") + AttrbSmsSent = kc.AttributeKey("smsSent") + AttrbTrustIDGroups = kc.AttributeKey("trustIDGroups") +) diff --git a/internal/constants/regexp.go b/internal/constants/regexp.go new file mode 100644 index 000000000..6c5e0b677 --- /dev/null +++ b/internal/constants/regexp.go @@ -0,0 +1,39 @@ +package constants + +// Regular expressions for parameters validation +const ( + RegExpID = `^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$` + RegExpName = `^[a-zA-Z0-9-_]{1,128}$` + RegExpDescription = `^.{1,255}$` + + // Client + RegExpClientID = `^[a-zA-Z0-9-_.]{1,255}$` + + // User + RegExpUsername = `^[a-zA-Z0-9-_.]{1,128}$` + RegExpEmail = `^.+\@.+\..+$` + RegExpNameSpecialChars = `^([\wàáâäçèéêëìíîïñòóôöùúûüß]+([ '-][\wàáâäçèéêëìíîïñòóôöùúûüß]+)*){1,50}$` + RegExpFirstName = `^.{1,128}$` + RegExpLastName = `^.{1,128}$` + RegExpPhoneNumber = `^\+[1-9]\d{1,14}$` + RegExpLabel = `^.{1,255}$` + RegExpGender = `^[MF]$` + RegExpBirthDate = `^(\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))$` + RegExpLocale = `^[a-z]{2}$` + + // Password + RegExpPassword = `^.{1,255}$` + + // RealmCustomConfiguration + RegExpRedirectURI = `^\w+:(\/?\/?)[^\s]+$` + + // RequiredAction + RegExpRequiredAction = `^[a-zA-Z0-9-_]{1,255}$` + + // Others + RegExpRealmName = `^[a-zA-Z0-9_-]{1,36}$` + RegExpSearch = `^.{1,128}$` + RegExpLifespan = `^[0-9]{1,10}$` + RegExpGroupIds = `^([a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12})(,[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}){0,20}$` + RegExpNumber = `^\d+$` +) diff --git a/internal/keycloakb/configdbmodule.go b/internal/keycloakb/configdbmodule.go index 729ae692a..ab18f6580 100644 --- a/internal/keycloakb/configdbmodule.go +++ b/internal/keycloakb/configdbmodule.go @@ -11,8 +11,8 @@ import ( "github.com/cloudtrust/common-service/database/sqltypes" errorhandler "github.com/cloudtrust/common-service/errors" "github.com/cloudtrust/common-service/log" + msg "github.com/cloudtrust/keycloak-bridge/internal/constants" "github.com/cloudtrust/keycloak-bridge/internal/dto" - msg "github.com/cloudtrust/keycloak-bridge/internal/messages" ) const ( diff --git a/internal/keycloakb/configdbmodule_test.go b/internal/keycloakb/configdbmodule_test.go index 7d8909e7e..50d174fd6 100644 --- a/internal/keycloakb/configdbmodule_test.go +++ b/internal/keycloakb/configdbmodule_test.go @@ -11,8 +11,8 @@ import ( "github.com/cloudtrust/common-service/configuration" "github.com/cloudtrust/common-service/log" + msg "github.com/cloudtrust/keycloak-bridge/internal/constants" "github.com/cloudtrust/keycloak-bridge/internal/keycloakb/mock" - msg "github.com/cloudtrust/keycloak-bridge/internal/messages" "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" ) diff --git a/internal/keycloakb/datehelpers.go b/internal/keycloakb/datehelpers.go index 3477d48ca..ab6682005 100644 --- a/internal/keycloakb/datehelpers.go +++ b/internal/keycloakb/datehelpers.go @@ -7,7 +7,7 @@ import ( errorhandler "github.com/cloudtrust/common-service/errors" stats_api "github.com/cloudtrust/keycloak-bridge/api/statistics" - msg "github.com/cloudtrust/keycloak-bridge/internal/messages" + msg "github.com/cloudtrust/keycloak-bridge/internal/constants" ) // ConvertMinutesShift converts a string describing a timezone shift to a numeric value diff --git a/internal/keycloakb/eventsdbmodule.go b/internal/keycloakb/eventsdbmodule.go index 732f2d688..79e516c26 100644 --- a/internal/keycloakb/eventsdbmodule.go +++ b/internal/keycloakb/eventsdbmodule.go @@ -13,7 +13,7 @@ import ( api "github.com/cloudtrust/keycloak-bridge/api/events" api_stat "github.com/cloudtrust/keycloak-bridge/api/statistics" - msg "github.com/cloudtrust/keycloak-bridge/internal/messages" + msg "github.com/cloudtrust/keycloak-bridge/internal/constants" ) // EventsDBModule is the interface of the audit events module. diff --git a/pkg/account/component.go b/pkg/account/component.go index 1f3484319..a4abcc1a2 100644 --- a/pkg/account/component.go +++ b/pkg/account/component.go @@ -4,13 +4,13 @@ import ( "context" "encoding/json" "net/http" - "strconv" "strings" cs "github.com/cloudtrust/common-service" "github.com/cloudtrust/common-service/database" errorhandler "github.com/cloudtrust/common-service/errors" api "github.com/cloudtrust/keycloak-bridge/api/account" + "github.com/cloudtrust/keycloak-bridge/internal/constants" "github.com/cloudtrust/keycloak-bridge/internal/dto" "github.com/cloudtrust/keycloak-bridge/internal/keycloakb" internal "github.com/cloudtrust/keycloak-bridge/internal/keycloakb" @@ -190,7 +190,7 @@ func (c *component) UpdateAccount(ctx context.Context, user api.AccountRepresent } // Merge the attributes coming from the old user representation and the updated user representation in order not to lose anything - var mergedAttributes = make(map[string][]string) + var mergedAttributes = make(kc.Attributes) //Populate with the old attributes if oldUserKc.Attributes != nil { @@ -199,25 +199,11 @@ func (c *component) UpdateAccount(ctx context.Context, user api.AccountRepresent } } - if user.PhoneNumber != nil { - mergedAttributes["phoneNumber"] = []string{*user.PhoneNumber} - } - - if phoneNumberVerified != nil { - mergedAttributes["phoneNumberVerified"] = []string{strconv.FormatBool(*phoneNumberVerified)} - } - - if user.Gender != nil { - mergedAttributes["gender"] = []string{*user.Gender} - } - - if user.BirthDate != nil { - mergedAttributes["birthDate"] = []string{*user.BirthDate} - } - - if user.Locale != nil { - mergedAttributes["locale"] = []string{*user.Locale} - } + mergedAttributes.SetStringWhenNotNil(constants.AttrbPhoneNumber, user.PhoneNumber) + mergedAttributes.SetBoolWhenNotNil(constants.AttrbPhoneNumberVerified, phoneNumberVerified) + mergedAttributes.SetStringWhenNotNil(constants.AttrbGender, user.Gender) + mergedAttributes.SetDateWhenNotNil(constants.AttrbBirthDate, user.BirthDate, constants.SupportedDateLayouts) + mergedAttributes.SetStringWhenNotNil(constants.AttrbLocale, user.Locale) userRep.Attributes = &mergedAttributes diff --git a/pkg/account/component_test.go b/pkg/account/component_test.go index a158cb34d..ecbafaf45 100644 --- a/pkg/account/component_test.go +++ b/pkg/account/component_test.go @@ -20,6 +20,7 @@ import ( "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" + "github.com/cloudtrust/keycloak-bridge/internal/constants" "github.com/cloudtrust/keycloak-bridge/internal/dto" ) @@ -152,9 +153,7 @@ func TestUpdateAccount(t *testing.T) { var phoneNumber = "+41789456" var phoneNumberVerified = true var label = "Label" - var previousGender = "F" var gender = "M" - var previousBirthDate = "02/02/1988" var birthDate = "01/01/1988" var birthLocation = "Antananarivo" var locale = "de" @@ -163,13 +162,13 @@ func TestUpdateAccount(t *testing.T) { var idDocExpiration = "01.01.2050" var createdTimestamp = time.Now().UTC().Unix() - var attributes = make(map[string][]string) - attributes["phoneNumber"] = []string{phoneNumber} - attributes["label"] = []string{label} - attributes["gender"] = []string{previousGender} - attributes["birthDate"] = []string{previousBirthDate} - attributes["phoneNumberVerified"] = []string{strconv.FormatBool(phoneNumberVerified)} - attributes["locale"] = []string{locale} + var attributes = make(kc.Attributes) + attributes.SetString(constants.AttrbPhoneNumber, phoneNumber) + attributes.SetString(constants.AttrbLabel, label) + attributes.SetString(constants.AttrbGender, gender) + attributes.SetString(constants.AttrbBirthDate, birthDate) + attributes.SetBool(constants.AttrbPhoneNumberVerified, phoneNumberVerified) + attributes.SetString(constants.AttrbLocale, locale) var kcUserRep = kc.UserRepresentation{ Id: &id, @@ -268,7 +267,7 @@ func TestUpdateAccount(t *testing.T) { }) var oldNumber = "+41789467" - var oldAttributes = make(map[string][]string) + var oldAttributes = make(kc.Attributes) oldAttributes["phoneNumber"] = []string{oldNumber} oldAttributes["phoneNumberVerified"] = []string{strconv.FormatBool(phoneNumberVerified)} var oldkcUserRep2 = kc.UserRepresentation{ @@ -391,7 +390,7 @@ func TestGetUser(t *testing.T) { var createdTimestamp = time.Now().UTC().Unix() var locale = "it" - var attributes = make(map[string][]string) + var attributes = make(kc.Attributes) attributes["phoneNumber"] = []string{phoneNumber} attributes["label"] = []string{label} attributes["gender"] = []string{gender} diff --git a/pkg/account/endpoint.go b/pkg/account/endpoint.go index 7401aa403..da69bd224 100644 --- a/pkg/account/endpoint.go +++ b/pkg/account/endpoint.go @@ -7,7 +7,7 @@ import ( cs "github.com/cloudtrust/common-service" errrorhandler "github.com/cloudtrust/common-service/errors" api "github.com/cloudtrust/keycloak-bridge/api/account" - msg "github.com/cloudtrust/keycloak-bridge/internal/messages" + msg "github.com/cloudtrust/keycloak-bridge/internal/constants" "github.com/go-kit/kit/endpoint" ) diff --git a/pkg/event/endpoint.go b/pkg/event/endpoint.go index 167da1ae4..23cd9f2f6 100755 --- a/pkg/event/endpoint.go +++ b/pkg/event/endpoint.go @@ -5,7 +5,7 @@ import ( "fmt" cs "github.com/cloudtrust/common-service" - msg "github.com/cloudtrust/keycloak-bridge/internal/messages" + msg "github.com/cloudtrust/keycloak-bridge/internal/constants" "github.com/go-kit/kit/endpoint" ) diff --git a/pkg/event/http.go b/pkg/event/http.go index c6659440b..bfb0aca71 100755 --- a/pkg/event/http.go +++ b/pkg/event/http.go @@ -9,7 +9,7 @@ import ( cs "github.com/cloudtrust/common-service" "github.com/cloudtrust/common-service/log" - msg "github.com/cloudtrust/keycloak-bridge/internal/messages" + msg "github.com/cloudtrust/keycloak-bridge/internal/constants" "github.com/go-kit/kit/endpoint" http_transport "github.com/go-kit/kit/transport/http" "github.com/pkg/errors" diff --git a/pkg/events/component.go b/pkg/events/component.go index a4ce8ffa5..10945bd54 100644 --- a/pkg/events/component.go +++ b/pkg/events/component.go @@ -7,7 +7,7 @@ import ( errorhandler "github.com/cloudtrust/common-service/errors" api "github.com/cloudtrust/keycloak-bridge/api/events" app "github.com/cloudtrust/keycloak-bridge/internal/keycloakb" - msg "github.com/cloudtrust/keycloak-bridge/internal/messages" + msg "github.com/cloudtrust/keycloak-bridge/internal/constants" ) // Component is the interface of the events component. diff --git a/pkg/export/component.go b/pkg/export/component.go index bbf279c0c..71f487ebc 100644 --- a/pkg/export/component.go +++ b/pkg/export/component.go @@ -5,7 +5,7 @@ import ( "encoding/json" internal "github.com/cloudtrust/keycloak-bridge/internal/keycloakb" - msg "github.com/cloudtrust/keycloak-bridge/internal/messages" + msg "github.com/cloudtrust/keycloak-bridge/internal/constants" keycloak "github.com/cloudtrust/keycloak-client" "github.com/pkg/errors" ) diff --git a/pkg/export/http.go b/pkg/export/http.go index 5f712d36c..8c2e95633 100755 --- a/pkg/export/http.go +++ b/pkg/export/http.go @@ -7,7 +7,7 @@ import ( cs "github.com/cloudtrust/common-service" "github.com/cloudtrust/keycloak-bridge/internal/keycloakb" - msg "github.com/cloudtrust/keycloak-bridge/internal/messages" + msg "github.com/cloudtrust/keycloak-bridge/internal/constants" "github.com/go-kit/kit/endpoint" http_transport "github.com/go-kit/kit/transport/http" "github.com/pkg/errors" diff --git a/pkg/export/module.go b/pkg/export/module.go index a1c8a6954..713a83b16 100644 --- a/pkg/export/module.go +++ b/pkg/export/module.go @@ -4,7 +4,7 @@ import ( "context" internal "github.com/cloudtrust/keycloak-bridge/internal/keycloakb" - msg "github.com/cloudtrust/keycloak-bridge/internal/messages" + msg "github.com/cloudtrust/keycloak-bridge/internal/constants" keycloak "github.com/cloudtrust/keycloak-client" "github.com/pkg/errors" ) diff --git a/pkg/export/storage.go b/pkg/export/storage.go index a284c0fc5..c08e99f89 100644 --- a/pkg/export/storage.go +++ b/pkg/export/storage.go @@ -4,7 +4,7 @@ import ( "database/sql" "github.com/cloudtrust/common-service/database/sqltypes" - msg "github.com/cloudtrust/keycloak-bridge/internal/messages" + msg "github.com/cloudtrust/keycloak-bridge/internal/constants" "github.com/pkg/errors" ) diff --git a/pkg/kyc/component.go b/pkg/kyc/component.go index 14dfbbcba..24871ff32 100644 --- a/pkg/kyc/component.go +++ b/pkg/kyc/component.go @@ -2,17 +2,16 @@ package kyc import ( "context" - "strconv" "time" cs "github.com/cloudtrust/common-service" "github.com/cloudtrust/common-service/database" errorhandler "github.com/cloudtrust/common-service/errors" apikyc "github.com/cloudtrust/keycloak-bridge/api/kyc" + "github.com/cloudtrust/keycloak-bridge/internal/constants" "github.com/cloudtrust/keycloak-bridge/internal/dto" "github.com/cloudtrust/keycloak-bridge/internal/keycloakb" internal "github.com/cloudtrust/keycloak-bridge/internal/keycloakb" - messages "github.com/cloudtrust/keycloak-bridge/internal/messages" kc "github.com/cloudtrust/keycloak-client" ) @@ -160,11 +159,11 @@ func (c *component) ValidateUser(ctx context.Context, userID string, user apikyc if kcUser.EmailVerified == nil || !*kcUser.EmailVerified { c.logger.Warn(ctx, "msg", "Can't validate user with unverified email", "uid", userID) - return errorhandler.CreateBadRequestError(messages.MsgErrUnverified + "." + messages.Email) + return errorhandler.CreateBadRequestError(constants.MsgErrUnverified + "." + constants.Email) } - if !isPhoneNumberVerified(kcUser.Attributes) { + if verified, verifiedErr := kcUser.GetAttributeBool(constants.AttrbPhoneNumberVerified); verifiedErr != nil || verified == nil || !*verified { c.logger.Warn(ctx, "msg", "Can't validate user with unverified phone number", "uid", userID) - return errorhandler.CreateBadRequestError(messages.MsgErrUnverified + "." + messages.PhoneNumber) + return errorhandler.CreateBadRequestError(constants.MsgErrUnverified + "." + constants.PhoneNumber) } // Gets user from database @@ -234,17 +233,6 @@ func (c *component) getUserByUsername(accessToken, reqRealmName, targetRealmName return kcUsers.Users[0], nil } -func isPhoneNumberVerified(attribs *map[string][]string) bool { - if attribs == nil { - return false - } - if value, ok := (*attribs)["phoneNumberVerified"]; ok && len(value) > 0 { - verified, err := strconv.ParseBool(value[0]) - return verified && err == nil - } - return false -} - func ptr(value string) *string { return &value } diff --git a/pkg/kyc/component_test.go b/pkg/kyc/component_test.go index 8a7c05dbd..5c3938736 100644 --- a/pkg/kyc/component_test.go +++ b/pkg/kyc/component_test.go @@ -155,7 +155,7 @@ func createUser(userID, username string, emailVerified bool, phoneNumberVerified if phoneNumberVerified { pnv = "true" } - var attributes = map[string][]string{"phoneNumberVerified": []string{pnv}} + var attributes = kc.Attributes{"phoneNumberVerified": []string{pnv}} return kc.UserRepresentation{ Id: &userID, Username: &username, @@ -283,30 +283,3 @@ func TestValidateUser(t *testing.T) { assert.Nil(t, err) }) } - -func TestIsPhoneNumberVerified(t *testing.T) { - t.Run("No attributes", func(t *testing.T) { - assert.False(t, isPhoneNumberVerified(nil)) - }) - - var attrbs = make(map[string][]string) - - t.Run("No phoneNumberVerified attribute", func(t *testing.T) { - assert.False(t, isPhoneNumberVerified(&attrbs)) - }) - - t.Run("Invalid phoneNumberVerified attribute", func(t *testing.T) { - attrbs["phoneNumberVerified"] = []string{"not a boolean"} - assert.False(t, isPhoneNumberVerified(&attrbs)) - }) - - t.Run("phoneNumberVerified is false", func(t *testing.T) { - attrbs["phoneNumberVerified"] = []string{"false"} - assert.False(t, isPhoneNumberVerified(&attrbs)) - }) - - t.Run("phoneNumberVerified is true", func(t *testing.T) { - attrbs["phoneNumberVerified"] = []string{"true"} - assert.True(t, isPhoneNumberVerified(&attrbs)) - }) -} diff --git a/pkg/kyc/endpoint.go b/pkg/kyc/endpoint.go index 694803706..bac8e3a64 100644 --- a/pkg/kyc/endpoint.go +++ b/pkg/kyc/endpoint.go @@ -7,7 +7,7 @@ import ( cs "github.com/cloudtrust/common-service" commonerrors "github.com/cloudtrust/common-service/errors" apikyc "github.com/cloudtrust/keycloak-bridge/api/kyc" - msg "github.com/cloudtrust/keycloak-bridge/internal/messages" + msg "github.com/cloudtrust/keycloak-bridge/internal/constants" "github.com/go-kit/kit/endpoint" ) diff --git a/pkg/kyc/http.go b/pkg/kyc/http.go index 6c786f5ec..bf3a870ae 100644 --- a/pkg/kyc/http.go +++ b/pkg/kyc/http.go @@ -6,16 +6,16 @@ import ( commonhttp "github.com/cloudtrust/common-service/http" "github.com/cloudtrust/common-service/log" - apimgmt "github.com/cloudtrust/keycloak-bridge/api/management" + "github.com/cloudtrust/keycloak-bridge/internal/constants" "github.com/go-kit/kit/endpoint" http_transport "github.com/go-kit/kit/transport/http" ) // Regular expressions const ( - RegExpUserName = apimgmt.RegExpUsername - RegExpUserID = apimgmt.RegExpID - RegExpGroupIds = apimgmt.RegExpGroupIds + RegExpUserName = constants.RegExpUsername + RegExpUserID = constants.RegExpID + RegExpGroupIds = constants.RegExpGroupIds ) // MakeKYCHandler make an HTTP handler for the KYC endpoint. diff --git a/internal/keycloakb/authorizationutil.go b/pkg/management/authorizationutil.go similarity index 98% rename from internal/keycloakb/authorizationutil.go rename to pkg/management/authorizationutil.go index 962339961..daae48213 100644 --- a/internal/keycloakb/authorizationutil.go +++ b/pkg/management/authorizationutil.go @@ -1,4 +1,4 @@ -package keycloakb +package management import ( "errors" diff --git a/internal/keycloakb/authorizationutil_test.go b/pkg/management/authorizationutil_test.go similarity index 99% rename from internal/keycloakb/authorizationutil_test.go rename to pkg/management/authorizationutil_test.go index 927262e9c..6fcd72245 100644 --- a/internal/keycloakb/authorizationutil_test.go +++ b/pkg/management/authorizationutil_test.go @@ -1,4 +1,4 @@ -package keycloakb +package management import ( "testing" diff --git a/pkg/management/component.go b/pkg/management/component.go index d00223231..678022880 100644 --- a/pkg/management/component.go +++ b/pkg/management/component.go @@ -11,9 +11,9 @@ import ( "github.com/cloudtrust/common-service/database" errorhandler "github.com/cloudtrust/common-service/errors" api "github.com/cloudtrust/keycloak-bridge/api/management" + "github.com/cloudtrust/keycloak-bridge/internal/constants" "github.com/cloudtrust/keycloak-bridge/internal/dto" "github.com/cloudtrust/keycloak-bridge/internal/keycloakb" - msg "github.com/cloudtrust/keycloak-bridge/internal/messages" kc "github.com/cloudtrust/keycloak-client" "github.com/pkg/errors" ) @@ -348,13 +348,8 @@ func (c *component) UpdateUser(ctx context.Context, realmName, userID string, us // when the phone number changes, set the PhoneNumberVerified to false if user.PhoneNumber != nil { - if oldUserKc.Attributes != nil { - var m = *oldUserKc.Attributes - if _, ok := m["phoneNumber"]; !ok || m["phoneNumber"][0] != *user.PhoneNumber { - var verified = false - user.PhoneNumberVerified = &verified - } - } else { // the user has no attributes until now, i.e. he has not set yet his phone number + var oldPhoneNumber = oldUserKc.GetAttributeString(constants.AttrbPhoneNumber) + if oldPhoneNumber == nil || *oldPhoneNumber != *user.PhoneNumber { var verified = false user.PhoneNumberVerified = &verified } @@ -363,7 +358,7 @@ func (c *component) UpdateUser(ctx context.Context, realmName, userID string, us userRep = api.ConvertToKCUser(user) // Merge the attributes coming from the old user representation and the updated user representation in order not to lose anything - var mergedAttributes = make(map[string][]string) + var mergedAttributes = make(kc.Attributes) //Populate with the old attributes if oldUserKc.Attributes != nil { @@ -511,7 +506,7 @@ func (c *component) SetTrustIDGroups(ctx context.Context, realmName, userID stri } else { // unauthorized call (unknown trustID group) --> error c.logger.Warn(ctx, "msg", groupName+" group is not allowed to be set as a trustID group") - return errorhandler.CreateBadRequestError(msg.MsgErrInvalidParam + "." + msg.TrustIDGroupName) + return errorhandler.CreateBadRequestError(constants.MsgErrInvalidParam + "." + constants.TrustIDGroupName) } } @@ -524,7 +519,7 @@ func (c *component) SetTrustIDGroups(ctx context.Context, realmName, userID stri // set the trustID groups attributes if currentUser.Attributes == nil { - var emtpyMap = make(map[string][]string) + var emtpyMap = make(kc.Attributes) currentUser.Attributes = &emtpyMap } (*currentUser.Attributes)["trustIDGroups"] = extGroupNames @@ -773,7 +768,7 @@ func (c *component) DeleteCredentialsForUser(ctx context.Context, realmName stri if !ownedByUser { c.logger.Warn(ctx, "msg", "Try to delete credential of another user", "credId", credentialID, "userId", userID) - return errorhandler.CreateNotFoundError(msg.MsgErrInvalidParam + "." + msg.CredentialID) + return errorhandler.CreateNotFoundError(constants.MsgErrInvalidParam + "." + constants.CredentialID) } err = c.keycloakClient.DeleteCredential(accessToken, realmName, userID, credentialID) @@ -979,10 +974,10 @@ func (c *component) UpdateAuthorizations(ctx context.Context, realmName string, } // Perform validation - err := keycloakb.Validate(authorizations, allowedTargetRealmsAndGroupNames) + err := Validate(authorizations, allowedTargetRealmsAndGroupNames) if err != nil { c.logger.Warn(ctx, "err", err.Error()) - return errorhandler.CreateBadRequestError(msg.MsgErrInvalidParam + "." + msg.Authorization) + return errorhandler.CreateBadRequestError(constants.MsgErrInvalidParam + "." + constants.Authorization) } } @@ -1235,7 +1230,7 @@ func (c *component) UpdateRealmCustomConfiguration(ctx context.Context, realmNam (customConfig.DefaultClientID != nil && customConfig.DefaultRedirectURI == nil) { return errorhandler.Error{ Status: 400, - Message: keycloakb.ComponentName + "." + msg.MsgErrInvalidParam + "." + msg.ClientID + "AND" + msg.RedirectURI, + Message: keycloakb.ComponentName + "." + constants.MsgErrInvalidParam + "." + constants.ClientID + "AND" + constants.RedirectURI, } } @@ -1261,7 +1256,7 @@ func (c *component) UpdateRealmCustomConfiguration(ctx context.Context, realmNam if !match { return errorhandler.Error{ Status: 400, - Message: keycloakb.ComponentName + "." + msg.MsgErrInvalidParam + "." + msg.ClientID + "OR" + msg.RedirectURI, + Message: keycloakb.ComponentName + "." + constants.MsgErrInvalidParam + "." + constants.ClientID + "OR" + constants.RedirectURI, } } } diff --git a/pkg/management/component_test.go b/pkg/management/component_test.go index 7ef061e48..0e830fa2f 100644 --- a/pkg/management/component_test.go +++ b/pkg/management/component_test.go @@ -16,6 +16,7 @@ import ( errorhandler "github.com/cloudtrust/common-service/errors" "github.com/cloudtrust/common-service/log" api "github.com/cloudtrust/keycloak-bridge/api/management" + "github.com/cloudtrust/keycloak-bridge/internal/constants" "github.com/cloudtrust/keycloak-bridge/internal/dto" "github.com/cloudtrust/keycloak-bridge/pkg/management/mock" @@ -625,14 +626,14 @@ func TestGetUser(t *testing.T) { var locale = "it" var trustIDGroups = []string{"grp1", "grp2"} - var attributes = make(map[string][]string) - attributes["phoneNumber"] = []string{phoneNumber} - attributes["label"] = []string{label} - attributes["gender"] = []string{gender} - attributes["birthDate"] = []string{birthDate} - attributes["phoneNumberVerified"] = []string{strconv.FormatBool(phoneNumberVerified)} - attributes["locale"] = []string{locale} - attributes["trustIDGroups"] = trustIDGroups + var attributes = make(kc.Attributes) + attributes.SetString(constants.AttrbPhoneNumber, phoneNumber) + attributes.SetString(constants.AttrbLabel, label) + attributes.SetString(constants.AttrbGender, gender) + attributes.SetString(constants.AttrbBirthDate, birthDate) + attributes.SetBool(constants.AttrbPhoneNumberVerified, phoneNumberVerified) + attributes.SetString(constants.AttrbLocale, locale) + attributes.Set(constants.AttrbTrustIDGroups, trustIDGroups) var kcUserRep = kc.UserRepresentation{ Id: &id, @@ -740,13 +741,13 @@ func TestUpdateUser(t *testing.T) { var locale = "de" var createdTimestamp = time.Now().UTC().Unix() - var attributes = make(map[string][]string) - attributes["phoneNumber"] = []string{phoneNumber} - attributes["label"] = []string{label} - attributes["gender"] = []string{gender} - attributes["birthDate"] = []string{birthDate} - attributes["phoneNumberVerified"] = []string{strconv.FormatBool(phoneNumberVerified)} - attributes["locale"] = []string{locale} + var attributes = make(kc.Attributes) + attributes.SetString(constants.AttrbPhoneNumber, phoneNumber) + attributes.SetString(constants.AttrbLabel, label) + attributes.SetString(constants.AttrbGender, gender) + attributes.SetString(constants.AttrbBirthDate, birthDate) + attributes.SetBool(constants.AttrbPhoneNumberVerified, phoneNumberVerified) + attributes.SetString(constants.AttrbLocale, locale) var kcUserRep = kc.UserRepresentation{ Id: &id, @@ -793,8 +794,8 @@ func TestUpdateUser(t *testing.T) { assert.Equal(t, firstName, *kcUserRep.FirstName) assert.Equal(t, lastName, *kcUserRep.LastName) assert.Equal(t, phoneNumber, (*kcUserRep.Attributes)["phoneNumber"][0]) - verified, _ := strconv.ParseBool(((*kcUserRep.Attributes)["phoneNumberVerified"][0])) - assert.Equal(t, phoneNumberVerified, verified) + verified, _ := kcUserRep.Attributes.GetBool(constants.AttrbPhoneNumberVerified) + assert.Equal(t, phoneNumberVerified, *verified) assert.Equal(t, label, (*kcUserRep.Attributes)["label"][0]) assert.Equal(t, gender, (*kcUserRep.Attributes)["gender"][0]) assert.Equal(t, birthDate, (*kcUserRep.Attributes)["birthDate"][0]) @@ -868,9 +869,9 @@ func TestUpdateUser(t *testing.T) { // update by changing the phone number var oldNumber = "+41789467" - var oldAttributes = make(map[string][]string) - oldAttributes["phoneNumber"] = []string{oldNumber} - oldAttributes["phoneNumberVerified"] = []string{strconv.FormatBool(phoneNumberVerified)} + var oldAttributes = make(kc.Attributes) + oldAttributes.SetString(constants.AttrbPhoneNumber, oldNumber) + oldAttributes.SetBool(constants.AttrbPhoneNumberVerified, phoneNumberVerified) var oldkcUserRep2 = kc.UserRepresentation{ Id: &id, Attributes: &oldAttributes, @@ -1002,12 +1003,12 @@ func TestGetUsers(t *testing.T) { var birthDate = "01/01/1988" var createdTimestamp = time.Now().UTC().Unix() - var attributes = make(map[string][]string) - attributes["phoneNumber"] = []string{phoneNumber} - attributes["label"] = []string{label} - attributes["gender"] = []string{gender} - attributes["birthDate"] = []string{birthDate} - attributes["phoneNumberVerified"] = []string{strconv.FormatBool(phoneNumberVerified)} + var attributes = make(kc.Attributes) + attributes.SetString(constants.AttrbPhoneNumber, phoneNumber) + attributes.SetString(constants.AttrbLabel, label) + attributes.SetString(constants.AttrbGender, gender) + attributes.SetString(constants.AttrbBirthDate, birthDate) + attributes.SetBool(constants.AttrbPhoneNumberVerified, phoneNumberVerified) var count = 1 var kcUserRep = kc.UserRepresentation{ @@ -1426,8 +1427,8 @@ func TestSetTrustIDGroups(t *testing.T) { } grpNames := []string{"grp1", "grp2"} extGrpNames := []string{"/grp1", "/grp2"} - attrs := make(map[string][]string) - attrs["trustIDGroups"] = extGrpNames + attrs := make(kc.Attributes) + attrs.Set(constants.AttrbTrustIDGroups, extGrpNames) var kcUserRep2 = kc.UserRepresentation{ Username: &username, Attributes: &attrs, @@ -1471,8 +1472,8 @@ func TestSetTrustIDGroups(t *testing.T) { } grpNames := []string{"grp1", "grp2"} extGrpNames := []string{"/grp1", "/grp2"} - attrs := make(map[string][]string) - attrs["trustIDGroups"] = extGrpNames + attrs := make(kc.Attributes) + attrs.Set(constants.AttrbTrustIDGroups, extGrpNames) var kcUserRep2 = kc.UserRepresentation{ Username: &username, Attributes: &attrs, @@ -1901,13 +1902,13 @@ func TestResetSmsCounter(t *testing.T) { var gender = "M" var birthDate = "01/01/1988" var createdTimestamp = time.Now().UTC().Unix() - var attributes = make(map[string][]string) - attributes["phoneNumber"] = []string{phoneNumber} - attributes["label"] = []string{label} - attributes["gender"] = []string{gender} - attributes["birthDate"] = []string{birthDate} - attributes["phoneNumberVerified"] = []string{strconv.FormatBool(phoneNumberVerified)} - attributes["smsSent"] = []string{"5"} + var attributes = make(kc.Attributes) + attributes.SetString(constants.AttrbPhoneNumber, phoneNumber) + attributes.SetString(constants.AttrbLabel, label) + attributes.SetString(constants.AttrbGender, gender) + attributes.SetString(constants.AttrbBirthDate, birthDate) + attributes.SetBool(constants.AttrbPhoneNumberVerified, phoneNumberVerified) + attributes.SetInt(constants.AttrbSmsSent, 5) var kcUserRep = kc.UserRepresentation{ Id: &id, diff --git a/pkg/management/endpoint.go b/pkg/management/endpoint.go index 95e948243..1a0e3a31c 100644 --- a/pkg/management/endpoint.go +++ b/pkg/management/endpoint.go @@ -10,7 +10,7 @@ import ( cs "github.com/cloudtrust/common-service" errorhandler "github.com/cloudtrust/common-service/errors" api "github.com/cloudtrust/keycloak-bridge/api/management" - msg "github.com/cloudtrust/keycloak-bridge/internal/messages" + msg "github.com/cloudtrust/keycloak-bridge/internal/constants" "github.com/go-kit/kit/endpoint" ) diff --git a/pkg/management/http.go b/pkg/management/http.go index f78f70ec9..5ad441c30 100644 --- a/pkg/management/http.go +++ b/pkg/management/http.go @@ -6,9 +6,9 @@ import ( commonhttp "github.com/cloudtrust/common-service/http" "github.com/cloudtrust/common-service/log" - management_api "github.com/cloudtrust/keycloak-bridge/api/management" + api "github.com/cloudtrust/keycloak-bridge/api/management" + msg "github.com/cloudtrust/keycloak-bridge/internal/constants" "github.com/cloudtrust/keycloak-bridge/internal/keycloakb" - msg "github.com/cloudtrust/keycloak-bridge/internal/messages" kc_client "github.com/cloudtrust/keycloak-client" "github.com/go-kit/kit/endpoint" http_transport "github.com/go-kit/kit/transport/http" @@ -28,28 +28,28 @@ func MakeManagementHandler(e endpoint.Endpoint, logger log.Logger) *http_transpo // decodeEventsRequest gets the HTTP parameters and body content func decodeManagementRequest(ctx context.Context, req *http.Request) (interface{}, error) { var pathParams = map[string]string{ - "realm": management_api.RegExpRealmName, - "userID": management_api.RegExpID, - "clientID": management_api.RegExpClientID, - "roleID": management_api.RegExpID, - "groupID": management_api.RegExpID, - "credentialID": management_api.RegExpID, - "provider": management_api.RegExpName, + "realm": api.RegExpRealmName, + "userID": api.RegExpID, + "clientID": api.RegExpClientID, + "roleID": api.RegExpID, + "groupID": api.RegExpID, + "credentialID": api.RegExpID, + "provider": api.RegExpName, } var queryParams = map[string]string{ - "email": management_api.RegExpEmail, - "firstName": management_api.RegExpFirstName, - "lastName": management_api.RegExpLastName, - "username": management_api.RegExpUsername, - "search": management_api.RegExpSearch, - "client_id": management_api.RegExpClientID, - "redirect_uri": management_api.RegExpRedirectURI, - "lifespan": management_api.RegExpLifespan, - "groupIds": management_api.RegExpGroupIds, - "first": management_api.RegExpNumber, - "max": management_api.RegExpNumber, - "groupName": management_api.RegExpName, + "email": api.RegExpEmail, + "firstName": api.RegExpFirstName, + "lastName": api.RegExpLastName, + "username": api.RegExpUsername, + "search": api.RegExpSearch, + "client_id": api.RegExpClientID, + "redirect_uri": api.RegExpRedirectURI, + "lifespan": api.RegExpLifespan, + "groupIds": api.RegExpGroupIds, + "first": api.RegExpNumber, + "max": api.RegExpNumber, + "groupName": api.RegExpName, } return commonhttp.DecodeRequest(ctx, req, pathParams, queryParams) diff --git a/pkg/register/endpoint.go b/pkg/register/endpoint.go index df4918fe1..31eed4238 100644 --- a/pkg/register/endpoint.go +++ b/pkg/register/endpoint.go @@ -6,7 +6,7 @@ import ( cs "github.com/cloudtrust/common-service" commonerrors "github.com/cloudtrust/common-service/errors" apiregister "github.com/cloudtrust/keycloak-bridge/api/register" - msg "github.com/cloudtrust/keycloak-bridge/internal/messages" + msg "github.com/cloudtrust/keycloak-bridge/internal/constants" "github.com/go-kit/kit/endpoint" ) diff --git a/pkg/statistics/component.go b/pkg/statistics/component.go index 11948b058..a542fba36 100644 --- a/pkg/statistics/component.go +++ b/pkg/statistics/component.go @@ -10,7 +10,7 @@ import ( "github.com/cloudtrust/common-service/log" api "github.com/cloudtrust/keycloak-bridge/api/statistics" "github.com/cloudtrust/keycloak-bridge/internal/keycloakb" - msg "github.com/cloudtrust/keycloak-bridge/internal/messages" + msg "github.com/cloudtrust/keycloak-bridge/internal/constants" kc "github.com/cloudtrust/keycloak-client" ) diff --git a/pkg/statistics/endpoint.go b/pkg/statistics/endpoint.go index 3a38a6e2c..134745f50 100644 --- a/pkg/statistics/endpoint.go +++ b/pkg/statistics/endpoint.go @@ -5,7 +5,7 @@ import ( cs "github.com/cloudtrust/common-service" errorhandler "github.com/cloudtrust/common-service/errors" - msg "github.com/cloudtrust/keycloak-bridge/internal/messages" + msg "github.com/cloudtrust/keycloak-bridge/internal/constants" "github.com/go-kit/kit/endpoint" ) diff --git a/pkg/validation/component.go b/pkg/validation/component.go index 786f93668..7f69795dc 100644 --- a/pkg/validation/component.go +++ b/pkg/validation/component.go @@ -9,12 +9,17 @@ import ( "github.com/cloudtrust/common-service/database" errorhandler "github.com/cloudtrust/common-service/errors" api "github.com/cloudtrust/keycloak-bridge/api/validation" + "github.com/cloudtrust/keycloak-bridge/internal/constants" "github.com/cloudtrust/keycloak-bridge/internal/dto" "github.com/cloudtrust/keycloak-bridge/internal/keycloakb" internal "github.com/cloudtrust/keycloak-bridge/internal/keycloakb" kc "github.com/cloudtrust/keycloak-client" ) +var ( + dateLayout = constants.SupportedDateLayouts[0] +) + // KeycloakClient are methods from keycloak-client used by this component type KeycloakClient interface { UpdateUser(accessToken string, realmName, userID string, user kc.UserRepresentation) error @@ -99,7 +104,7 @@ func (c *component) GetUser(ctx context.Context, userID string) (api.UserReprese res.IDDocumentNumber = dbUser.IDDocumentNumber if dbUser.IDDocumentExpiration != nil { - expirationTime, err := time.Parse(api.DateLayout, *dbUser.IDDocumentExpiration) + expirationTime, err := time.Parse(dateLayout, *dbUser.IDDocumentExpiration) if err != nil { return api.UserRepresentation{}, err } @@ -145,7 +150,7 @@ func (c *component) UpdateUser(ctx context.Context, userID string, user api.User } if user.IDDocumentExpiration != nil { - var expiration = (*user.IDDocumentExpiration).Format(api.DateLayout) + var expiration = (*user.IDDocumentExpiration).Format(dateLayout) userDB.IDDocumentExpiration = &expiration } diff --git a/pkg/validation/endpoint.go b/pkg/validation/endpoint.go index beb2a6f10..eed3fff46 100644 --- a/pkg/validation/endpoint.go +++ b/pkg/validation/endpoint.go @@ -7,7 +7,7 @@ import ( cs "github.com/cloudtrust/common-service" errorhandler "github.com/cloudtrust/common-service/errors" api "github.com/cloudtrust/keycloak-bridge/api/validation" - msg "github.com/cloudtrust/keycloak-bridge/internal/messages" + msg "github.com/cloudtrust/keycloak-bridge/internal/constants" "github.com/go-kit/kit/endpoint" )