feat(auth): двухфакторная аутентификация (TOTP) для входа в панель
All checks were successful
Backend Tests / pytest (Python 3.12) (push) Successful in 58s
Deploy to Production / Trigger production update (push) Successful in 8s

Бэкенд:
- two_factor_service: генерация секрета, otpauth-URI, QR как inline-SVG,
  одноразовые backup-коды (хранятся как SHA-256)
- эндпоинты /api/auth/2fa/{status,setup,enable,disable}; гейт 2FA в login
  (неверный код -> 400, чтобы SPA не сбрасывала форму); two_factor_enabled в /me
- админский сброс: POST /api/users/{id}/disable-2fa
- модель/схемы/init.sql + идемпотентная миграция №30 (two_factor_backup_codes)
- зависимости: pyotp, qrcode

Фронтенд:
- двухшаговый вход с полем кода (TOTP/backup)
- карточка 2FA в профиле: QR, проверка, backup-коды, отключение
- бейдж/сброс 2FA в списке пользователей; локали ru/en

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
markmorado
2026-07-06 21:08:38 +05:00
parent fc42e8fc19
commit bec7b20e31
16 changed files with 671 additions and 64 deletions

View File

@@ -9,7 +9,7 @@ interface AuthState {
isLoading: boolean;
error: string | null;
login: (credentials: LoginRequest) => Promise<void>;
login: (credentials: LoginRequest) => Promise<{ twoFactorRequired: boolean }>;
logout: () => Promise<void>;
loadUser: () => Promise<void>;
clearError: () => void;
@@ -26,12 +26,20 @@ export const useAuthStore = create<AuthState>((set) => ({
set({ isLoading: true, error: null });
try {
const data = await apiClient.post<AuthResponse>('/api/auth/login', credentials);
// Password accepted but a second factor is still required.
if (data.two_factor_required || !data.access_token) {
set({ isLoading: false });
return { twoFactorRequired: true };
}
apiClient.setToken(data.access_token);
set({ token: data.access_token, isAuthenticated: true, isLoading: false });
// Load user info
const user = await apiClient.get<User>('/api/auth/me');
set({ user });
return { twoFactorRequired: false };
} catch (err) {
const message = err instanceof Error ? err.message : 'Login failed';
set({ error: message, isLoading: false, isAuthenticated: false });