csauthlogic.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package logic
  2. import (
  3. "context"
  4. "github.com/golang-jwt/jwt/v4"
  5. "github.com/pkg/errors"
  6. "time"
  7. "ylink/comm/jwtkey"
  8. "ylink/comm/result"
  9. "ylink/core/auth/rpc/internal/svc"
  10. "ylink/core/auth/rpc/pb"
  11. "github.com/zeromicro/go-zero/core/logx"
  12. )
  13. type CsAuthLogic struct {
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. logx.Logger
  17. }
  18. func NewCsAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CsAuthLogic {
  19. return &CsAuthLogic{
  20. ctx: ctx,
  21. svcCtx: svcCtx,
  22. Logger: logx.WithContext(ctx),
  23. }
  24. }
  25. func (l *CsAuthLogic) CsAuth(in *pb.CsAuthReq) (*pb.AuthResp, error) {
  26. now := time.Now().Unix()
  27. token, err := l.generateCsToken(now, in.CsId)
  28. if err != nil {
  29. return nil, errors.Wrap(result.NewErrCode(result.TokenGenerateError), "")
  30. }
  31. return &pb.AuthResp{
  32. AccessToken: token,
  33. }, nil
  34. }
  35. //
  36. // generateCsToken
  37. // @Description: 客服token签发
  38. // @receiver l
  39. // @param iat
  40. // @param csId
  41. // @return string
  42. // @return error
  43. //
  44. func (l *CsAuthLogic) generateCsToken(iat int64, csId string) (string, error) {
  45. secret := l.svcCtx.Config.JwtAuth.AccessSecret
  46. expire := l.svcCtx.Config.JwtAuth.AccessExpire
  47. claims := make(jwt.MapClaims)
  48. claims["iat"] = iat
  49. claims["exp"] = iat + expire
  50. claims[jwtkey.CsId] = csId
  51. token := jwt.New(jwt.SigningMethodHS256)
  52. token.Claims = claims
  53. return token.SignedString([]byte(secret))
  54. }