28 lines
575 B
Go
28 lines
575 B
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
)
|
|
|
|
type Principal struct {
|
|
Subject string
|
|
Email string
|
|
Role string
|
|
}
|
|
|
|
type principalContextKey struct{}
|
|
|
|
func PrincipalFromContext(ctx context.Context) (Principal, bool) {
|
|
principal, ok := ctx.Value(principalContextKey{}).(Principal)
|
|
return principal, ok
|
|
}
|
|
|
|
func ContextWithPrincipal(ctx context.Context, principal Principal) context.Context {
|
|
return context.WithValue(ctx, principalContextKey{}, principal)
|
|
}
|
|
|
|
func PrincipalFromRequest(r *http.Request) (Principal, bool) {
|
|
return PrincipalFromContext(r.Context())
|
|
}
|