@terminal3/t3n-sdk 0.2.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.
Files changed (52) hide show
  1. package/LICENSE +21 -0
  2. package/README.OIDC.md +216 -0
  3. package/README.md +639 -0
  4. package/dist/demo.d.ts +25 -0
  5. package/dist/index.d.ts +819 -0
  6. package/dist/index.esm.js +2 -0
  7. package/dist/index.js +2 -0
  8. package/dist/src/client/actions.d.ts +17 -0
  9. package/dist/src/client/config.d.ts +35 -0
  10. package/dist/src/client/encryption.d.ts +30 -0
  11. package/dist/src/client/handlers.d.ts +45 -0
  12. package/dist/src/client/index.d.ts +10 -0
  13. package/dist/src/client/request-parser.d.ts +48 -0
  14. package/dist/src/client/t3n-client.d.ts +70 -0
  15. package/dist/src/client/transport.d.ts +107 -0
  16. package/dist/src/config/index.d.ts +67 -0
  17. package/dist/src/config/loader.d.ts +11 -0
  18. package/dist/src/config/types.d.ts +25 -0
  19. package/dist/src/index.d.ts +23 -0
  20. package/dist/src/types/auth.d.ts +54 -0
  21. package/dist/src/types/index.d.ts +35 -0
  22. package/dist/src/types/session.d.ts +24 -0
  23. package/dist/src/utils/contract-version.d.ts +5 -0
  24. package/dist/src/utils/crypto.d.ts +52 -0
  25. package/dist/src/utils/errors.d.ts +61 -0
  26. package/dist/src/utils/index.d.ts +9 -0
  27. package/dist/src/utils/logger.d.ts +102 -0
  28. package/dist/src/utils/redaction.d.ts +13 -0
  29. package/dist/src/utils/session.d.ts +37 -0
  30. package/dist/src/wasm/index.d.ts +5 -0
  31. package/dist/src/wasm/interface.d.ts +105 -0
  32. package/dist/src/wasm/loader.d.ts +43 -0
  33. package/dist/wasm/generated/interfaces/component-session-client-auth.d.ts +12 -0
  34. package/dist/wasm/generated/interfaces/component-session-client-handshake.d.ts +12 -0
  35. package/dist/wasm/generated/interfaces/component-session-server-auth.d.ts +12 -0
  36. package/dist/wasm/generated/interfaces/component-session-server-handshake.d.ts +12 -0
  37. package/dist/wasm/generated/interfaces/component-session-session.d.ts +8 -0
  38. package/dist/wasm/generated/interfaces/wasi-cli-environment.d.ts +2 -0
  39. package/dist/wasm/generated/interfaces/wasi-cli-exit.d.ts +3 -0
  40. package/dist/wasm/generated/interfaces/wasi-cli-stderr.d.ts +3 -0
  41. package/dist/wasm/generated/interfaces/wasi-cli-stdin.d.ts +3 -0
  42. package/dist/wasm/generated/interfaces/wasi-cli-stdout.d.ts +3 -0
  43. package/dist/wasm/generated/interfaces/wasi-clocks-wall-clock.d.ts +5 -0
  44. package/dist/wasm/generated/interfaces/wasi-filesystem-preopens.d.ts +3 -0
  45. package/dist/wasm/generated/interfaces/wasi-filesystem-types.d.ts +124 -0
  46. package/dist/wasm/generated/interfaces/wasi-io-error.d.ts +8 -0
  47. package/dist/wasm/generated/interfaces/wasi-io-streams.d.ts +28 -0
  48. package/dist/wasm/generated/session.core.wasm +0 -0
  49. package/dist/wasm/generated/session.core2.wasm +0 -0
  50. package/dist/wasm/generated/session.d.ts +16 -0
  51. package/dist/wasm/generated/session.js +3437 -0
  52. package/package.json +104 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 T3n Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.OIDC.md ADDED
