mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2026-01-21 19:25:43 +00:00
Add TOTP-based two-factor authentication
- Add 2FA setup, enable, disable, and backup code management - Integrate 2FA challenge flow into login process - Add frontend modal for 2FA configuration - Support backup codes for account recovery
This commit is contained in:
@@ -1,9 +1,22 @@
|
||||
import * as api from "./base";
|
||||
import type { TokenResponse } from "./responseTypes";
|
||||
import type { TokenResponse, TwoFactorChallengeResponse } from "./responseTypes";
|
||||
|
||||
export async function getToken(identity: string, secret: string): Promise<TokenResponse> {
|
||||
export type LoginResponse = TokenResponse | TwoFactorChallengeResponse;
|
||||
|
||||
export function isTwoFactorChallenge(response: LoginResponse): response is TwoFactorChallengeResponse {
|
||||
return "requires2fa" in response && response.requires2fa === true;
|
||||
}
|
||||
|
||||
export async function getToken(identity: string, secret: string): Promise<LoginResponse> {
|
||||
return await api.post({
|
||||
url: "/tokens",
|
||||
data: { identity, secret },
|
||||
});
|
||||
}
|
||||
|
||||
export async function verify2FA(challengeToken: string, code: string): Promise<TokenResponse> {
|
||||
return await api.post({
|
||||
url: "/tokens/2fa",
|
||||
data: { challengeToken, code },
|
||||
});
|
||||
}
|
||||
|
||||
@@ -60,3 +60,4 @@ export * from "./updateStream";
|
||||
export * from "./updateUser";
|
||||
export * from "./uploadCertificate";
|
||||
export * from "./validateCertificate";
|
||||
export * from "./twoFactor";
|
||||
|
||||
@@ -25,3 +25,22 @@ export interface VersionCheckResponse {
|
||||
latest: string | null;
|
||||
updateAvailable: boolean;
|
||||
}
|
||||
|
||||
export interface TwoFactorChallengeResponse {
|
||||
requires2fa: boolean;
|
||||
challengeToken: string;
|
||||
}
|
||||
|
||||
export interface TwoFactorStatusResponse {
|
||||
enabled: boolean;
|
||||
backupCodesRemaining: number;
|
||||
}
|
||||
|
||||
export interface TwoFactorSetupResponse {
|
||||
secret: string;
|
||||
otpauthUrl: string;
|
||||
}
|
||||
|
||||
export interface TwoFactorEnableResponse {
|
||||
backupCodes: string[];
|
||||
}
|
||||
|
||||
58
frontend/src/api/backend/twoFactor.ts
Normal file
58
frontend/src/api/backend/twoFactor.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { camelizeKeys, decamelizeKeys } from "humps";
|
||||
import AuthStore from "src/modules/AuthStore";
|
||||
import type {
|
||||
TwoFactorEnableResponse,
|
||||
TwoFactorSetupResponse,
|
||||
TwoFactorStatusResponse,
|
||||
} from "./responseTypes";
|
||||
import * as api from "./base";
|
||||
|
||||
export async function get2FAStatus(userId: number | "me"): Promise<TwoFactorStatusResponse> {
|
||||
return await api.get({
|
||||
url: `/users/${userId}/2fa`,
|
||||
});
|
||||
}
|
||||
|
||||
export async function start2FASetup(userId: number | "me"): Promise<TwoFactorSetupResponse> {
|
||||
return await api.post({
|
||||
url: `/users/${userId}/2fa/setup`,
|
||||
});
|
||||
}
|
||||
|
||||
export async function enable2FA(userId: number | "me", code: string): Promise<TwoFactorEnableResponse> {
|
||||
return await api.put({
|
||||
url: `/users/${userId}/2fa/enable`,
|
||||
data: { code },
|
||||
});
|
||||
}
|
||||
|
||||
export async function disable2FA(userId: number | "me", code: string): Promise<{ success: boolean }> {
|
||||
const headers: Record<string, string> = {
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
if (AuthStore.token) {
|
||||
headers.Authorization = `Bearer ${AuthStore.token.token}`;
|
||||
}
|
||||
|
||||
const response = await fetch(`/api/users/${userId}/2fa`, {
|
||||
method: "DELETE",
|
||||
headers,
|
||||
body: JSON.stringify(decamelizeKeys({ code })),
|
||||
});
|
||||
|
||||
const payload = await response.json();
|
||||
if (!response.ok) {
|
||||
throw new Error(payload.error?.messageI18n || payload.error?.message || "Failed to disable 2FA");
|
||||
}
|
||||
return camelizeKeys(payload) as { success: boolean };
|
||||
}
|
||||
|
||||
export async function regenerateBackupCodes(
|
||||
userId: number | "me",
|
||||
code: string,
|
||||
): Promise<TwoFactorEnableResponse> {
|
||||
return await api.post({
|
||||
url: `/users/${userId}/2fa/backup-codes`,
|
||||
data: { code },
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user