login.go 3.4 KB

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