passport.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package dao
  2. import (
  3. "context"
  4. "net/http"
  5. "net/url"
  6. "go-common/app/service/main/identify/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "go-common/library/net/metadata"
  10. )
  11. // AccessCookie .
  12. func (d *Dao) AccessCookie(c context.Context, cookie string) (res *model.IdentifyInfo, err error) {
  13. params := url.Values{}
  14. // new request
  15. req, err := d.client.NewRequest(http.MethodGet, d.cookieURI, metadata.String(c, metadata.RemoteIP), params)
  16. if err != nil {
  17. log.Error("client.NewRequest(GET, %s) error(%v)", req.URL.String(), err)
  18. return
  19. }
  20. req.Header.Set("Cookie", cookie)
  21. var response struct {
  22. Code int `json:"code"`
  23. Data model.IdentifyInfo `json:"data"`
  24. }
  25. if err = d.client.Do(c, req, &response); err != nil {
  26. log.Error("client.Do(%s) error(%v)", req.URL.String(), err)
  27. return
  28. }
  29. if response.Code != ecode.OK.Code() {
  30. log.Warn("identify auth url(%s) code(%d)", req.URL.String(), response.Code)
  31. err = ecode.Int(response.Code)
  32. return
  33. }
  34. res = &response.Data
  35. return
  36. }
  37. // AccessToken .
  38. func (d *Dao) AccessToken(c context.Context, accesskey string) (res *model.IdentifyInfo, err error) {
  39. params := url.Values{}
  40. params.Set("access_key", accesskey)
  41. // new request
  42. req, err := d.client.NewRequest(http.MethodGet, d.tokenURI, metadata.String(c, metadata.RemoteIP), params)
  43. if err != nil {
  44. log.Error("client.NewRequest(GET, %s) error(%v)", req.URL.String(), err)
  45. return
  46. }
  47. var response struct {
  48. Code int `json:"code"`
  49. Data model.IdentifyInfo `json:"data"`
  50. }
  51. if err = d.client.Do(c, req, &response); err != nil {
  52. log.Error("client.Do(%s) error(%v)", req.URL.String(), err)
  53. return
  54. }
  55. if response.Code != 0 {
  56. log.Warn("identify auth url(%s) code(%d)", req.URL.String(), response.Code)
  57. err = ecode.Int(response.Code)
  58. return
  59. }
  60. res = &response.Data
  61. return
  62. }