@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.
- package/LICENSE +21 -0
- package/README.OIDC.md +216 -0
- package/README.md +639 -0
- package/dist/demo.d.ts +25 -0
- package/dist/index.d.ts +819 -0
- package/dist/index.esm.js +2 -0
- package/dist/index.js +2 -0
- package/dist/src/client/actions.d.ts +17 -0
- package/dist/src/client/config.d.ts +35 -0
- package/dist/src/client/encryption.d.ts +30 -0
- package/dist/src/client/handlers.d.ts +45 -0
- package/dist/src/client/index.d.ts +10 -0
- package/dist/src/client/request-parser.d.ts +48 -0
- package/dist/src/client/t3n-client.d.ts +70 -0
- package/dist/src/client/transport.d.ts +107 -0
- package/dist/src/config/index.d.ts +67 -0
- package/dist/src/config/loader.d.ts +11 -0
- package/dist/src/config/types.d.ts +25 -0
- package/dist/src/index.d.ts +23 -0
- package/dist/src/types/auth.d.ts +54 -0
- package/dist/src/types/index.d.ts +35 -0
- package/dist/src/types/session.d.ts +24 -0
- package/dist/src/utils/contract-version.d.ts +5 -0
- package/dist/src/utils/crypto.d.ts +52 -0
- package/dist/src/utils/errors.d.ts +61 -0
- package/dist/src/utils/index.d.ts +9 -0
- package/dist/src/utils/logger.d.ts +102 -0
- package/dist/src/utils/redaction.d.ts +13 -0
- package/dist/src/utils/session.d.ts +37 -0
- package/dist/src/wasm/index.d.ts +5 -0
- package/dist/src/wasm/interface.d.ts +105 -0
- package/dist/src/wasm/loader.d.ts +43 -0
- package/dist/wasm/generated/interfaces/component-session-client-auth.d.ts +12 -0
- package/dist/wasm/generated/interfaces/component-session-client-handshake.d.ts +12 -0
- package/dist/wasm/generated/interfaces/component-session-server-auth.d.ts +12 -0
- package/dist/wasm/generated/interfaces/component-session-server-handshake.d.ts +12 -0
- package/dist/wasm/generated/interfaces/component-session-session.d.ts +8 -0
- package/dist/wasm/generated/interfaces/wasi-cli-environment.d.ts +2 -0
- package/dist/wasm/generated/interfaces/wasi-cli-exit.d.ts +3 -0
- package/dist/wasm/generated/interfaces/wasi-cli-stderr.d.ts +3 -0
- package/dist/wasm/generated/interfaces/wasi-cli-stdin.d.ts +3 -0
- package/dist/wasm/generated/interfaces/wasi-cli-stdout.d.ts +3 -0
- package/dist/wasm/generated/interfaces/wasi-clocks-wall-clock.d.ts +5 -0
- package/dist/wasm/generated/interfaces/wasi-filesystem-preopens.d.ts +3 -0
- package/dist/wasm/generated/interfaces/wasi-filesystem-types.d.ts +124 -0
- package/dist/wasm/generated/interfaces/wasi-io-error.d.ts +8 -0
- package/dist/wasm/generated/interfaces/wasi-io-streams.d.ts +28 -0
- package/dist/wasm/generated/session.core.wasm +0 -0
- package/dist/wasm/generated/session.core2.wasm +0 -0
- package/dist/wasm/generated/session.d.ts +16 -0
- package/dist/wasm/generated/session.js +3437 -0
- 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
|
+
|