-
Notifications
You must be signed in to change notification settings - Fork 9
/
api.go
198 lines (174 loc) · 7.98 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package apiaccount
import (
"context"
"encoding/json"
"github.com/cloudtrust/common-service/validation"
"github.com/cloudtrust/keycloak-bridge/internal/constants"
msg "github.com/cloudtrust/keycloak-bridge/internal/constants"
"github.com/cloudtrust/keycloak-bridge/internal/keycloakb"
kc "github.com/cloudtrust/keycloak-client"
)
// AccountRepresentation struct
type AccountRepresentation struct {
Username *string `json:"username,omitempty"`
Email *string `json:"email,omitempty"`
EmailVerified *bool `json:"emailVerified,omitempty"`
Gender *string `json:"gender,omitempty"`
FirstName *string `json:"firstName,omitempty"`
LastName *string `json:"lastName,omitempty"`
PhoneNumber *string `json:"phoneNumber,omitempty"`
PhoneNumberVerified *bool `json:"phoneNumberVerified,omitempty"`
BirthDate *string `json:"birthDate,omitempty"`
BirthLocation *string `json:"birthLocation,omitempty"`
IDDocumentType *string `json:"idDocumentType,omitempty"`
IDDocumentNumber *string `json:"idDocumentNumber,omitempty"`
IDDocumentExpiration *string `json:"idDocumentExpiration,omitempty"`
Locale *string `json:"locale,omitempty"`
Accreditations *[]AccreditationRepresentation `json:"accreditations,omitempty"`
}
// AccreditationRepresentation is a representation of accreditations
type AccreditationRepresentation struct {
Type *string `json:"type"`
ExpiryDate *string `json:"expiryDate"`
Expired *bool `json:"expired,omitempty"`
}
// CredentialRepresentation struct
type CredentialRepresentation struct {
ID *string `json:"id,omitempty"`
Type *string `json:"type,omitempty"`
UserLabel *string `json:"userLabel,omitempty"`
CreatedDate *int64 `json:"createdDate,omitempty"`
CredentialData *string `json:"credentialData,omitempty"`
Value *string `json:"value,omitempty"`
Temporary *bool `json:"temporary,omitempty"`
}
// Configuration struct
type Configuration struct {
EditingEnabled *bool `json:"editing_enabled"`
ShowAuthenticatorsTab *bool `json:"show_authenticators_tab"`
ShowPasswordTab *bool `json:"show_password_tab"`
ShowProfileTab *bool `json:"show_profile_tab"`
ShowAccountDeletionButton *bool `json:"show_account_deletion_button"`
RedirectSuccessfulRegistrationURL *string `json:"redirect_successful_registration_url"`
AvailableChecks map[string]bool `json:"available-checks"`
}
// UpdatePasswordBody is the definition of the expected body content of UpdatePassword method
type UpdatePasswordBody struct {
CurrentPassword string `json:"currentPassword"`
NewPassword string `json:"newPassword"`
ConfirmPassword string `json:"confirmPassword"`
}
// LabelBody struct
type LabelBody struct {
Label string `json:"label,omitempty"`
}
// ConvertCredential creates an API credential from a KC credential
func ConvertCredential(credKc *kc.CredentialRepresentation) CredentialRepresentation {
var cred CredentialRepresentation
cred.ID = credKc.ID
cred.Type = credKc.Type
cred.UserLabel = credKc.UserLabel
cred.CreatedDate = credKc.CreatedDate
cred.CredentialData = credKc.CredentialData
cred.Temporary = credKc.Temporary
// credKc.Value is ignored. It may contains secret for legacy credential thus we don't want to transmit it.
return cred
}
// ConvertToAPIAccount creates an API account representation from a KC user representation
func ConvertToAPIAccount(ctx context.Context, userKc kc.UserRepresentation, logger keycloakb.Logger) AccountRepresentation {
var userRep AccountRepresentation
userRep.Username = userKc.Username
userRep.Email = userKc.Email
userRep.EmailVerified = userKc.EmailVerified
userRep.FirstName = userKc.FirstName
userRep.LastName = userKc.LastName
if value := userKc.GetAttributeString(constants.AttrbPhoneNumber); value != nil {
userRep.PhoneNumber = value
}
if verified, err := userKc.GetAttributeBool(constants.AttrbPhoneNumberVerified); err == nil && verified != nil {
userRep.PhoneNumberVerified = verified
}
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 values := userKc.GetAttribute(constants.AttrbAccreditations); len(values) > 0 {
var accreds []AccreditationRepresentation
for _, accredJSON := range values {
var accred AccreditationRepresentation
if json.Unmarshal([]byte(accredJSON), &accred) == nil {
accred.Expired = keycloakb.IsDateInThePast(accred.ExpiryDate)
accreds = append(accreds, accred)
} else {
logger.Warn(ctx, "msg", "Can't unmarshall JSON", "json", accredJSON)
}
}
userRep.Accreditations = &accreds
}
return userRep
}
// ConvertToKCUser creates a KC user representation from an API user
func ConvertToKCUser(user AccountRepresentation) kc.UserRepresentation {
var userRep kc.UserRepresentation
userRep.Username = user.Username
userRep.Email = user.Email
userRep.FirstName = user.FirstName
userRep.LastName = user.LastName
var attributes = make(kc.Attributes)
attributes.SetStringWhenNotNil(constants.AttrbPhoneNumber, user.PhoneNumber)
attributes.SetStringWhenNotNil(constants.AttrbLocale, user.Locale)
if len(attributes) > 0 {
userRep.Attributes = &attributes
}
return userRep
}
// Validators
// Validate is a validator for AccountRepresentation
func (user AccountRepresentation) Validate() error {
return validation.NewParameterValidator().
ValidateParameterRegExp(msg.Username, user.Username, RegExpUsername, false).
ValidateParameterRegExp(msg.Email, user.Email, RegExpEmail, false).
ValidateParameterRegExp(msg.Firstname, user.FirstName, RegExpFirstName, false).
ValidateParameterRegExp(msg.Lastname, user.LastName, RegExpLastName, false).
ValidateParameterRegExp(msg.PhoneNumber, user.PhoneNumber, RegExpPhoneNumber, false).
ValidateParameterRegExp(msg.Locale, user.Locale, RegExpLocale, false).
Status()
}
// Validate is a validator for UpdatePasswordBody
func (updatePwd UpdatePasswordBody) Validate() error {
return validation.NewParameterValidator().
ValidateParameterRegExp(msg.CurrentPassword, &updatePwd.CurrentPassword, RegExpPassword, true).
ValidateParameterRegExp(msg.NewPassword, &updatePwd.NewPassword, RegExpPassword, true).
ValidateParameterRegExp(msg.ConfirmPassword, &updatePwd.ConfirmPassword, RegExpPassword, true).
Status()
}
// Validate is a validator for CredentialRepresentation
func (credential CredentialRepresentation) Validate() error {
return validation.NewParameterValidator().
ValidateParameterRegExp(msg.ID, credential.ID, RegExpID, false).
ValidateParameterRegExp(msg.Type, credential.Type, RegExpType, false).
ValidateParameterRegExp(msg.Label, credential.UserLabel, RegExpLabel, false).
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}$`
RegExpIDNullable = `^([a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12})|(null)$`
RegExpLabel = `^.{0,255}$`
RegExpType = `^[a-zA-Z0-9-_]{1,128}$`
RegExpRealmName = `^[a-zA-Z0-9_-]{1,36}$`
// Password
RegExpPassword = `^.{1,255}$`
// User
RegExpUsername = `^[a-zA-Z0-9-_.]{1,128}$`
RegExpEmail = `^.+\@.+\..+`
RegExpFirstName = `^.{1,128}$`
RegExpLastName = `^.{1,128}$`
RegExpPhoneNumber = `^\+[1-9]\d{1,14}$`
RegExpLocale = `^\w{2}(-\w{2})?$`
)