Skip to content

Commit b32a717

Browse files
authored
[CLOUDTRUST-7170] Add methods in IDP API to manage multi-links (#684)
1 parent 60e2e0d commit b32a717

20 files changed

Lines changed: 1275 additions & 511 deletions

.devcontainer/docker-compose.dev.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ services:
99
- "8866:8866"
1010
- "8855:8855"
1111
- "8844:8844"
12+
- "8870:8870"
1213
volumes:
1314
- ${BRIDGE_REPOSITORY_FOLDER}:/workspace:cached
1415
# Overrides default command so things don't shut down after the process ends.

api/idp/api.go

Lines changed: 80 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type IdentityProviderRepresentation struct {
2727
HrdSettings *HrdSettingModel `json:"hrdSettings,omitempty"`
2828
}
2929

30+
// HrdSettingModel struct
3031
type HrdSettingModel struct {
3132
IPRangesList string `json:"ipRangesList"`
3233
Priority int `json:"priority"`
@@ -41,6 +42,38 @@ type IdentityProviderMapperRepresentation struct {
4142
Name *string `json:"name,omitempty"`
4243
}
4344

45+
// UserRepresentation struct
46+
type UserRepresentation struct {
47+
ID *string `json:"id,omitempty"`
48+
Username *string `json:"username,omitempty"`
49+
FirstName *string `json:"firstName,omitempty"`
50+
LastName *string `json:"lastName,omitempty"`
51+
Email *string `json:"email,omitempty"`
52+
Enabled *bool `json:"enabled,omitempty"`
53+
}
54+
55+
// FederatedIdentityRepresentation struct
56+
type FederatedIdentityRepresentation struct {
57+
UserID *string `json:"userID,omitempty"`
58+
Username *string `json:"username,omitempty"`
59+
IdentityProvider *string `json:"identityProvider,omitempty"`
60+
}
61+
62+
func validateConfig(config map[string]string) func() error {
63+
return func() error {
64+
if len(config) != 0 {
65+
configJSON, err := json.Marshal(config)
66+
if err != nil {
67+
return err
68+
}
69+
configStr := string(configJSON)
70+
return validation.NewParameterValidator().ValidateParameterLength("config", &configStr, 0, 10000, false).Status()
71+
}
72+
return nil
73+
}
74+
}
75+
76+
// Validate validates a HrdSettingModel
4477
func (settings HrdSettingModel) Validate() error {
4578
return validation.NewParameterValidator().
4679
ValidateParameterRegExp("ipRangesList", &settings.IPRangesList, constants.RegExpIpRangesList, true).
@@ -69,7 +102,7 @@ func ConvertToAPIIdentityProvider(idp kc.IdentityProviderRepresentation) Identit
69102
}
70103

71104
// ConvertToKCIdentityProvider creates a KC IdentityProviderRepresentation from an API IdentityProviderRepresentation
72-
func ConvertToKCIdentityProvider(idp IdentityProviderRepresentation) kc.IdentityProviderRepresentation {
105+
func (idp IdentityProviderRepresentation) ConvertToKCIdentityProvider() kc.IdentityProviderRepresentation {
73106
return kc.IdentityProviderRepresentation{
74107
AddReadTokenRoleOnCreate: idp.AddReadTokenRoleOnCreate,
75108
Alias: idp.Alias,
@@ -90,30 +123,16 @@ func ConvertToKCIdentityProvider(idp IdentityProviderRepresentation) kc.Identity
90123

91124
// Validate is a validator for IdentityProviderRepresentation
92125
func (idp IdentityProviderRepresentation) Validate() error {
93-
v := validation.NewParameterValidator().
126+
return validation.NewParameterValidator().
94127
ValidateParameterRegExp("alias", idp.Alias, constants.RegExpAlias, true).
95128
ValidateParameterRegExp("displayName", idp.DisplayName, constants.RegExpDisplayName, true).
96129
ValidateParameterRegExp("firstBrokerLoginFlowAlias", idp.FirstBrokerLoginFlowAlias, constants.RegExpFirstBrokerLoginFlowAlias, true).
97130
ValidateParameterRegExp("internalId", idp.InternalID, constants.RegExpID, false).
98131
ValidateParameterRegExp("postBrokerLoginFlowAlias", idp.PostBrokerLoginFlowAlias, constants.RegExpPostBrokerLoginFlowAlias, true).
99-
ValidateParameterRegExp("providerId", idp.ProviderID, constants.RegExpProviderID, true)
100-
101-
if len(idp.Config) != 0 {
102-
configJSON, err := json.Marshal(idp.Config)
103-
if err != nil {
104-
return err
105-
}
106-
configStr := string(configJSON)
107-
v = v.ValidateParameterLength("config", &configStr, 0, 10000, false)
108-
}
109-
110-
if idp.HrdSettings != nil {
111-
v = v.ValidateParameterFunc(func() error {
112-
return idp.HrdSettings.Validate()
113-
})
114-
}
115-
116-
return v.Status()
132+
ValidateParameterRegExp("providerId", idp.ProviderID, constants.RegExpProviderID, true).
133+
ValidateParameterFunc(validateConfig(idp.Config)).
134+
ValidateParameter("hrdSettings", idp.HrdSettings, false).
135+
Status()
117136
}
118137

119138
// convertToAPIIdentityProviderMapper creates an API IdentityProviderMapperRepresentation from a KC IdentityProviderMapperRepresentation
@@ -138,31 +157,56 @@ func ConvertToAPIIdentityProviderMappers(kcMappers []kc.IdentityProviderMapperRe
138157
}
139158

140159
// ConvertToKCIdentityProviderMapper creates a KC IdentityProviderMapperRepresentation from an API IdentityProviderMapperRepresentation
141-
func ConvertToKCIdentityProviderMapper(apiMapper IdentityProviderMapperRepresentation) kc.IdentityProviderMapperRepresentation {
160+
func (mapperRep IdentityProviderMapperRepresentation) ConvertToKCIdentityProviderMapper() kc.IdentityProviderMapperRepresentation {
142161
return kc.IdentityProviderMapperRepresentation{
143-
Config: apiMapper.Config,
144-
ID: apiMapper.ID,
145-
IdentityProviderAlias: apiMapper.IdentityProviderAlias,
146-
IdentityProviderMapper: apiMapper.IdentityProviderMapper,
147-
Name: apiMapper.Name,
162+
Config: mapperRep.Config,
163+
ID: mapperRep.ID,
164+
IdentityProviderAlias: mapperRep.IdentityProviderAlias,
165+
IdentityProviderMapper: mapperRep.IdentityProviderMapper,
166+
Name: mapperRep.Name,
148167
}
149168
}
150169

151170
// Validate is a validator for IdentityProviderRepresentation
152171
func (mapperRep IdentityProviderMapperRepresentation) Validate() error {
153-
v := validation.NewParameterValidator().
172+
return validation.NewParameterValidator().
154173
ValidateParameterRegExp("id", mapperRep.ID, constants.RegExpID, false).
155174
ValidateParameterRegExp("identityProviderAlias", mapperRep.IdentityProviderAlias, constants.RegExpAlias, true).
156175
ValidateParameterRegExp("identityProviderMapper", mapperRep.IdentityProviderMapper, constants.RegExpAlias, true).
157-
ValidateParameterRegExp("name", mapperRep.Name, constants.RegExpDisplayName, true)
176+
ValidateParameterRegExp("name", mapperRep.Name, constants.RegExpDisplayName, true).
177+
ValidateParameterFunc(validateConfig(mapperRep.Config)).
178+
Status()
179+
}
158180

159-
if len(mapperRep.Config) != 0 {
160-
configJSON, err := json.Marshal(mapperRep.Config)
161-
if err != nil {
162-
return err
163-
}
164-
configStr := string(configJSON)
165-
v = v.ValidateParameterLength("config", &configStr, 0, 10000, false)
181+
// ConvertToAPIUserRepresentations converts a slice of KC user representation to a slice of API UserRepresentation
182+
func ConvertToAPIUserRepresentations(kcUsers []kc.UserRepresentation) []UserRepresentation {
183+
var res []UserRepresentation
184+
for _, user := range kcUsers {
185+
res = append(res, ConvertToAPIUserRepresentation(user))
186+
}
187+
return res
188+
}
189+
190+
// ConvertToAPIUserRepresentation converts a KC user representation to an API UserRepresentation
191+
func ConvertToAPIUserRepresentation(kcUser kc.UserRepresentation) UserRepresentation {
192+
return UserRepresentation{
193+
ID: kcUser.ID,
194+
Username: kcUser.Username,
195+
FirstName: kcUser.FirstName,
196+
LastName: kcUser.LastName,
197+
Email: kcUser.Email,
198+
Enabled: kcUser.Enabled,
199+
}
200+
}
201+
202+
// ConvertToKCUserRepresentation converts an API UserRepresentation to a KC UserRepresentation
203+
func (u UserRepresentation) ConvertToKCUserRepresentation() kc.UserRepresentation {
204+
return kc.UserRepresentation{
205+
ID: u.ID,
206+
Username: u.Username,
207+
FirstName: u.FirstName,
208+
LastName: u.LastName,
209+
Email: u.Email,
210+
Enabled: u.Enabled,
166211
}
167-
return v.Status()
168212
}

0 commit comments

Comments
 (0)