login.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package api
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "github.com/lejianwen/rustdesk-api/v2/global"
  7. "github.com/lejianwen/rustdesk-api/v2/http/request/api"
  8. "github.com/lejianwen/rustdesk-api/v2/http/response"
  9. apiResp "github.com/lejianwen/rustdesk-api/v2/http/response/api"
  10. "github.com/lejianwen/rustdesk-api/v2/model"
  11. "github.com/lejianwen/rustdesk-api/v2/service"
  12. "net/http"
  13. )
  14. type Login struct {
  15. }
  16. // Login 登录
  17. // @Tags 登录
  18. // @Summary 登录
  19. // @Description 登录
  20. // @Accept json
  21. // @Produce json
  22. // @Param body body api.LoginForm true "登录表单"
  23. // @Success 200 {object} apiResp.LoginRes
  24. // @Failure 500 {object} response.ErrorResponse
  25. // @Router /login [post]
  26. func (l *Login) Login(c *gin.Context) {
  27. if global.Config.App.DisablePwdLogin {
  28. response.Error(c, response.TranslateMsg(c, "PwdLoginDisabled"))
  29. return
  30. }
  31. // 检查登录限制
  32. loginLimiter := global.LoginLimiter
  33. clientIp := c.ClientIP()
  34. f := &api.LoginForm{}
  35. err := c.ShouldBindJSON(f)
  36. //fmt.Println(f)
  37. if err != nil {
  38. loginLimiter.RecordFailedAttempt(clientIp)
  39. global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "ParamsError", c.RemoteIP(), c.ClientIP()))
  40. response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
  41. return
  42. }
  43. errList := global.Validator.ValidStruct(c, f)
  44. if len(errList) > 0 {
  45. loginLimiter.RecordFailedAttempt(clientIp)
  46. global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "ParamsError", c.RemoteIP(), c.ClientIP()))
  47. response.Error(c, errList[0])
  48. return
  49. }
  50. u := service.AllService.UserService.InfoByUsernamePassword(f.Username, f.Password)
  51. if u.Id == 0 {
  52. loginLimiter.RecordFailedAttempt(clientIp)
  53. global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "UsernameOrPasswordError", c.RemoteIP(), c.ClientIP()))
  54. response.Error(c, response.TranslateMsg(c, "UsernameOrPasswordError"))
  55. return
  56. }
  57. if !service.AllService.UserService.CheckUserEnable(u) {
  58. response.Error(c, response.TranslateMsg(c, "UserDisabled"))
  59. return
  60. }
  61. //根据refer判断是webclient还是app
  62. ref := c.GetHeader("referer")
  63. if ref != "" {
  64. f.DeviceInfo.Type = model.LoginLogClientWeb
  65. }
  66. ut := service.AllService.UserService.Login(u, &model.LoginLog{
  67. UserId: u.Id,
  68. Client: f.DeviceInfo.Type,
  69. DeviceId: f.Id,
  70. Uuid: f.Uuid,
  71. Ip: c.ClientIP(),
  72. Type: model.LoginLogTypeAccount,
  73. Platform: f.DeviceInfo.Os,
  74. })
  75. c.JSON(http.StatusOK, apiResp.LoginRes{
  76. AccessToken: ut.Token,
  77. Type: "access_token",
  78. User: *(&apiResp.UserPayload{}).FromUser(u),
  79. })
  80. }
  81. // LoginOptions
  82. // @Tags 登录
  83. // @Summary 登录选项
  84. // @Description 登录选项
  85. // @Accept json
  86. // @Produce json
  87. // @Success 200 {object} []string
  88. // @Failure 500 {object} response.ErrorResponse
  89. // @Router /login-options [get]
  90. func (l *Login) LoginOptions(c *gin.Context) {
  91. ops := service.AllService.OauthService.GetOauthProviders()
  92. if global.Config.App.WebSso {
  93. ops = append(ops, model.OauthTypeWebauth)
  94. }
  95. var oidcItems []map[string]string
  96. for _, v := range ops {
  97. oidcItems = append(oidcItems, map[string]string{"name": v})
  98. }
  99. common, err := json.Marshal(oidcItems)
  100. if err != nil {
  101. response.Error(c, response.TranslateMsg(c, "SystemError")+err.Error())
  102. return
  103. }
  104. var res []string
  105. res = append(res, "common-oidc/"+string(common))
  106. for _, v := range ops {
  107. res = append(res, "oidc/"+v)
  108. }
  109. c.JSON(http.StatusOK, res)
  110. }
  111. // Logout
  112. // @Tags 登录
  113. // @Summary 登出
  114. // @Description 登出
  115. // @Accept json
  116. // @Produce json
  117. // @Success 200 {string} string
  118. // @Failure 500 {object} response.ErrorResponse
  119. // @Router /logout [post]
  120. func (l *Login) Logout(c *gin.Context) {
  121. u := service.AllService.UserService.CurUser(c)
  122. token, ok := c.Get("token")
  123. if ok {
  124. service.AllService.UserService.Logout(u, token.(string))
  125. }
  126. c.JSON(http.StatusOK, nil)
  127. }