From 20ceb91db8405172d5beb9df646d3d912cea40a5 Mon Sep 17 00:00:00 2001
From: whoswhip
Date: Wed, 7 Jan 2026 16:36:41 -0500
Subject: [PATCH] feat: totp 2fa; httpcontext extension for jwtuser; asp.net
data protection; prevent banned users from certain actions; account deletion
requires 2fa if enabled; making users admins requires 2fa if enabled;
httpcontext extension to get ip; enhance user store
refactor: organize dtos by domain; verification providers use verification context; move ip parsing into http context extensions
chore: clean code
---
SharpbinV3.Client/package-lock.json | 281 +++++++++++++++++-
SharpbinV3.Client/package.json | 4 +-
.../src/lib/components/Modal.svelte | 259 +++++++++++++++-
SharpbinV3.Client/src/lib/stores/user.ts | 4 +-
SharpbinV3.Client/src/lib/types/user.ts | 4 +
SharpbinV3.Client/src/lib/utils/totp.ts | 18 ++
SharpbinV3.Client/src/routes/+layout.svelte | 4 +-
SharpbinV3.Client/src/routes/+page.svelte | 17 +-
.../src/routes/login/+page.svelte | 34 ++-
.../src/routes/register/+page.svelte | 5 +
.../src/routes/user/[username]/+page.svelte | 126 +++++++-
SharpbinV3.Server/.gitignore | 1 +
.../Authorization/NotBannedHandler.cs | 29 ++
.../Authorization/NotBannedRequirement.cs | 8 +
.../Controllers/AuthController.cs | 118 +++++++-
.../Controllers/PasteController.cs | 56 ++--
.../Controllers/UserController.cs | 82 +++--
.../DTOs/{JWTResult.cs => Auth/CreateJWT.cs} | 4 +-
SharpbinV3.Server/DTOs/Auth/DisableTotpDto.cs | 7 +
SharpbinV3.Server/DTOs/Auth/EnableTotpDto.cs | 8 +
.../RefreshTokenDto.cs} | 4 +-
.../TurnstileResponseDto.cs} | 4 +-
.../UpdatePasteDto.cs} | 4 +-
SharpbinV3.Server/DTOs/User/DeleteUserDto.cs | 8 +
.../{LoginRequest.cs => User/LoginUserDto.cs} | 5 +-
.../RegisterUserDto.cs} | 2 +-
.../UpdateUserDto.cs} | 3 +-
SharpbinV3.Server/Data/AppDbContext.cs | 7 +
SharpbinV3.Server/Data/Entities/User.cs | 1 +
SharpbinV3.Server/Data/Entities/UserTotp.cs | 13 +
.../Extensions/HttpContextExtensions.cs | 68 +++++
.../20260106211303_NullableAuthorUUID.cs | 3 +-
.../20260107181640_UserTotps.Designer.cs | 221 ++++++++++++++
.../Migrations/20260107181640_UserTotps.cs | 40 +++
.../Migrations/AppDbContextModelSnapshot.cs | 32 +-
SharpbinV3.Server/Program.cs | 33 +-
SharpbinV3.Server/Services/AuthService.cs | 38 ++-
.../Providers/IVerificationProvider.cs | 2 +-
.../Providers/TotpVerificationProvider.cs | 73 +++++
.../TurnstileVerificationProvider.cs | 14 +-
.../Verification/VerificationContext.cs | 12 +
.../Verification/VerificationService.cs | 11 +-
SharpbinV3.Server/SharpbinV3.Server.csproj | 15 +-
SharpbinV3.Server/SharpbinV3.Server.sln | 24 ++
SharpbinV3.Server/Utilities.cs | 23 --
docker-compose.yml | 1 +
46 files changed, 1568 insertions(+), 162 deletions(-)
create mode 100644 SharpbinV3.Client/src/lib/utils/totp.ts
create mode 100644 SharpbinV3.Server/.gitignore
create mode 100644 SharpbinV3.Server/Authorization/NotBannedHandler.cs
create mode 100644 SharpbinV3.Server/Authorization/NotBannedRequirement.cs
rename SharpbinV3.Server/DTOs/{JWTResult.cs => Auth/CreateJWT.cs} (74%)
create mode 100644 SharpbinV3.Server/DTOs/Auth/DisableTotpDto.cs
create mode 100644 SharpbinV3.Server/DTOs/Auth/EnableTotpDto.cs
rename SharpbinV3.Server/DTOs/{RefreshTokenRequest.cs => Auth/RefreshTokenDto.cs} (74%)
rename SharpbinV3.Server/DTOs/{TurnstileResponse.cs => Auth/TurnstileResponseDto.cs} (75%)
rename SharpbinV3.Server/DTOs/{PasteMetadataUpdateRequest.cs => Paste/UpdatePasteDto.cs} (70%)
create mode 100644 SharpbinV3.Server/DTOs/User/DeleteUserDto.cs
rename SharpbinV3.Server/DTOs/{LoginRequest.cs => User/LoginUserDto.cs} (74%)
rename SharpbinV3.Server/DTOs/{RegisterRequest.cs => User/RegisterUserDto.cs} (98%)
rename SharpbinV3.Server/DTOs/{UpdateUserRequest.cs => User/UpdateUserDto.cs} (96%)
create mode 100644 SharpbinV3.Server/Data/Entities/UserTotp.cs
create mode 100644 SharpbinV3.Server/Extensions/HttpContextExtensions.cs
create mode 100644 SharpbinV3.Server/Migrations/20260107181640_UserTotps.Designer.cs
create mode 100644 SharpbinV3.Server/Migrations/20260107181640_UserTotps.cs
create mode 100644 SharpbinV3.Server/Services/Verification/Providers/TotpVerificationProvider.cs
create mode 100644 SharpbinV3.Server/Services/Verification/VerificationContext.cs
create mode 100644 SharpbinV3.Server/SharpbinV3.Server.sln
diff --git a/SharpbinV3.Client/package-lock.json b/SharpbinV3.Client/package-lock.json
index af65b20..a307ccb 100644
--- a/SharpbinV3.Client/package-lock.json
+++ b/SharpbinV3.Client/package-lock.json
@@ -10,7 +10,8 @@
"dependencies": {
"@lucide/svelte": "^0.562.0",
"@sveltejs/adapter-node": "^5.4.0",
- "highlight.js": "^11.8.0"
+ "highlight.js": "^11.8.0",
+ "qrcode": "^1.5.4"
},
"devDependencies": {
"@eslint/compat": "^1.4.0",
@@ -20,6 +21,7 @@
"@sveltejs/vite-plugin-svelte": "^6.2.1",
"@tailwindcss/vite": "^4.1.17",
"@types/node": "^22",
+ "@types/qrcode": "^1.5.6",
"eslint": "^9.39.1",
"eslint-config-prettier": "^10.1.8",
"eslint-plugin-svelte": "^3.13.1",
@@ -1544,6 +1546,16 @@
"undici-types": "~6.21.0"
}
},
+ "node_modules/@types/qrcode": {
+ "version": "1.5.6",
+ "resolved": "https://registry.npmjs.org/@types/qrcode/-/qrcode-1.5.6.tgz",
+ "integrity": "sha512-te7NQcV2BOvdj2b1hCAHzAoMNuj65kNBMz0KBaxM6c3VGBOhU0dURQKOtH8CFNI/dsKkwlv32p26qYQTWoB5bw==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "@types/node": "*"
+ }
+ },
"node_modules/@types/resolve": {
"version": "1.20.2",
"resolved": "https://registry.npmjs.org/@types/resolve/-/resolve-1.20.2.tgz",
@@ -1845,11 +1857,19 @@
"url": "https://github.com/sponsors/epoberezkin"
}
},
+ "node_modules/ansi-regex": {
+ "version": "5.0.1",
+ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz",
+ "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==",
+ "license": "MIT",
+ "engines": {
+ "node": ">=8"
+ }
+ },
"node_modules/ansi-styles": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
"integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
- "dev": true,
"license": "MIT",
"dependencies": {
"color-convert": "^2.0.1"
@@ -1914,6 +1934,15 @@
"node": ">=6"
}
},
+ "node_modules/camelcase": {
+ "version": "5.3.1",
+ "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz",
+ "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==",
+ "license": "MIT",
+ "engines": {
+ "node": ">=6"
+ }
+ },
"node_modules/chalk": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
@@ -1947,6 +1976,17 @@
"url": "https://paulmillr.com/funding/"
}
},
+ "node_modules/cliui": {
+ "version": "6.0.0",
+ "resolved": "https://registry.npmjs.org/cliui/-/cliui-6.0.0.tgz",
+ "integrity": "sha512-t6wbgtoCXvAzst7QgXxJYqPt0usEfbgQdftEPbLL/cvv6HPE5VgvqCuAIDR0NgU52ds6rFwqrgakNLrHEjCbrQ==",
+ "license": "ISC",
+ "dependencies": {
+ "string-width": "^4.2.0",
+ "strip-ansi": "^6.0.0",
+ "wrap-ansi": "^6.2.0"
+ }
+ },
"node_modules/clsx": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/clsx/-/clsx-2.1.1.tgz",
@@ -1960,7 +2000,6 @@
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
"integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
- "dev": true,
"license": "MIT",
"dependencies": {
"color-name": "~1.1.4"
@@ -1973,7 +2012,6 @@
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
- "dev": true,
"license": "MIT"
},
"node_modules/commondir": {
@@ -2043,6 +2081,15 @@
}
}
},
+ "node_modules/decamelize": {
+ "version": "1.2.0",
+ "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz",
+ "integrity": "sha512-z2S+W9X73hAUUki+N+9Za2lBlun89zigOyGrsax+KUQ6wKW4ZoWpEYBkGhQjwAjjDCkWxhY0VKEhk8wzY7F5cA==",
+ "license": "MIT",
+ "engines": {
+ "node": ">=0.10.0"
+ }
+ },
"node_modules/deep-is": {
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/deep-is/-/deep-is-0.1.4.tgz",
@@ -2075,6 +2122,18 @@
"integrity": "sha512-jDwizj+IlEZBunHcOuuFVBnIMPAEHvTsJj0BcIp94xYguLRVBcXO853px/MyIJvbVzWdsGvrRweIUWJw8hBP7A==",
"license": "MIT"
},
+ "node_modules/dijkstrajs": {
+ "version": "1.0.3",
+ "resolved": "https://registry.npmjs.org/dijkstrajs/-/dijkstrajs-1.0.3.tgz",
+ "integrity": "sha512-qiSlmBq9+BCdCA/L46dw8Uy93mloxsPSbwnm5yrKn2vMPiy8KyAskTF6zuV/j5BMsmOGZDPs7KjU+mjb670kfA==",
+ "license": "MIT"
+ },
+ "node_modules/emoji-regex": {
+ "version": "8.0.0",
+ "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz",
+ "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==",
+ "license": "MIT"
+ },
"node_modules/enhanced-resolve": {
"version": "5.18.4",
"resolved": "https://registry.npmjs.org/enhanced-resolve/-/enhanced-resolve-5.18.4.tgz",
@@ -2480,6 +2539,15 @@
"url": "https://github.com/sponsors/ljharb"
}
},
+ "node_modules/get-caller-file": {
+ "version": "2.0.5",
+ "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz",
+ "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==",
+ "license": "ISC",
+ "engines": {
+ "node": "6.* || 8.* || >= 10.*"
+ }
+ },
"node_modules/glob-parent": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-6.0.2.tgz",
@@ -2606,6 +2674,15 @@
"node": ">=0.10.0"
}
},
+ "node_modules/is-fullwidth-code-point": {
+ "version": "3.0.0",
+ "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz",
+ "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==",
+ "license": "MIT",
+ "engines": {
+ "node": ">=8"
+ }
+ },
"node_modules/is-glob": {
"version": "4.0.3",
"resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz",
@@ -3135,6 +3212,15 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
+ "node_modules/p-try": {
+ "version": "2.2.0",
+ "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz",
+ "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==",
+ "license": "MIT",
+ "engines": {
+ "node": ">=6"
+ }
+ },
"node_modules/parent-module": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/parent-module/-/parent-module-1.0.1.tgz",
@@ -3152,7 +3238,6 @@
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz",
"integrity": "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==",
- "dev": true,
"license": "MIT",
"engines": {
"node": ">=8"
@@ -3192,6 +3277,15 @@
"url": "https://github.com/sponsors/jonschlinkert"
}
},
+ "node_modules/pngjs": {
+ "version": "5.0.0",
+ "resolved": "https://registry.npmjs.org/pngjs/-/pngjs-5.0.0.tgz",
+ "integrity": "sha512-40QW5YalBNfQo5yRYmiw7Yz6TKKVr3h6970B2YE+3fQpsWcrbj1PzJgxeJ19DRQjhMbKPIuMY8rFaXc8moolVw==",
+ "license": "MIT",
+ "engines": {
+ "node": ">=10.13.0"
+ }
+ },
"node_modules/postcss": {
"version": "8.5.6",
"resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.6.tgz",
@@ -3454,6 +3548,23 @@
"node": ">=6"
}
},
+ "node_modules/qrcode": {
+ "version": "1.5.4",
+ "resolved": "https://registry.npmjs.org/qrcode/-/qrcode-1.5.4.tgz",
+ "integrity": "sha512-1ca71Zgiu6ORjHqFBDpnSMTR2ReToX4l1Au1VFLyVeBTFavzQnv5JxMFr3ukHVKpSrSA2MCk0lNJSykjUfz7Zg==",
+ "license": "MIT",
+ "dependencies": {
+ "dijkstrajs": "^1.0.1",
+ "pngjs": "^5.0.0",
+ "yargs": "^15.3.1"
+ },
+ "bin": {
+ "qrcode": "bin/qrcode"
+ },
+ "engines": {
+ "node": ">=10.13.0"
+ }
+ },
"node_modules/readdirp": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/readdirp/-/readdirp-4.1.2.tgz",
@@ -3468,6 +3579,21 @@
"url": "https://paulmillr.com/funding/"
}
},
+ "node_modules/require-directory": {
+ "version": "2.1.1",
+ "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz",
+ "integrity": "sha512-fGxEI7+wsG9xrvdjsrlmL22OMTTiHRwAMroiEeMgq8gzoLC/PQr7RsRDSTLUg/bZAZtF+TVIkHc6/4RIKrui+Q==",
+ "license": "MIT",
+ "engines": {
+ "node": ">=0.10.0"
+ }
+ },
+ "node_modules/require-main-filename": {
+ "version": "2.0.0",
+ "resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-2.0.0.tgz",
+ "integrity": "sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==",
+ "license": "ISC"
+ },
"node_modules/resolve": {
"version": "1.22.11",
"resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.11.tgz",
@@ -3564,6 +3690,12 @@
"node": ">=10"
}
},
+ "node_modules/set-blocking": {
+ "version": "2.0.0",
+ "resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz",
+ "integrity": "sha512-KiKBS8AnWGEyLzofFfmvKwpdPzqiy16LvQfK3yv/fVH7Bj13/wl3JSR1J+rfgRE9q7xUJK4qvgS8raSOeLUehw==",
+ "license": "ISC"
+ },
"node_modules/set-cookie-parser": {
"version": "2.7.2",
"resolved": "https://registry.npmjs.org/set-cookie-parser/-/set-cookie-parser-2.7.2.tgz",
@@ -3616,6 +3748,32 @@
"node": ">=0.10.0"
}
},
+ "node_modules/string-width": {
+ "version": "4.2.3",
+ "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz",
+ "integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==",
+ "license": "MIT",
+ "dependencies": {
+ "emoji-regex": "^8.0.0",
+ "is-fullwidth-code-point": "^3.0.0",
+ "strip-ansi": "^6.0.1"
+ },
+ "engines": {
+ "node": ">=8"
+ }
+ },
+ "node_modules/strip-ansi": {
+ "version": "6.0.1",
+ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz",
+ "integrity": "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==",
+ "license": "MIT",
+ "dependencies": {
+ "ansi-regex": "^5.0.1"
+ },
+ "engines": {
+ "node": ">=8"
+ }
+ },
"node_modules/strip-json-comments": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz",
@@ -3977,6 +4135,12 @@
"node": ">= 8"
}
},
+ "node_modules/which-module": {
+ "version": "2.0.1",
+ "resolved": "https://registry.npmjs.org/which-module/-/which-module-2.0.1.tgz",
+ "integrity": "sha512-iBdZ57RDvnOR9AGBhML2vFZf7h8vmBjhoaZqODJBFWHVtKkDmKuHai3cx5PgVMrX5YDNp27AofYbAwctSS+vhQ==",
+ "license": "ISC"
+ },
"node_modules/word-wrap": {
"version": "1.2.5",
"resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.5.tgz",
@@ -3987,6 +4151,113 @@
"node": ">=0.10.0"
}
},
+ "node_modules/wrap-ansi": {
+ "version": "6.2.0",
+ "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-6.2.0.tgz",
+ "integrity": "sha512-r6lPcBGxZXlIcymEu7InxDMhdW0KDxpLgoFLcguasxCaJ/SOIZwINatK9KY/tf+ZrlywOKU0UDj3ATXUBfxJXA==",
+ "license": "MIT",
+ "dependencies": {
+ "ansi-styles": "^4.0.0",
+ "string-width": "^4.1.0",
+ "strip-ansi": "^6.0.0"
+ },
+ "engines": {
+ "node": ">=8"
+ }
+ },
+ "node_modules/y18n": {
+ "version": "4.0.3",
+ "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.3.tgz",
+ "integrity": "sha512-JKhqTOwSrqNA1NY5lSztJ1GrBiUodLMmIZuLiDaMRJ+itFd+ABVE8XBjOvIWL+rSqNDC74LCSFmlb/U4UZ4hJQ==",
+ "license": "ISC"
+ },
+ "node_modules/yargs": {
+ "version": "15.4.1",
+ "resolved": "https://registry.npmjs.org/yargs/-/yargs-15.4.1.tgz",
+ "integrity": "sha512-aePbxDmcYW++PaqBsJ+HYUFwCdv4LVvdnhBy78E57PIor8/OVvhMrADFFEDh8DHDFRv/O9i3lPhsENjO7QX0+A==",
+ "license": "MIT",
+ "dependencies": {
+ "cliui": "^6.0.0",
+ "decamelize": "^1.2.0",
+ "find-up": "^4.1.0",
+ "get-caller-file": "^2.0.1",
+ "require-directory": "^2.1.1",
+ "require-main-filename": "^2.0.0",
+ "set-blocking": "^2.0.0",
+ "string-width": "^4.2.0",
+ "which-module": "^2.0.0",
+ "y18n": "^4.0.0",
+ "yargs-parser": "^18.1.2"
+ },
+ "engines": {
+ "node": ">=8"
+ }
+ },
+ "node_modules/yargs-parser": {
+ "version": "18.1.3",
+ "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-18.1.3.tgz",
+ "integrity": "sha512-o50j0JeToy/4K6OZcaQmW6lyXXKhq7csREXcDwk2omFPJEwUNOVtJKvmDr9EI1fAJZUyZcRF7kxGBWmRXudrCQ==",
+ "license": "ISC",
+ "dependencies": {
+ "camelcase": "^5.0.0",
+ "decamelize": "^1.2.0"
+ },
+ "engines": {
+ "node": ">=6"
+ }
+ },
+ "node_modules/yargs/node_modules/find-up": {
+ "version": "4.1.0",
+ "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz",
+ "integrity": "sha512-PpOwAdQ/YlXQ2vj8a3h8IipDuYRi3wceVQQGYWxNINccq40Anw7BlsEXCMbt1Zt+OLA6Fq9suIpIWD0OsnISlw==",
+ "license": "MIT",
+ "dependencies": {
+ "locate-path": "^5.0.0",
+ "path-exists": "^4.0.0"
+ },
+ "engines": {
+ "node": ">=8"
+ }
+ },
+ "node_modules/yargs/node_modules/locate-path": {
+ "version": "5.0.0",
+ "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-5.0.0.tgz",
+ "integrity": "sha512-t7hw9pI+WvuwNJXwk5zVHpyhIqzg2qTlklJOf0mVxGSbe3Fp2VieZcduNYjaLDoy6p9uGpQEGWG87WpMKlNq8g==",
+ "license": "MIT",
+ "dependencies": {
+ "p-locate": "^4.1.0"
+ },
+ "engines": {
+ "node": ">=8"
+ }
+ },
+ "node_modules/yargs/node_modules/p-limit": {
+ "version": "2.3.0",
+ "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz",
+ "integrity": "sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w==",
+ "license": "MIT",
+ "dependencies": {
+ "p-try": "^2.0.0"
+ },
+ "engines": {
+ "node": ">=6"
+ },
+ "funding": {
+ "url": "https://github.com/sponsors/sindresorhus"
+ }
+ },
+ "node_modules/yargs/node_modules/p-locate": {
+ "version": "4.1.0",
+ "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-4.1.0.tgz",
+ "integrity": "sha512-R79ZZ/0wAxKGu3oYMlz8jy/kbhsNrS7SKZ7PxEHBgJ5+F2mtFW2fK2cOtBh1cHYkQsbzFV7I+EoRKe6Yt0oK7A==",
+ "license": "MIT",
+ "dependencies": {
+ "p-limit": "^2.2.0"
+ },
+ "engines": {
+ "node": ">=8"
+ }
+ },
"node_modules/yocto-queue": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz",
diff --git a/SharpbinV3.Client/package.json b/SharpbinV3.Client/package.json
index ce01ce1..c796cdd 100644
--- a/SharpbinV3.Client/package.json
+++ b/SharpbinV3.Client/package.json
@@ -21,6 +21,7 @@
"@sveltejs/vite-plugin-svelte": "^6.2.1",
"@tailwindcss/vite": "^4.1.17",
"@types/node": "^22",
+ "@types/qrcode": "^1.5.6",
"eslint": "^9.39.1",
"eslint-config-prettier": "^10.1.8",
"eslint-plugin-svelte": "^3.13.1",
@@ -38,6 +39,7 @@
"dependencies": {
"@lucide/svelte": "^0.562.0",
"@sveltejs/adapter-node": "^5.4.0",
- "highlight.js": "^11.8.0"
+ "highlight.js": "^11.8.0",
+ "qrcode": "^1.5.4"
}
}
diff --git a/SharpbinV3.Client/src/lib/components/Modal.svelte b/SharpbinV3.Client/src/lib/components/Modal.svelte
index 11e7e45..37b1460 100644
--- a/SharpbinV3.Client/src/lib/components/Modal.svelte
+++ b/SharpbinV3.Client/src/lib/components/Modal.svelte
@@ -1,6 +1,17 @@
{#if show}
@@ -71,7 +253,58 @@
{/if}
- {#if shouldShowInput}
+ {#if mode === 'totpSetup'}
+
+
+ {totpEnabled
+ ? 'Two-factor authentication is enabled. Enter a current 6-digit code to disable it.'
+ : 'Scan the QR code with your authenticator app or copy the secret to add it manually.'}
+
+ {#if totpLoading}
+
Preparing TOTP enrollment...
+ {:else}
+ {#if !totpEnabled}
+ {#if totpQr}
+
+
+
+ {/if}
+ {#if totpSecret}
+
+ {totpSecret}
+
+ Copy
+
+
+ {/if}
+ {/if}
+
+ {/if}
+
+ {:else if shouldShowInput}
{/if}
- {#if error}
+ {#if displayError}
- {error}
+ {displayError}
{/if}
diff --git a/SharpbinV3.Client/src/lib/stores/user.ts b/SharpbinV3.Client/src/lib/stores/user.ts
index 7a13b4e..02d2727 100644
--- a/SharpbinV3.Client/src/lib/stores/user.ts
+++ b/SharpbinV3.Client/src/lib/stores/user.ts
@@ -1,4 +1,4 @@
import { writable } from 'svelte/store';
-import type { User } from '$lib/types/user';
+import type { AuthUser } from '$lib/types/user';
-export const user = writable(null);
+export const user = writable(null);
diff --git a/SharpbinV3.Client/src/lib/types/user.ts b/SharpbinV3.Client/src/lib/types/user.ts
index 886b065..c423549 100644
--- a/SharpbinV3.Client/src/lib/types/user.ts
+++ b/SharpbinV3.Client/src/lib/types/user.ts
@@ -13,3 +13,7 @@ export interface User {
pastes: Paste[] | null;
pagination: Pagination | null;
}
+
+export interface AuthUser extends User {
+ totpEnabled: boolean;
+}
diff --git a/SharpbinV3.Client/src/lib/utils/totp.ts b/SharpbinV3.Client/src/lib/utils/totp.ts
new file mode 100644
index 0000000..9d64022
--- /dev/null
+++ b/SharpbinV3.Client/src/lib/utils/totp.ts
@@ -0,0 +1,18 @@
+export function parseTotpEnabled(tokenValue: string | null) {
+ if (!tokenValue) return false;
+ try {
+ const payload = JSON.parse(atob(tokenValue.split('.')[1]));
+ const flag = payload?.TOTP_Enabled ?? payload?.totp_enabled ?? payload?.totpEnabled;
+ if (typeof flag === 'string') return flag.toLowerCase() === 'true';
+ return Boolean(flag);
+ } catch {
+ return false;
+ }
+}
+
+export function needsAdminTotp(totpEnabled: boolean, currentRoles: number[], nextRoles: number[]) {
+ if (!totpEnabled) return false;
+ const nextHasAdmin = nextRoles.includes(255);
+ const alreadyAdmin = currentRoles.includes(255);
+ return nextHasAdmin && !alreadyAdmin;
+}
diff --git a/SharpbinV3.Client/src/routes/+layout.svelte b/SharpbinV3.Client/src/routes/+layout.svelte
index 83e961e..f00ab80 100644
--- a/SharpbinV3.Client/src/routes/+layout.svelte
+++ b/SharpbinV3.Client/src/routes/+layout.svelte
@@ -5,6 +5,7 @@
import Navbar from '$lib/components/Navbar.svelte';
import { user } from '$lib/stores/user';
import { startTokenRefreshInterval, getToken, refreshTokenIfNeeded } from '$lib/utils/auth';
+ import { parseTotpEnabled } from '$lib/utils/totp';
import { page } from '$app/state';
import type { LayoutData } from './$types';
@@ -27,7 +28,8 @@
});
if (res.ok) {
- user.set(await res.json());
+ const me = await res.json();
+ user.set({ ...me, totpEnabled: parseTotpEnabled(getToken()) });
} else {
user.set(null);
}
diff --git a/SharpbinV3.Client/src/routes/+page.svelte b/SharpbinV3.Client/src/routes/+page.svelte
index bae73a7..3954e35 100644
--- a/SharpbinV3.Client/src/routes/+page.svelte
+++ b/SharpbinV3.Client/src/routes/+page.svelte
@@ -82,7 +82,11 @@
}
window.location.href = `/${result.id}`;
} else {
- error = 'Failed to create paste. Please try again.';
+ if (response.status === 403) {
+ error = 'You are banned from creating new pastes.';
+ } else {
+ error = 'Failed to create paste. Please try again.';
+ }
setInterval(() => {
error = '';
}, 5000);
@@ -152,7 +156,8 @@
{/if}
Create Paste
{#if error}
@@ -163,6 +168,14 @@
{error}
{/if}
+ {#if $user?.roles && $user.roles.includes(403)}
+
+ You are banned from creating new pastes.
+
+ {/if}
diff --git a/SharpbinV3.Client/src/routes/login/+page.svelte b/SharpbinV3.Client/src/routes/login/+page.svelte
index fb8430e..322ab66 100644
--- a/SharpbinV3.Client/src/routes/login/+page.svelte
+++ b/SharpbinV3.Client/src/routes/login/+page.svelte
@@ -7,6 +7,8 @@
let username = '';
let password = '';
let error = '';
+ let totpEnabled = false;
+ let totpCode = '';
export let data: PageData;
onMount(() => {
@@ -31,7 +33,7 @@
});
async function login() {
- const body: { username: string; password: string; token?: string } = {
+ const body: { username: string; password: string; token?: string; totpcode?: string } = {
username,
password
};
@@ -43,6 +45,10 @@
}
}
+ if (totpEnabled && totpCode) {
+ body.totpcode = totpCode;
+ }
+
const res = await fetch('/api/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
@@ -67,6 +73,14 @@
const messages = Object.values(resData.errors).flat();
error = messages.join('\n');
} else {
+ if (resData.message === 'TOTP code is required.') {
+ totpEnabled = true;
+ }
+ if (resData.message === 'Verification failed.') {
+ if (window.turnstile) {
+ window.turnstile.reset();
+ }
+ }
error = resData.message || 'Login failed. Please try again.';
}
}
@@ -95,6 +109,24 @@
autocomplete="current-password"
class="focus:bg-neutral-750 w-full rounded border border-neutral-700 bg-neutral-800 p-2 placeholder-neutral-500 transition-colors duration-200 focus:border-neutral-600"
/>
+ {#if totpEnabled}
+ {
+ if (e.target instanceof HTMLInputElement) {
+ e.target.value = e.target.value.replace(/\D/g, '').slice(0, 6);
+ totpCode = e.target.value;
+ }
+ }}
+ class="focus:bg-neutral-750 w-full rounded border border-neutral-700 bg-neutral-800 p-2 placeholder-neutral-500 transition-colors duration-200 focus:border-neutral-600"
+ />
+ {/if}
{#if data.options?.cf_turnstile_site_key}
diff --git a/SharpbinV3.Client/src/routes/register/+page.svelte b/SharpbinV3.Client/src/routes/register/+page.svelte
index d724dfa..d2fda59 100644
--- a/SharpbinV3.Client/src/routes/register/+page.svelte
+++ b/SharpbinV3.Client/src/routes/register/+page.svelte
@@ -100,6 +100,11 @@
const messages = Object.values(resData.errors).flat();
error = messages.join('\n');
} else {
+ if (resData.message === 'Verification failed.') {
+ if (window.turnstile) {
+ window.turnstile.reset();
+ }
+ }
error = resData.message || 'Registration failed. Please try again.';
}
}
diff --git a/SharpbinV3.Client/src/routes/user/[username]/+page.svelte b/SharpbinV3.Client/src/routes/user/[username]/+page.svelte
index 1c0c9e3..fa406ac 100644
--- a/SharpbinV3.Client/src/routes/user/[username]/+page.svelte
+++ b/SharpbinV3.Client/src/routes/user/[username]/+page.svelte
@@ -17,6 +17,7 @@
import { extractDateFromUUIDv7, tooltip, dateToRelativeString } from '$lib/utils/misc';
import { roles } from '$lib/consts';
import { getToken } from '$lib/utils/auth';
+ import { needsAdminTotp } from '$lib/utils/totp';
import Modal from '$lib/components/Modal.svelte';
import { user } from '$lib/stores/user';
import { onMount } from 'svelte';
@@ -36,11 +37,19 @@
let showEditModal = false;
let showDeleteModal = false;
let showRoleModal = false;
+ let showTotpModal = false;
+ let showRoleTotpModal = false;
+ let showDeleteTotpModal = false;
let modalError = '';
+ let totpEnabled = false;
+ let pendingRoles: number[] | null = null;
+ let roleTotpCode = '';
+ let deleteTotpCode = '';
let now = new Date();
let interval: ReturnType
| null = null;
$: isOwner = data.user && $user ? data.user?.uuid === $user.uuid : false;
+ $: totpEnabled = Boolean($user?.totpEnabled);
const roleOptions = [
{ label: 'Member', value: 0 },
@@ -69,7 +78,16 @@
async function handleUserUpdate(displayName?: string, selectedRoles?: number[]) {
loading = true;
+ modalError = '';
const token = getToken();
+ const currentRoles = data.user?.roles ?? [];
+ const nextRoles = selectedRoles ?? currentRoles;
+ if (needsAdminTotp(totpEnabled, currentRoles, nextRoles) && !roleTotpCode) {
+ pendingRoles = nextRoles;
+ showRoleTotpModal = true;
+ loading = false;
+ return;
+ }
const res = await fetch(`/api/user/uuid/${data.user?.uuid}`, {
method: 'PATCH',
headers: {
@@ -78,7 +96,8 @@
},
body: JSON.stringify({
...(displayName !== undefined ? { displayName: displayName.trim() } : {}),
- ...(selectedRoles !== undefined ? { roles: selectedRoles } : {})
+ ...(selectedRoles !== undefined ? { roles: selectedRoles } : {}),
+ ...(roleTotpCode ? { totpcode: roleTotpCode } : {})
})
});
loading = false;
@@ -93,17 +112,36 @@
if (selectedRoles !== undefined) {
data.user!.roles = selectedRoles;
}
+ roleTotpCode = '';
+ showRoleTotpModal = false;
+ pendingRoles = null;
modalError = '';
}
+ function handleRoleTotpConfirm(value: unknown) {
+ if (typeof value === 'string') {
+ roleTotpCode = value.trim();
+ }
+ showRoleTotpModal = false;
+ if (!roleTotpCode) return;
+ handleUserUpdate(undefined, pendingRoles ?? data.user?.roles ?? []);
+ }
+
async function handleDeleteAccount() {
loading = true;
+ modalError = '';
const token = getToken();
+ if (totpEnabled && !deleteTotpCode) {
+ showDeleteTotpModal = true;
+ loading = false;
+ return;
+ }
const res = await fetch(`/api/user/uuid/${data.user?.uuid}`, {
method: 'DELETE',
headers: {
Authorization: `Bearer ${token}`
- }
+ },
+ body: totpEnabled && deleteTotpCode ? JSON.stringify({ totpcode: deleteTotpCode }) : undefined
});
loading = false;
if (!res.ok) {
@@ -112,9 +150,29 @@
return;
}
showDeleteModal = false;
+ showDeleteTotpModal = false;
+ deleteTotpCode = '';
window.location.href = '/';
}
+ function handleTotpConfirm(value: unknown) {
+ if (value && typeof value === 'object' && 'enabled' in value) {
+ totpEnabled = Boolean((value as { enabled?: boolean }).enabled);
+ }
+ user.update((u) => (u ? { ...u, totpEnabled } : u));
+ showTotpModal = false;
+ modalError = '';
+ }
+
+ function handleDeleteTotpConfirm(value: unknown) {
+ if (typeof value === 'string') {
+ deleteTotpCode = value.trim();
+ }
+ showDeleteTotpModal = false;
+ if (!deleteTotpCode) return;
+ handleDeleteAccount();
+ }
+
onMount(() => {
interval = setInterval(() => {
now = new Date();
@@ -201,6 +259,10 @@
{data.user?.uid}
+
+
+ 2FA {totpEnabled ? 'enabled' : 'disabled'}
+
{#if isOwner && data.user?.lastLogin}
@@ -225,6 +287,18 @@
Edit Display Name
+ {#if isOwner}
+
{
+ showTotpModal = true;
+ modalError = '';
+ }}
+ class="flex items-center gap-2 rounded bg-neutral-700 px-4 py-2 text-sm font-medium transition-colors hover:bg-neutral-600"
+ >
+
+ {totpEnabled ? 'Manage 2FA' : 'Enable 2FA'}
+
+ {/if}
{#if $user?.roles.some((r) => r === 255)}
{
@@ -323,6 +397,22 @@
}}
/>
+ handleDeleteTotpConfirm(value)}
+ onCancel={() => {
+ showDeleteTotpModal = false;
+ deleteTotpCode = '';
+ modalError = '';
+ }}
+/>
+
+ handleTotpConfirm(value)}
+ onCancel={() => {
+ showTotpModal = false;
+ modalError = '';
+ }}
+/>
+ handleRoleTotpConfirm(value)}
+ onCancel={() => {
+ showRoleTotpModal = false;
+ roleTotpCode = '';
+ pendingRoles = null;
+ modalError = '';
+ }}
+/>
+