面包店
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 897 B

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