auth.go 540 B

123456789101112131415161718192021222324252627
  1. package biz
  2. import (
  3. "context"
  4. "github.com/go-kratos/kratos/v2/middleware/auth/jwt"
  5. jwt2 "github.com/golang-jwt/jwt/v5"
  6. "github.com/google/uuid"
  7. )
  8. // getIdByContext returns the ID from the context.
  9. func getIdByContext(ctx context.Context) string {
  10. token, ok := jwt.FromContext(ctx)
  11. if ok {
  12. mapClaims := token.(*jwt2.MapClaims)
  13. id := (*mapClaims)["id"].(string)
  14. return id
  15. }
  16. panic("get id from context failed")
  17. }
  18. func newUUID() uuid.UUID {
  19. id, err := uuid.NewV7()
  20. if err != nil {
  21. panic("new uuid failed")
  22. }
  23. return id
  24. }