server.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Package grpc server generate by warden_gen
  2. package grpc
  3. import (
  4. "context"
  5. "go-common/app/service/main/passport-auth/api/grpc/v1"
  6. "go-common/app/service/main/passport-auth/service"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "go-common/library/net/metadata"
  10. "go-common/library/net/rpc/warden"
  11. "google.golang.org/grpc"
  12. )
  13. // New Auth warden rpc server
  14. func New(cfg *warden.ServerConfig, s *service.Service) *warden.Server {
  15. w := warden.NewServer(cfg)
  16. v1.RegisterAuthServer(w.Server(), &server{s})
  17. w.Use(func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
  18. if resp, err = handler(ctx, req); err == nil {
  19. log.Infov(ctx,
  20. log.KV("path", info.FullMethod),
  21. log.KV("caller", metadata.String(ctx, metadata.Caller)),
  22. log.KV("args", req), log.KV("retVal", resp))
  23. }
  24. return
  25. })
  26. ws, err := w.Start()
  27. if err != nil {
  28. panic(err)
  29. }
  30. return ws
  31. }
  32. type server struct {
  33. svr *service.Service
  34. }
  35. var _ v1.AuthServer = &server{}
  36. var (
  37. emptyCookieReply = &v1.GetCookieInfoReply{
  38. IsLogin: false,
  39. }
  40. emptyTokenReply = &v1.GetTokenInfoReply{
  41. IsLogin: false,
  42. }
  43. )
  44. // CookieInfo verify user info by cookie.
  45. func (s *server) GetCookieInfo(c context.Context, req *v1.GetCookieInfoReq) (*v1.GetCookieInfoReply, error) {
  46. res, err := s.svr.CookieInfo(c, req.GetCookie())
  47. if err != nil {
  48. if err == ecode.NoLogin {
  49. return emptyCookieReply, nil
  50. }
  51. return nil, err
  52. }
  53. return &v1.GetCookieInfoReply{
  54. IsLogin: true,
  55. Mid: res.Mid,
  56. Expires: res.Expires,
  57. Csrf: res.CSRF,
  58. }, nil
  59. }
  60. // TokenInfo verify user info by accesskey.
  61. func (s *server) GetTokenInfo(c context.Context, req *v1.GetTokenInfoReq) (*v1.GetTokenInfoReply, error) {
  62. res, err := s.svr.TokenInfo(c, req.GetToken())
  63. if err != nil {
  64. if err == ecode.NoLogin {
  65. return emptyTokenReply, nil
  66. }
  67. return nil, err
  68. }
  69. return &v1.GetTokenInfoReply{
  70. IsLogin: true,
  71. Mid: res.Mid,
  72. Expires: res.Expires,
  73. Csrf: res.CSRF,
  74. }, nil
  75. }