diff --git a/apis/auth/auth.api b/apis/auth/auth.api index c257e04..b33ec00 100644 --- a/apis/auth/auth.api +++ b/apis/auth/auth.api @@ -149,11 +149,12 @@ type ( State string `form:"state"` } DeviceLoginRequest { - Identifier string `json:"identifier" validate:"required"` - IP string `header:"X-Original-Forwarded-For"` - UserAgent string `json:"user_agent" validate:"required"` - CfToken string `json:"cf_token,optional"` - ShortCode string `json:"short_code,optional"` + Identifier string `json:"identifier" validate:"required"` + IP string `header:"X-Original-Forwarded-For"` + UserAgent string `json:"user_agent" validate:"required"` + CfToken string `json:"cf_token,optional"` + ShortCode string `json:"short_code,optional"` + BasePayload string `json:"base_payload,optional"` } GenerateCaptchaResponse { Id string `json:"id"` diff --git a/initialize/migrate/database/02146_device_base_payload.down.sql b/initialize/migrate/database/02146_device_base_payload.down.sql new file mode 100644 index 0000000..4cb191a --- /dev/null +++ b/initialize/migrate/database/02146_device_base_payload.down.sql @@ -0,0 +1 @@ +ALTER TABLE `user_device` DROP COLUMN `base_payload`; diff --git a/initialize/migrate/database/02146_device_base_payload.up.sql b/initialize/migrate/database/02146_device_base_payload.up.sql new file mode 100644 index 0000000..fb76103 --- /dev/null +++ b/initialize/migrate/database/02146_device_base_payload.up.sql @@ -0,0 +1 @@ +ALTER TABLE `user_device` ADD COLUMN `base_payload` TEXT DEFAULT NULL COMMENT 'Base Payload' AFTER `short_code`; diff --git a/internal/logic/auth/deviceLoginLogic.go b/internal/logic/auth/deviceLoginLogic.go index e0a0d0e..81594cb 100644 --- a/internal/logic/auth/deviceLoginLogic.go +++ b/internal/logic/auth/deviceLoginLogic.go @@ -96,6 +96,17 @@ func (l *DeviceLoginLogic) DeviceLogin(req *types.DeviceLoginRequest) (resp *typ return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query user failed: %v", err.Error()) } + // Update base_payload if provided + if req.BasePayload != "" && req.BasePayload != deviceInfo.BasePayload { + deviceInfo.BasePayload = req.BasePayload + if updateErr := l.svcCtx.UserModel.UpdateDevice(l.ctx, deviceInfo); updateErr != nil { + l.Errorw("update device base_payload failed", + logger.Field("device_id", deviceInfo.Id), + logger.Field("error", updateErr.Error()), + ) + } + } + // 注销后 device auth_method 被删除,重新登录时需要补回 hasDeviceAuth := false for _, am := range userInfo.AuthMethods { @@ -220,13 +231,14 @@ func (l *DeviceLoginLogic) registerUserAndDevice(req *types.DeviceLoginRequest) // Insert device record deviceInfo := &user.Device{ - Ip: req.IP, - UserId: userInfo.Id, - UserAgent: req.UserAgent, - Identifier: req.Identifier, - ShortCode: req.ShortCode, - Enabled: true, - Online: false, + Ip: req.IP, + UserId: userInfo.Id, + UserAgent: req.UserAgent, + Identifier: req.Identifier, + ShortCode: req.ShortCode, + BasePayload: req.BasePayload, + Enabled: true, + Online: false, } if err := db.Create(deviceInfo).Error; err != nil { l.Errorw("failed to insert device", diff --git a/internal/model/user/user.go b/internal/model/user/user.go index 425af75..c9ff2bd 100644 --- a/internal/model/user/user.go +++ b/internal/model/user/user.go @@ -130,16 +130,17 @@ func (*AuthMethods) TableName() string { } type Device struct { - Id int64 `gorm:"primaryKey"` - Ip string `gorm:"type:varchar(255);not null;comment:Device IP"` - UserId int64 `gorm:"index:idx_user_id;not null;comment:User ID"` - UserAgent string `gorm:"default:null;comment:UserAgent."` - Identifier string `gorm:"type:varchar(255);unique;index:idx_identifier;default:'';comment:Device Identifier"` - ShortCode string `gorm:"type:varchar(255);default:'';comment:Short Code"` - Online bool `gorm:"default:false;not null;comment:Online"` - Enabled bool `gorm:"default:true;not null;comment:Enabled"` - CreatedAt time.Time `gorm:"<-:create;comment:Creation Time"` - UpdatedAt time.Time `gorm:"comment:Update Time"` + Id int64 `gorm:"primaryKey"` + Ip string `gorm:"type:varchar(255);not null;comment:Device IP"` + UserId int64 `gorm:"index:idx_user_id;not null;comment:User ID"` + UserAgent string `gorm:"default:null;comment:UserAgent."` + Identifier string `gorm:"type:varchar(255);unique;index:idx_identifier;default:'';comment:Device Identifier"` + ShortCode string `gorm:"type:varchar(255);default:'';comment:Short Code"` + BasePayload string `gorm:"type:text;default:null;comment:Base Payload"` + Online bool `gorm:"default:false;not null;comment:Online"` + Enabled bool `gorm:"default:true;not null;comment:Enabled"` + CreatedAt time.Time `gorm:"<-:create;comment:Creation Time"` + UpdatedAt time.Time `gorm:"comment:Update Time"` } func (*Device) TableName() string { diff --git a/internal/types/types.go b/internal/types/types.go index 65cd75b..6ea8472 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -637,11 +637,12 @@ type DeviceAuthticateConfig struct { } type DeviceLoginRequest struct { - Identifier string `json:"identifier" validate:"required"` - IP string `header:"X-Original-Forwarded-For"` - UserAgent string `json:"user_agent" validate:"required"` - CfToken string `json:"cf_token,optional"` - ShortCode string `json:"short_code,optional"` + Identifier string `json:"identifier" validate:"required"` + IP string `header:"X-Original-Forwarded-For"` + UserAgent string `json:"user_agent" validate:"required"` + CfToken string `json:"cf_token,optional"` + ShortCode string `json:"short_code,optional"` + BasePayload string `json:"base_payload,optional"` } type DissolveFamilyRequest struct {