-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
23 lines (18 loc) · 791 Bytes
/
Copy pathcontext.go
File metadata and controls
23 lines (18 loc) · 791 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package authorizer
import "context"
// Principal is the authenticated subject plus its selected tenant. A tenant is
// always explicit to prevent accidental cross-tenant authorization.
type Principal struct {
UserID uint64
TenantID uint64
}
type principalContextKey struct{}
// WithPrincipal stores an authenticated principal in a request context.
func WithPrincipal(ctx context.Context, principal Principal) context.Context {
return context.WithValue(ctx, principalContextKey{}, principal)
}
// PrincipalFromContext returns a principal previously stored with WithPrincipal.
func PrincipalFromContext(ctx context.Context) (Principal, bool) {
principal, ok := ctx.Value(principalContextKey{}).(Principal)
return principal, ok && principal.UserID != 0 && principal.TenantID != 0
}