-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeycloak.go
236 lines (208 loc) · 5.42 KB
/
keycloak.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
package keycloak
import (
"fmt"
"github.com/getevo/evo/v2/lib/db/schema"
"github.com/getevo/evo/v2/lib/text"
"reflect"
"strings"
)
func (connection *Connection) EditUserFromStruct(user interface{}) error {
var _typ = reflect.TypeOf(user)
if _typ.Kind() != reflect.Ptr {
return fmt.Errorf("user must be a pointer")
}
_, err := getUserModel(_typ)
if err != nil {
return err
}
var keycloakUser = UserInstance{
Attributes: map[string][]string{},
}
var uuid = getUUID(user)
keycloakUser, err = connection.GetUser(uuid)
if err != nil {
return err
}
err, _ = setFieldValues(user, &keycloakUser)
if err != nil {
return err
}
if keycloakUser.Credentials != nil && len(*keycloakUser.Credentials) > 0 && (*keycloakUser.Credentials)[0].Value != "" {
err = connection.SetCredentials(&keycloakUser, (*keycloakUser.Credentials)[0])
if err != nil {
return err
}
}
err = connection.EditUser(&keycloakUser)
if err != nil {
return err
}
return nil
}
func getUUID(user interface{}) string {
var ref = reflect.ValueOf(user)
for ref.Kind() == reflect.Ptr {
ref = ref.Elem()
}
for _, field := range userModel.Schema.Fields {
if field.Name == "UUID" {
return sprint(ref.FieldByName(field.Name))
}
var chunks = strings.Split(field.Tag.Get("keycloak"), ":")
if len(chunks) == 2 && chunks[0] == "field" && chunks[1] == "UUID" {
return sprint(ref.FieldByName(field.Name))
}
}
return ""
}
func setFieldValues(user interface{}, keycloakUser *UserInstance) (error, string) {
var uuidField string
var keycloakRef = reflect.ValueOf(keycloakUser)
for keycloakRef.Kind() == reflect.Ptr {
keycloakRef = keycloakRef.Elem()
}
var userRef = reflect.ValueOf(user)
for userRef.Kind() == reflect.Ptr {
userRef = userRef.Elem()
}
for _, field := range userModel.Schema.Fields {
r := userRef.FieldByName(field.Name)
f := keycloakRef.FieldByName(field.Name)
if field.Name == "UUID" {
uuidField = field.Name
continue
}
if f.IsValid() && field.FieldType == f.Type() && !r.IsZero() {
f.Set(reflect.ValueOf(r.Interface()))
continue
}
var tag = field.Tag.Get("keycloak")
var chunks = strings.Split(tag, ":")
if len(chunks) != 2 {
continue
}
if chunks[0] == "field" {
if chunks[1] == "UUID" {
uuidField = field.Name
continue
}
if chunks[1] == "Password" {
var value = sprint(r)
if value != "" {
keycloakUser.Credentials = &[]Credentials{
{
Type: "password",
Value: value,
Temporary: false,
},
}
}
continue
}
f = keycloakRef.FieldByName(chunks[1])
if f.IsValid() && field.FieldType == f.Type() {
if r.IsValid() && !r.IsZero() {
f.Set(reflect.ValueOf(r.Interface()))
break
}
} else {
return fmt.Errorf("field %s not found or type missmatch in user model %s", chunks[1], userRef.Type().String()), uuidField
}
}
if chunks[0] == "attribute" {
//r = userRef.FieldByName(field.Name)
if !r.IsValid() || r.IsZero() || (r.Kind() == reflect.Ptr && r.IsNil()) || r.Interface() == nil {
keycloakUser.Attributes.Set(chunks[1], "")
continue
}
keycloakUser.Attributes.Set(chunks[1], sprint(r))
continue
}
}
if keycloakUser.Username == "" {
keycloakUser.Username = keycloakUser.Email
}
return nil, uuidField
}
func getUserModel(_typ reflect.Type) (*schema.Model, error) {
if userModel == nil {
for idx, _ := range schema.Models {
if schema.Models[idx].Name == _typ.Elem().String() {
userModel = &schema.Models[idx]
break
}
}
if userModel == nil {
return nil, fmt.Errorf("user model %s not found", _typ.String())
}
}
return userModel, nil
}
func (connection *Connection) NewUserFromStruct(user interface{}) error {
var _typ = reflect.TypeOf(user)
if _typ.Kind() != reflect.Ptr {
return fmt.Errorf("user must be a pointer")
}
_, err := getUserModel(_typ)
if err != nil {
return err
}
var keycloakUser = &UserInstance{
Attributes: map[string][]string{},
}
var uuidField string
err, uuidField = setFieldValues(user, keycloakUser)
if err != nil {
return err
}
err = connection.CreateUser(keycloakUser)
if err != nil {
return err
}
if uuidField != "" {
reflect.ValueOf(user).Elem().FieldByName(uuidField).Set(reflect.ValueOf(keycloakUser.UUID))
}
return nil
}
func NewUser(user interface{}) error {
return conn.NewUserFromStruct(user)
}
func EditUser(user interface{}) error {
return conn.EditUserFromStruct(user)
}
func Login(username, password string) (*JWT, error) {
return conn.Login(username, password)
}
func RefreshToken(refreshToken string) (*JWT, error) {
return conn.RefreshToken(refreshToken)
}
func GetUser(uuid string) (UserInstance, error) {
return conn.GetUser(uuid)
}
func ParseToken(accessToken string, claims interface{}, strict bool) (Spec, error) {
return conn.ParseToken(accessToken, claims, strict)
}
func LogoutSession(session *Session) error {
return conn.LogoutSession(session)
}
func ChangePassword(user *UserInstance, password string) error {
return conn.ChangePassword(user, password)
}
func VerifyOffline(accessToken string, claims interface{}) (Spec, error) {
return conn.VerifyOffline(accessToken, claims)
}
func sprint(v reflect.Value) string {
if !v.IsValid() || v.IsZero() {
return ""
}
var result string
switch v.Kind() {
case reflect.Struct, reflect.Map, reflect.Slice:
result = text.ToJSON(v)
case reflect.Ptr:
result = sprint(v.Elem())
default:
result = fmt.Sprint(v.Interface())
}
return result
}