feat: add backend jwt middleware
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
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 withPrincipal(ctx context.Context, principal Principal) context.Context {
|
||||
return context.WithValue(ctx, principalContextKey{}, principal)
|
||||
}
|
||||
|
||||
func PrincipalFromRequest(r *http.Request) (Principal, bool) {
|
||||
return PrincipalFromContext(r.Context())
|
||||
}
|
||||
Reference in New Issue
Block a user