@@ -0,0 +1,216 @@
1
+ # OIDC Authentication Guide
2
+
3
+ ## Quick Start
4
+
5
+ ### 1. Setup Google OAuth Client
6
+
7
+ 1. Go to [Google Cloud Console](https://console.cloud.google.com/)
8
+ 2. Create a new project or select existing one
9
+ 3. Enable "Google Sign-In" API
10
+ 4. Go to **Credentials** → **Create Credentials** → **OAuth 2.0 Client ID**
11
+ 5. Application type: **Web application**
12
+ 6. Authorized JavaScript origins:
13
+ ```
14
+ http://localhost:8081
15
+ ```
16
+ (Port 8081 is what the demo uses)
17
+ 7. Copy your **Client ID**
18
+
19
+ ### 2. Update the HTML File
20
+
21
+ Edit `oidc-login.html` and replace:
22
+
23
+ ```html
24
+ data-client_id="PUT_YOUR_WEB_CLIENT_ID_HERE"
25
+ ```
26
+
27
+ With your actual Client ID:
28
+
29
+ ```html
30
+ data-client_id="YOUR_ACTUAL_CLIENT_ID.apps.googleusercontent.com"
31
+ ```
32
+
33
+ ### 3. Get ID Token
34
+
35
+ #### Option A: Using the Helper Script (Recommended)
36
+
37
+ ```bash
38
+ npx tsx get-oidc-token.ts
39
+ ```
40
+
41
+ This will:
42
+ 1. Open the browser with Google Sign-In
43
+ 2. Display your ID token after sign-in
44
+ 3. Provide code examples
45
+
46
+ #### Option B: Manual (No CLI needed)
47
+
48
+ 1. Open `oidc-login.html` in your browser:
49
+ ```bash
50
+ open oidc-login.html # macOS
51
+ # or
52
+ xdg-open oidc-login.html # Linux
53
+ # or just double-click the file
54
+ ```
55
+
56
+ 2. Click "Sign in with Google"
57
+
58
+ 3. Copy the ID token displayed
59
+
60
+ 4. Use it in your code:
61
+ ```typescript
62
+ const authenticator = {
63
+ provider: "google",
64
+ id_token: "eyJhbGciOiJS..." // Your copied token
65
+ };
66
+
67
+ await client.performAuthentication(authenticator);
68
+ ```
69
+
70
+ ### 4. Use in Demo
71
+
72
+ ```typescript
73
+ // In demo.ts
74
+ import * as readline from 'readline';
75
+
76
+ async function getOidcToken(): Promise<string> {
77
+ console.log('\nšŸ” OIDC Authentication');
78
+ console.log('1. Open oidc-login.html in your browser');
79
+ console.log('2. Sign in with Google');
80
+ console.log('3. Copy the ID token\n');
81
+
82
+ const rl = readline.createInterface({
83
+ input: process.stdin,
84
+ output: process.stdout
85
+ });
86
+
87
+ return new Promise((resolve) => {
88
+ rl.question('Paste your ID token: ', (token) => {
89
+ rl.close();
90
+ resolve(token.trim());
91
+ });
92
+ });
93
+ }
94
+
95
+ // Usage
96
+ const idToken = await getOidcToken();
97
+ const authenticator = {
98
+ provider: "google",
99
+ id_token: idToken
100
+ };
101
+
102
+ await client.performAuthentication(authenticator);
103
+ ```
104
+
105
+ ## Supported Providers
106
+
107
+ | Provider | Status | Setup Guide |
108
+ | ------------ | --------------- | -------------------------------------------------- |
109
+ | **Google** | āœ… Ready | [Google OAuth Setup](https://developers.google.com/identity/sign-in/web/sign-in) |
110
+ | **Microsoft** | āœ… Ready | [Azure AD Setup](https://learn.microsoft.com/en-us/azure/active-directory/) |
111
+ | **Apple** | āœ… Ready | [Sign in with Apple](https://developer.apple.com/sign-in-with-apple/) |
112
+ | **GitHub** | āœ… Ready | [GitHub OAuth](https://docs.github.com/en/developers/apps/building-oauth-apps) |
113
+ | **Facebook** | āœ… Ready | [Facebook Login](https://developers.facebook.com/docs/facebook-login/) |
114
+
115
+ ## Architecture
116
+
117
+ ### Frontend-Driven PKCE Flow
118
+
119
+ ```
120
+ ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
121
+ │ Browser │ │ T3n │ │ Google │
122
+ │ (Popup) │ │ Backend │ │ OAuth │
123
+ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
124
+ │ │ │
125
+ │ 1. Generate PKCE │ │
126
+ ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā–ŗā”‚ │
127
+ │ (code_verifier) │ │
128
+ │ │ │
129
+ │ 2. Redirect to OAuth │ │
130
+ ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā–ŗā”‚
131
+ │ (with code_challenge)│ │
132
+ │ │ │
133
+ │ 3. User signs in │ │
134
+ │◄──────────────────────┼────────────────────────┤
135
+ │ │ │
136
+ │ 4. Exchange code │ │
137
+ │ (with code_verifier) │ │
138
+ ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā–ŗā”‚
139
+ │ │ │
140
+ │ 5. Receive ID token │ │
141
+ │◄──────────────────────┼────────────────────────┤
142
+ │ │ │
143
+ │ 6. Send ID token │ │
144
+ ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā–ŗā”‚ │
145
+ │ │ │
146
+ │ │ 7. Verify token │
147
+ │ │ (fetch JWKS & validate)
148
+ │ │ │
149
+ │ 8. Session created │ │
150
+ │◄──────────────────────┤ │
151
+ │ │ │
152
+ ```
153
+
154
+ ### Key Benefits
155
+
156
+ - āœ… **No Backend PKCE**: Frontend handles PKCE entirely
157
+ - āœ… **Stateless Backend**: No session storage for PKCE
158
+ - āœ… **Secure**: Backend independently verifies tokens
159
+ - āœ… **Simple CLI**: Just paste the token, no complex flow
160
+ - āœ… **Works Everywhere**: Browser handles OAuth, CLI gets result
161
+
162
+ ## Token Format
163
+
164
+ The ID token is a JWT with three parts:
165
+
166
+ ```
167
+ eyJhbGciOiJSUzI1NiIsImtpZCI6Ijc... (header)
168
+ .
169
+ eyJpc3MiOiJodHRwczovL2FjY291bnR... (payload)
170
+ .
171
+ SflKxwRJSMeKKF2QT4fwpMeJf36POk6... (signature)
172
+ ```
173
+
174
+ ### Payload Example
175
+
176
+ ```json
177
+ {
178
+ "iss": "https://accounts.google.com",
179
+ "sub": "1234567890",
180
+ "email": "user@example.com",
181
+ "email_verified": true,
182
+ "name": "John Doe",
183
+ "picture": "https://...",
184
+ "iat": 1700000000,
185
+ "exp": 1700003600
186
+ }
187
+ ```
188
+
189
+ ## Security Notes
190
+
191
+ 1. **Token Expiration**: ID tokens are short-lived (typically 1 hour)
192
+ 2. **Verification**: Backend always verifies token signature with JWKS
193
+ 3. **Email Verified**: Backend checks `email_verified` claim
194
+ 4. **CSRF Protection**: State parameter prevents CSRF attacks
195
+ 5. **PKCE**: Code verifier prevents authorization code interception
196
+
197
+ ## Troubleshooting
198
+
199
+ ### Token Already Expired
200
+
201
+ ID tokens expire quickly. If you get an error:
202
+ 1. Get a fresh token from the browser
203
+ 2. Use it immediately
204
+
205
+ ### Invalid Token Format
206
+
207
+ Make sure you copied the entire token (it's quite long!). Should have 3 parts separated by dots.
208
+
209
+ ### Provider Not Found
210
+
211
+ Make sure `provider` matches exactly: `"google"`, `"microsoft"`, `"apple"`, etc. (lowercase)
212
+
213
+ ### CORS Errors in Browser
214
+
215
+ For production, add your domain to OAuth client's authorized origins in Google Cloud Console.
216
+