/
utils_user_auth_standalone.go
327 lines (288 loc) · 13 KB
/
utils_user_auth_standalone.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
//go:build !aws_lambda
// +build !aws_lambda
// Copyright The Linux Foundation and each contributor to CommunityBridge.
// SPDX-License-Identifier: MIT
package utils
import (
"context"
"fmt"
"os"
"strconv"
"strings"
"github.com/sirupsen/logrus"
"github.com/LF-Engineering/lfx-kit/auth"
log "github.com/communitybridge/easycla/cla-backend-go/logging"
)
const (
// ALLOW_ADMIN_SCOPE indicates that a given permissions check allows for admins to access to that resource
ALLOW_ADMIN_SCOPE = true // nolint
// DISALLOW_ADMIN_SCOPE indicates that a given permissions check does not allow for admins to access to that resource
DISALLOW_ADMIN_SCOPE = false // nolint
)
// IsUserAdmin helper function for determining if the user is an admin
func IsUserAdmin(user *auth.User) bool {
return user.Admin
}
// skipPermissionChecks determines if we can skip the permissions checks when running locally (for testing)
func skipPermissionChecks() bool {
environmentKey := "DISABLE_LOCAL_PERMISSION_CHECKS"
disablePermissionChecks, err := strconv.ParseBool(os.Getenv(environmentKey))
if err != nil {
log.Warnf("unable to check local permissions flag: %t", disablePermissionChecks)
return false
}
if disablePermissionChecks {
log.Debugf("skipping permission checks since %s is set to true", environmentKey)
}
return disablePermissionChecks
}
// IsUserAuthorizedForOrganization helper function for determining if the user is authorized for this company
func IsUserAuthorizedForOrganization(ctx context.Context, user *auth.User, companySFID string, adminScopeAllowed bool) bool {
f := logrus.Fields{
"functionName": "utils.IsUserAuthorizedForOrganization",
XREQUESTID: ctx.Value(XREQUESTID),
"userName": user.UserName,
"userEmail": user.Email,
"companySFID": companySFID,
"adminScopeAllowed": adminScopeAllowed,
}
// If we are running locally and want to disable permission checks
if skipPermissionChecks() {
log.WithFields(f).Debug("skipping permissions check")
return true
}
if adminScopeAllowed && user.Admin {
log.WithFields(f).Debug("user is authorized - admin scope is allowed and admin scope set for user")
return true
}
val := user.IsUserAuthorizedForOrganizationScope(companySFID)
if val {
log.WithFields(f).Debugf("user '%s' is authorized for companySFID: %s admin flag=%t",
user.UserName, companySFID, user.Admin)
} else {
var scopeInfo string
for i, scope := range user.Scopes {
scopeInfo = fmt.Sprintf("%sscope[%d] = {type=%s, id=%s, level=%s, role=%s, related=[%s]} ",
scopeInfo, i, scope.Type, scope.ID, scope.Level, scope.Role, strings.Join(scope.Related, ","))
}
log.WithFields(f).Debugf("user '%s' is not authorized for companySFID: %s, admin flag=%t, scopeInfo: %s",
user.UserName, companySFID, user.Admin, scopeInfo)
}
return val
}
// IsUserAuthorizedForProjectTree helper function for determining if the user is authorized for this project hierarchy/tree
func IsUserAuthorizedForProjectTree(ctx context.Context, user *auth.User, projectSFID string, adminScopeAllowed bool) bool {
f := logrus.Fields{
"functionName": "utils.IsUserAuthorizedForProjectTree",
XREQUESTID: ctx.Value(XREQUESTID),
"userName": user.UserName,
"userEmail": user.Email,
"projectSFID": projectSFID,
"adminScopeAllowed": adminScopeAllowed,
}
log.WithFields(f).Debugf("checking user auth for project tree")
// If we are running locally and want to disable permission checks
if skipPermissionChecks() {
log.WithFields(f).Debug("skipping permissions check")
return true
}
if adminScopeAllowed && user.Admin {
log.WithFields(f).Debug("user is authorized - admin scope is allowed and admin scope set for user")
return true
}
log.WithFields(f).Debugf("checking project scope for projectSFID: %s...", projectSFID)
val := user.IsUserAuthorized(auth.Project, projectSFID, true)
if val {
log.WithFields(f).Debugf("user '%s' is authorized for projectSFID: %s tree, admin flag=%t",
user.UserName, projectSFID, user.Admin)
} else {
var scopeInfo string
for i, scope := range user.Scopes {
scopeInfo = fmt.Sprintf("%sscope[%d] = {type=%s, id=%s, level=%s, role=%s, related=[%s]} ",
scopeInfo, i, scope.Type, scope.ID, scope.Level, scope.Role, strings.Join(scope.Related, ","))
}
log.WithFields(f).Debugf("user '%s' is not authorized for projectSFID: %s tree, admin flag=%t, scopeInfo: %s",
user.UserName, projectSFID, user.Admin, scopeInfo)
}
return val
}
// IsUserAuthorizedForProject helper function for determining if the user is authorized for this project
func IsUserAuthorizedForProject(ctx context.Context, user *auth.User, projectSFID string, adminScopeAllowed bool) bool {
f := logrus.Fields{
"functionName": "utils.IsUserAuthorizedForProject",
XREQUESTID: ctx.Value(XREQUESTID),
"userName": user.UserName,
"userEmail": user.Email,
"projectSFID": projectSFID,
"adminScopeAllowed": adminScopeAllowed,
}
// If we are running locally and want to disable permission checks
if skipPermissionChecks() {
log.WithFields(f).Debug("skipping permissions check")
return true
}
if adminScopeAllowed && user.Admin {
log.WithFields(f).Debug("user is authorized - admin scope is allowed and admin scope set for user")
return true
}
log.WithFields(f).Debugf("checking project scope for projectSFID: %s...", projectSFID)
val := user.IsUserAuthorizedForProjectScope(projectSFID)
if val {
log.WithFields(f).Debugf("user '%s' is authorized for projectSFID: %s, admin flag=%t",
user.UserName, projectSFID, user.Admin)
} else {
var scopeInfo string
for i, scope := range user.Scopes {
scopeInfo = fmt.Sprintf("%sscope[%d] = {type=%s, id=%s, level=%s, role=%s, related=[%s]} ",
scopeInfo, i, scope.Type, scope.ID, scope.Level, scope.Role, strings.Join(scope.Related, ","))
}
log.WithFields(f).Debugf("user '%s' is not authorized for projectSFID: %s, admin flag=%t, scopeInfo: %s",
user.UserName, projectSFID, user.Admin, scopeInfo)
}
return val
}
// IsUserAuthorizedForAnyProjects helper function for determining if the user is authorized for any of the specified projects
func IsUserAuthorizedForAnyProjects(ctx context.Context, user *auth.User, projectSFIDs []string, adminScopeAllowed bool) bool {
f := logrus.Fields{
"functionName": "utils.IsUserAuthorizedForAnyProjects",
XREQUESTID: ctx.Value(XREQUESTID),
"userName": user.UserName,
"userEmail": user.Email,
"projectSFIDs": strings.Join(projectSFIDs, ","),
"adminScopeAllowed": adminScopeAllowed,
}
// If we are running locally and want to disable permission checks
if skipPermissionChecks() {
log.WithFields(f).Debug("skipping permissions check")
return true
}
for _, projectSFID := range projectSFIDs {
log.WithFields(f).Debugf("checking project tree scope for: %s...", projectSFID)
if IsUserAuthorizedForProjectTree(ctx, user, projectSFID, adminScopeAllowed) {
log.WithFields(f).Debugf("project tree scope check passed for: %s...", projectSFID)
return true
}
log.WithFields(f).Debugf("checking project scope for: %s...", projectSFID)
if IsUserAuthorizedForProject(ctx, user, projectSFID, adminScopeAllowed) {
log.WithFields(f).Debugf("project scope check passed for: %s...", projectSFID)
return true
}
}
var scopeInfo string
for i, scope := range user.Scopes {
scopeInfo = fmt.Sprintf("%sscope[%d] = {type=%s, id=%s, level=%s, role=%s, related=[%s]} ",
scopeInfo, i, scope.Type, scope.ID, scope.Level, scope.Role, strings.Join(scope.Related, ","))
}
log.WithFields(f).Debugf("user '%s' is not authorized for project scope checks for any projects: %s, admin flag=%t, scopeInfo: %s",
user.UserName, strings.Join(projectSFIDs, ","), user.Admin, scopeInfo)
return false
}
// IsUserAuthorizedForProjectOrganization helper function for determining if the user is authorized for this project organization scope
func IsUserAuthorizedForProjectOrganization(ctx context.Context, user *auth.User, projectSFID, companySFID string, adminScopeAllowed bool) bool {
f := logrus.Fields{
"functionName": "utils.IsUserAuthorizedForProjectOrganization",
XREQUESTID: ctx.Value(XREQUESTID),
"userName": user.UserName,
"userEmail": user.Email,
"projectSFID": projectSFID,
"companySFID": companySFID,
"adminScopeAllowed": adminScopeAllowed,
}
// If we are running locally and want to disable permission checks
if skipPermissionChecks() {
log.WithFields(f).Debug("skipping permissions check")
return true
}
if adminScopeAllowed && user.Admin {
log.WithFields(f).Debug("user is authorized - admin scope is allowed and admin scope set for user")
return true
}
val := user.IsUserAuthorizedByProject(projectSFID, companySFID)
if val {
log.WithFields(f).Debugf("user '%s' is authorized for projectSFID: %s + companySFID: %s tree, admin flag=%t",
user.UserName, projectSFID, companySFID, user.Admin)
} else {
var scopeInfo string
for i, scope := range user.Scopes {
scopeInfo = fmt.Sprintf("%sscope[%d] = {type=%s, id=%s, level=%s, role=%s, related=[%s]} ",
scopeInfo, i, scope.Type, scope.ID, scope.Level, scope.Role, strings.Join(scope.Related, ","))
}
log.WithFields(f).Debugf("user '%s' is not authorized for projectSFID: %s + companySFID: %s tree, admin flag=%t, scopeInfo: %s",
user.UserName, projectSFID, companySFID, user.Admin, scopeInfo)
}
return val
}
// IsUserAuthorizedForAnyProjectOrganization helper function for determining if the user is authorized for any of the specified projects with scope of project + organization
func IsUserAuthorizedForAnyProjectOrganization(ctx context.Context, user *auth.User, projectSFIDs []string, companySFID string, adminScopeAllowed bool) bool {
f := logrus.Fields{
"functionName": "utils.IsUserAuthorizedForAnyProjectOrganization",
XREQUESTID: ctx.Value(XREQUESTID),
"userName": user.UserName,
"userEmail": user.Email,
"projectSFIDs": strings.Join(projectSFIDs, ","),
"companySFID": companySFID,
"adminScopeAllowed": adminScopeAllowed,
}
// If we are running locally and want to disable permission checks
if skipPermissionChecks() {
log.WithFields(f).Debug("skipping permissions check")
return true
}
if adminScopeAllowed && user.Admin {
log.WithFields(f).Debug("user is authorized - admin scope is allowed and admin scope set for user")
return true
}
for _, projectSFID := range projectSFIDs {
if IsUserAuthorizedForProjectOrganizationTree(ctx, user, projectSFID, companySFID, adminScopeAllowed) {
log.WithFields(f).Debugf("user is authorized for projectSFID: %s + companySFID: %s tree", projectSFID, companySFID)
return true
}
if IsUserAuthorizedForProjectOrganization(ctx, user, projectSFID, companySFID, adminScopeAllowed) {
log.WithFields(f).Debugf("user is authorized for projectSFID: %s + companySFID: %s", projectSFID, companySFID)
return true
}
}
var scopeInfo string
for i, scope := range user.Scopes {
scopeInfo = fmt.Sprintf("%sscope[%d] = {type=%s, id=%s, level=%s, role=%s, related=[%s]} ",
scopeInfo, i, scope.Type, scope.ID, scope.Level, scope.Role, strings.Join(scope.Related, ","))
}
log.WithFields(f).Debugf("user '%s' is not authorized for any projectSFID: %s + companySFID: %s, admin flag=%t, scopeInfo: %s",
user.UserName, strings.Join(projectSFIDs, ","), companySFID, user.Admin, scopeInfo)
return false
}
// IsUserAuthorizedForProjectOrganizationTree helper function for determining if the user is authorized for this project organization scope and nested projects/orgs
func IsUserAuthorizedForProjectOrganizationTree(ctx context.Context, user *auth.User, projectSFID, companySFID string, adminScopeAllowed bool) bool {
f := logrus.Fields{
"functionName": "utils.IsUserAuthorizedForProjectOrganizationTree",
XREQUESTID: ctx.Value(XREQUESTID),
"userName": user.UserName,
"userEmail": user.Email,
"projectSFID": projectSFID,
"companySFID": companySFID,
"adminScopeAllowed": adminScopeAllowed,
}
// If we are running locally and want to disable permission checks
if skipPermissionChecks() {
log.WithFields(f).Debug("skipping permissions check")
return true
}
if adminScopeAllowed && user.Admin {
log.WithFields(f).Debug("user is authorized - admin scope is allowed and admin scope set for user")
return true
}
val := user.IsUserAuthorized(auth.ProjectOrganization, projectSFID+"|"+companySFID, true)
if val {
log.WithFields(f).Debugf("user '%s' is authorized for projectSFID: %s + companySFID: %s tree, admin flag=%t",
user.UserName, projectSFID, companySFID, user.Admin)
} else {
var scopeInfo string
for i, scope := range user.Scopes {
scopeInfo = fmt.Sprintf("%sscope[%d] = {type=%s, id=%s, level=%s, role=%s, related=[%s]} ",
scopeInfo, i, scope.Type, scope.ID, scope.Level, scope.Role, strings.Join(scope.Related, ","))
}
log.WithFields(f).Debugf("user '%s' is not authorized for projectSFID: %s + companySFID: %s tree, admin flag=%t, scopeInfo: %s",
user.UserName, projectSFID, companySFID, user.Admin, scopeInfo)
}
return val
}