31 lines
1.0 KiB
Go
31 lines
1.0 KiB
Go
|
|
package authmw
|
||
|
|
|
||
|
|
import "context"
|
||
|
|
|
||
|
|
type ctxUIDKey struct{}
|
||
|
|
type ctxClaimsKey struct{}
|
||
|
|
|
||
|
|
// SetTokenData stores uid and token claims in context.
|
||
|
|
// Called by provider-specific AuthMiddleware (auth-jwt, auth-firebase) after
|
||
|
|
// token verification. EnrichmentMiddleware reads these values downstream.
|
||
|
|
func SetTokenData(ctx context.Context, uid string, claims map[string]any) context.Context {
|
||
|
|
ctx = context.WithValue(ctx, ctxUIDKey{}, uid)
|
||
|
|
ctx = context.WithValue(ctx, ctxClaimsKey{}, claims)
|
||
|
|
return ctx
|
||
|
|
}
|
||
|
|
|
||
|
|
func getUID(ctx context.Context) (string, bool) {
|
||
|
|
v, ok := ctx.Value(ctxUIDKey{}).(string)
|
||
|
|
return v, ok && v != ""
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetClaims returns the raw token claims stored by SetTokenData.
|
||
|
|
// Returns nil if SetTokenData was not called on this context.
|
||
|
|
// Useful for custom IdentityEnricher implementations and ClaimsPermissionProvider.
|
||
|
|
func GetClaims(ctx context.Context) map[string]any {
|
||
|
|
v, _ := ctx.Value(ctxClaimsKey{}).(map[string]any)
|
||
|
|
return v
|
||
|
|
}
|
||
|
|
|
||
|
|
func getClaims(ctx context.Context) map[string]any { return GetClaims(ctx) }
|