| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- package api
- import (
- "encoding/json"
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/lejianwen/rustdesk-api/global"
- "github.com/lejianwen/rustdesk-api/http/request/api"
- "github.com/lejianwen/rustdesk-api/http/response"
- apiResp "github.com/lejianwen/rustdesk-api/http/response/api"
- "github.com/lejianwen/rustdesk-api/model"
- "github.com/lejianwen/rustdesk-api/service"
- "net/http"
- )
- type Login struct {
- }
- // Login 登录
- // @Tags 登录
- // @Summary 登录
- // @Description 登录
- // @Accept json
- // @Produce json
- // @Param body body api.LoginForm true "登录表单"
- // @Success 200 {object} apiResp.LoginRes
- // @Failure 500 {object} response.ErrorResponse
- // @Router /login [post]
- func (l *Login) Login(c *gin.Context) {
- f := &api.LoginForm{}
- err := c.ShouldBindJSON(f)
- //fmt.Println(f)
- if err != nil {
- global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "ParamsError", c.RemoteIP(), c.ClientIP()))
- response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
- return
- }
- errList := global.Validator.ValidStruct(c, f)
- if len(errList) > 0 {
- global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "ParamsError", c.RemoteIP(), c.ClientIP()))
- response.Error(c, errList[0])
- return
- }
- u := service.AllService.UserService.InfoByUsernamePassword(f.Username, f.Password)
- if u.Id == 0 {
- global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "UsernameOrPasswordError", c.RemoteIP(), c.ClientIP()))
- response.Error(c, response.TranslateMsg(c, "UsernameOrPasswordError"))
- return
- }
- if !service.AllService.UserService.CheckUserEnable(u) {
- response.Error(c, response.TranslateMsg(c, "UserDisabled"))
- return
- }
- //根据refer判断是webclient还是app
- ref := c.GetHeader("referer")
- if ref != "" {
- f.DeviceInfo.Type = model.LoginLogClientWeb
- }
- ut := service.AllService.UserService.Login(u, &model.LoginLog{
- UserId: u.Id,
- Client: f.DeviceInfo.Type,
- DeviceId: f.Id,
- Uuid: f.Uuid,
- Ip: c.ClientIP(),
- Type: model.LoginLogTypeAccount,
- Platform: f.DeviceInfo.Os,
- })
- c.JSON(http.StatusOK, apiResp.LoginRes{
- AccessToken: ut.Token,
- Type: "access_token",
- User: *(&apiResp.UserPayload{}).FromUser(u),
- })
- }
- // LoginOptions
- // @Tags 登录
- // @Summary 登录选项
- // @Description 登录选项
- // @Accept json
- // @Produce json
- // @Success 200 {object} []string
- // @Failure 500 {object} response.ErrorResponse
- // @Router /login-options [get]
- func (l *Login) LoginOptions(c *gin.Context) {
- ops := service.AllService.OauthService.GetOauthProviders()
- if global.Config.App.WebSso {
- ops = append(ops, model.OauthTypeWebauth)
- }
- var oidcItems []map[string]string
- for _, v := range ops {
- oidcItems = append(oidcItems, map[string]string{"name": v})
- }
- common, err := json.Marshal(oidcItems)
- if err != nil {
- response.Error(c, response.TranslateMsg(c, "SystemError")+err.Error())
- return
- }
- var res []string
- res = append(res, "common-oidc/"+string(common))
- for _, v := range ops {
- res = append(res, "oidc/"+v)
- }
- c.JSON(http.StatusOK, res)
- }
- // Logout
- // @Tags 登录
- // @Summary 登出
- // @Description 登出
- // @Accept json
- // @Produce json
- // @Success 200 {string} string
- // @Failure 500 {object} response.ErrorResponse
- // @Router /logout [post]
- func (l *Login) Logout(c *gin.Context) {
- u := service.AllService.UserService.CurUser(c)
- token, ok := c.Get("token")
- if ok {
- service.AllService.UserService.Logout(u, token.(string))
- }
- c.JSON(http.StatusOK, nil)
- }
|