login.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. f := &api.LoginForm{}
  32. err := c.ShouldBindJSON(f)
  33. //fmt.Println(f)
  34. if err != nil {
  35. global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "ParamsError", c.RemoteIP(), c.ClientIP()))
  36. response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
  37. return
  38. }
  39. errList := global.Validator.ValidStruct(c, f)
  40. if len(errList) > 0 {
  41. global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "ParamsError", c.RemoteIP(), c.ClientIP()))
  42. response.Error(c, errList[0])
  43. return
  44. }
  45. u := service.AllService.UserService.InfoByUsernamePassword(f.Username, f.Password)
  46. if u.Id == 0 {
  47. global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "UsernameOrPasswordError", c.RemoteIP(), c.ClientIP()))
  48. response.Error(c, response.TranslateMsg(c, "UsernameOrPasswordError"))
  49. return
  50. }
  51. if !service.AllService.UserService.CheckUserEnable(u) {
  52. response.Error(c, response.TranslateMsg(c, "UserDisabled"))
  53. return
  54. }
  55. //根据refer判断是webclient还是app
  56. ref := c.GetHeader("referer")
  57. if ref != "" {
  58. f.DeviceInfo.Type = model.LoginLogClientWeb
  59. }
  60. ut := service.AllService.UserService.Login(u, &model.LoginLog{
  61. UserId: u.Id,
  62. Client: f.DeviceInfo.Type,
  63. DeviceId: f.Id,
  64. Uuid: f.Uuid,
  65. Ip: c.ClientIP(),
  66. Type: model.LoginLogTypeAccount,
  67. Platform: f.DeviceInfo.Os,
  68. })
  69. c.JSON(http.StatusOK, apiResp.LoginRes{
  70. AccessToken: ut.Token,
  71. Type: "access_token",
  72. User: *(&apiResp.UserPayload{}).FromUser(u),
  73. })
  74. }
  75. // LoginOptions
  76. // @Tags 登录
  77. // @Summary 登录选项
  78. // @Description 登录选项
  79. // @Accept json
  80. // @Produce json
  81. // @Success 200 {object} []string
  82. // @Failure 500 {object} response.ErrorResponse
  83. // @Router /login-options [get]
  84. func (l *Login) LoginOptions(c *gin.Context) {
  85. ops := service.AllService.OauthService.GetOauthProviders()
  86. if global.Config.App.WebSso {
  87. ops = append(ops, model.OauthTypeWebauth)
  88. }
  89. var oidcItems []map[string]string
  90. for _, v := range ops {
  91. oidcItems = append(oidcItems, map[string]string{"name": v})
  92. }
  93. common, err := json.Marshal(oidcItems)
  94. if err != nil {
  95. response.Error(c, response.TranslateMsg(c, "SystemError")+err.Error())
  96. return
  97. }
  98. var res []string
  99. res = append(res, "common-oidc/"+string(common))
  100. for _, v := range ops {
  101. res = append(res, "oidc/"+v)
  102. }
  103. c.JSON(http.StatusOK, res)
  104. }
  105. // Logout
  106. // @Tags 登录
  107. // @Summary 登出
  108. // @Description 登出
  109. // @Accept json
  110. // @Produce json
  111. // @Success 200 {string} string
  112. // @Failure 500 {object} response.ErrorResponse
  113. // @Router /logout [post]
  114. func (l *Login) Logout(c *gin.Context) {
  115. u := service.AllService.UserService.CurUser(c)
  116. token, ok := c.Get("token")
  117. if ok {
  118. service.AllService.UserService.Logout(u, token.(string))
  119. }
  120. c.JSON(http.StatusOK, nil)
  121. }