-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
helpers.go
238 lines (208 loc) · 6.78 KB
/
helpers.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
package idp
import (
"fmt"
"strings"
log "github.com/sirupsen/logrus"
"github.com/slashdevops/idp-scim-sync/internal/convert"
"github.com/slashdevops/idp-scim-sync/internal/model"
admin "google.golang.org/api/admin/directory/v1"
)
// buildUser builds a User model from a User coming from the IDP API
func buildUser(usr *admin.User) *model.User {
if usr == nil {
return nil
}
// these fields are required because the Constrains defined here:
// https://docs.aws.amazon.com/singlesignon/latest/developerguide/createuser.html
if usr.Name == nil {
log.Warn("idp: User name is nil")
return nil
}
if usr.Name.GivenName == "" {
log.Warn("idp: User given name is empty")
return nil
}
if usr.Name.FamilyName == "" {
log.Warn("idp: User family name is empty")
return nil
}
if usr.PrimaryEmail == "" {
log.Warn("idp: User primary email is empty")
return nil
}
var emails []model.Email
if m, ok := usr.Emails.([]interface{}); ok {
for _, v := range m {
if v.(map[string]interface{})["primary"] != nil {
if v.(map[string]interface{})["primary"].(bool) {
var emailValue string
if v.(map[string]interface{})["address"] != nil {
emailValue = strings.TrimSpace(v.(map[string]interface{})["address"].(string))
}
var emailType string
if v.(map[string]interface{})["type"] != nil {
emailType = strings.TrimSpace(v.(map[string]interface{})["type"].(string))
}
emails = append(emails,
model.EmailBuilder().
WithPrimary(v.(map[string]interface{})["primary"].(bool)).
WithType(emailType).
WithValue(emailValue).
Build(),
)
break
}
}
}
}
if len(emails) == 0 {
log.Warn("idp: User emails is empty, setting primary email as the only email")
emails = append(emails,
model.EmailBuilder().
WithPrimary(true).
WithType("work").
WithValue(strings.TrimSpace(usr.PrimaryEmail)).
Build(),
)
}
// get the first language from the list of languages
var preferredLanguage string
if m, ok := usr.Languages.([]interface{}); ok {
for _, v := range m {
if v.(map[string]interface{})["preference"].(string) == "preferred" {
preferredLanguage = strings.TrimSpace(v.(map[string]interface{})["languageCode"].(string))
break
}
}
}
// get the Addresses
var addresses []model.Address
if m, ok := usr.Addresses.([]interface{}); ok {
for _, v := range m {
if v.(map[string]interface{})["type"].(string) == "work" {
addresses = append(addresses,
model.AddressBuilder().
WithFormatted(strings.TrimSpace(v.(map[string]interface{})["formatted"].(string))).
Build())
break
} else if v.(map[string]interface{})["type"].(string) == "home" {
addresses = append(addresses,
model.AddressBuilder().
WithFormatted(strings.TrimSpace(v.(map[string]interface{})["formatted"].(string))).
Build())
break
}
}
}
// get the phones
var phoneNumbers []model.PhoneNumber
if m, ok := usr.Phones.([]interface{}); ok {
for _, v := range m {
if v.(map[string]interface{})["type"].(string) == "work" {
phoneNumbers = append(phoneNumbers,
model.PhoneNumberBuilder().
WithValue(strings.TrimSpace(v.(map[string]interface{})["value"].(string))).
WithType(strings.TrimSpace(v.(map[string]interface{})["type"].(string))).
Build())
break
} else if v.(map[string]interface{})["type"].(string) == "home" {
phoneNumbers = append(phoneNumbers,
model.PhoneNumberBuilder().
WithValue(strings.TrimSpace(v.(map[string]interface{})["value"].(string))).
WithType(strings.TrimSpace(v.(map[string]interface{})["type"].(string))).
Build())
break
}
}
}
// get the relations (manager)
var manager *model.Manager
if m, ok := usr.Relations.([]interface{}); ok {
for _, v := range m {
if v.(map[string]interface{})["type"].(string) == "manager" {
manager = model.ManagerBuilder().
WithValue(strings.TrimSpace(v.(map[string]interface{})["value"].(string))).
WithRef(strings.TrimSpace(v.(map[string]interface{})["customType"].(string))).
Build()
break
}
}
}
// get the organizations
var mainOrganization *model.EnterpriseData
var title string
if m, ok := usr.Organizations.([]interface{}); ok {
for _, v := range m {
if v.(map[string]interface{})["primary"] != nil && v.(map[string]interface{})["primary"].(bool) {
var employeeNumber string
if v.(map[string]interface{})["employeeNumber"] != nil {
employeeNumber = strings.TrimSpace(v.(map[string]interface{})["employeeNumber"].(string))
}
var costCenter string
if v.(map[string]interface{})["costCenter"] != nil {
costCenter = strings.TrimSpace(v.(map[string]interface{})["costCenter"].(string))
}
var organization string
if v.(map[string]interface{})["name"] != nil {
organization = strings.TrimSpace(v.(map[string]interface{})["name"].(string))
}
var division string
if v.(map[string]interface{})["division"] != nil {
division = strings.TrimSpace(v.(map[string]interface{})["division"].(string))
}
var department string
if v.(map[string]interface{})["department"] != nil {
department = strings.TrimSpace(v.(map[string]interface{})["department"].(string))
}
if v.(map[string]interface{})["title"] != nil {
title = strings.TrimSpace(v.(map[string]interface{})["title"].(string))
}
mainOrganization = model.EnterpriseDataBuilder().
WithEmployeeNumber(employeeNumber).
WithCostCenter(costCenter).
WithOrganization(organization).
WithDivision(division).
WithDepartment(department).
WithManager(manager).
Build()
break
}
}
}
var displayName string
if usr.Name.FullName != "" {
displayName = usr.Name.FullName
} else {
displayName = fmt.Sprintf("%s %s", usr.Name.GivenName, usr.Name.FamilyName)
}
var name *model.Name
if usr.Name != nil {
name = model.NameBuilder().
WithGivenName(strings.TrimSpace(usr.Name.GivenName)).
WithFamilyName(strings.TrimSpace(usr.Name.FamilyName)).
WithFormatted(strings.TrimSpace(usr.Name.FullName)).
Build()
}
userModel := model.UserBuilder().
WithIPID(strings.TrimSpace(usr.Id)).
WithUserName(strings.TrimSpace(usr.PrimaryEmail)).
WithDisplayName(strings.TrimSpace(displayName)).
// WithNickName("Not Provided").
// WithProfileURL("Not Provided").
WithTitle(title).
WithUserType(strings.TrimSpace(usr.Kind)).
WithPreferredLanguage(preferredLanguage).
// WithLocale("Not Provided").
// WithTimezone("Not Provided").
WithActive(!usr.Suspended).
// Arrays
WithEmails(emails).
WithAddresses(addresses).
WithPhoneNumbers(phoneNumbers).
// Pointers
WithName(name).
WithEnterpriseData(mainOrganization).
Build()
log.Tracef("idp: buildUser() from: \n%s, \n--> to:\n %s\n", convert.ToJSONString(usr), convert.ToJSONString(userModel))
return userModel
}