-
Notifications
You must be signed in to change notification settings - Fork 9
/
component.go
200 lines (161 loc) · 6.26 KB
/
component.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
199
200
package account
import (
"context"
"encoding/json"
"net/http"
"strconv"
cs "github.com/cloudtrust/common-service"
"github.com/cloudtrust/common-service/database"
commonhttp "github.com/cloudtrust/common-service/http"
api "github.com/cloudtrust/keycloak-bridge/api/account"
internal "github.com/cloudtrust/keycloak-bridge/internal/keycloakb"
kc "github.com/cloudtrust/keycloak-client"
)
// KeycloakClient interface exposes methods we need to call to send requests to Keycloak API
type KeycloakClient interface {
UpdatePassword(accessToken, realm, currentPassword, newPassword, confirmPassword string) (string, error)
UpdateAccount(accessToken, realm string, user kc.UserRepresentation) error
GetAccount(accessToken, realm string) (kc.UserRepresentation, error)
DeleteAccount(accessToken, realm string) error
}
// Component interface exposes methods used by the bridge API
type Component interface {
UpdatePassword(ctx context.Context, currentPassword, newPassword, confirmPassword string) error
GetAccount(ctx context.Context) (api.AccountRepresentation, error)
UpdateAccount(context.Context, api.AccountRepresentation) error
DeleteAccount(context.Context) error
}
// Component is the management component.
type component struct {
keycloakClient KeycloakClient
eventDBModule database.EventsDBModule
logger internal.Logger
}
// NewComponent returns the self-service component.
func NewComponent(keycloakClient KeycloakClient, eventDBModule database.EventsDBModule, logger internal.Logger) Component {
return &component{
keycloakClient: keycloakClient,
eventDBModule: eventDBModule,
logger: logger,
}
}
func (c *component) reportEvent(ctx context.Context, apiCall string, values ...string) error {
return c.eventDBModule.ReportEvent(ctx, apiCall, "self-service", values...)
}
func (c *component) UpdatePassword(ctx context.Context, currentPassword, newPassword, confirmPassword string) error {
var accessToken = ctx.Value(cs.CtContextAccessToken).(string)
var realm = ctx.Value(cs.CtContextRealm).(string)
var userID = ctx.Value(cs.CtContextUserID).(string)
var username = ctx.Value(cs.CtContextUsername).(string)
if currentPassword == newPassword || newPassword != confirmPassword {
return commonhttp.Error{
Status: http.StatusBadRequest,
}
}
_, err := c.keycloakClient.UpdatePassword(accessToken, realm, currentPassword, newPassword, confirmPassword)
var updateError error = nil
if err != nil {
switch err.Error() {
case "invalidPasswordExistingMessage":
updateError = commonhttp.Error{
Status: http.StatusBadRequest,
Message: err.Error()}
default:
updateError = err
}
}
//store the API call into the DB
err = c.reportEvent(ctx, "PASSWORD_RESET", database.CtEventRealmName, realm, database.CtEventUserID, userID, database.CtEventUsername, username)
if err != nil {
//store in the logs also the event that failed to be stored in the DB
m := map[string]interface{}{"event_name": "PASSWORD_RESET", database.CtEventRealmName: realm, database.CtEventUserID: userID, database.CtEventUsername: username}
eventJSON, errMarshal := json.Marshal(m)
if errMarshal == nil {
c.logger.Error("err", err.Error(), "event", string(eventJSON))
} else {
c.logger.Error("err", err.Error())
}
}
return updateError
}
func (c *component) GetAccount(ctx context.Context) (api.AccountRepresentation, error) {
var accessToken = ctx.Value(cs.CtContextAccessToken).(string)
var realm = ctx.Value(cs.CtContextRealm).(string)
var userRep api.AccountRepresentation
userKc, err := c.keycloakClient.GetAccount(accessToken, realm)
if err != nil {
c.logger.Warn("err", err.Error())
return userRep, err
}
userRep = api.ConvertToAPIAccount(userKc)
return userRep, nil
}
func (c *component) UpdateAccount(ctx context.Context, user api.AccountRepresentation) error {
var accessToken = ctx.Value(cs.CtContextAccessToken).(string)
var realm = ctx.Value(cs.CtContextRealm).(string)
var userID = ctx.Value(cs.CtContextUserID).(string)
var username = ctx.Value(cs.CtContextUsername).(string)
var userRep kc.UserRepresentation
// get the "old" user representation
oldUserKc, err := c.keycloakClient.GetAccount(accessToken, realm)
if err != nil {
c.logger.Warn("err", err.Error())
return err
}
var emailVerified, phoneNumberVerified *bool
// when the email changes, set the EmailVerified to false
if user.Email != nil && oldUserKc.Email != nil && *oldUserKc.Email != *user.Email {
var verified = false
emailVerified = &verified
}
// 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
phoneNumberVerified = &verified
}
} else { // the user has no attributes until now, i.e. he has not set yet his phone number
var verified = false
phoneNumberVerified = &verified
}
}
userRep = api.ConvertToKCUser(user)
if emailVerified != nil {
userRep.EmailVerified = emailVerified
}
// 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)
//Populate with the old attributes
if oldUserKc.Attributes != nil {
for key, attribute := range *oldUserKc.Attributes {
mergedAttributes[key] = attribute
}
}
if user.PhoneNumber != nil {
mergedAttributes["phoneNumber"] = []string{*user.PhoneNumber}
}
if phoneNumberVerified != nil {
mergedAttributes["phoneNumberVerified"] = []string{strconv.FormatBool(*phoneNumberVerified)}
}
userRep.Attributes = &mergedAttributes
err = c.keycloakClient.UpdateAccount(accessToken, realm, userRep)
if err != nil {
c.logger.Warn("err", err.Error())
return err
}
//store the API call into the DB
_ = c.reportEvent(ctx, "UPDATE_ACCOUNT", database.CtEventRealmName, realm, database.CtEventUserID, userID, database.CtEventUsername, username)
return nil
}
func (c *component) DeleteAccount(ctx context.Context) error {
var accessToken = ctx.Value(cs.CtContextAccessToken).(string)
var realm = ctx.Value(cs.CtContextRealm).(string)
err := c.keycloakClient.DeleteAccount(accessToken, realm)
if err != nil {
c.logger.Warn("err", err.Error())
return err
}
return nil
}