ci / api / api (push) Successful in 52s
ci / contract / contract (сгенерированный код совпадает с openapi.yaml) (push) Successful in 1m9s
ci / mobile / mobile (Metro-бандл Android) (push) Successful in 1m35s
release / release (версия и заметки) (push) Successful in 5s
release / android (APK в релиз) (push) Successful in 4m6s
ci / db / db (push) Successful in 1m15s
ci / docker / docker (образы собираются — их сервер собирает сам при обновлении) (push) Successful in 1m19s
ci / ts / ts (packages + web) (push) Successful in 2m34s
release / desktop (Linux и Windows в релиз) (push) Successful in 14m27s
release / prune (старые файлы и прогоны) (push) Successful in 6s
127 lines
4.0 KiB
Go
127 lines
4.0 KiB
Go
// Package auth — пользователи, вход, токены, политика прав (docs/auth-and-roles.md).
|
|
package auth
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// Role — роль пользователя.
|
|
type Role string
|
|
|
|
// Роли по возрастанию прав.
|
|
const (
|
|
RoleUser Role = "user"
|
|
RoleModerator Role = "moderator"
|
|
RoleAdmin Role = "admin"
|
|
)
|
|
|
|
// Valid сообщает, известна ли роль.
|
|
func (r Role) Valid() bool { return r == RoleUser || r == RoleModerator || r == RoleAdmin }
|
|
|
|
// ClientType — тип клиента, от него зависит доставка refresh-токена.
|
|
type ClientType string
|
|
|
|
// Типы клиентов.
|
|
const (
|
|
ClientWeb ClientType = "web"
|
|
ClientAndroid ClientType = "android"
|
|
ClientDesktop ClientType = "desktop"
|
|
)
|
|
|
|
// Valid сообщает, известен ли тип клиента.
|
|
func (c ClientType) Valid() bool { return c == ClientWeb || c == ClientAndroid || c == ClientDesktop }
|
|
|
|
// UIPrefs — настройки интерфейса пользователя (users.ui_prefs). Пустые поля — значения по умолчанию клиента.
|
|
type UIPrefs struct {
|
|
Theme *string `json:"theme,omitempty"`
|
|
WaveVariant *string `json:"wave_variant,omitempty"`
|
|
WebPlayer *string `json:"web_player,omitempty"`
|
|
SearchPos *string `json:"search_position,omitempty"`
|
|
SearchFold *bool `json:"search_collapse,omitempty"`
|
|
HomeActs *bool `json:"home_activities,omitempty"`
|
|
HomeMoods *bool `json:"home_moods,omitempty"`
|
|
MobilePlayer *string `json:"mobile_player,omitempty"`
|
|
Ambient *bool `json:"ambient,omitempty"`
|
|
CrossfadeS *int `json:"crossfade_s,omitempty"`
|
|
Normalize *bool `json:"normalize,omitempty"`
|
|
}
|
|
|
|
// User — учётная запись.
|
|
type User struct {
|
|
ID uuid.UUID
|
|
Username string
|
|
DisplayName *string
|
|
PasswordHash string
|
|
Role Role
|
|
ManagesUpdates bool
|
|
ReviewsUploads bool // аудит очереди на добавление; не следует из роли
|
|
Locale string
|
|
UIPrefs UIPrefs
|
|
MustChangePassword bool
|
|
MustChangeCredentials bool
|
|
IsBlocked bool
|
|
TokenVersion int
|
|
FailedLoginCount int
|
|
LockedUntil *time.Time
|
|
CreatedAt time.Time
|
|
LastLoginAt *time.Time
|
|
}
|
|
|
|
// RefreshToken — строка refresh_tokens. Сам токен в БД не хранится, только SHA-256.
|
|
type RefreshToken struct {
|
|
ID uuid.UUID
|
|
UserID uuid.UUID
|
|
FamilyID uuid.UUID
|
|
FamilyStartedAt time.Time
|
|
TokenHash []byte
|
|
DeviceName *string
|
|
ClientType ClientType
|
|
CreatedAt time.Time
|
|
ExpiresAt time.Time
|
|
UsedAt *time.Time
|
|
RevokedAt *time.Time
|
|
}
|
|
|
|
// Session — активная семья refresh-токенов (одно устройство).
|
|
type Session struct {
|
|
ID uuid.UUID // family_id
|
|
DeviceName *string
|
|
ClientType ClientType
|
|
CreatedAt time.Time // начало семьи (вход)
|
|
LastUsedAt time.Time // последняя ротация
|
|
}
|
|
|
|
// Principal — аутентифицированный пользователь запроса (из access-токена и актуальной строки users).
|
|
type Principal struct {
|
|
UserID uuid.UUID
|
|
Role Role
|
|
ManagesUpdates bool
|
|
ReviewsUploads bool
|
|
SessionID uuid.UUID
|
|
MustChangePassword bool
|
|
MustChangeCredentials bool
|
|
}
|
|
|
|
// AuditRecord — прочитанная запись audit_log с логином автора.
|
|
type AuditRecord struct {
|
|
ID uuid.UUID
|
|
ActorID *uuid.UUID
|
|
ActorUsername *string
|
|
Action string
|
|
TargetType *string
|
|
TargetID *uuid.UUID
|
|
Payload map[string]any
|
|
CreatedAt time.Time
|
|
}
|
|
|
|
// AuditEntry — запись audit_log.
|
|
type AuditEntry struct {
|
|
ActorID *uuid.UUID
|
|
Action string
|
|
TargetType string
|
|
TargetID *uuid.UUID
|
|
Payload map[string]any
|
|
}
|