sso_service.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package sso
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "eta/eta_bridge/global"
  6. "eta/eta_bridge/logic/htfutures"
  7. "eta/eta_bridge/utils"
  8. "google.golang.org/grpc/codes"
  9. "google.golang.org/grpc/metadata"
  10. "google.golang.org/grpc/status"
  11. )
  12. type SSOService struct {
  13. UnimplementedSSOLoginServer
  14. }
  15. var (
  16. source = []string{"weapp"}
  17. )
  18. func (s *SSOService) Login(ctx context.Context, req *LoginRequest) (*LoginResponse, error) {
  19. // 获取AES秘钥
  20. md, ok := metadata.FromIncomingContext(ctx)
  21. if !ok {
  22. return nil, status.Errorf(codes.InvalidArgument, "元数据为空")
  23. }
  24. // 从元数据中提取签名相关的信息encrypted-aes-key
  25. encryptedAESKey, ok := md["key"]
  26. if !ok || len(encryptedAESKey) == 0 {
  27. return nil, status.Errorf(codes.InvalidArgument, "encryptedAESKey不能为空")
  28. }
  29. decryptedAESKey, err := base64Decode(encryptedAESKey[0])
  30. if err != nil {
  31. return nil, status.Errorf(codes.InvalidArgument, "base64解码AES秘钥失败")
  32. }
  33. //加密code
  34. encryptedCode, err := base64Decode(req.Code)
  35. if err != nil {
  36. return nil, status.Errorf(codes.InvalidArgument, "base64解码code失败")
  37. }
  38. if len(encryptedCode) == 0 {
  39. return &LoginResponse{
  40. ErrCode: 10001,
  41. ErrMsg: "登录失败,code不能为空",
  42. Msg: "登录失败",
  43. }, nil
  44. }
  45. privateKey, err := utils.ParsePrivateKeyFromPEM(global.CONFIG.HTFutures.PrivateKeyPemPath)
  46. if err != nil {
  47. return &LoginResponse{
  48. ErrCode: 10002,
  49. ErrMsg: "登录失败,解析私钥失败",
  50. Msg: "登录失败",
  51. }, nil
  52. }
  53. aesKey, err := utils.DecryptWithRSA(privateKey, decryptedAESKey)
  54. if err != nil {
  55. return &LoginResponse{
  56. ErrCode: 10003,
  57. ErrMsg: "登录失败,解密失败",
  58. Msg: "登录失败",
  59. }, nil
  60. }
  61. code, err := utils.DecryptWithAES(aesKey, encryptedCode)
  62. if err != nil {
  63. return &LoginResponse{
  64. ErrCode: 10004,
  65. ErrMsg: "登录失败,解析code失败",
  66. Msg: "登录失败",
  67. }, nil
  68. }
  69. global.FILE_LOG.Info("传入参数code:%v", code)
  70. token, err := htfutures.GetToken(string(code), req.Source)
  71. if err != nil {
  72. return &LoginResponse{
  73. ErrCode: 10005,
  74. ErrMsg: "登录失败,获取token失败",
  75. Msg: "登录失败",
  76. }, nil
  77. }
  78. global.FILE_LOG.Info("返回的token结果集%v", token)
  79. user, err := htfutures.GetUserInfo(token, req.Source)
  80. if err != nil {
  81. return &LoginResponse{
  82. ErrCode: 10006,
  83. ErrMsg: "登录失败,获取token失败",
  84. Msg: "登录失败",
  85. }, nil
  86. }
  87. global.FILE_LOG.Info("返回的userInfo结果集%v", user)
  88. return &LoginResponse{
  89. Msg: "登录成功",
  90. Data: &UserInfo{
  91. Name: user.Name,
  92. UserId: user.UserId,
  93. Username: user.Username,
  94. },
  95. }, nil
  96. }
  97. func base64Encode(data []byte) string {
  98. return base64.StdEncoding.EncodeToString(data)
  99. }
  100. // Base64Decode 对数据进行 Base64 解码
  101. func base64Decode(data string) ([]byte, error) {
  102. return base64.StdEncoding.DecodeString(data)
  103. }