面包店
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

svc_auth.go 949 B

9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package svc
  2. import (
  3. "applet/app/bigData/lib/auth"
  4. "applet/app/bigData/md"
  5. "errors"
  6. "github.com/gin-gonic/gin"
  7. "strings"
  8. )
  9. func GetUser(c *gin.Context) *md.User {
  10. user, _ := c.Get("user")
  11. if user == nil {
  12. return &md.User{
  13. Account: "",
  14. Password: "",
  15. LoginKind: "",
  16. }
  17. }
  18. return user.(*md.User)
  19. }
  20. func CheckUser(c *gin.Context) (*md.User, error) {
  21. token := c.GetHeader("Authorization")
  22. if token == "" {
  23. return nil, errors.New("token not exist")
  24. }
  25. // 按空格分割
  26. parts := strings.SplitN(token, " ", 2)
  27. if !(len(parts) == 2 && parts[0] == "Bearer") {
  28. return nil, errors.New("token format error")
  29. }
  30. // parts[1]是获取到的tokenString,我们使用之前定义好的解析JWT的函数来解析它
  31. mc, err := auth.ParseToken(parts[1])
  32. if err != nil {
  33. return nil, err
  34. }
  35. // 获取user
  36. user := &md.User{
  37. Account: mc.Account,
  38. Password: mc.Password,
  39. LoginKind: mc.LoginKind,
  40. }
  41. return user, nil
  42. }