mirror of
https://github.com/handsomezhuzhu/2fa-tool.git
synced 2026-04-18 22:32:53 +00:00
Merge pull request #4 from handsomezhuzhu/v0/kdaugh14-4907-13930268
Add GitHub link and optimize client-side library loading
This commit is contained in:
@@ -49,16 +49,16 @@ A pure frontend TOTP two-factor authentication tool with multiple token import m
|
|||||||
|
|
||||||
**构建配置:**
|
**构建配置:**
|
||||||
|
|
||||||
```
|
\`\`\`
|
||||||
安装命令:npm install
|
安装命令:npm install
|
||||||
构建命令:npm run build
|
构建命令:npm run build
|
||||||
静态资源目录:out
|
静态资源目录:out
|
||||||
Node.js 版本:20.x 或 22.x
|
Node.js 版本:20.x 或 22.x
|
||||||
```
|
\`\`\`
|
||||||
|
|
||||||
### 本地开发
|
### 本地开发
|
||||||
|
|
||||||
```bash
|
\`\`\`bash
|
||||||
# 安装依赖
|
# 安装依赖
|
||||||
npm install
|
npm install
|
||||||
|
|
||||||
@@ -67,7 +67,7 @@ npm run dev
|
|||||||
|
|
||||||
# 构建
|
# 构建
|
||||||
npm run build
|
npm run build
|
||||||
```
|
\`\`\`
|
||||||
|
|
||||||
## Security / 安全说明
|
## Security / 安全说明
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import type React from "react"
|
import type React from "react"
|
||||||
import type { Metadata } from "next"
|
import type { Metadata } from "next"
|
||||||
import { Geist, Geist_Mono } from "next/font/google"
|
import { Geist, Geist_Mono } from "next/font/google"
|
||||||
import { Analytics } from "@vercel/analytics/next"
|
|
||||||
import { ThemeProvider } from "@/components/theme-provider"
|
import { ThemeProvider } from "@/components/theme-provider"
|
||||||
import { LanguageProvider } from "@/lib/i18n"
|
import { LanguageProvider } from "@/lib/i18n"
|
||||||
import "./globals.css"
|
import "./globals.css"
|
||||||
@@ -36,7 +35,6 @@ export default function RootLayout({
|
|||||||
>
|
>
|
||||||
<LanguageProvider>{children}</LanguageProvider>
|
<LanguageProvider>{children}</LanguageProvider>
|
||||||
</ThemeProvider>
|
</ThemeProvider>
|
||||||
<Analytics />
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
)
|
)
|
||||||
|
|||||||
168
app/page.tsx
168
app/page.tsx
@@ -3,7 +3,6 @@
|
|||||||
import type React from "react"
|
import type React from "react"
|
||||||
|
|
||||||
import { useState, useEffect, useRef, useCallback } from "react"
|
import { useState, useEffect, useRef, useCallback } from "react"
|
||||||
import jsQR from "jsqr"
|
|
||||||
import {
|
import {
|
||||||
Plus,
|
Plus,
|
||||||
Camera,
|
Camera,
|
||||||
@@ -164,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)
|
||||||
@@ -450,7 +454,7 @@ export default function TwoFactorAuth() {
|
|||||||
|
|
||||||
if (!ctx) return
|
if (!ctx) return
|
||||||
|
|
||||||
const scan = () => {
|
const scan = async () => {
|
||||||
if (!streamRef.current) return
|
if (!streamRef.current) return
|
||||||
|
|
||||||
if (video.readyState === video.HAVE_ENOUGH_DATA) {
|
if (video.readyState === video.HAVE_ENOUGH_DATA) {
|
||||||
@@ -459,6 +463,7 @@ export default function TwoFactorAuth() {
|
|||||||
ctx.drawImage(video, 0, 0, canvas.width, canvas.height)
|
ctx.drawImage(video, 0, 0, canvas.width, canvas.height)
|
||||||
|
|
||||||
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height)
|
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height)
|
||||||
|
const jsQR = (await import("jsqr")).default
|
||||||
const code = jsQR(imageData.data, imageData.width, imageData.height, {
|
const code = jsQR(imageData.data, imageData.width, imageData.height, {
|
||||||
inversionAttempts: "dontInvert",
|
inversionAttempts: "dontInvert",
|
||||||
})
|
})
|
||||||
@@ -494,7 +499,7 @@ export default function TwoFactorAuth() {
|
|||||||
|
|
||||||
const img = new Image()
|
const img = new Image()
|
||||||
img.crossOrigin = "anonymous"
|
img.crossOrigin = "anonymous"
|
||||||
img.onload = () => {
|
img.onload = async () => {
|
||||||
const canvas = document.createElement("canvas")
|
const canvas = document.createElement("canvas")
|
||||||
canvas.width = img.width
|
canvas.width = img.width
|
||||||
canvas.height = img.height
|
canvas.height = img.height
|
||||||
@@ -503,6 +508,7 @@ export default function TwoFactorAuth() {
|
|||||||
|
|
||||||
ctx.drawImage(img, 0, 0)
|
ctx.drawImage(img, 0, 0)
|
||||||
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height)
|
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height)
|
||||||
|
const jsQR = (await import("jsqr")).default
|
||||||
const code = jsQR(imageData.data, imageData.width, imageData.height, {
|
const code = jsQR(imageData.data, imageData.width, imageData.height, {
|
||||||
inversionAttempts: "attemptBoth",
|
inversionAttempts: "attemptBoth",
|
||||||
})
|
})
|
||||||
@@ -559,33 +565,62 @@ 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)
|
||||||
|
|
||||||
|
// Dynamic import of crypto-js
|
||||||
|
const CryptoJS = (await import("crypto-js")).default
|
||||||
|
|
||||||
|
// Encrypt the data with password
|
||||||
|
const encrypted = CryptoJS.AES.encrypt(data, exportPassword).toString()
|
||||||
|
|
||||||
|
// Create blob and download
|
||||||
|
const blob = new Blob([encrypted], { type: "application/octet-stream" })
|
||||||
const url = URL.createObjectURL(blob)
|
const url = URL.createObjectURL(blob)
|
||||||
const a = document.createElement("a")
|
const a = document.createElement("a")
|
||||||
a.href = url
|
a.href = url
|
||||||
a.download = "2fa-tokens-backup.json"
|
a.download = "2fa-tokens-backup.enc"
|
||||||
a.click()
|
a.click()
|
||||||
URL.revokeObjectURL(url)
|
URL.revokeObjectURL(url)
|
||||||
|
|
||||||
|
setExportPassword("")
|
||||||
|
setShowExportPassword(false)
|
||||||
toast({
|
toast({
|
||||||
title: t.exportSuccess,
|
title: t.exportSuccess,
|
||||||
description: t.exportedJson,
|
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]
|
|
||||||
if (!file) return
|
|
||||||
|
|
||||||
const reader = new FileReader()
|
|
||||||
reader.onload = (e) => {
|
|
||||||
try {
|
try {
|
||||||
const imported = JSON.parse(e.target?.result as string)
|
const encryptedData = await file.text()
|
||||||
|
const CryptoJS = (await import("crypto-js")).default
|
||||||
|
|
||||||
|
// Decrypt the data with password
|
||||||
|
const decrypted = CryptoJS.AES.decrypt(encryptedData, importPassword).toString(
|
||||||
|
CryptoJS.enc.Utf8
|
||||||
|
)
|
||||||
|
|
||||||
|
if (!decrypted) {
|
||||||
|
throw new Error("Invalid password")
|
||||||
|
}
|
||||||
|
|
||||||
|
const imported = JSON.parse(decrypted)
|
||||||
if (Array.isArray(imported)) {
|
if (Array.isArray(imported)) {
|
||||||
setTokens([...tokens, ...imported])
|
setTokens([...tokens, ...imported])
|
||||||
|
setImportPassword("")
|
||||||
|
setImportFile(null)
|
||||||
|
setShowImportPassword(false)
|
||||||
toast({
|
toast({
|
||||||
title: t.importSuccess,
|
title: t.importSuccess,
|
||||||
description: `${t.added} ${imported.length} ${t.importedTokens}`,
|
description: `${t.added} ${imported.length} ${t.importedTokens}`,
|
||||||
@@ -594,13 +629,11 @@ export default function TwoFactorAuth() {
|
|||||||
} catch {
|
} catch {
|
||||||
toast({
|
toast({
|
||||||
title: t.importFailed,
|
title: t.importFailed,
|
||||||
description: t.invalidFormat,
|
description: "Invalid password or corrupted file",
|
||||||
variant: "destructive",
|
variant: "destructive",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
reader.readAsText(file)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Filter and sort tokens
|
// Filter and sort tokens
|
||||||
const filteredTokens = tokens
|
const filteredTokens = tokens
|
||||||
@@ -744,16 +777,13 @@ 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}
|
||||||
<input type="file" accept=".json" className="hidden" onChange={importTokens} />
|
|
||||||
</label>
|
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -1155,6 +1185,96 @@ export default function TwoFactorAuth() {
|
|||||||
</footer>
|
</footer>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{/* Export Password Dialog */}
|
||||||
|
<Dialog open={showExportPassword} onOpenChange={setShowExportPassword}>
|
||||||
|
<DialogContent>
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>Set Export Password</DialogTitle>
|
||||||
|
</DialogHeader>
|
||||||
|
<div className="space-y-4 py-4">
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>Password</Label>
|
||||||
|
<Input
|
||||||
|
type="password"
|
||||||
|
placeholder="Enter a password to protect your backup"
|
||||||
|
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>Import Backup</DialogTitle>
|
||||||
|
</DialogHeader>
|
||||||
|
<div className="space-y-4 py-4">
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label>Select File</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="Enter the password for this backup"
|
||||||
|
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>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ const translations = {
|
|||||||
parseFailed: "解析失败",
|
parseFailed: "解析失败",
|
||||||
invalidUri: "无效的 otpauth URI",
|
invalidUri: "无效的 otpauth URI",
|
||||||
exportSuccess: "导出成功",
|
exportSuccess: "导出成功",
|
||||||
exportedJson: "令牌已导出为 JSON 文件",
|
exportedJson: "令牌已导出为加密备份文件",
|
||||||
importSuccess: "导入成功",
|
importSuccess: "导入成功",
|
||||||
importedTokens: "个令牌",
|
importedTokens: "个令牌",
|
||||||
importFailed: "导入失败",
|
importFailed: "导入失败",
|
||||||
@@ -214,7 +214,7 @@ const translations = {
|
|||||||
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",
|
||||||
|
|||||||
@@ -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