1Panel/core/app/api/v1/auth.go

187 lines
5.3 KiB
Go
Raw Normal View History

2024-07-19 19:04:11 +08:00
package v1
import (
"encoding/base64"
"github.com/1Panel-dev/1Panel/core/app/api/v1/helper"
"github.com/1Panel-dev/1Panel/core/app/dto"
"github.com/1Panel-dev/1Panel/core/app/model"
"github.com/1Panel-dev/1Panel/core/constant"
"github.com/1Panel-dev/1Panel/core/global"
2024-07-25 14:43:41 +08:00
"github.com/1Panel-dev/1Panel/core/middleware"
2024-07-19 19:04:11 +08:00
"github.com/1Panel-dev/1Panel/core/utils/captcha"
2024-07-25 14:43:41 +08:00
"github.com/1Panel-dev/1Panel/core/utils/qqwry"
2024-07-19 19:04:11 +08:00
"github.com/gin-gonic/gin"
)
type BaseApi struct{}
// @Tags Auth
// @Summary User login
// @Description 用户登录
// @Accept json
// @Param EntranceCode header string true "安全入口 base64 加密串"
// @Param request body dto.Login true "request"
// @Success 200 {object} dto.UserLoginInfo
2024-08-06 16:20:31 +08:00
// @Router /core/auth/login [post]
2024-07-19 19:04:11 +08:00
func (b *BaseApi) Login(c *gin.Context) {
var req dto.Login
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
if req.AuthMethod != "jwt" && !req.IgnoreCaptcha {
if err := captcha.VerifyCode(req.CaptchaID, req.Captcha); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
}
entranceItem := c.Request.Header.Get("EntranceCode")
var entrance []byte
if len(entranceItem) != 0 {
entrance, _ = base64.StdEncoding.DecodeString(entranceItem)
}
user, err := authService.Login(c, req, string(entrance))
go saveLoginLogs(c, err)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, user)
}
// @Tags Auth
// @Summary User login with mfa
// @Description 用户 mfa 登录
// @Accept json
// @Param request body dto.MFALogin true "request"
// @Success 200 {object} dto.UserLoginInfo
2024-08-06 16:20:31 +08:00
// @Router /core/auth/mfalogin [post]
2024-07-19 19:04:11 +08:00
// @Header 200 {string} EntranceCode "安全入口"
func (b *BaseApi) MFALogin(c *gin.Context) {
var req dto.MFALogin
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
entranceItem := c.Request.Header.Get("EntranceCode")
var entrance []byte
if len(entranceItem) != 0 {
entrance, _ = base64.StdEncoding.DecodeString(entranceItem)
}
user, err := authService.MFALogin(c, req, string(entrance))
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, user)
}
// @Tags Auth
// @Summary User logout
// @Description 用户登出
// @Success 200
// @Security ApiKeyAuth
2024-08-06 16:20:31 +08:00
// @Router /core/auth/logout [post]
2024-07-19 19:04:11 +08:00
func (b *BaseApi) LogOut(c *gin.Context) {
if err := authService.LogOut(c); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, nil)
}
// @Tags Auth
// @Summary Load captcha
// @Description 加载验证码
// @Success 200 {object} dto.CaptchaResponse
2024-08-06 16:20:31 +08:00
// @Router /core/auth/captcha [get]
2024-07-19 19:04:11 +08:00
func (b *BaseApi) Captcha(c *gin.Context) {
captcha, err := captcha.CreateCaptcha()
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, captcha)
}
// @Tags Auth
// @Summary Load safety status
// @Description 获取系统安全登录状态
// @Success 200
2024-08-06 16:20:31 +08:00
// @Router /core/auth/issafety [get]
2024-07-19 19:04:11 +08:00
func (b *BaseApi) CheckIsSafety(c *gin.Context) {
code := c.DefaultQuery("code", "")
status, err := authService.CheckIsSafety(code)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
if status == "disable" && len(code) != 0 {
helper.ErrorWithDetail(c, constant.CodeErrNotFound, constant.ErrTypeInternalServer, err)
return
}
if status == "unpass" {
2024-07-25 14:43:41 +08:00
if middleware.LoadErrCode("err-entrance") != 200 {
helper.ErrResponse(c, middleware.LoadErrCode("err-entrance"))
return
}
2024-07-19 19:04:11 +08:00
helper.ErrorWithDetail(c, constant.CodeErrEntrance, constant.ErrTypeInternalServer, nil)
return
}
helper.SuccessWithOutData(c)
}
func (b *BaseApi) GetResponsePage(c *gin.Context) {
pageCode, err := authService.GetResponsePage()
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, pageCode)
}
// @Tags Auth
// @Summary Check System isDemo
// @Description 判断是否为demo环境
// @Success 200
2024-08-06 16:20:31 +08:00
// @Router /core/auth/demo [get]
2024-07-19 19:04:11 +08:00
func (b *BaseApi) CheckIsDemo(c *gin.Context) {
helper.SuccessWithData(c, global.CONF.System.IsDemo)
}
// @Tags Auth
// @Summary Load System Language
// @Description 获取系统语言设置
// @Success 200
2024-08-06 16:20:31 +08:00
// @Router /core/auth/language [get]
2024-07-19 19:04:11 +08:00
func (b *BaseApi) GetLanguage(c *gin.Context) {
settingInfo, err := settingService.GetSettingInfo()
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, settingInfo.Language)
}
func saveLoginLogs(c *gin.Context, err error) {
var logs model.LoginLog
if err != nil {
logs.Status = constant.StatusFailed
logs.Message = err.Error()
} else {
logs.Status = constant.StatusSuccess
}
logs.IP = c.ClientIP()
2024-07-25 14:43:41 +08:00
qqWry, err := qqwry.NewQQwry()
if err != nil {
global.LOG.Errorf("load qqwry datas failed: %s", err)
}
res := qqWry.Find(logs.IP)
2024-07-19 19:04:11 +08:00
logs.Agent = c.GetHeader("User-Agent")
2024-07-25 14:43:41 +08:00
logs.Address = res.Area
2024-07-19 19:04:11 +08:00
_ = logService.CreateLoginLog(logs)
}