ouath.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package api
  2. import (
  3. "Gwen/global"
  4. "Gwen/http/request/api"
  5. "Gwen/http/response"
  6. apiResp "Gwen/http/response/api"
  7. "Gwen/model"
  8. "Gwen/service"
  9. "github.com/gin-gonic/gin"
  10. "net/http"
  11. "strconv"
  12. )
  13. type Oauth struct {
  14. }
  15. // OidcAuth
  16. // @Tags Oauth
  17. // @Summary OidcAuth
  18. // @Description OidcAuth
  19. // @Accept json
  20. // @Produce json
  21. // @Success 200 {object} apiResp.LoginRes
  22. // @Failure 500 {object} response.ErrorResponse
  23. // @Router /oidc/auth [post]
  24. func (o *Oauth) OidcAuth(c *gin.Context) {
  25. f := &api.OidcAuthRequest{}
  26. err := c.ShouldBindJSON(&f)
  27. if err != nil {
  28. response.Error(c, "参数错误")
  29. return
  30. }
  31. if f.Op != model.OauthTypeWebauth && f.Op != model.OauthTypeGoogle && f.Op != model.OauthTypeGithub {
  32. response.Error(c, "参数错误")
  33. return
  34. }
  35. err, code, url := service.AllService.OauthService.BeginAuth(f.Op)
  36. if err != nil {
  37. response.Error(c, err.Error())
  38. return
  39. }
  40. service.AllService.OauthService.SetOauthCache(code, &service.OauthCacheItem{
  41. Action: service.OauthActionTypeLogin,
  42. Id: f.Id,
  43. Op: f.Op,
  44. Uuid: f.Uuid,
  45. DeviceName: f.DeviceInfo.Name,
  46. DeviceOs: f.DeviceInfo.Os,
  47. DeviceType: f.DeviceInfo.Type,
  48. }, 5*60)
  49. //fmt.Println("code url", code, url)
  50. c.JSON(http.StatusOK, gin.H{
  51. "code": code,
  52. "url": url,
  53. })
  54. }
  55. // OidcAuthQuery
  56. // @Tags Oauth
  57. // @Summary OidcAuthQuery
  58. // @Description OidcAuthQuery
  59. // @Accept json
  60. // @Produce json
  61. // @Success 200 {object} apiResp.LoginRes
  62. // @Failure 500 {object} response.ErrorResponse
  63. // @Router /oidc/auth-query [get]
  64. func (o *Oauth) OidcAuthQuery(c *gin.Context) {
  65. q := &api.OidcAuthQuery{}
  66. err := c.ShouldBindQuery(q)
  67. if err != nil {
  68. response.Error(c, "参数错误")
  69. return
  70. }
  71. v := service.AllService.OauthService.GetOauthCache(q.Code)
  72. if v == nil {
  73. response.Error(c, "授权已过期,请重新授权")
  74. return
  75. }
  76. if v.UserId == 0 {
  77. //正在授权
  78. c.JSON(http.StatusOK, gin.H{})
  79. return
  80. }
  81. u := service.AllService.UserService.InfoById(v.UserId)
  82. //fmt.Println("auth success u", u)
  83. if u.Id > 0 {
  84. service.AllService.OauthService.DeleteOauthCache(q.Code)
  85. ut := service.AllService.UserService.Login(u, &model.LoginLog{
  86. UserId: u.Id,
  87. Client: v.DeviceType,
  88. Uuid: v.Uuid,
  89. Ip: c.ClientIP(),
  90. Type: model.LoginLogTypeOauth,
  91. Platform: v.DeviceOs,
  92. })
  93. c.JSON(http.StatusOK, apiResp.LoginRes{
  94. AccessToken: ut.Token,
  95. Type: "access_token",
  96. User: *(&apiResp.UserPayload{}).FromUser(u),
  97. })
  98. return
  99. }
  100. response.Error(c, "用户不存在")
  101. }
  102. // OauthCallback 回调
  103. // @Tags Oauth
  104. // @Summary OauthCallback
  105. // @Description OauthCallback
  106. // @Accept json
  107. // @Produce json
  108. // @Success 200 {object} apiResp.LoginRes
  109. // @Failure 500 {object} response.ErrorResponse
  110. // @Router /oauth/callback [get]
  111. func (o *Oauth) OauthCallback(c *gin.Context) {
  112. state := c.Query("state")
  113. if state == "" {
  114. c.String(http.StatusInternalServerError, "state为空")
  115. return
  116. }
  117. cacheKey := state
  118. //从缓存中获取
  119. v := service.AllService.OauthService.GetOauthCache(cacheKey)
  120. if v == nil {
  121. c.String(http.StatusInternalServerError, "授权已过期,请重新授权")
  122. return
  123. }
  124. ty := v.Op
  125. ac := v.Action
  126. //fmt.Println("ty ac ", ty, ac)
  127. if ty == model.OauthTypeGithub {
  128. code := c.Query("code")
  129. err, userData := service.AllService.OauthService.GithubCallback(code)
  130. if err != nil {
  131. c.String(http.StatusInternalServerError, "授权失败:"+err.Error())
  132. return
  133. }
  134. if ac == service.OauthActionTypeBind {
  135. //fmt.Println("bind", ty, userData)
  136. utr := service.AllService.OauthService.UserThirdInfo(ty, strconv.Itoa(userData.Id))
  137. if utr.UserId > 0 {
  138. c.String(http.StatusInternalServerError, "已经绑定其他账号")
  139. return
  140. }
  141. //绑定
  142. u := service.AllService.UserService.InfoById(v.UserId)
  143. if u == nil {
  144. c.String(http.StatusInternalServerError, "用户不存在")
  145. return
  146. }
  147. //绑定github
  148. err = service.AllService.OauthService.BindGithubUser(strconv.Itoa(userData.Id), userData.Login, v.UserId)
  149. if err != nil {
  150. c.String(http.StatusInternalServerError, "绑定失败")
  151. return
  152. }
  153. c.String(http.StatusOK, "绑定成功")
  154. return
  155. }
  156. //登录
  157. if ac == service.OauthActionTypeLogin {
  158. if v.UserId != 0 {
  159. c.String(http.StatusInternalServerError, "授权已经成功")
  160. return
  161. }
  162. u := service.AllService.UserService.InfoByGithubId(strconv.Itoa(userData.Id))
  163. if u == nil {
  164. oa := service.AllService.OauthService.InfoByOp(ty)
  165. if !*oa.AutoRegister {
  166. //c.String(http.StatusInternalServerError, "还未绑定用户,请先绑定")
  167. v.ThirdName = userData.Login
  168. v.ThirdOpenId = strconv.Itoa(userData.Id)
  169. url := global.Config.Rustdesk.ApiServer + "/_admin/#/oauth/bind/" + cacheKey
  170. c.Redirect(http.StatusFound, url)
  171. return
  172. }
  173. //自动注册
  174. u = service.AllService.UserService.RegisterByGithub(userData.Login, int64(userData.Id))
  175. if u.Id == 0 {
  176. c.String(http.StatusInternalServerError, "注册失败")
  177. return
  178. }
  179. }
  180. v.UserId = u.Id
  181. service.AllService.OauthService.SetOauthCache(cacheKey, v, 0)
  182. c.String(http.StatusOK, "授权成功")
  183. return
  184. }
  185. //返回js
  186. c.Header("Content-Type", "text/html; charset=utf-8")
  187. c.String(http.StatusOK, "授权错误")
  188. //up := &apiResp.UserPayload{}
  189. //c.JSON(http.StatusOK, apiResp.LoginRes{
  190. // AccessToken: ut.Token,
  191. // Type: "access_token",
  192. // User: *up.FromUser(u),
  193. //})
  194. }
  195. }
  196. // WebOauthLogin
  197. // @Tags Oauth
  198. // @Summary WebOauthLogin
  199. // @Description WebOauthLogin
  200. // @Accept json
  201. // @Produce json
  202. // @Success 200 {string} string
  203. // @Failure 500 {string} string
  204. // @Router /oauth/login [get]
  205. func (o *Oauth) WebOauthLogin(c *gin.Context) {
  206. }