24 lines
556 B
Go
24 lines
556 B
Go
package httpserver
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"github.com/tim/cairnquire/apps/server/internal/auth"
|
|
)
|
|
|
|
type authContextKey struct{}
|
|
|
|
func withPrincipal(ctx context.Context, principal auth.Principal) context.Context {
|
|
return context.WithValue(ctx, authContextKey{}, principal)
|
|
}
|
|
|
|
func principalFromContext(ctx context.Context) (auth.Principal, bool) {
|
|
principal, ok := ctx.Value(authContextKey{}).(auth.Principal)
|
|
return principal, ok
|
|
}
|
|
|
|
func requirePrincipal(r *http.Request) (auth.Principal, bool) {
|
|
return principalFromContext(r.Context())
|
|
}
|