| package permission |
|
|
| import ( |
| "context" |
| "errors" |
| "regexp" |
| "strings" |
| "sync/atomic" |
|
|
| "github.com/google/uuid" |
| "github.com/target/goalert/util/log" |
| ) |
|
|
| |
| func SourceContext(ctx context.Context, src *SourceInfo) context.Context { |
| if src == nil { |
| return ctx |
| } |
| |
| dup := *src |
| ctx = log.WithField(ctx, "AuthSource", src.String()) |
| return context.WithValue(ctx, contextKeySourceInfo, dup) |
| } |
|
|
| |
| func Source(ctx context.Context) *SourceInfo { |
| src, ok := ctx.Value(contextKeySourceInfo).(SourceInfo) |
| if !ok { |
| return nil |
| } |
| return &src |
| } |
|
|
| |
| func UserSourceContext(ctx context.Context, id string, r Role, src *SourceInfo) context.Context { |
| ctx = SourceContext(ctx, src) |
| ctx = UserContext(ctx, id, r) |
| return ctx |
| } |
|
|
| |
| func UserContext(ctx context.Context, id string, r Role) context.Context { |
| id = strings.ToLower(id) |
| ctx = context.WithValue(ctx, contextHasAuth, 1) |
| ctx = context.WithValue(ctx, contextKeyUserID, id) |
| ctx = ensureAuthCheckCountContext(ctx) |
| ctx = context.WithValue(ctx, contextKeyUserRole, r) |
| ctx = log.WithField(ctx, "AuthUserID", id) |
| return ctx |
| } |
|
|
| var sysRx = regexp.MustCompile(`^([a-zA-Z0-9]+|Sudo\[[a-zA-Z0-9]+\])$`) |
|
|
| |
| |
| func SystemContext(ctx context.Context, componentName string) context.Context { |
| if !sysRx.MatchString(componentName) { |
| panic(errors.New("invalid system component name: " + componentName)) |
| } |
| ctx = context.WithValue(ctx, contextHasAuth, 1) |
| ctx = context.WithValue(ctx, contextKeySystem, componentName) |
| ctx = AuthCheckCountContext(ctx, 0) |
| ctx = log.WithField(ctx, "AuthSystemComponent", componentName) |
| return ctx |
| } |
|
|
| |
| |
| func AuthCheckCount(ctx context.Context) (value, max uint64) { |
| val, ok := ctx.Value(contextKeyCheckCount).(*uint64) |
| if ok { |
| value = atomic.LoadUint64(val) |
| } |
|
|
| max, _ = ctx.Value(contextKeyCheckCountMax).(uint64) |
|
|
| return value, max |
| } |
|
|
| func ensureAuthCheckCountContext(ctx context.Context) context.Context { |
| _, ok := ctx.Value(contextKeyCheckCount).(*uint64) |
| if !ok { |
| return AuthCheckCountContext(ctx, 0) |
| } |
| return ctx |
| } |
|
|
| |
| |
| func AuthCheckCountContext(ctx context.Context, max uint64) context.Context { |
| val, _ := ctx.Value(contextKeyCheckCount).(*uint64) |
| if val == nil { |
| ctx = context.WithValue(ctx, contextKeyCheckCount, new(uint64)) |
| } |
| ctx = context.WithValue(ctx, contextKeyCheckCountMax, max) |
|
|
| return ctx |
| } |
|
|
| |
| func ServiceSourceContext(ctx context.Context, id string, src *SourceInfo) context.Context { |
| ctx = SourceContext(ctx, src) |
| ctx = ServiceContext(ctx, id) |
| return ctx |
| } |
|
|
| |
| func ServiceContext(ctx context.Context, serviceID string) context.Context { |
| serviceID = strings.ToLower(serviceID) |
| ctx = context.WithValue(ctx, contextHasAuth, 1) |
| ctx = ensureAuthCheckCountContext(ctx) |
| ctx = context.WithValue(ctx, contextKeyServiceID, serviceID) |
| ctx = log.WithField(ctx, "AuthServiceID", serviceID) |
|
|
| return ctx |
| } |
|
|
| |
| func TeamContext(ctx context.Context, teamID string) context.Context { |
| teamID = strings.ToLower(teamID) |
| ctx = context.WithValue(ctx, contextHasAuth, 1) |
| ctx = context.WithValue(ctx, contextKeyCheckCount, new(uint64)) |
| ctx = context.WithValue(ctx, contextKeyTeamID, teamID) |
| ctx = log.WithField(ctx, "AuthTeamID", teamID) |
|
|
| return ctx |
| } |
|
|
| |
| func WithoutAuth(ctx context.Context) context.Context { |
| if System(ctx) { |
| ctx = context.WithValue(ctx, contextKeySystem, nil) |
| } |
| if id, ok := ctx.Value(contextKeyUserID).(string); ok && id != "" { |
| ctx = context.WithValue(ctx, contextKeyUserID, nil) |
| ctx = context.WithValue(ctx, contextKeyUserRole, nil) |
| } |
| if Service(ctx) { |
| ctx = context.WithValue(ctx, contextKeyServiceID, nil) |
| } |
|
|
| v, _ := ctx.Value(contextHasAuth).(int) |
| if v == 1 { |
| ctx = context.WithValue(ctx, contextHasAuth, nil) |
| } |
|
|
| return ctx |
| } |
|
|
| |
| |
| func SudoContext(ctx context.Context, f func(context.Context)) { |
| name := "Sudo" |
| cname := SystemComponentName(ctx) |
| if cname != "" { |
| name += "[" + cname + "]" |
| } |
| sCtx, cancel := context.WithCancel(SystemContext(ctx, name)) |
| defer cancel() |
| f(sCtx) |
| } |
|
|
| |
| func UserID(ctx context.Context) string { |
| uid, _ := ctx.Value(contextKeyUserID).(string) |
| return uid |
| } |
|
|
| |
| func UserNullUUID(ctx context.Context) uuid.NullUUID { |
| if id, err := uuid.Parse(UserID(ctx)); err == nil { |
| return uuid.NullUUID{UUID: id, Valid: true} |
| } |
|
|
| return uuid.NullUUID{} |
| } |
|
|
| |
| func ServiceNullUUID(ctx context.Context) uuid.NullUUID { |
| if id, err := uuid.Parse(ServiceID(ctx)); err == nil { |
| return uuid.NullUUID{UUID: id, Valid: true} |
| } |
|
|
| return uuid.NullUUID{} |
| } |
|
|
| |
| func SystemComponentName(ctx context.Context) string { |
| name, _ := ctx.Value(contextKeySystem).(string) |
| return name |
| } |
|
|
| |
| func ServiceID(ctx context.Context) string { |
| sid, _ := ctx.Value(contextKeyServiceID).(string) |
| return sid |
| } |
|
|
| |
| func TeamID(ctx context.Context) string { |
| sid, _ := ctx.Value(contextKeyTeamID).(string) |
| return sid |
| } |
|
|