20 lines
503 B
Go
20 lines
503 B
Go
|
|
package middleware
|
||
|
|
|
||
|
|
import "context"
|
||
|
|
|
||
|
|
// contextKey är en privat typ för att undvika kollisioner
|
||
|
|
type contextKey int
|
||
|
|
|
||
|
|
const claimsKey contextKey = iota
|
||
|
|
|
||
|
|
// FromContext hämtar claims från context
|
||
|
|
func FromContext(ctx context.Context) (*Claims, bool) {
|
||
|
|
claims, ok := ctx.Value(claimsKey).(*Claims)
|
||
|
|
return claims, ok
|
||
|
|
}
|
||
|
|
|
||
|
|
// WithContext lägger till claims i context
|
||
|
|
func WithContext(ctx context.Context, claims *Claims) context.Context {
|
||
|
|
return context.WithValue(ctx, claimsKey, claims)
|
||
|
|
}
|