token.go 747 B

123456789101112131415161718192021222324252627282930313233343536
  1. package main
  2. import (
  3. "sparrow/pkg/models"
  4. "time"
  5. jwt "github.com/dgrijalva/jwt-go"
  6. )
  7. const SignedString = "www.yehaoji.com"
  8. type AppClaims struct {
  9. AppID uint
  10. AppName string
  11. AppKey string
  12. SecretKey string
  13. VendorID string
  14. jwt.StandardClaims
  15. }
  16. // TokenMaker 生成token
  17. func TokenMaker(app *models.Application) string {
  18. claims := AppClaims{
  19. AppID: app.ID,
  20. AppName: app.AppName,
  21. AppKey: app.AppKey,
  22. SecretKey: app.SecretKey,
  23. VendorID: app.VendorID,
  24. }
  25. claims.ExpiresAt = time.Now().Add(time.Hour * 24).Unix()
  26. claims.IssuedAt = time.Now().Unix()
  27. claims.Issuer = "apiprovider"
  28. token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
  29. ser, _ := token.SignedString([]byte(SignedString))
  30. return ser
  31. }