auth.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package gn
  2. import (
  3. "eta_gn/eta_bridge/models/eta"
  4. "eta_gn/eta_bridge/models/response"
  5. "eta_gn/eta_bridge/services"
  6. "eta_gn/eta_bridge/services/gn"
  7. "fmt"
  8. "time"
  9. )
  10. // LoginEta
  11. // @Description: 获取eta的session
  12. // @author: Roc
  13. // @datetime 2024-01-23 17:44:15
  14. // @param code string
  15. // @return resp response.LoginResp
  16. // @return err error
  17. // @return errMsg string
  18. // func LoginEta(code string) (resp response.GnUserResp, err error, errMsg string) {
  19. func LoginEta(code string) (resp response.LoginResp, err error, errMsg string) {
  20. tokenResp, e := gn.GetToken(code)
  21. if e != nil {
  22. err = fmt.Errorf("获取AccessToken失败, %v", e)
  23. return
  24. }
  25. accessToken := tokenResp.AccessToken
  26. // 获取用户信息
  27. res, e := gn.GetUserInfo(code, accessToken)
  28. if e != nil {
  29. err = fmt.Errorf("获取用户基础信息失败, Err: %v, Code: %s, Token: %s", e, code, accessToken)
  30. return
  31. }
  32. //resp.Sub = res.Sub
  33. //resp.PreferredUsername = res.PreferredUsername
  34. //resp.Name = res.Name
  35. //resp.FamilyName = res.FamilyName
  36. //resp.GivenName = res.GivenName
  37. // 获取eta用户信息
  38. adminInfo, e := eta.GetSysUserByAdminName(res.PreferredUsername)
  39. if e != nil {
  40. err = fmt.Errorf("用户不存在, UserName: %s", res.PreferredUsername)
  41. return
  42. }
  43. // 生成登录session
  44. sessionResp, session, e, _ := services.CreateEtaSession(adminInfo)
  45. if e != nil {
  46. err = fmt.Errorf("登录失败, %v", e)
  47. return
  48. }
  49. resp = sessionResp
  50. // 记录两方session的绑定信息
  51. thirdSession := &eta.SysThirdSession{
  52. SysUserID: uint64(session.SysUserId),
  53. UserName: session.UserName,
  54. AccessToken: session.AccessToken,
  55. ThirdAccessToken: tokenResp.AccessToken,
  56. ThirdExpiredTime: time.Now().Add(time.Duration(tokenResp.ExpiresIn-100) * time.Second),
  57. CreatedTime: time.Now(),
  58. LastUpdatedTime: time.Now(),
  59. }
  60. err = eta.AddSysThirdSession(thirdSession)
  61. return
  62. }
  63. func GetUserInfoByToken(token string) (res *gn.UserBaseInfoResp, err error, errMsg string) {
  64. // 获取用户信息
  65. res, e := gn.GetUserInfoBySci(token)
  66. if e != nil {
  67. err = fmt.Errorf("获取用户基础信息失败, Err: %v, Token: %s", e, token)
  68. return
  69. }
  70. return
  71. }