server auth
This commit is contained in:
23
apps/server/internal/httpserver/auth_context.go
Normal file
23
apps/server/internal/httpserver/auth_context.go
Normal file
@@ -0,0 +1,23 @@
|
||||
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())
|
||||
}
|
||||
Reference in New Issue
Block a user