@pondoknusa/telegram-2fa 6.0.0 → 6.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +89 -0
- package/package.json +8 -8
package/README.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# @pondoknusa/telegram-2fa
|
|
2
|
+
|
|
3
|
+
Two-factor authentication for Pondoknusa that delivers one-time codes to a user's Telegram chat
|
|
4
|
+
and enforces a verified session on protected routes.
|
|
5
|
+
|
|
6
|
+
Passwords get phished; a code sent to a chat the attacker doesn't control does not. This package
|
|
7
|
+
sends a short-lived, hashed verification code over the bot from `@pondoknusa/telegram`, verifies
|
|
8
|
+
submissions in constant time, and exposes middleware that gates routes on a session marked
|
|
9
|
+
2FA-verified. It is its own package because it depends on both the auth stack and the Telegram
|
|
10
|
+
bot, neither of which every app needs together.
|
|
11
|
+
|
|
12
|
+
> Part of the [Pondoknusa](https://pondoknusa.dev) framework — a TypeScript-native full-stack
|
|
13
|
+
> framework built on standard Web APIs. Requires **Node.js ≥ 26**.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @pondoknusa/telegram-2fa
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`@pondoknusa/telegram` (the bot) and `@pondoknusa/auth` are installed automatically as
|
|
22
|
+
dependencies of this package.
|
|
23
|
+
|
|
24
|
+
## Quick start
|
|
25
|
+
|
|
26
|
+
Register the providers, then send and verify a code. Config lives in `config/telegram-2fa.ts`
|
|
27
|
+
(`Telegram2FaConfig`): `code_length`, `code_ttl_seconds`, `max_attempts`, `session_key`, and more.
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
// src/main.ts
|
|
31
|
+
import { TelegramServiceProvider } from '@pondoknusa/telegram';
|
|
32
|
+
import { Telegram2FaServiceProvider, TwoFactor } from '@pondoknusa/telegram-2fa';
|
|
33
|
+
|
|
34
|
+
app.register(TelegramServiceProvider);
|
|
35
|
+
app.register(Telegram2FaServiceProvider);
|
|
36
|
+
await app.boot();
|
|
37
|
+
TwoFactor.setApplication(app);
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import { TwoFactor } from '@pondoknusa/telegram-2fa';
|
|
42
|
+
|
|
43
|
+
// 1. Enabling — send a one-time code to the user's Telegram chat.
|
|
44
|
+
await TwoFactor.manager().send(user); // stores a SHA-256 hash in the cache under the TTL
|
|
45
|
+
|
|
46
|
+
// 2. Verifying — check the code the user submits.
|
|
47
|
+
const result = await TwoFactor.manager().verify(user, submittedCode);
|
|
48
|
+
|
|
49
|
+
if (result.success) {
|
|
50
|
+
TwoFactor.markVerified(request); // stamp the session as 2FA-verified
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
`verify()` returns `{ success, reason? }`, where a failure is `'expired'`, `'max_attempts'`, or
|
|
55
|
+
`'invalid'`. Call `TwoFactor.manager().invalidate(user)` to drop any pending code, and
|
|
56
|
+
`TwoFactor.isVerified(request)` to read the current session state.
|
|
57
|
+
|
|
58
|
+
### Protecting routes
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
import { createRequireTwoFactorMiddleware } from '@pondoknusa/telegram-2fa';
|
|
62
|
+
|
|
63
|
+
Route.middleware('2fa', createRequireTwoFactorMiddleware()).get('/admin', handler);
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
The middleware returns `403` unless the session key (default `telegram_2fa_verified`) is present
|
|
67
|
+
and within `session_ttl_seconds`. Pass `skipSafeMethods: true` to challenge only mutating
|
|
68
|
+
requests, or `onChallengeRequired` to supply your own response. The manager reads a
|
|
69
|
+
`telegram_chat_id` model attribute, then falls back to `routeNotificationForTelegram()`; override
|
|
70
|
+
`resolveChatIdResolver()` in a `Telegram2FaServiceProvider` subclass to customize it.
|
|
71
|
+
|
|
72
|
+
## Related packages
|
|
73
|
+
|
|
74
|
+
- [`@pondoknusa/telegram`](https://www.npmjs.com/package/@pondoknusa/telegram) — the bot client
|
|
75
|
+
that delivers the codes.
|
|
76
|
+
- [`@pondoknusa/auth`](https://www.npmjs.com/package/@pondoknusa/auth) — sessions and the
|
|
77
|
+
authenticated user the challenge runs against.
|
|
78
|
+
- [`@pondoknusa/cache`](https://www.npmjs.com/package/@pondoknusa/cache) — stores the hashed
|
|
79
|
+
codes with a TTL.
|
|
80
|
+
- [`@pondoknusa/core`](https://www.npmjs.com/package/@pondoknusa/core) — service providers, the
|
|
81
|
+
container, and the `TwoFactor` facade.
|
|
82
|
+
|
|
83
|
+
## Documentation
|
|
84
|
+
|
|
85
|
+
- [Authentication guide](https://pondoknusa.dev/guide/auth)
|
|
86
|
+
- [Package reference](https://pondoknusa.dev/reference/generated/packages/telegram-2fa)
|
|
87
|
+
- [Source](https://git.simonharms.com/thesimonharms/pondoknusa/src/branch/main/packages/telegram-2fa)
|
|
88
|
+
|
|
89
|
+
MIT © Pondoknusa Contributors
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pondoknusa/telegram-2fa",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -15,13 +15,13 @@
|
|
|
15
15
|
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@pondoknusa/auth": "6.0.
|
|
19
|
-
"@pondoknusa/cache": "6.0.
|
|
20
|
-
"@pondoknusa/config": "6.0.
|
|
21
|
-
"@pondoknusa/core": "6.0.
|
|
22
|
-
"@pondoknusa/http": "6.0.
|
|
23
|
-
"@pondoknusa/support": "6.0.
|
|
24
|
-
"@pondoknusa/telegram": "6.0.
|
|
18
|
+
"@pondoknusa/auth": "6.0.1",
|
|
19
|
+
"@pondoknusa/cache": "6.0.1",
|
|
20
|
+
"@pondoknusa/config": "6.0.1",
|
|
21
|
+
"@pondoknusa/core": "6.0.1",
|
|
22
|
+
"@pondoknusa/http": "6.0.1",
|
|
23
|
+
"@pondoknusa/support": "6.0.1",
|
|
24
|
+
"@pondoknusa/telegram": "6.0.1"
|
|
25
25
|
},
|
|
26
26
|
"files": [
|
|
27
27
|
"dist"
|