passport.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "go-common/app/service/main/identify-game/api/grpc/v1"
  8. "go-common/app/service/main/identify-game/model"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. "go-common/library/net/metadata"
  12. )
  13. const (
  14. _createCookieURI = "/intranet/auth/createCookie/byMid"
  15. )
  16. // AccessToken .
  17. func (d *Dao) AccessToken(c context.Context, accesskey, target string) (token *model.AccessInfo, err error) {
  18. params := url.Values{}
  19. params.Set("access_key", accesskey)
  20. var res struct {
  21. Code int `json:"code"`
  22. Token *struct {
  23. Mid string `json:"mid"`
  24. AppID int64 `json:"appid"`
  25. Token string `json:"access_key"`
  26. CreateAt int64 `json:"create_at"`
  27. UserID string `json:"userid"`
  28. Name string `json:"uname"`
  29. Expires string `json:"expires"`
  30. Permission string `json:"permission"`
  31. } `json:"access_info,omitempty"`
  32. Data *model.AccessInfo `json:"data,omitempty"`
  33. }
  34. tokenURL := d.c.Dispatcher.Oauth[target]
  35. if err = d.client.Get(c, tokenURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  36. log.Error("oauth for region %s, url(%s) error(%v)", target, tokenURL+"?"+params.Encode(), err)
  37. return
  38. }
  39. if res.Code != 0 {
  40. err = ecode.Int(res.Code)
  41. log.Error("oauth for region %s, url(%s) error(%v)", target, tokenURL+"?"+params.Encode(), err)
  42. return
  43. }
  44. if res.Token != nil {
  45. t := res.Token
  46. var mid int64
  47. if mid, err = strconv.ParseInt(t.Mid, 10, 64); err != nil {
  48. log.Error("strconv.ParseInt(%s, 10, 64) error(%v)", t.Mid, err)
  49. return
  50. }
  51. var expires int64
  52. if expires, err = strconv.ParseInt(t.Expires, 10, 64); err != nil {
  53. log.Error("strconv.ParseInt(%s, 10, 64) error(%v)", t.Expires, err)
  54. return
  55. }
  56. token = &model.AccessInfo{
  57. Mid: mid,
  58. AppID: t.AppID,
  59. Token: t.Token,
  60. CreateAt: t.CreateAt,
  61. UserID: t.UserID,
  62. Name: t.Name,
  63. Expires: expires,
  64. Permission: t.Permission,
  65. }
  66. } else {
  67. token = res.Data
  68. }
  69. return
  70. }
  71. // RenewToken request passport renewToken .
  72. func (d *Dao) RenewToken(c context.Context, accesskey, target string) (renewToken *model.RenewInfo, err error) {
  73. params := url.Values{}
  74. params.Set("access_key", accesskey)
  75. var res struct {
  76. Code int `json:"code"`
  77. Expires int64 `json:"expires"`
  78. Data struct {
  79. Expires int64 `json:"expires"`
  80. }
  81. }
  82. renewURL := d.c.Dispatcher.RenewToken[target]
  83. if err = d.client.Get(c, renewURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  84. log.Error("renewtoken for region %s, url(%s) error(%v)", target, renewURL+"?"+params.Encode(), err)
  85. return
  86. }
  87. if res.Code != 0 {
  88. err = ecode.Int(res.Code)
  89. log.Error("renewtoken for region %s, url(%s) error(%v)", target, renewURL+"?"+params.Encode(), err)
  90. return
  91. }
  92. expires := res.Expires
  93. if expires == 0 {
  94. expires = res.Data.Expires
  95. }
  96. renewToken = &model.RenewInfo{
  97. Expires: expires,
  98. }
  99. return
  100. }
  101. // GetCookieByMid get cookie by mid
  102. func (d *Dao) GetCookieByMid(c context.Context, mid int64) (cookies *v1.CreateCookieReply, err error) {
  103. params := url.Values{}
  104. params.Set("mid", fmt.Sprintf("%d", mid))
  105. URL := d.c.Passport.Host["auth"] + _createCookieURI
  106. var res struct {
  107. Code int `json:"code"`
  108. Data *v1.CreateCookieReply `json:"data,omitempty"`
  109. }
  110. if err = d.client.Get(c, URL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  111. log.Error("get cookies by mid(%d) from url(%s) error(%v)", mid, URL, err)
  112. return
  113. }
  114. if res.Code != 0 {
  115. err = ecode.Int(res.Code)
  116. log.Error("get cookies error, mid(%d), error(%v)", mid, err)
  117. return
  118. }
  119. cookies = res.Data
  120. return
  121. }