159 lines
5.4 KiB
Go
159 lines
5.4 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
|
|
authentity "spark-server/internal/module/auth/entity"
|
|
deviceentity "spark-server/internal/module/device/entity"
|
|
devicelogic "spark-server/internal/module/device/logic"
|
|
userentity "spark-server/internal/module/user/entity"
|
|
"spark-server/internal/pkg/errs"
|
|
"spark-server/internal/pkg/i18n"
|
|
"spark-server/internal/pkg/reqctx"
|
|
"spark-server/internal/pkg/validate"
|
|
)
|
|
|
|
// Verifier checks email OTP and records the login device. The user module owns the implementation.
|
|
type Verifier interface {
|
|
AuthenticateEmail(ctx context.Context, email, verifyCode string, device *userentity.LoginDevice) (*userentity.User, error)
|
|
}
|
|
|
|
// DeviceRegistrar resolves physical device_id from idfv / android_id.
|
|
type DeviceRegistrar interface {
|
|
Register(ctx context.Context, in devicelogic.RegisterInput) (uint64, error)
|
|
}
|
|
|
|
// LoginInput is the HTTP login body.
|
|
type LoginInput struct {
|
|
Email string `json:"email" validate:"required,email" label:"field.email" errmsg:"required:validate.login.email_required;email:validate.login.email_invalid"`
|
|
VerifyCode string `json:"verify_code" validate:"required" label:"field.verify_code" errmsg:"required:validate.login.code_required"`
|
|
AppID uint8 `json:"app_id" validate:"required,min=1" label:"field.app_id" errmsg:"required:validate.login.app_id_required"`
|
|
DeviceType uint8 `json:"device_type" validate:"omitempty,min=1,max=6" label:"field.device_type" errmsg:"min:validate.login.device_type_invalid;max:validate.login.device_type_invalid"`
|
|
Platform uint8 `json:"platform" validate:"omitempty,min=1,max=6" label:"field.device_type" errmsg:"min:validate.login.device_type_invalid;max:validate.login.device_type_invalid"`
|
|
DeviceName string `json:"device_name" validate:"omitempty,max=128" label:"field.device_name"`
|
|
IDFV string `json:"idfv" validate:"omitempty,max=36" label:"field.idfv"`
|
|
AndroidID string `json:"android_id" validate:"omitempty,max=20" label:"field.android_id"`
|
|
}
|
|
|
|
type RefreshInput struct {
|
|
RefreshToken string `json:"refresh_token" validate:"required" label:"field.refresh_token" errmsg:"required:validate.login.refresh_required"`
|
|
}
|
|
|
|
// SessionKicker drops the IM connection for one device after HTTP logout.
|
|
type SessionKicker interface {
|
|
KickDevice(userID int64, deviceID uint64)
|
|
}
|
|
|
|
// LoginService is the application login use-case.
|
|
type LoginService struct {
|
|
Issuer Issuer
|
|
Users Verifier
|
|
Devices DeviceRegistrar
|
|
Sessions SessionKicker
|
|
}
|
|
|
|
func NewLoginService(issuer Issuer, users Verifier) *LoginService {
|
|
return &LoginService{Issuer: issuer, Users: users}
|
|
}
|
|
|
|
func (s *LoginService) Login(ctx context.Context, in *LoginInput) (*authentity.IssuedToken, error) {
|
|
if s == nil || s.Issuer == nil || s.Users == nil || s.Devices == nil || in == nil {
|
|
return nil, errs.ErrUnauthorized
|
|
}
|
|
if err := validate.StructLang(reqctx.Lang(ctx), in); err != nil {
|
|
return nil, err
|
|
}
|
|
if !deviceentity.AppID(in.AppID).Valid() {
|
|
return nil, errs.ErrInvalidArgument.WithMsg(i18n.T(reqctx.Lang(ctx), i18n.KeyLoginAppIDInvalid, nil))
|
|
}
|
|
dtype := loginResolvedDeviceType(in)
|
|
if !dtype.Valid() {
|
|
return nil, errs.ErrInvalidArgument.WithMsg(i18n.T(reqctx.Lang(ctx), i18n.KeyLoginDeviceTypeRequired, nil))
|
|
}
|
|
deviceID, err := s.Devices.Register(ctx, devicelogic.RegisterInput{
|
|
DeviceType: dtype,
|
|
DeviceName: in.DeviceName,
|
|
IDFV: in.IDFV,
|
|
AndroidID: in.AndroidID,
|
|
ClientIP: reqctx.RealIP(ctx),
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ld := loginDevice(ctx, in, deviceID)
|
|
u, err := s.Users.AuthenticateEmail(ctx, in.Email, in.VerifyCode, ld)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if u == nil || u.ID <= 0 {
|
|
return nil, errs.ErrUnauthorized
|
|
}
|
|
tok, err := s.Issuer.Issue(ctx, u.ID, deviceID, uint32(in.AppID))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tok.SparkID = u.SparkID
|
|
return tok, nil
|
|
}
|
|
|
|
func (s *LoginService) Refresh(ctx context.Context, in RefreshInput) (*authentity.IssuedToken, error) {
|
|
if s == nil || s.Issuer == nil {
|
|
return nil, errs.ErrUnauthorized
|
|
}
|
|
if err := validate.StructLang(reqctx.Lang(ctx), in); err != nil {
|
|
return nil, err
|
|
}
|
|
return s.Issuer.Refresh(ctx, in.RefreshToken)
|
|
}
|
|
|
|
func (s *LoginService) Logout(ctx context.Context) error {
|
|
if s == nil || s.Issuer == nil {
|
|
return errs.ErrUnauthorized
|
|
}
|
|
rc := reqctx.From(ctx)
|
|
if rc == nil || rc.UserID <= 0 || rc.DeviceID == 0 || rc.AppID == 0 {
|
|
return errs.ErrUnauthorized
|
|
}
|
|
if err := s.Issuer.Logout(ctx, rc.UserID, rc.DeviceID, rc.AppID, rc.JTI, rc.TokenExpires); err != nil {
|
|
return err
|
|
}
|
|
if s.Sessions != nil {
|
|
s.Sessions.KickDevice(rc.UserID, rc.DeviceID)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func loginResolvedDeviceType(in *LoginInput) deviceentity.DeviceType {
|
|
if in == nil {
|
|
return deviceentity.TypeUnknown
|
|
}
|
|
if in.DeviceType >= 1 {
|
|
return deviceentity.DeviceTypeFromWire(in.DeviceType)
|
|
}
|
|
if in.Platform >= 1 {
|
|
return deviceentity.DeviceTypeFromWire(in.Platform)
|
|
}
|
|
return deviceentity.TypeUnknown
|
|
}
|
|
|
|
func loginDevice(ctx context.Context, in *LoginInput, deviceID uint64) *userentity.LoginDevice {
|
|
if in == nil {
|
|
return &userentity.LoginDevice{DeviceID: deviceID}
|
|
}
|
|
dtype := loginResolvedDeviceType(in)
|
|
d := &userentity.LoginDevice{
|
|
DeviceID: deviceID,
|
|
AppID: in.AppID,
|
|
DeviceType: dtype.PlatformName(),
|
|
DeviceName: in.DeviceName,
|
|
IDFV: in.IDFV,
|
|
AndroidID: in.AndroidID,
|
|
RealIP: reqctx.RealIP(ctx),
|
|
Language: reqctx.Language(ctx),
|
|
}
|
|
if rc := reqctx.From(ctx); rc != nil {
|
|
d.AppVersion = rc.AppVersion
|
|
}
|
|
return d
|
|
}
|