/
user.go
355 lines (303 loc) · 9.56 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package middleware
import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"sync"
"time"
cache "github.com/Code-Hex/go-generics-cache"
"github.com/ansel1/merry/v2"
"github.com/bcc-code/bcc-media-platform/backend/auth0"
"github.com/bcc-code/bcc-media-platform/backend/common"
"github.com/bcc-code/bcc-media-platform/backend/members"
"github.com/bcc-code/bcc-media-platform/backend/remotecache"
"github.com/bcc-code/bcc-media-platform/backend/sqlc"
"github.com/bcc-code/bcc-media-platform/backend/user"
"github.com/bcc-code/bcc-media-platform/backend/utils"
"github.com/bcc-code/mediabank-bridge/log"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/samber/lo"
"go.opentelemetry.io/otel"
)
const explicitRolesHeader = "x-explicit-roles"
var userCacheLocks = utils.SyncMap[string, *sync.Mutex]{}
var userCache = cache.New[string, *common.User]()
var rolesCache = cache.New[string, map[string][]string]()
func ageFromBirthDate(birthDate string) int {
date, err := time.Parse("2006-04-02", birthDate)
if err != nil {
return 0
}
now := time.Now()
// years since birth
years := now.Year() - date.Year()
// if the user hasn't had their birthday yet this year, subtract a year
if now.Month() < date.Month() || (now.Month() == date.Month() && now.Day() < date.Day()) {
years--
}
return years
}
func getExplicitRolesFromContext(ctx *gin.Context, queries *sqlc.Queries) []string {
var result []string
if explicitRoles := ctx.GetHeader(explicitRolesHeader); explicitRoles != "" {
allRoles, err := getRoles(ctx, queries)
if err != nil {
log.L.Error().Err(err).Send()
} else {
specifiedRoles := strings.Split(explicitRoles, ",")
for _, r := range specifiedRoles {
if _, ok := allRoles["explicit:"+r]; ok {
result = append(result, r)
}
}
}
}
return result
}
func getFeatureFlagRolesFromContext(ctx *gin.Context) []string {
var roles []string
featureFlags := utils.GetFeatureFlags(ctx)
for _, flag := range featureFlags.List() {
roles = append(roles, "feature-flag:"+flag)
}
return roles
}
// NewUserMiddleware returns a gin middleware that ingests a populated User struct
// into the gin context
func NewUserMiddleware(queries *sqlc.Queries, remoteCache *remotecache.Client, ls *common.BatchLoaders, auth0Client *auth0.Client) func(*gin.Context) {
return func(ctx *gin.Context) {
reqCtx, span := otel.Tracer("user/middleware").Start(ctx.Request.Context(), "run")
defer span.End()
var roles []string
explicitRoles := getExplicitRolesFromContext(ctx, queries)
if len(explicitRoles) > 0 {
roles = append(roles, explicitRoles...)
}
featureRoles := getFeatureFlagRolesFromContext(ctx)
if len(featureRoles) > 0 {
roles = append(roles, featureRoles...)
}
authed := ctx.GetBool(auth0.CtxAuthenticated)
// This can't be on the user object because that is cached for too long
ctx.Set(user.CtxLanguages, user.GetAcceptedLanguagesFromCtx(ctx))
// If the user is anonymous we just create a simple object and bail
if !authed {
span.AddEvent("Anonymous")
roles = append(roles, user.RolePublic)
ctx.Set(user.CtxUser,
&common.User{
Roles: roles,
Anonymous: true,
ActiveBCC: false,
})
return
}
userID := ctx.GetString(auth0.CtxUserID)
if u, ok := userCache.Get(userID); ok {
span.AddEvent("User From Cache")
ctx.Set(user.CtxUser, u)
return
}
getUserFromMembers := func(o *remotecache.Options) (*common.User, error) {
o.SetTTL(time.Minute * 5)
pid := ctx.GetString(auth0.CtxPersonID)
intID, _ := strconv.ParseInt(pid, 10, 32)
roles = append(roles, user.RoleRegistered)
if ctx.GetBool(auth0.CtxIsBCCMember) {
roles = append(roles, user.RoleBCCMember)
} else {
roles = append(roles, user.RoleNonBCCMember, user.RolePublic)
}
if pid == "" || pid == "0" {
pid = ctx.GetString(auth0.CtxUserID)
}
u := &common.User{
PersonID: pid,
Roles: roles,
Anonymous: false,
ActiveBCC: ctx.GetBool(auth0.CtxIsBCCMember),
AgeGroup: "unknown",
Gender: "unknown",
}
saveUser := func() error {
return queries.UpsertUser(ctx, sqlc.UpsertUserParams{
ID: u.PersonID,
Roles: u.Roles,
DisplayName: u.DisplayName,
FirstName: u.FirstName,
ActiveBcc: u.ActiveBCC,
EmailVerified: u.EmailVerified,
Email: u.Email,
AgeGroup: u.AgeGroup,
Age: int32(u.Age),
Gender: u.Gender,
ChurchIds: lo.Map(u.ChurchIDs, func(i int, _ int) int32 {
return int32(i)
}),
})
}
//info, err := auth0Client.GetUserInfoForAuthHeader(ctx, ctx.GetHeader("Authorization"))
//if err != nil {
// return nil, err
//}
//u.EmailVerified = info.EmailVerified
u.EmailVerified = true
if u.IsActiveBCC() {
member, err := ls.MemberLoader.Get(ctx, int(intID))
if err != nil || member == nil {
marshalledU, _ := json.Marshal(u)
log.L.Warn().
Err(err).
Int64("personId", intID).
Str("user", string(marshalledU)).
Msg("Failed to retrieve user from members.")
span.AddEvent("User failed to load from members")
dbUser, err := ls.UserLoader.Get(ctx, pid)
if err != nil {
log.L.Error().Err(err).Msg("Failed to retrieve user from database.")
}
if dbUser != nil {
u = dbUser
}
userCache.Set(userID, u, cache.WithExpiration(1*time.Minute))
ctx.Set(user.CtxUser, u)
return u, nil
}
u.FirstName = member.FirstName
switch member.Gender {
case "Male":
u.Gender = "male"
case "Female":
u.Gender = "female"
default:
u.Gender = "unknown"
}
u.Email = member.Email
u.DisplayName = member.DisplayName
u.Age = ageFromBirthDate(member.BirthDate)
now := time.Now()
affiliations := lo.Filter(member.Affiliations, func(i members.Affiliation, _ int) bool {
return (i.ValidTo == nil || i.ValidTo.After(now)) && (i.ValidFrom == nil || i.ValidFrom.Before(now))
})
organizations, err := ls.OrganizationLoader.GetMany(ctx, lo.Map(affiliations, func(i members.Affiliation, _ int) uuid.UUID {
return i.OrgUid
}))
if err != nil {
log.L.Error().Err(err).Send()
}
for _, org := range organizations {
if org != nil && org.Type == "Church" {
u.ChurchIDs = append(u.ChurchIDs, org.OrgID)
}
}
} else {
info, err := auth0Client.GetUser(ctx, ctx.GetString(auth0.CtxUserID))
if err != nil {
return nil, err
}
u.PersonID = info.UserId
u.Email = info.Email
u.DisplayName = info.Nickname
u.EmailVerified = info.EmailVerified
if by, ok := info.UserMetadata["birth_year"]; ok {
year, err := strconv.ParseInt(by, 10, 64)
if err == nil {
u.Age = time.Now().Year() - int(year)
}
}
u.CompletedRegistration = info.CompletedRegistration()
}
if u.Email == "" {
// Explicit values make it easier to see that it was intended when debugging
u.Email = "<MISSING>"
}
if u.EmailVerified {
userRoles, err := GetRolesForEmail(reqCtx, queries, u.Email)
if err != nil {
err = merry.Wrap(err)
log.L.Warn().Err(err).Str("email", u.Email).Msg("Unable to get roles")
} else {
roles = append(roles, userRoles...)
}
}
ageGroupMin := 0
for minAge, group := range user.AgeGroups {
// Note: Maps are not iterated in a sorted order, so we have to find the lowest applicable range
if u.Age >= minAge && minAge > ageGroupMin {
u.AgeGroup = group
ageGroupMin = minAge
}
}
if u.AgeGroup != "" && u.IsActiveBCC() {
roles = append(roles, "age-group:"+strings.Replace(u.AgeGroup, " ", "", -1))
}
u.Roles = roles
err := saveUser()
if err != nil {
log.L.Error().Err(err).Send()
}
return u, nil
}
lock, _ := userCacheLocks.LoadOrStore(userID, &sync.Mutex{})
lock.Lock()
defer func() {
lock.Unlock()
userCacheLocks.Delete(userID)
}()
if u, err := remotecache.GetOrCreate[*common.User](ctx, remoteCache, fmt.Sprintf("users:%s", userID), getUserFromMembers); err == nil {
span.AddEvent("User loaded into cache")
userCache.Set(userID, u, cache.WithExpiration(60*time.Second))
ctx.Set(user.CtxUser, u)
return
} else {
log.L.Error().Err(err).Send()
ctx.Set(user.CtxUser, &common.User{
Roles: roles,
Anonymous: true,
ActiveBCC: false,
})
}
}
}
func getRoles(ctx context.Context, queries *sqlc.Queries) (map[string][]string, error) {
ctx, span := otel.Tracer("user").Start(ctx, "getRoles")
defer span.End()
if roles, ok := rolesCache.Get(user.CacheRoles); ok {
span.AddEvent("from cache")
return roles, nil
}
allRoles := map[string][]string{}
roles, err := queries.GetRoles(ctx)
if err != nil {
return nil, merry.Wrap(err)
}
lo.ForEach(roles, func(x sqlc.GetRolesRow, _ int) {
if x.ExplicitlyAvailable {
allRoles["explicit:"+x.Code] = x.Emails
}
allRoles[x.Code] = x.Emails
})
span.AddEvent("loaded into cache")
rolesCache.Set(user.CacheRoles, allRoles, cache.WithExpiration(60*time.Minute))
return allRoles, nil
}
// GetRolesForEmail returns all roles applicable for a specific email address.
// The roles are fetched from a local cache or if that is not available from the DB
func GetRolesForEmail(ctx context.Context, queries *sqlc.Queries, email string) ([]string, error) {
ctx, span := otel.Tracer("user").Start(ctx, "GetRolesForEmail")
defer span.End()
var rtnRoles []string
allRoles, err := getRoles(ctx, queries)
if err != nil {
return rtnRoles, merry.Wrap(err)
}
for k, emails := range allRoles {
if lo.Contains(emails, email) {
rtnRoles = append(rtnRoles, k)
}
}
return rtnRoles, nil
}