RezinaLMS-BackEnd/internal/domain/auth.go
n3p-tun3 a81fe3cd19 feat(auth): add learner device sessions with a 2-device limit
- Track learner sessions and revoke them immediately on every request
- Add a device replacement challenge for logins over the limit
- Add learner and admin endpoints to list and remove devices
- Trim excess sessions when enforcement is enabled
- Stop logging refresh tokens and authorization headers

Enforcement is off by default through DEVICE_LIMIT_ENFORCED.
2026-11-23 17:38:48 +03:00

117 lines
2.5 KiB
Go

package domain
import (
"time"
"github.com/google/uuid"
)
type GoogleUser struct {
ID string
Email string
VerifiedEmail bool
GivenName string
FamilyName string
Picture string
}
// AppleUser is populated from a validated Sign in with Apple identity token and optional client-supplied profile fields.
type AppleUser struct {
ID string
Email string
VerifiedEmail bool
GivenName string
FamilyName string
}
type LoginSuccess struct {
UserId int64
Role Role
RfToken string
SessionID uuid.UUID
IsNewAccount bool
// WelcomeEmailEligible is true when a learner completed email-based registration OTP verification.
WelcomeEmailEligible bool
}
type LoginRequest struct {
Email string `json:"email"`
PhoneNumber string `json:"phone_number"`
Password string `json:"password"`
OTPCode string `json:"otp_code"`
}
type RefreshToken struct {
ID int64
UserID int64
Token string
ExpiresAt time.Time
CreatedAt time.Time
Revoked bool
SessionID uuid.NullUUID
}
type DeviceInfo struct {
Key string
Label string
Platform string
}
type UserSession struct {
ID uuid.UUID `json:"id"`
UserID int64 `json:"-"`
Label string `json:"label"`
Platform string `json:"platform"`
CreatedAt time.Time `json:"created_at,omitempty"`
LastSeenAt time.Time `json:"last_seen_at"`
ExpiresAt time.Time `json:"expires_at,omitempty"`
IsCurrent bool `json:"is_current"`
}
type SessionIssueParams struct {
UserID int64
Device DeviceInfo
SessionExpires time.Time
RefreshToken string
RefreshExpires time.Time
MaxDevices int
EnforceLimit bool
ChallengeHash []byte
ChallengeExpiry time.Time
}
type SessionIssueResult struct {
Session UserSession
RefreshToken string
LimitReached bool
ChallengeToken string
ChallengeExpires time.Time
Devices []UserSession
}
type SessionReplaceParams struct {
ChallengeHash []byte
SessionID uuid.UUID
RefreshToken string
RefreshExpires time.Time
MaxDevices int
EnforceLimit bool
}
type SessionRefreshParams struct {
OldToken string
NewToken string
RefreshExpires time.Time
Device DeviceInfo
IsLearner bool
MaxDevices int
EnforceLimit bool
}
// I used this because i was getting an error with the ValidInt64
// when it was being unmarshaled by the jwt
type NullJwtInt64 struct {
Value int64
Valid bool
}