2024-08-07 13:52:34 +08:00
package v2
2024-07-19 19:04:11 +08:00
import (
"encoding/base64"
2026-01-20 10:28:04 +08:00
"net/http"
2025-10-20 11:37:32 +08:00
2024-08-07 13:52:34 +08:00
"github.com/1Panel-dev/1Panel/core/app/api/v2/helper"
2026-07-29 16:48:59 +08:00
appauth "github.com/1Panel-dev/1Panel/core/app/auth"
2024-07-19 19:04:11 +08:00
"github.com/1Panel-dev/1Panel/core/app/dto"
"github.com/1Panel-dev/1Panel/core/app/model"
2026-09-20 18:32:54 +08:00
"github.com/1Panel-dev/1Panel/core/app/service"
2026-02-25 16:39:46 +08:00
"github.com/1Panel-dev/1Panel/core/buserr"
2024-07-19 19:04:11 +08:00
"github.com/1Panel-dev/1Panel/core/constant"
2025-06-18 17:58:56 +08:00
"github.com/1Panel-dev/1Panel/core/global"
2026-04-09 10:28:39 +08:00
initauth "github.com/1Panel-dev/1Panel/core/init/auth"
2024-07-19 19:04:11 +08:00
"github.com/1Panel-dev/1Panel/core/utils/captcha"
2026-01-20 10:28:04 +08:00
"github.com/1Panel-dev/1Panel/core/utils/common"
2026-04-23 17:00:50 +08:00
"github.com/1Panel-dev/1Panel/core/utils/xpack"
2024-07-19 19:04:11 +08:00
"github.com/gin-gonic/gin"
)
type BaseApi struct {}
// @Tags Auth
// @Summary User login
// @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
}
2025-11-28 11:05:27 +08:00
ip := common . GetRealClientIP ( c )
2026-03-31 19:00:06 +08:00
if global . IPTracker . IsLocked ( ip ) {
helper . BadAuth ( c , "ErrLoginLocked" , nil )
return
}
2025-11-28 11:05:27 +08:00
needCaptcha := global . IPTracker . NeedCaptcha ( ip )
if needCaptcha {
2025-02-07 18:15:20 +08:00
if errMsg := captcha . VerifyCode ( req . CaptchaID , req . Captcha ); errMsg != "" {
helper . BadAuth ( c , errMsg , nil )
2024-07-19 19:04:11 +08:00
return
}
}
2025-11-28 11:05:27 +08:00
2024-07-19 19:04:11 +08:00
entranceItem := c . Request . Header . Get ( "EntranceCode" )
var entrance [] byte
if len ( entranceItem ) != 0 {
entrance , _ = base64 . StdEncoding . DecodeString ( entranceItem )
}
2025-04-28 22:43:48 +08:00
if len ( entrance ) == 0 {
cookieValue , err := c . Cookie ( "SecurityEntrance" )
if err == nil {
entrance , _ = base64 . StdEncoding . DecodeString ( cookieValue )
}
}
2024-07-19 19:04:11 +08:00
2026-04-23 17:00:50 +08:00
user , msgKey , err := xpack . AuthProvider . Login ( c , req , string ( entrance ))
2026-02-25 16:39:46 +08:00
if user == nil || user . MfaStatus != constant . StatusEnable {
2026-11-08 22:39:54 +08:00
go saveLoginLogs ( snapshotLoginLog ( c , req . Name , wrapLoginErr ( msgKey , err )))
2026-02-25 16:39:46 +08:00
}
2026-05-28 18:04:29 +08:00
if msgKey == "ErrAuth" || msgKey == "ErrEntrance" || msgKey == "ErrNoneNode" {
2025-11-28 11:05:27 +08:00
if msgKey == "ErrAuth" {
2026-03-31 19:00:06 +08:00
global . IPTracker . RecordFailure ( ip )
2025-11-28 11:05:27 +08:00
global . IPTracker . SetNeedCaptcha ( ip )
}
2025-02-07 18:15:20 +08:00
helper . BadAuth ( c , msgKey , err )
return
}
2024-07-19 19:04:11 +08:00
if err != nil {
2026-03-31 19:00:06 +08:00
global . IPTracker . RecordFailure ( ip )
2025-11-28 11:05:27 +08:00
global . IPTracker . SetNeedCaptcha ( ip )
2024-10-29 18:42:12 +08:00
helper . InternalServer ( c , err )
2024-07-19 19:04:11 +08:00
return
}
2026-03-31 19:00:06 +08:00
if user == nil || user . MfaStatus != constant . StatusEnable {
global . IPTracker . Clear ( ip )
}
2024-07-19 19:04:11 +08:00
helper . SuccessWithData ( c , user )
}
// @Tags Auth
// @Summary User login with 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
}
2026-03-30 22:28:32 +08:00
ip := common . GetRealClientIP ( c )
2026-03-31 19:00:06 +08:00
if global . IPTracker . IsLocked ( ip ) {
helper . BadAuth ( c , "ErrLoginLocked" , nil )
2026-03-30 22:28:32 +08:00
return
}
2024-07-19 19:04:11 +08:00
entranceItem := c . Request . Header . Get ( "EntranceCode" )
var entrance [] byte
if len ( entranceItem ) != 0 {
entrance , _ = base64 . StdEncoding . DecodeString ( entranceItem )
}
2026-04-23 17:00:50 +08:00
user , msgKey , err := xpack . AuthProvider . MFALogin ( c , req , string ( entrance ))
2026-11-08 22:39:54 +08:00
go saveLoginLogs ( snapshotLoginLog ( c , loginLogUserName ( user , req . SessionID ), wrapLoginErr ( msgKey , err )))
2026-05-28 18:04:29 +08:00
if msgKey == "ErrNoneNode" {
helper . BadAuth ( c , msgKey , err )
return
}
2026-03-31 19:00:06 +08:00
if msgKey == "ErrMFA" {
global . IPTracker . RecordFailure ( ip )
2026-04-09 10:28:39 +08:00
failures := initauth . GetMFASessionStore (). RecordFailure ( req . SessionID )
if failures >= initauth . MFASessionMaxFailures {
global . IPTracker . SetNeedCaptcha ( ip )
helper . BadAuth ( c , "ErrCaptchaCode" , nil )
return
}
2025-02-07 18:15:20 +08:00
helper . BadAuth ( c , msgKey , err )
return
}
2024-07-19 19:04:11 +08:00
if err != nil {
2026-03-31 19:00:06 +08:00
global . IPTracker . RecordFailure ( ip )
2024-10-29 18:42:12 +08:00
helper . InternalServer ( c , err )
2024-07-19 19:04:11 +08:00
return
}
2026-03-30 22:28:32 +08:00
global . IPTracker . Clear ( ip )
2024-07-19 19:04:11 +08:00
helper . SuccessWithData ( c , user )
}
2026-01-20 10:28:04 +08:00
// @Tags Auth
// @Summary User login with passkey
// @Success 200 {object} dto.PasskeyBeginResponse
// @Router /core/auth/passkey/begin [post]
func ( b * BaseApi ) PasskeyBeginLogin ( c * gin . Context ) {
entrance := loadEntranceFromRequest ( c )
2026-04-23 17:00:50 +08:00
res , msgKey , err := xpack . AuthProvider . PasskeyBeginLogin ( c , entrance )
2026-01-20 10:28:04 +08:00
if msgKey != "" {
if msgKey == "ErrEntrance" {
helper . BadAuth ( c , msgKey , err )
return
}
if msgKey == "ErrPasskeyNotConfigured" {
helper . ErrorWithDetail ( c , http . StatusNotFound , msgKey , err )
return
}
helper . ErrorWithDetail ( c , http . StatusBadRequest , msgKey , err )
return
}
if err != nil {
helper . InternalServer ( c , err )
return
}
helper . SuccessWithData ( c , res )
}
// @Tags Auth
// @Summary User login with passkey
// @Success 200 {object} dto.UserLoginInfo
// @Router /core/auth/passkey/finish [post]
func ( b * BaseApi ) PasskeyFinishLogin ( c * gin . Context ) {
sessionID := c . GetHeader ( "Passkey-Session" )
entrance := loadEntranceFromRequest ( c )
2026-04-23 17:00:50 +08:00
user , msgKey , err := xpack . AuthProvider . PasskeyFinishLogin ( c , sessionID , entrance )
2026-11-08 22:39:54 +08:00
go saveLoginLogs ( snapshotLoginLog ( c , loginLogUserName ( user , "" ), wrapLoginErr ( msgKey , err )))
2026-05-28 18:04:29 +08:00
if msgKey == "ErrAuth" || msgKey == "ErrEntrance" || msgKey == "ErrNoneNode" {
2026-01-20 10:28:04 +08:00
if msgKey == "ErrAuth" {
global . IPTracker . SetNeedCaptcha ( common . GetRealClientIP ( c ))
}
helper . BadAuth ( c , msgKey , err )
return
}
if msgKey != "" {
helper . ErrorWithDetail ( c , http . StatusBadRequest , msgKey , err )
return
}
if err != nil {
global . IPTracker . SetNeedCaptcha ( common . GetRealClientIP ( c ))
helper . InternalServer ( c , err )
return
}
global . IPTracker . Clear ( common . GetRealClientIP ( c ))
helper . SuccessWithData ( c , user )
}
2024-07-19 19:04:11 +08:00
// @Tags Auth
// @Summary User logout
// @Success 200
// @Security ApiKeyAuth
2025-01-21 18:32:14 +08:00
// @Security Timestamp
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 ) {
2026-08-03 13:43:01 +08:00
result , prepareErr := xpack . AuthProvider . PrepareLogout ( c )
2024-07-19 19:04:11 +08:00
if err := authService . LogOut ( c ); err != nil {
2024-10-29 18:42:12 +08:00
helper . InternalServer ( c , err )
2024-07-19 19:04:11 +08:00
return
}
2026-08-03 13:43:01 +08:00
if prepareErr != nil {
global . LOG . Warnf ( "prepare external logout failed: %v" , prepareErr )
result = & dto . LogoutResult {}
}
if result == nil {
result = & dto . LogoutResult {}
}
helper . SuccessWithData ( c , result )
2024-07-19 19:04:11 +08:00
}
// @Tags Auth
// @Summary Load captcha
// @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 {
2024-10-29 18:42:12 +08:00
helper . InternalServer ( c , err )
2024-07-19 19:04:11 +08:00
return
}
helper . SuccessWithData ( c , captcha )
}
// @Tags Auth
2025-02-08 22:57:08 +08:00
// @Summary Get Setting For Login
2026-04-09 10:01:34 +08:00
// @Success 200 {object} dto.LoginSetting
2025-02-08 22:57:08 +08:00
// @Router /core/auth/setting [get]
func ( b * BaseApi ) GetLoginSetting ( c * gin . Context ) {
2024-07-19 19:04:11 +08:00
settingInfo , err := settingService . GetSettingInfo ()
if err != nil {
2024-10-29 18:42:12 +08:00
helper . InternalServer ( c , err )
2024-07-19 19:04:11 +08:00
return
}
2025-11-28 11:05:27 +08:00
ip := common . GetRealClientIP ( c )
needCaptcha := global . IPTracker . NeedCaptcha ( ip )
2025-06-18 17:58:56 +08:00
res := & dto . LoginSetting {
2026-11-12 14:05:47 +08:00
IsDemo : global . CONF . Base . IsDemo ,
IsIntl : global . CONF . Base . Edition == "intl" ,
IsOffline : global . CONF . Base . IsOffline ,
IsEnterprise : global . CONF . Base . IsEnterprise ,
IsMasterProductPro : xpack . IsMasterProductPro (),
Language : settingInfo . Language ,
MenuTabs : settingInfo . MenuTabs ,
MenuAccordion : settingInfo . MenuAccordion ,
PanelCache : settingInfo . PanelCache ,
PanelName : settingInfo . PanelName ,
Theme : settingInfo . Theme ,
NeedCaptcha : needCaptcha ,
2025-06-18 17:58:56 +08:00
}
2026-04-23 17:00:50 +08:00
res . PasskeySetting = xpack . AuthProvider . PasskeyStatus ( c )
2025-06-18 17:58:56 +08:00
helper . SuccessWithData ( c , res )
2024-07-19 19:04:11 +08:00
}
2026-04-23 17:00:50 +08:00
// @Tags Auth
// @Summary Begin passkey registration
// @Accept json
// @Param request body dto.PasskeyRegisterRequest true "request"
// @Success 200 {object} dto.PasskeyBeginResponse
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /core/auth/passkey/register/begin [post]
func ( b * BaseApi ) PasskeyRegisterBegin ( c * gin . Context ) {
var req dto . PasskeyRegisterRequest
if err := helper . CheckBindAndValidate ( & req , c ); err != nil {
return
}
res , msgKey , err := xpack . AuthProvider . PasskeyBeginRegister ( c , req . Name )
if msgKey != "" {
helper . ErrorWithDetail ( c , http . StatusBadRequest , msgKey , err )
return
}
if err != nil {
helper . InternalServer ( c , err )
return
}
helper . SuccessWithData ( c , res )
}
// @Tags Auth
// @Summary Finish passkey registration
// @Accept json
// @Success 200
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /core/auth/passkey/register/finish [post]
func ( b * BaseApi ) PasskeyRegisterFinish ( c * gin . Context ) {
sessionID := c . GetHeader ( "Passkey-Session" )
msgKey , err := xpack . AuthProvider . PasskeyFinishRegister ( c , sessionID )
if msgKey != "" {
helper . ErrorWithDetail ( c , http . StatusBadRequest , msgKey , err )
return
}
if err != nil {
helper . InternalServer ( c , err )
return
}
helper . Success ( c )
}
// @Tags Auth
// @Summary List passkeys
// @Success 200 {array} dto.PasskeyInfo
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /core/auth/passkey/list [get]
func ( b * BaseApi ) PasskeyList ( c * gin . Context ) {
list , err := xpack . AuthProvider . PasskeyList ( c )
if err != nil {
helper . InternalServer ( c , err )
return
}
helper . SuccessWithData ( c , list )
}
// @Tags Auth
// @Summary Delete passkey
// @Success 200
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /core/auth/passkey/del [post]
func ( b * BaseApi ) PasskeyDelete ( c * gin . Context ) {
var req dto . PasskeyID
if err := helper . CheckBindAndValidate ( & req , c ); err != nil {
return
}
if err := xpack . AuthProvider . PasskeyDelete ( c , req . ID ); err != nil {
helper . InternalServer ( c , err )
return
}
helper . Success ( c )
}
// @Tags System Setting
// @Summary Load mfa info
// @Accept json
// @Param request body dto.MfaCredential true "request"
// @Success 200 {object} mfa.Otp
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /core/auth/mfa [post]
func ( b * BaseApi ) LoadMFA ( c * gin . Context ) {
var req dto . MfaRequest
if err := helper . CheckBindAndValidate ( & req , c ); err != nil {
return
}
otp , err := xpack . AuthProvider . LoadMFA ( c , req )
if err != nil {
helper . InternalServer ( c , err )
return
}
helper . SuccessWithData ( c , otp )
}
// @Tags System Setting
// @Summary Bind mfa
// @Accept json
// @Param request body dto.MfaCredential true "request"
// @Success 200
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /core/auth/mfa/bind [post]
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"mfa 绑定","formatEN":"bind mfa"}
func ( b * BaseApi ) MFABind ( c * gin . Context ) {
var req dto . MfaCredential
if err := helper . CheckBindAndValidate ( & req , c ); err != nil {
return
}
if err := xpack . AuthProvider . MFABind ( c , req ); err != nil {
2026-05-07 09:34:40 +08:00
helper . InternalServer ( c , err )
return
}
helper . Success ( c )
}
// @Tags System Setting
// @Summary Close mfa
// @Accept json
// @Success 200
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /core/auth/mfa/close [post]
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"mfa 关闭","formatEN":"close mfa"}
func ( b * BaseApi ) MFAClose ( c * gin . Context ) {
if err := xpack . AuthProvider . MFAClose ( c ); err != nil {
2026-04-23 17:00:50 +08:00
helper . InternalServer ( c , err )
return
}
helper . Success ( c )
}
// @Tags Auth
2026-10-30 10:11:35 +08:00
// @Summary Generate or reset the legacy API key
// @Description Requires a panel login session and CSRF token. API Key authentication is not supported. This replaces the current account's legacy secret; existing clients using the previous secret must be updated. Independent API keys are unaffected.
2026-04-23 17:00:50 +08:00
// @Accept json
2026-10-30 10:11:35 +08:00
// @Produce json
// @Success 200 {object} dto.Response{data=string} "code=200 with the new secret; authentication and business errors use envelope code 400, 401 or 500"
// @Header 200 {string} Cache-Control "no-store"
// @Security PanelSession || CSRFToken
2026-04-23 17:00:50 +08:00
// @Router /core/auth/api/generate [post]
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"生成 API 接口密钥","formatEN":"generate api key"}
func ( b * BaseApi ) GenerateApiKey ( c * gin . Context ) {
2026-09-20 18:32:54 +08:00
if _ , err := appauth . RequireAPIKeySession ( c ); err != nil {
2026-04-23 17:00:50 +08:00
helper . BadAuth ( c , "ErrApiConfigDisable" , nil )
return
}
2026-11-08 22:40:12 +08:00
if err := appauth . RequireUnexpiredPassword ( c , xpack . AuthProvider ); err != nil {
apiKeyError ( c , err )
return
}
2026-04-23 17:00:50 +08:00
apiKey , err := xpack . AuthProvider . GenerateApiKey ( c )
if err != nil {
helper . InternalServer ( c , err )
return
}
2026-09-20 18:32:54 +08:00
service . NewAPIKeyService (). AuditLegacyChange ( c )
c . Header ( "Cache-Control" , "no-store" )
2026-04-23 17:00:50 +08:00
helper . SuccessWithData ( c , apiKey )
}
// @Tags Auth
2026-10-30 10:11:35 +08:00
// @Summary Update the legacy API key configuration
// @Description Requires a panel login session and CSRF token. API Key authentication is not supported. This updates only the current account's legacy API configuration.
2026-04-23 17:00:50 +08:00
// @Accept json
2026-10-30 10:11:35 +08:00
// @Produce json
2026-04-23 17:00:50 +08:00
// @Param request body dto.ApiInterfaceConfig true "request"
2026-10-30 10:11:35 +08:00
// @Success 200 {object} dto.Response "code=200 on success; authentication and business errors use envelope code 400, 401 or 500"
// @Security PanelSession || CSRFToken
2026-04-23 17:00:50 +08:00
// @Router /core/auth/api/update [post]
2026-07-29 16:48:59 +08:00
// @x-panel-log {"bodyKeys":["ipWhiteList","apiTrustedProxies"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新 API 接口配置 => IP 白名单: [ipWhiteList], API 可信代理: [apiTrustedProxies]","formatEN":"update api config => IP Allowlist: [ipWhiteList], API Trusted Proxies: [apiTrustedProxies]"}
2026-04-23 17:00:50 +08:00
func ( b * BaseApi ) UpdateApiConfig ( c * gin . Context ) {
2026-09-20 18:32:54 +08:00
if _ , err := appauth . RequireAPIKeySession ( c ); err != nil {
2026-04-23 17:00:50 +08:00
helper . BadAuth ( c , "ErrApiConfigDisable" , nil )
return
}
2026-11-08 22:40:12 +08:00
if err := appauth . RequireUnexpiredPassword ( c , xpack . AuthProvider ); err != nil {
apiKeyError ( c , err )
return
}
2026-04-23 17:00:50 +08:00
var req dto . ApiInterfaceConfig
if err := helper . CheckBindAndValidate ( & req , c ); err != nil {
return
}
2026-07-29 16:48:59 +08:00
trustedProxies , err := appauth . NormalizeAPITrustedProxies ( req . ApiTrustedProxies )
if err != nil {
helper . BadRequest ( c , err )
return
}
req . ApiTrustedProxies = trustedProxies
2026-04-23 17:00:50 +08:00
if err := xpack . AuthProvider . UpdateApiConfig ( c , req ); err != nil {
helper . InternalServer ( c , err )
return
}
2026-09-20 18:32:54 +08:00
service . NewAPIKeyService (). AuditLegacyChange ( c )
2026-04-23 17:00:50 +08:00
helper . Success ( c )
}
// @Tags Auth
// @Summary Load current user info
// @Success 200 {object} dto.CurrentUserInfo
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /core/auth/current [get]
func ( b * BaseApi ) GetCurrentUser ( c * gin . Context ) {
userInfo , err := xpack . AuthProvider . GetCurrentUserInfo ( c )
if err != nil {
helper . InternalServer ( c , err )
return
}
2026-09-20 18:32:54 +08:00
if c . GetBool ( "API_AUTH" ) || appauth . HasAPICredentials ( c ) {
userInfo . ApiKey = ""
2026-11-08 22:40:12 +08:00
} else if expired , policyErr := appauth . IsPasswordExpired ( c , xpack . AuthProvider ); expired || policyErr != nil {
// Bootstrap must remain usable for password recovery. Policy failures
// never disclose a reusable legacy secret through this read endpoint.
userInfo . ApiKey = ""
2026-09-20 18:32:54 +08:00
}
c . Header ( "Cache-Control" , "no-store" )
2026-04-23 17:00:50 +08:00
helper . SuccessWithData ( c , userInfo )
}
// @Tags Auth
// @Summary Update current user info
// @Accept json
// @Param request body dto.CurrentUserUpdate true "request"
// @Success 200
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /core/auth/current/update [post]
func ( b * BaseApi ) UpdateCurrentUser ( c * gin . Context ) {
var req dto . CurrentUserUpdate
if err := helper . CheckBindAndValidate ( & req , c ); err != nil {
return
}
if err := xpack . AuthProvider . UpdateCurrentUserInfo ( c , req ); err != nil {
helper . InternalServer ( c , err )
return
}
helper . Success ( c )
}
2026-04-28 16:53:16 +08:00
// @Tags Auth
// @Summary Reset system password expired
// @Accept json
// @Param request body dto.PasswordUpdate true "request"
// @Success 200
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /core/auth/expired/reset [post]
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"重置过期密码","formatEN":"reset an expired Password"}
func ( b * BaseApi ) ResetPassword ( c * gin . Context ) {
var req dto . PasswordUpdate
if err := helper . CheckBindAndValidate ( & req , c ); err != nil {
return
}
if err := xpack . AuthProvider . HandlePasswordExpired ( c , req . OldPassword , req . NewPassword ); err != nil {
helper . InternalServer ( c , err )
return
}
helper . Success ( c )
}
2026-11-08 22:39:54 +08:00
// Capture request and authentication values synchronously. Arguments to the go
// statement are evaluated before it starts; the DB writer never keeps a Gin
// context that may already have been reset for another request.
func snapshotLoginLog ( c * gin . Context , userName string , err error ) model . LoginLog {
2024-07-19 19:04:11 +08:00
var logs model . LoginLog
if err != nil {
logs . Status = constant . StatusFailed
logs . Message = err . Error ()
} else {
logs . Status = constant . StatusSuccess
}
2026-11-08 22:39:54 +08:00
logs . IP = common . AuditClientIP ( c )
2026-05-27 13:44:14 +08:00
logs . User = userName
2024-07-19 19:04:11 +08:00
logs . Agent = c . GetHeader ( "User-Agent" )
2026-11-08 22:39:54 +08:00
return logs
}
func saveLoginLogs ( logs model . LoginLog ) {
2024-07-19 19:04:11 +08:00
_ = logService . CreateLoginLog ( logs )
}
2026-01-20 10:28:04 +08:00
2026-05-27 13:44:14 +08:00
func loginLogUserName ( user * dto . UserLoginInfo , mfaSessionID string ) string {
if user != nil {
return user . Name
}
if mfaSessionID == "" {
return ""
}
if session , ok := initauth . GetMFASessionStore (). Get ( mfaSessionID ); ok {
return session . Name
}
return ""
}
2026-02-25 16:39:46 +08:00
func wrapLoginErr ( msgKey string , err error ) error {
if err != nil {
return err
}
if msgKey == "ErrAuth" || msgKey == "ErrEntrance" {
return buserr . New ( msgKey )
}
return nil
}
2026-01-20 10:28:04 +08:00
func loadEntranceFromRequest ( c * gin . Context ) string {
entranceItem := c . Request . Header . Get ( "EntranceCode" )
var entrance [] byte
if len ( entranceItem ) != 0 {
entrance , _ = base64 . StdEncoding . DecodeString ( entranceItem )
}
if len ( entrance ) == 0 {
cookieValue , err := c . Cookie ( "SecurityEntrance" )
if err == nil {
entrance , _ = base64 . StdEncoding . DecodeString ( cookieValue )
}
}
return string ( entrance )
}