auth.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. package xiangyu
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_bridge/global"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "strings"
  10. )
  11. // ErrResp
  12. // @Description: 错误信息返回
  13. type ErrResp struct {
  14. ErrCode string `json:"errcode" description:"失败编码 1001:缺少参数client_id, 2001:缺少参数access_token, 2006:缺少参数uid, 2002:参数access _token,不正确或过期, 1005:参数client_id非法"`
  15. Msg string `json:"msg" description:"失败信息"`
  16. }
  17. // GetTokenResp
  18. // @Description: 获取token返回
  19. type GetTokenResp struct {
  20. ErrResp
  21. AccessToken string `json:"access_token"`
  22. RefreshToken string `json:"refresh_token"`
  23. Uid string `json:"uid"`
  24. ExpiresIn int `json:"expires_in"`
  25. }
  26. // GetToken
  27. // @Description: 获取token信息
  28. // @author: Roc
  29. // @datetime 2024-01-23 15:40:56
  30. // @param code string
  31. // @return resp *GetTokenResp
  32. // @return err error
  33. func GetToken(code string) (resp *GetTokenResp, err error) {
  34. urlPath := fmt.Sprintf(`/idp/oauth2/getToken?client_id=%s&grant_type=authorization_code&code=%s&client_secret=%s`, global.CONFIG.Xiangyu.SystemCode, code, global.CONFIG.Xiangyu.UserKey)
  35. if err != nil {
  36. return
  37. }
  38. result, err := HttpPostAuth(urlPath, ``, "application/json")
  39. if err != nil {
  40. return
  41. }
  42. // 解析响应结果
  43. err = json.Unmarshal(result, &resp)
  44. if err != nil {
  45. return
  46. }
  47. if resp.AccessToken == "" {
  48. err = errors.New(fmt.Sprintf("响应代码:%s,错误信息:%s", resp.ErrCode, resp.Msg))
  49. return
  50. }
  51. return
  52. }
  53. // UserInfoResp
  54. // @Description: 用户信息
  55. type UserInfoResp struct {
  56. ErrResp
  57. Country string `json:"country"`
  58. SorgId string `json:"sorgId"`
  59. UpdateDate string `json:"updateDate"`
  60. Mail string `json:"mail"`
  61. Gender string `json:"gender"`
  62. LoginType string `json:"loginType"`
  63. Nation string `json:"nation"`
  64. DisplayName string `json:"displayName" description:"姓名"`
  65. Uid string `json:"uid" description:"唯一标识"`
  66. LoginName string `json:"loginName" description:"登录账号"`
  67. //PositionNumber string `json:"positionNumber"`
  68. //Title string `json:"title"`
  69. //LoginInterceptFlagTwo int `json:"loginInterceptFlagTwo"`
  70. //LoginInterceptFlagFour int `json:"loginInterceptFlagFour"`
  71. //EmployeeNumber string `json:"employeeNumber"`
  72. //ChangePwdAt string `json:"changePwdAt"`
  73. ////SpRoleList []interface{} `json:"spRoleList"`
  74. //PwdPolicy string `json:"pwdPolicy"`
  75. //LoginInterceptFlagFive int `json:"loginInterceptFlagFive"`
  76. //LoginInterceptFlagThree int `json:"loginInterceptFlagThree"`
  77. //IdentityNumber string `json:"identityNumber"`
  78. //IdentityType string `json:"identityType"`
  79. //SecAccValid int `json:"secAccValid"`
  80. //PinyinShortName string `json:"pinyinShortName"`
  81. //OrgNumber string `json:"orgNumber"`
  82. //WechatNo string `json:"wechatNo"`
  83. //OrgNamePath string `json:"orgNamePath"`
  84. //PasswordModifyRequired int `json:"passwordModifyRequired"`
  85. //BirthDay string `json:"birthDay"`
  86. //GivenName string `json:"givenName"`
  87. //Mobile string `json:"mobile"`
  88. //LoginInterceptFlagOne int `json:"loginInterceptFlagOne"`
  89. //CertSn interface{} `json:"certSn"`
  90. //EmployeeType interface{} `json:"employeeType"`
  91. //OrgCodePath interface{} `json:"orgCodePath"`
  92. //OtpKey interface{} `json:"otpKey"`
  93. //PositionStatus interface{} `json:"positionStatus"`
  94. //DepartmentNumber interface{} `json:"departmentNumber"`
  95. //CertDn interface{} `json:"certDn"`
  96. //SpNameList []string `json:"spNameList"`
  97. //IsPassRemind int `json:"isPassRemind"`
  98. }
  99. // GetUserInfo
  100. // @Description: 获取用户信息
  101. // @author: Roc
  102. // @datetime 2024-01-23 15:49:38
  103. // @param token string
  104. // @return resp *UserInfoResp
  105. // @return err error
  106. func GetUserInfo(token string) (resp *UserInfoResp, err error) {
  107. urlPath := fmt.Sprintf(`/idp/oauth2/getUserInfo?client_id=%s&access_token=%s`, global.CONFIG.Xiangyu.SystemCode, token)
  108. if err != nil {
  109. return
  110. }
  111. result, err := HttpGetAuth(urlPath)
  112. if err != nil {
  113. return
  114. }
  115. // 解析响应结果
  116. err = json.Unmarshal(result, &resp)
  117. if err != nil {
  118. return
  119. }
  120. if resp.ErrCode != "" {
  121. err = errors.New(fmt.Sprintf("响应代码:%s,错误信息:%s", resp.ErrCode, resp.Msg))
  122. return
  123. }
  124. return
  125. }
  126. // CheckTokenResp
  127. // @Description: 校验token是否有效的返回
  128. type CheckTokenResp struct {
  129. ErrResp
  130. Result bool `json:"result" description:"校验结果,true 有效"`
  131. }
  132. // CheckToken
  133. // @Description: 校验token是否有效
  134. // @author: Roc
  135. // @datetime 2024-01-23 15:54:16
  136. // @param token string
  137. // @return isOk bool
  138. // @return err error
  139. func CheckToken(token string) (isOk bool, err error) {
  140. urlPath := fmt.Sprintf(`/idp/oauth2/checkTokenValid?&access_token=%s`, token)
  141. if err != nil {
  142. return
  143. }
  144. result, err := HttpGetAuth(urlPath)
  145. if err != nil {
  146. return
  147. }
  148. var resp *CheckTokenResp
  149. // 解析响应结果
  150. err = json.Unmarshal(result, &resp)
  151. if err != nil {
  152. return
  153. }
  154. if resp.ErrCode != "" {
  155. err = errors.New(fmt.Sprintf("响应代码:%s,错误信息:%s", resp.ErrCode, resp.Msg))
  156. return
  157. }
  158. if resp.Result {
  159. isOk = true
  160. }
  161. return
  162. }
  163. // GetTokenInfo
  164. // @Description: 查询授权信息
  165. // @author: Roc
  166. // @datetime 2024-01-23 15:57:04
  167. // @param token string
  168. // @return resp *GetTokenResp
  169. // @return err error
  170. func GetTokenInfo(token string) (resp *GetTokenResp, err error) {
  171. urlPath := fmt.Sprintf(`/idp/oauth2/getTokenInfo?&access_token=%s`, token)
  172. if err != nil {
  173. return
  174. }
  175. result, err := HttpGetAuth(urlPath)
  176. if err != nil {
  177. return
  178. }
  179. // 解析响应结果
  180. err = json.Unmarshal(result, &resp)
  181. if err != nil {
  182. return
  183. }
  184. if resp.ErrCode != "" {
  185. err = errors.New(fmt.Sprintf("响应代码:%s,错误信息:%s", resp.ErrCode, resp.Msg))
  186. return
  187. }
  188. return
  189. }
  190. // RefreshToken
  191. // @Description: 刷新token
  192. // @author: Roc
  193. // @datetime 2024-01-23 16:02:03
  194. // @param refreshToken string
  195. // @return resp *GetTokenResp
  196. // @return err error
  197. func RefreshToken(refreshToken string) (resp *GetTokenResp, err error) {
  198. urlPath := fmt.Sprintf(`/idp/oauth2/refreshToken?client_id=%s&grant_type=refresh_token&client_secret=%s&refresh_token=%s`, global.CONFIG.Xiangyu.SystemCode, global.CONFIG.Xiangyu.UserKey, refreshToken)
  199. if err != nil {
  200. return
  201. }
  202. result, err := HttpPostAuth(urlPath, ``, "application/json")
  203. if err != nil {
  204. return
  205. }
  206. // 解析响应结果
  207. err = json.Unmarshal(result, &resp)
  208. if err != nil {
  209. return
  210. }
  211. if resp.ErrCode != "" {
  212. err = errors.New(fmt.Sprintf("响应代码:%s,错误信息:%s", resp.ErrCode, resp.Msg))
  213. return
  214. }
  215. return
  216. }
  217. // RevokeToken
  218. // @Description: token销毁
  219. // @author: Roc
  220. // @datetime 2024-01-23 16:06:52
  221. // @param token string
  222. // @return isOk bool
  223. // @return err error
  224. func RevokeToken(token string) (isOk bool, err error) {
  225. urlPath := fmt.Sprintf(`/idp/oauth2/revokeToken?&access_token=%s`, token)
  226. if err != nil {
  227. return
  228. }
  229. result, err := HttpGetAuth(urlPath)
  230. if err != nil {
  231. return
  232. }
  233. var resp *CheckTokenResp
  234. // 解析响应结果
  235. err = json.Unmarshal(result, &resp)
  236. if err != nil {
  237. return
  238. }
  239. if resp.ErrCode != "" {
  240. err = errors.New(fmt.Sprintf("响应代码:%s,错误信息:%s", resp.ErrCode, resp.Msg))
  241. return
  242. }
  243. if resp.Result {
  244. isOk = true
  245. }
  246. return
  247. }
  248. // HttpPostAuth
  249. // @Description: post请求
  250. // @author: Roc
  251. // @datetime 2024-01-23 16:20:10
  252. // @param urlPath string
  253. // @param postData string
  254. // @param params ...string
  255. // @return []byte
  256. // @return error
  257. func HttpPostAuth(urlPath, postData string, params ...string) ([]byte, error) {
  258. if global.CONFIG.Xiangyu.UserAuthHost == `` {
  259. return nil, errors.New("统一用户同步接口地址为空")
  260. }
  261. // 请求地址
  262. postUrl := global.CONFIG.Xiangyu.UserAuthHost + urlPath
  263. body := io.NopCloser(strings.NewReader(postData))
  264. client := &http.Client{}
  265. req, err := http.NewRequest("POST", postUrl, body)
  266. if err != nil {
  267. return nil, err
  268. }
  269. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  270. if len(params) > 0 && params[0] != "" {
  271. contentType = params[0]
  272. }
  273. req.Header.Set("content-Type", contentType)
  274. resp, err := client.Do(req)
  275. if err != nil {
  276. return nil, err
  277. }
  278. defer resp.Body.Close()
  279. result, err := io.ReadAll(resp.Body)
  280. if err != nil {
  281. return nil, err
  282. }
  283. // 日志记录
  284. global.FILE_LOG.Debug("统一认证:地址:" + postUrl + ";\n请求参数:" + postData + ";\n返回参数:" + string(result))
  285. // 解析返回参数,判断是否是json
  286. if !json.Valid(result) {
  287. err = errors.New("返回参数不是json格式")
  288. }
  289. return result, err
  290. }
  291. // HttpGetAuth
  292. // @Description: get请求
  293. // @author: Roc
  294. // @datetime 2024-01-23 16:20:16
  295. // @param urlPath string
  296. // @return []byte
  297. // @return error
  298. func HttpGetAuth(urlPath string) ([]byte, error) {
  299. if global.CONFIG.Xiangyu.UserAuthHost == `` {
  300. return nil, errors.New("统一用户同步接口地址为空")
  301. }
  302. // 请求地址
  303. getUrl := global.CONFIG.Xiangyu.UserAuthHost + urlPath
  304. client := &http.Client{}
  305. req, err := http.NewRequest("GET", getUrl, nil)
  306. if err != nil {
  307. return nil, err
  308. }
  309. resp, err := client.Do(req)
  310. if err != nil {
  311. return nil, err
  312. }
  313. defer resp.Body.Close()
  314. result, err := io.ReadAll(resp.Body)
  315. if err != nil {
  316. return nil, err
  317. }
  318. // 日志记录
  319. global.FILE_LOG.Debug("统一认证:地址:" + getUrl + ";\n返回参数:" + string(result))
  320. // 解析返回参数,判断是否是json
  321. if !json.Valid(result) {
  322. err = errors.New("返回参数不是json格式")
  323. }
  324. return result, err
  325. }