oauth.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package oauth
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/url"
  6. "github.com/silenceper/wechat/v2/util"
  7. "github.com/silenceper/wechat/v2/work/context"
  8. )
  9. // Oauth auth
  10. type Oauth struct {
  11. *context.Context
  12. }
  13. var (
  14. // oauthTargetURL 企业微信内跳转地址
  15. oauthTargetURL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect"
  16. // oauthTargetURL 企业微信内跳转地址(获取成员的详细信息)
  17. oauthTargetPrivateURL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_privateinfo&agentid=%s&state=STATE#wechat_redirect"
  18. // oauthUserInfoURL 获取用户信息地址
  19. oauthUserInfoURL = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=%s&code=%s"
  20. // oauthQrContentTargetURL 构造独立窗口登录二维码
  21. oauthQrContentTargetURL = "https://open.work.weixin.qq.com/wwopen/sso/qrConnect?appid=%s&agentid=%s&redirect_uri=%s&state=%s"
  22. // getUserInfoURL 获取访问用户身份&获取用户登录身份
  23. getUserInfoURL = "https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token=%s&code=%s"
  24. // getUserDetailURL 获取访问用户敏感信息
  25. getUserDetailURL = "https://qyapi.weixin.qq.com/cgi-bin/auth/getuserdetail?access_token=%s"
  26. )
  27. // NewOauth new init oauth
  28. func NewOauth(ctx *context.Context) *Oauth {
  29. return &Oauth{
  30. ctx,
  31. }
  32. }
  33. // GetTargetURL 获取授权地址
  34. func (ctr *Oauth) GetTargetURL(callbackURL string) string {
  35. // url encode
  36. return fmt.Sprintf(
  37. oauthTargetURL,
  38. ctr.CorpID,
  39. url.QueryEscape(callbackURL),
  40. )
  41. }
  42. // GetTargetPrivateURL 获取个人信息授权地址
  43. func (ctr *Oauth) GetTargetPrivateURL(callbackURL string, agentID string) string {
  44. // url encode
  45. return fmt.Sprintf(
  46. oauthTargetPrivateURL,
  47. ctr.CorpID,
  48. url.QueryEscape(callbackURL),
  49. agentID,
  50. )
  51. }
  52. // GetQrContentTargetURL 构造独立窗口登录二维码
  53. func (ctr *Oauth) GetQrContentTargetURL(callbackURL string) string {
  54. // url encode
  55. return fmt.Sprintf(
  56. oauthQrContentTargetURL,
  57. ctr.CorpID,
  58. ctr.AgentID,
  59. url.QueryEscape(callbackURL),
  60. util.RandomStr(16),
  61. )
  62. }
  63. // ResUserInfo 返回得用户信息
  64. type ResUserInfo struct {
  65. util.CommonError
  66. // 当用户为企业成员时返回
  67. UserID string `json:"UserId"`
  68. DeviceID string `json:"DeviceId"`
  69. // 非企业成员授权时返回
  70. OpenID string `json:"OpenId"`
  71. ExternalUserID string `json:"external_userid"`
  72. }
  73. // UserFromCode 根据code获取用户信息
  74. func (ctr *Oauth) UserFromCode(code string) (result ResUserInfo, err error) {
  75. var accessToken string
  76. if accessToken, err = ctr.GetAccessToken(); err != nil {
  77. return
  78. }
  79. var response []byte
  80. if response, err = util.HTTPGet(fmt.Sprintf(oauthUserInfoURL, accessToken, code)); err != nil {
  81. return
  82. }
  83. err = json.Unmarshal(response, &result)
  84. if result.ErrCode != 0 {
  85. err = fmt.Errorf("GetUserAccessToken error : errcode=%v , errmsg=%v", result.ErrCode, result.ErrMsg)
  86. return
  87. }
  88. return
  89. }
  90. // GetUserInfoResponse 获取访问用户身份&获取用户登录身份响应
  91. type GetUserInfoResponse struct {
  92. util.CommonError
  93. UserID string `json:"userid"`
  94. UserTicket string `json:"user_ticket"`
  95. OpenID string `json:"openid"`
  96. ExternalUserID string `json:"external_userid"`
  97. }
  98. // GetUserInfo 获取访问用户身份&获取用户登录身份
  99. // @see https://developer.work.weixin.qq.com/document/path/90213 获取访问用户身份
  100. // @see https://developer.work.weixin.qq.com/document/path/98176 获取用户登录身份
  101. func (ctr *Oauth) GetUserInfo(code string) (*GetUserInfoResponse, error) {
  102. var (
  103. accessToken string
  104. err error
  105. )
  106. if accessToken, err = ctr.GetAccessToken(); err != nil {
  107. return nil, err
  108. }
  109. var response []byte
  110. if response, err = util.HTTPGet(fmt.Sprintf(getUserInfoURL, accessToken, code)); err != nil {
  111. return nil, err
  112. }
  113. result := &GetUserInfoResponse{}
  114. err = util.DecodeWithError(response, result, "GetUserInfo")
  115. return result, err
  116. }
  117. // GetUserDetailRequest 获取访问用户敏感信息请求
  118. type GetUserDetailRequest struct {
  119. UserTicket string `json:"user_ticket"`
  120. }
  121. // GetUserDetailResponse 获取访问用户敏感信息响应
  122. type GetUserDetailResponse struct {
  123. util.CommonError
  124. UserID string `json:"userid"`
  125. Gender string `json:"gender"`
  126. Avatar string `json:"avatar"`
  127. QrCode string `json:"qr_code"`
  128. Mobile string `json:"mobile"`
  129. Email string `json:"email"`
  130. BizMail string `json:"biz_mail"`
  131. Address string `json:"address"`
  132. }
  133. // GetUserDetail 获取访问用户敏感信息
  134. // @see https://developer.work.weixin.qq.com/document/path/95833
  135. func (ctr *Oauth) GetUserDetail(req *GetUserDetailRequest) (*GetUserDetailResponse, error) {
  136. var (
  137. accessToken string
  138. err error
  139. )
  140. if accessToken, err = ctr.GetAccessToken(); err != nil {
  141. return nil, err
  142. }
  143. var response []byte
  144. if response, err = util.PostJSON(fmt.Sprintf(getUserDetailURL, accessToken), req); err != nil {
  145. return nil, err
  146. }
  147. result := &GetUserDetailResponse{}
  148. err = util.DecodeWithError(response, result, "GetUserDetail")
  149. return result, err
  150. }