mirror of
https://github.com/handsomezhuzhu/2fa-tool.git
synced 2026-04-18 22:32:53 +00:00
Compare commits
8 Commits
b1be9aa5a4
...
v0/kdaugh1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d17a0676a4 | ||
|
|
417771b93e | ||
|
|
9a3c12f69e | ||
|
|
bf4433a054 | ||
|
|
90779eecdd | ||
|
|
5d94133c64 | ||
|
|
2a97c30530 | ||
|
|
756a0c5be1 |
216
app/page.tsx
216
app/page.tsx
@@ -163,6 +163,11 @@ export default function TwoFactorAuth() {
|
|||||||
const [isSettingsOpen, setIsSettingsOpen] = useState(false)
|
const [isSettingsOpen, setIsSettingsOpen] = useState(false)
|
||||||
const [editingToken, setEditingToken] = useState<TOTPToken | null>(null)
|
const [editingToken, setEditingToken] = useState<TOTPToken | null>(null)
|
||||||
const [showAdvanced, setShowAdvanced] = useState(false)
|
const [showAdvanced, setShowAdvanced] = useState(false)
|
||||||
|
const [showExportPassword, setShowExportPassword] = useState(false)
|
||||||
|
const [exportPassword, setExportPassword] = useState("")
|
||||||
|
const [importPassword, setImportPassword] = useState("")
|
||||||
|
const [showImportPassword, setShowImportPassword] = useState(false)
|
||||||
|
const [importFile, setImportFile] = useState<File | null>(null)
|
||||||
const videoRef = useRef<HTMLVideoElement>(null)
|
const videoRef = useRef<HTMLVideoElement>(null)
|
||||||
const canvasRef = useRef<HTMLCanvasElement>(null)
|
const canvasRef = useRef<HTMLCanvasElement>(null)
|
||||||
const fileInputRef = useRef<HTMLInputElement>(null)
|
const fileInputRef = useRef<HTMLInputElement>(null)
|
||||||
@@ -560,47 +565,81 @@ export default function TwoFactorAuth() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Export tokens
|
// Export tokens with password encryption
|
||||||
const exportTokens = () => {
|
const exportTokens = async () => {
|
||||||
const data = JSON.stringify(tokens, null, 2)
|
try {
|
||||||
const blob = new Blob([data], { type: "application/json" })
|
const data = JSON.stringify(tokens)
|
||||||
const url = URL.createObjectURL(blob)
|
|
||||||
const a = document.createElement("a")
|
// Dynamic import of crypto-js
|
||||||
a.href = url
|
const CryptoJS = (await import("crypto-js")).default
|
||||||
a.download = "2fa-tokens-backup.json"
|
|
||||||
a.click()
|
// Encrypt the data with password
|
||||||
URL.revokeObjectURL(url)
|
const encrypted = CryptoJS.AES.encrypt(data, exportPassword).toString()
|
||||||
toast({
|
|
||||||
title: t.exportSuccess,
|
// Create blob and download
|
||||||
description: t.exportedJson,
|
const blob = new Blob([encrypted], { type: "application/octet-stream" })
|
||||||
})
|
const url = URL.createObjectURL(blob)
|
||||||
|
const a = document.createElement("a")
|
||||||
|
a.href = url
|
||||||
|
a.download = "2fa-tokens-backup.enc"
|
||||||
|
a.click()
|
||||||
|
URL.revokeObjectURL(url)
|
||||||
|
|
||||||
|
setExportPassword("")
|
||||||
|
setShowExportPassword(false)
|
||||||
|
toast({
|
||||||
|
title: t.exportSuccess,
|
||||||
|
description: t.exportedJson,
|
||||||
|
})
|
||||||
|
} catch {
|
||||||
|
toast({
|
||||||
|
title: t.error,
|
||||||
|
description: "Failed to export backup",
|
||||||
|
variant: "destructive",
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Import tokens
|
// Import tokens with password decryption
|
||||||
const importTokens = (event: React.ChangeEvent<HTMLInputElement>) => {
|
const importTokens = async (file: File) => {
|
||||||
const file = event.target.files?.[0]
|
try {
|
||||||
if (!file) return
|
const encryptedData = await file.text()
|
||||||
|
const CryptoJS = (await import("crypto-js")).default
|
||||||
|
|
||||||
const reader = new FileReader()
|
// Decrypt the data with password
|
||||||
reader.onload = (e) => {
|
const decrypted = CryptoJS.AES.decrypt(encryptedData, importPassword).toString(
|
||||||
try {
|
CryptoJS.enc.Utf8
|
||||||
const imported = JSON.parse(e.target?.result as string)
|
)
|
||||||
if (Array.isArray(imported)) {
|
|
||||||
setTokens([...tokens, ...imported])
|
if (!decrypted) {
|
||||||
toast({
|
throw new Error("Invalid password")
|
||||||
title: t.importSuccess,
|
}
|
||||||
description: `${t.added} ${imported.length} ${t.importedTokens}`,
|
|
||||||
})
|
const imported = JSON.parse(decrypted)
|
||||||
}
|
if (Array.isArray(imported)) {
|
||||||
} catch {
|
const existingSecrets = new Set(tokens.map((tk) => tk.secret.toUpperCase()))
|
||||||
|
const newTokens = (imported as TOTPToken[]).filter(
|
||||||
|
(tk) => !existingSecrets.has(tk.secret.toUpperCase())
|
||||||
|
)
|
||||||
|
const skipped = imported.length - newTokens.length
|
||||||
|
setTokens((prev) => [...prev, ...newTokens])
|
||||||
|
setImportPassword("")
|
||||||
|
setImportFile(null)
|
||||||
|
setShowImportPassword(false)
|
||||||
toast({
|
toast({
|
||||||
title: t.importFailed,
|
title: t.importSuccess,
|
||||||
description: t.invalidFormat,
|
description: skipped > 0
|
||||||
variant: "destructive",
|
? `${t.added} ${newTokens.length} ${t.importedTokens},已跳过 ${skipped} 个重复令牌`
|
||||||
|
: `${t.added} ${newTokens.length} ${t.importedTokens}`,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
} catch {
|
||||||
|
toast({
|
||||||
|
title: t.importFailed,
|
||||||
|
description: "Invalid password or corrupted file",
|
||||||
|
variant: "destructive",
|
||||||
|
})
|
||||||
}
|
}
|
||||||
reader.readAsText(file)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter and sort tokens
|
// Filter and sort tokens
|
||||||
@@ -745,17 +784,14 @@ export default function TwoFactorAuth() {
|
|||||||
<div className="border-t pt-4 space-y-3">
|
<div className="border-t pt-4 space-y-3">
|
||||||
<Label>{t.dataManagement}</Label>
|
<Label>{t.dataManagement}</Label>
|
||||||
<div className="flex gap-2">
|
<div className="flex gap-2">
|
||||||
<Button variant="outline" size="sm" onClick={exportTokens}>
|
<Button variant="outline" size="sm" onClick={() => setShowExportPassword(true)}>
|
||||||
<Download className="h-4 w-4 mr-2" />
|
<Download className="h-4 w-4 mr-2" />
|
||||||
{t.exportBackup}
|
{t.exportBackup}
|
||||||
</Button>
|
</Button>
|
||||||
<Button variant="outline" size="sm" asChild>
|
<Button variant="outline" size="sm" onClick={() => setShowImportPassword(true)}>
|
||||||
<label>
|
<Upload className="h-4 w-4 mr-2" />
|
||||||
<Upload className="h-4 w-4 mr-2" />
|
{t.importBackup}
|
||||||
{t.importBackup}
|
</Button>
|
||||||
<input type="file" accept=".json" className="hidden" onChange={importTokens} />
|
|
||||||
</label>
|
|
||||||
</Button>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -1156,6 +1192,96 @@ export default function TwoFactorAuth() {
|
|||||||
</footer>
|
</footer>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{/* Export Password Dialog */}
|
||||||
|
<Dialog open={showExportPassword} onOpenChange={setShowExportPassword}>
|
||||||
|
<DialogContent>
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>{t.setExportPassword}</DialogTitle>
|
||||||
|
</DialogHeader>
|
||||||
|
<div className="space-y-4 py-4">
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>Password</Label>
|
||||||
|
<Input
|
||||||
|
type="password"
|
||||||
|
placeholder={t.passwordPlaceholder}
|
||||||
|
value={exportPassword}
|
||||||
|
onChange={(e) => setExportPassword(e.target.value)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<DialogFooter>
|
||||||
|
<Button variant="outline" onClick={() => setShowExportPassword(false)}>
|
||||||
|
{t.cancel}
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
onClick={exportTokens}
|
||||||
|
disabled={!exportPassword}
|
||||||
|
>
|
||||||
|
<Download className="h-4 w-4 mr-2" />
|
||||||
|
{t.exportBackup}
|
||||||
|
</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
|
||||||
|
{/* Import Password Dialog */}
|
||||||
|
<Dialog open={showImportPassword} onOpenChange={(open) => {
|
||||||
|
if (!open) {
|
||||||
|
setShowImportPassword(false)
|
||||||
|
setImportFile(null)
|
||||||
|
setImportPassword("")
|
||||||
|
} else {
|
||||||
|
setShowImportPassword(true)
|
||||||
|
}
|
||||||
|
}}>
|
||||||
|
<DialogContent>
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>{t.importBackup}</DialogTitle>
|
||||||
|
</DialogHeader>
|
||||||
|
<div className="space-y-4 py-4">
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>{t.selectFile}</Label>
|
||||||
|
<Input
|
||||||
|
type="file"
|
||||||
|
accept=".enc"
|
||||||
|
onChange={(e) => {
|
||||||
|
setImportFile(e.target.files?.[0] || null)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>Password</Label>
|
||||||
|
<Input
|
||||||
|
type="password"
|
||||||
|
placeholder={t.passwordInput}
|
||||||
|
value={importPassword}
|
||||||
|
onChange={(e) => setImportPassword(e.target.value)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<DialogFooter>
|
||||||
|
<Button variant="outline" onClick={() => {
|
||||||
|
setShowImportPassword(false)
|
||||||
|
setImportFile(null)
|
||||||
|
setImportPassword("")
|
||||||
|
}}>
|
||||||
|
{t.cancel}
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
onClick={() => {
|
||||||
|
if (importFile) {
|
||||||
|
importTokens(importFile)
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
disabled={!importPassword || !importFile}
|
||||||
|
>
|
||||||
|
<Upload className="h-4 w-4 mr-2" />
|
||||||
|
{t.importBackup}
|
||||||
|
</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
|
||||||
<Toaster />
|
<Toaster />
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,12 +1,101 @@
|
|||||||
"use client"
|
"use client"
|
||||||
|
|
||||||
import type * as React from "react"
|
import * as React from "react"
|
||||||
import { ThemeProvider as NextThemesProvider } from "next-themes"
|
import { createContext, useContext, useEffect, useState } from "react"
|
||||||
|
|
||||||
export { useTheme } from "next-themes"
|
type Theme = "dark" | "light" | "system"
|
||||||
|
|
||||||
type ThemeProviderProps = React.ComponentProps<typeof NextThemesProvider>
|
type ThemeProviderProps = {
|
||||||
|
children: React.ReactNode
|
||||||
export function ThemeProvider({ children, ...props }: ThemeProviderProps) {
|
defaultTheme?: Theme
|
||||||
return <NextThemesProvider {...props}>{children}</NextThemesProvider>
|
storageKey?: string
|
||||||
|
attribute?: string
|
||||||
|
enableSystem?: boolean
|
||||||
|
disableTransitionOnChange?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
type ThemeProviderState = {
|
||||||
|
theme: Theme
|
||||||
|
setTheme: (theme: Theme) => void
|
||||||
|
resolvedTheme?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const ThemeProviderContext = createContext<ThemeProviderState | undefined>(undefined)
|
||||||
|
|
||||||
|
export function ThemeProvider({
|
||||||
|
children,
|
||||||
|
defaultTheme = "system",
|
||||||
|
storageKey = "theme",
|
||||||
|
...props
|
||||||
|
}: ThemeProviderProps) {
|
||||||
|
const [theme, setTheme] = useState<Theme>(defaultTheme)
|
||||||
|
const [resolvedTheme, setResolvedTheme] = useState<string | undefined>(undefined)
|
||||||
|
const [mounted, setMounted] = useState(false)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setMounted(true)
|
||||||
|
const stored = localStorage.getItem(storageKey) as Theme | null
|
||||||
|
if (stored) {
|
||||||
|
setTheme(stored)
|
||||||
|
}
|
||||||
|
}, [storageKey])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!mounted) return
|
||||||
|
|
||||||
|
const root = window.document.documentElement
|
||||||
|
|
||||||
|
root.classList.remove("light", "dark")
|
||||||
|
|
||||||
|
let resolved: string
|
||||||
|
if (theme === "system") {
|
||||||
|
resolved = window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"
|
||||||
|
} else {
|
||||||
|
resolved = theme
|
||||||
|
}
|
||||||
|
|
||||||
|
root.classList.add(resolved)
|
||||||
|
setResolvedTheme(resolved)
|
||||||
|
}, [theme, mounted])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!mounted || theme !== "system") return
|
||||||
|
|
||||||
|
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)")
|
||||||
|
const handleChange = () => {
|
||||||
|
const resolved = mediaQuery.matches ? "dark" : "light"
|
||||||
|
const root = window.document.documentElement
|
||||||
|
root.classList.remove("light", "dark")
|
||||||
|
root.classList.add(resolved)
|
||||||
|
setResolvedTheme(resolved)
|
||||||
|
}
|
||||||
|
|
||||||
|
mediaQuery.addEventListener("change", handleChange)
|
||||||
|
return () => mediaQuery.removeEventListener("change", handleChange)
|
||||||
|
}, [theme, mounted])
|
||||||
|
|
||||||
|
const value = {
|
||||||
|
theme,
|
||||||
|
setTheme: (newTheme: Theme) => {
|
||||||
|
localStorage.setItem(storageKey, newTheme)
|
||||||
|
setTheme(newTheme)
|
||||||
|
},
|
||||||
|
resolvedTheme,
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ThemeProviderContext.Provider value={value}>
|
||||||
|
{children}
|
||||||
|
</ThemeProviderContext.Provider>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useTheme() {
|
||||||
|
const context = useContext(ThemeProviderContext)
|
||||||
|
|
||||||
|
if (context === undefined) {
|
||||||
|
throw new Error("useTheme must be used within a ThemeProvider")
|
||||||
|
}
|
||||||
|
|
||||||
|
return context
|
||||||
}
|
}
|
||||||
|
|||||||
16
lib/i18n.tsx
16
lib/i18n.tsx
@@ -102,8 +102,8 @@ const translations = {
|
|||||||
extractedInfo: "已从 URI 中提取信息",
|
extractedInfo: "已从 URI 中提取信息",
|
||||||
parseFailed: "解析失败",
|
parseFailed: "解析失败",
|
||||||
invalidUri: "无效的 otpauth URI",
|
invalidUri: "无效的 otpauth URI",
|
||||||
exportSuccess: "导出成功",
|
exportSuccess: "导出成功",
|
||||||
exportedJson: "令牌已导出为 JSON 文件",
|
exportedJson: "令牌已导出为加密备份文件",
|
||||||
importSuccess: "导入成功",
|
importSuccess: "导入成功",
|
||||||
importedTokens: "个令牌",
|
importedTokens: "个令牌",
|
||||||
importFailed: "导入失败",
|
importFailed: "导入失败",
|
||||||
@@ -116,6 +116,10 @@ const translations = {
|
|||||||
imageLoadFailed: "图片加载失败",
|
imageLoadFailed: "图片加载失败",
|
||||||
duplicateToken: "令牌已存在",
|
duplicateToken: "令牌已存在",
|
||||||
duplicateTokenDesc: "该密钥的令牌已添加过",
|
duplicateTokenDesc: "该密钥的令牌已添加过",
|
||||||
|
setExportPassword: "设置导出密码",
|
||||||
|
passwordPlaceholder: "输入密码以保护您的备份",
|
||||||
|
selectFile: "选择文件",
|
||||||
|
passwordInput: "输入备份密码",
|
||||||
},
|
},
|
||||||
en: {
|
en: {
|
||||||
// Header
|
// Header
|
||||||
@@ -213,8 +217,8 @@ const translations = {
|
|||||||
extractedInfo: "Extracted info from URI",
|
extractedInfo: "Extracted info from URI",
|
||||||
parseFailed: "Parse failed",
|
parseFailed: "Parse failed",
|
||||||
invalidUri: "Invalid otpauth URI",
|
invalidUri: "Invalid otpauth URI",
|
||||||
exportSuccess: "Export successful",
|
exportSuccess: "Export successful",
|
||||||
exportedJson: "Tokens exported as JSON file",
|
exportedJson: "Tokens exported as encrypted backup file",
|
||||||
importSuccess: "Import successful",
|
importSuccess: "Import successful",
|
||||||
importedTokens: "tokens",
|
importedTokens: "tokens",
|
||||||
importFailed: "Import failed",
|
importFailed: "Import failed",
|
||||||
@@ -227,6 +231,10 @@ const translations = {
|
|||||||
imageLoadFailed: "Failed to load image",
|
imageLoadFailed: "Failed to load image",
|
||||||
duplicateToken: "Token already exists",
|
duplicateToken: "Token already exists",
|
||||||
duplicateTokenDesc: "A token with this secret key has already been added",
|
duplicateTokenDesc: "A token with this secret key has already been added",
|
||||||
|
setExportPassword: "Set Export Password",
|
||||||
|
passwordPlaceholder: "Enter a password to protect your backup",
|
||||||
|
selectFile: "Select File",
|
||||||
|
passwordInput: "Enter backup password",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,6 +42,7 @@
|
|||||||
"class-variance-authority": "^0.7.1",
|
"class-variance-authority": "^0.7.1",
|
||||||
"clsx": "^2.1.1",
|
"clsx": "^2.1.1",
|
||||||
"cmdk": "1.0.4",
|
"cmdk": "1.0.4",
|
||||||
|
"crypto-js": "4.2.0",
|
||||||
"date-fns": "4.1.0",
|
"date-fns": "4.1.0",
|
||||||
"embla-carousel-react": "8.5.1",
|
"embla-carousel-react": "8.5.1",
|
||||||
"input-otp": "1.4.1",
|
"input-otp": "1.4.1",
|
||||||
|
|||||||
1331
pnpm-lock.yaml
generated
1331
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user