This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
/
user.go
272 lines (228 loc) · 8.15 KB
/
user.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*******************************************************************************
* Copyright (c) 2019 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package security
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strings"
logr "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
// RegisteredUsers : A collection of registered users
type RegisteredUsers struct {
Collection []RegisteredUser
}
// RegisteredUser : details of a registered user
type RegisteredUser struct {
ID string `json:"id"`
Username string `json:"username"`
}
// SecUserCreate : Create a new realm in Keycloak
func SecUserCreate(c *cli.Context) *SecError {
hostname := strings.TrimSpace(strings.ToLower(c.String("host")))
realm := strings.TrimSpace(c.String("realm"))
accesstoken := strings.TrimSpace(c.String("accesstoken"))
targetUsername := strings.TrimSpace(c.String("name"))
// authenticate if needed
if accesstoken == "" {
authToken, err := SecAuthenticate(http.DefaultClient, c, KeycloakMasterRealm, KeycloakAdminClientID)
if err != nil || authToken == nil {
return err
}
accesstoken = authToken.AccessToken
}
// build REST request
url := hostname + "/auth/admin/realms/" + realm + "/users"
// build the payload (JSON)
type PayloadUser struct {
Enabled bool `json:"enabled"`
Username string `json:"username"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
}
tempUser := &PayloadUser{
Enabled: true,
Username: targetUsername,
FirstName: "",
LastName: "",
}
jsonUser, err := json.Marshal(tempUser)
payload := strings.NewReader(string(jsonUser))
req, err := http.NewRequest("POST", url, payload)
if err != nil {
return &SecError{errOpConnection, err, err.Error()}
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Cache-Control", "no-cache")
req.Header.Add("cache-control", "no-cache")
req.Header.Add("Authorization", "Bearer "+accesstoken)
// send request
res, err := http.DefaultClient.Do(req)
if err != nil {
return &SecError{errOpConnection, err, err.Error()}
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if string(body) != "" {
keycloakAPIError := parseKeycloakError(string(body), res.StatusCode)
keycloakAPIError.Error = errOpCreate
kcError := errors.New(keycloakAPIError.ErrorDescription)
return &SecError{keycloakAPIError.Error, kcError, kcError.Error()}
}
return nil
}
// SecUserGet : Get user from Keycloak
func SecUserGet(c *cli.Context) (*RegisteredUser, *SecError) {
hostname := strings.TrimSpace(strings.ToLower(c.String("host")))
realm := strings.TrimSpace(c.String("realm"))
accesstoken := strings.TrimSpace(c.String("accesstoken"))
searchName := strings.TrimSpace(c.String("name"))
// authenticate if needed
if accesstoken == "" {
authToken, err := SecAuthenticate(http.DefaultClient, c, KeycloakMasterRealm, KeycloakAdminClientID)
if err != nil || authToken == nil {
return nil, err
}
accesstoken = authToken.AccessToken
}
// build REST request
url := hostname + "/auth/admin/realms/" + realm + "/users?username=" + searchName
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, &SecError{errOpConnection, err, err.Error()}
}
req.Header.Add("Authorization", "Bearer "+accesstoken)
req.Header.Add("cache-control", "no-cache")
req.Header.Add("Cache-Control", "no-cache")
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, &SecError{errOpConnection, err, err.Error()}
}
defer res.Body.Close()
// handle HTTP status codes
if res.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(res.Body)
err = errors.New(string(body))
return nil, &SecError{errOpResponse, err, err.Error()}
}
registeredUsers := RegisteredUsers{}
body, err := ioutil.ReadAll(res.Body)
err = json.Unmarshal([]byte(body), ®isteredUsers.Collection)
if err != nil {
return nil, &SecError{errOpResponseFormat, err, err.Error()}
}
registeredUser := RegisteredUser{}
if len(registeredUsers.Collection) > 0 {
registeredUser = registeredUsers.Collection[0]
return ®isteredUser, nil
}
// user not found
errNotFound := errors.New(textUserNotFound)
return nil, &SecError{errOpNotFound, errNotFound, errNotFound.Error()}
}
// SecUserSetPW : Resets the users password in keycloak to a new one supplied
func SecUserSetPW(c *cli.Context) *SecError {
hostname := strings.TrimSpace(strings.ToLower(c.String("host")))
realm := strings.TrimSpace(c.String("realm"))
accesstoken := strings.TrimSpace(c.String("accesstoken"))
newPassword := strings.TrimSpace(c.String("newpw"))
// authenticate if needed
if accesstoken == "" {
authToken, err := SecAuthenticate(http.DefaultClient, c, KeycloakMasterRealm, KeycloakAdminClientID)
if err != nil || authToken == nil {
return err
}
accesstoken = authToken.AccessToken
}
registeredUser, secError := SecUserGet(c)
if secError != nil {
return secError
}
// build REST request
url := hostname + "/auth/admin/realms/" + realm + "/users/" + registeredUser.ID + "/reset-password"
// build the payload (JSON)
type PayloadPasswordChange struct {
Type string `json:"type"`
Value string `json:"value"`
Temporary bool `json:"temporary"`
}
tempPasswordChange := &PayloadPasswordChange{Type: "password", Value: newPassword, Temporary: false}
jsonPasswordChange, err := json.Marshal(tempPasswordChange)
payload := strings.NewReader(string(jsonPasswordChange))
req, err := http.NewRequest("PUT", url, payload)
if err != nil {
return &SecError{errOpConnection, err, err.Error()}
}
req.Header.Add("Authorization", "Bearer "+accesstoken)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("cache-control", "no-cache")
req.Header.Add("Cache-Control", "no-cache")
res, err := http.DefaultClient.Do(req)
if err != nil {
return &SecError{errOpConnection, err, err.Error()}
}
defer res.Body.Close()
// handle HTTP status codes
if res.StatusCode != http.StatusNoContent {
errNotFound := errors.New(res.Status)
return &SecError{errOpNotFound, errNotFound, errNotFound.Error()}
}
return nil
}
// SecUserAddRole : Adds a role to a specified user
func SecUserAddRole(c *cli.Context) *SecError {
hostname := strings.TrimSpace(strings.ToLower(c.String("host")))
realm := strings.TrimSpace(c.String("realm"))
accesstoken := strings.TrimSpace(c.String("accesstoken"))
targetUser := strings.TrimSpace(c.String("name"))
roleName := strings.TrimSpace(c.String("role"))
// lookup an existing user
logr.Tracef("Looking up user : %v", targetUser)
registeredUser, secErr := SecUserGet(c)
if secErr != nil {
return secErr
}
// get the existing role
existingRole, secErr := getRoleByName(c, roleName)
if secErr != nil {
return secErr
}
// build REST request
logr.Printf("Adding role '%v' to user : '%v'", existingRole.Name, registeredUser.ID)
url := hostname + "/auth/admin/realms/" + realm + "/users/" + registeredUser.ID + "/role-mappings/realm"
type PayloadRole struct {
ID string `json:"id"`
Name string `json:"name"`
}
listOfRoles := []PayloadRole{{ID: existingRole.ID, Name: existingRole.Name}}
jsonRolesToAdd, err := json.Marshal(listOfRoles)
payload := strings.NewReader(string(jsonRolesToAdd))
req, err := http.NewRequest("POST", url, payload)
if err != nil {
return &SecError{errOpConnection, err, err.Error()}
}
req.Header.Add("Authorization", "Bearer "+accesstoken)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("cache-control", "no-cache")
req.Header.Add("Cache-Control", "no-cache")
res, err := http.DefaultClient.Do(req)
if err != nil {
return &SecError{errOpConnection, err, err.Error()}
}
// handle HTTP status codes (success returns status code StatusNoContent)
if res.StatusCode != http.StatusNoContent {
errNotFound := errors.New(res.Status)
return &SecError{errOpNotFound, errNotFound, errNotFound.Error()}
}
return nil
}