Files
rustdesk-api/http/controller/api/login.go
T

124 lines
3.1 KiB
Go
Raw Normal View History

2024-09-13 15:57:29 +08:00
package api
import (
"Gwen/global"
"Gwen/http/request/api"
"Gwen/http/response"
apiResp "Gwen/http/response/api"
2024-09-19 10:44:49 +08:00
"Gwen/model"
2024-09-13 15:57:29 +08:00
"Gwen/service"
2024-09-19 10:44:49 +08:00
"encoding/json"
2024-10-14 10:43:29 +08:00
"fmt"
2024-09-13 15:57:29 +08:00
"github.com/gin-gonic/gin"
"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)
2024-09-19 10:44:49 +08:00
//fmt.Println(f)
2024-09-13 15:57:29 +08:00
if err != nil {
2024-10-14 10:43:29 +08:00
global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "ParamsError", c.RemoteIP(), c.ClientIP()))
2024-09-25 22:41:57 +08:00
response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
2024-09-13 15:57:29 +08:00
return
}
2024-09-25 22:41:57 +08:00
errList := global.Validator.ValidStruct(c, f)
2024-09-13 15:57:29 +08:00
if len(errList) > 0 {
2024-10-14 10:43:29 +08:00
global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "ParamsError", c.RemoteIP(), c.ClientIP()))
2024-09-13 15:57:29 +08:00
response.Error(c, errList[0])
return
}
u := service.AllService.UserService.InfoByUsernamePassword(f.Username, f.Password)
if u.Id == 0 {
2024-10-14 10:43:29 +08:00
global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "UsernameOrPasswordError", c.RemoteIP(), c.ClientIP()))
2024-09-25 22:41:57 +08:00
response.Error(c, response.TranslateMsg(c, "UsernameOrPasswordError"))
2024-09-13 15:57:29 +08:00
return
}
2024-09-19 10:44:49 +08:00
//根据refer判断是webclient还是app
ref := c.GetHeader("referer")
if ref != "" {
2024-10-31 22:29:12 +08:00
f.DeviceInfo.Type = model.LoginLogClientWeb
2024-09-19 10:44:49 +08:00
}
ut := service.AllService.UserService.Login(u, &model.LoginLog{
UserId: u.Id,
Client: f.DeviceInfo.Type,
2024-11-02 07:35:26 +08:00
DeviceId: f.Id,
2024-09-19 10:44:49 +08:00
Uuid: f.Uuid,
Ip: c.ClientIP(),
Type: model.LoginLogTypeAccount,
Platform: f.DeviceInfo.Os,
})
2024-09-13 15:57:29 +08:00
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
2024-10-30 15:46:12 +08:00
// @Router /login-options [get]
2024-09-13 15:57:29 +08:00
func (l *Login) LoginOptions(c *gin.Context) {
2024-11-02 04:01:28 +08:00
ops := service.AllService.OauthService.GetOauthProviders()
ops = append(ops, model.OauthTypeWebauth)
2024-09-19 10:44:49 +08:00
var oidcItems []map[string]string
2024-11-02 04:01:28 +08:00
for _, v := range ops {
2024-09-19 10:44:49 +08:00
oidcItems = append(oidcItems, map[string]string{"name": v})
}
common, err := json.Marshal(oidcItems)
if err != nil {
2024-09-25 22:41:57 +08:00
response.Error(c, response.TranslateMsg(c, "SystemError")+err.Error())
2024-09-19 10:44:49 +08:00
return
}
var res []string
res = append(res, "common-oidc/"+string(common))
2024-11-02 04:01:28 +08:00
for _, v := range ops {
2024-09-19 10:44:49 +08:00
res = append(res, "oidc/"+v)
}
c.JSON(http.StatusOK, res)
2024-09-13 15:57:29 +08:00
}
// 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)
}