@auth-gate/core 0.2.0 → 0.5.0
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 +3 -7
- package/dist/index.cjs +74 -52
- package/dist/index.d.cts +3 -16
- package/dist/index.d.ts +3 -16
- package/dist/index.mjs +74 -52
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -15,9 +15,7 @@ import { createAuthGateClient } from "@auth-gate/core";
|
|
|
15
15
|
|
|
16
16
|
const client = createAuthGateClient({
|
|
17
17
|
apiKey: process.env.AUTHGATE_API_KEY!,
|
|
18
|
-
projectId: process.env.AUTHGATE_PROJECT_ID!,
|
|
19
18
|
baseUrl: "https://authgate.dev", // or your self-hosted instance
|
|
20
|
-
sessionSecret: process.env.SESSION_SECRET!, // min 32 characters
|
|
21
19
|
});
|
|
22
20
|
```
|
|
23
21
|
|
|
@@ -28,8 +26,8 @@ const client = createAuthGateClient({
|
|
|
28
26
|
Supports Google, GitHub, Discord, Azure (Microsoft), and Apple.
|
|
29
27
|
|
|
30
28
|
```ts
|
|
31
|
-
// Generate the authorization URL
|
|
32
|
-
const url = client.getOAuthUrl("google", "https://yourapp.com/auth/callback");
|
|
29
|
+
// Generate the authorization URL (async — resolves project ID from API key)
|
|
30
|
+
const url = await client.getOAuthUrl("google", "https://yourapp.com/auth/callback");
|
|
33
31
|
|
|
34
32
|
// After callback, verify the JWT token from AuthGate
|
|
35
33
|
const result = await client.verifyToken(token);
|
|
@@ -145,10 +143,8 @@ client.getNonceCookieOptions();
|
|
|
145
143
|
|
|
146
144
|
| Option | Type | Required | Description |
|
|
147
145
|
|--------|------|----------|-------------|
|
|
148
|
-
| `apiKey` | `string` | Yes | Your AuthGate API key |
|
|
149
|
-
| `projectId` | `string` | Yes | Your AuthGate project ID |
|
|
146
|
+
| `apiKey` | `string` | Yes | Your AuthGate API key (also used to resolve project ID) |
|
|
150
147
|
| `baseUrl` | `string` | Yes | AuthGate instance URL |
|
|
151
|
-
| `sessionSecret` | `string` | Yes | Encryption secret (min 32 chars) |
|
|
152
148
|
| `cookieName` | `string` | No | Session cookie name (default: `__authgate`) |
|
|
153
149
|
| `sessionMaxAge` | `number` | No | Session TTL in seconds (default: `604800` / 7 days) |
|
|
154
150
|
| `callbackPath` | `string` | No | OAuth callback path (default: `/api/auth/callback`) |
|
package/dist/index.cjs
CHANGED
|
@@ -301,17 +301,11 @@ var InvalidStateError = class extends AuthGateError {
|
|
|
301
301
|
var AuthGateClient = class {
|
|
302
302
|
/**
|
|
303
303
|
* @param config - Client configuration. See {@link AuthGateConfig}.
|
|
304
|
-
* @throws {@link AuthGateError} if `sessionSecret` is shorter than 32 characters.
|
|
305
304
|
*/
|
|
306
305
|
constructor(config) {
|
|
307
306
|
this.encryptionKey = null;
|
|
308
307
|
this.stateKey = null;
|
|
309
308
|
var _a, _b, _c;
|
|
310
|
-
if (!config.sessionSecret || config.sessionSecret.length < 32) {
|
|
311
|
-
throw new AuthGateError(
|
|
312
|
-
"sessionSecret must be at least 32 characters long"
|
|
313
|
-
);
|
|
314
|
-
}
|
|
315
309
|
this.config = __spreadProps(__spreadValues({}, config), {
|
|
316
310
|
cookieName: (_a = config.cookieName) != null ? _a : DEFAULT_COOKIE_NAME,
|
|
317
311
|
sessionMaxAge: (_b = config.sessionMaxAge) != null ? _b : SESSION_MAX_AGE,
|
|
@@ -321,8 +315,8 @@ var AuthGateClient = class {
|
|
|
321
315
|
}
|
|
322
316
|
async deriveKeys() {
|
|
323
317
|
const [encKey, stKey] = await Promise.all([
|
|
324
|
-
deriveEncryptionKey(this.config.
|
|
325
|
-
deriveStateKey(this.config.
|
|
318
|
+
deriveEncryptionKey(this.config.apiKey),
|
|
319
|
+
deriveStateKey(this.config.apiKey)
|
|
326
320
|
]);
|
|
327
321
|
this.encryptionKey = encKey;
|
|
328
322
|
this.stateKey = stKey;
|
|
@@ -459,17 +453,28 @@ var AuthGateClient = class {
|
|
|
459
453
|
* @returns The full authorization URL to redirect the user to.
|
|
460
454
|
* @throws {@link AuthGateError} if the provider is not in {@link SUPPORTED_PROVIDERS}.
|
|
461
455
|
*/
|
|
462
|
-
getOAuthUrl(provider, callbackUrl, options) {
|
|
456
|
+
async getOAuthUrl(provider, callbackUrl, options) {
|
|
463
457
|
if (!SUPPORTED_PROVIDERS.includes(provider)) {
|
|
464
458
|
throw new AuthGateError(`Unsupported provider: ${provider}`);
|
|
465
459
|
}
|
|
466
|
-
const
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
460
|
+
const response = await fetch(`${this.config.baseUrl}/api/proxy/${provider}`, {
|
|
461
|
+
method: "POST",
|
|
462
|
+
headers: {
|
|
463
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
464
|
+
"Content-Type": "application/json"
|
|
465
|
+
},
|
|
466
|
+
body: JSON.stringify({
|
|
467
|
+
callback_url: callbackUrl,
|
|
468
|
+
state: options == null ? void 0 : options.state,
|
|
469
|
+
link_to: options == null ? void 0 : options.linkTo
|
|
470
|
+
})
|
|
471
|
+
});
|
|
472
|
+
if (!response.ok) {
|
|
473
|
+
const error = await response.json().catch(() => ({}));
|
|
474
|
+
throw new AuthGateError(error.error || `OAuth initiation failed: ${response.status}`);
|
|
471
475
|
}
|
|
472
|
-
|
|
476
|
+
const data = await response.json();
|
|
477
|
+
return data.redirect_url;
|
|
473
478
|
}
|
|
474
479
|
// ---------------------------------------------------------------------------
|
|
475
480
|
// Email auth
|
|
@@ -483,12 +488,14 @@ var AuthGateClient = class {
|
|
|
483
488
|
async emailSignup(data) {
|
|
484
489
|
return fetch(`${this.config.baseUrl}/api/proxy/email/signup`, {
|
|
485
490
|
method: "POST",
|
|
486
|
-
headers: {
|
|
491
|
+
headers: {
|
|
492
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
493
|
+
"Content-Type": "application/json"
|
|
494
|
+
},
|
|
487
495
|
body: JSON.stringify({
|
|
488
496
|
email: data.email,
|
|
489
497
|
password: data.password,
|
|
490
498
|
name: data.name,
|
|
491
|
-
project_id: this.config.projectId,
|
|
492
499
|
callback_url: data.callbackUrl
|
|
493
500
|
})
|
|
494
501
|
});
|
|
@@ -505,11 +512,13 @@ var AuthGateClient = class {
|
|
|
505
512
|
async emailSignin(data) {
|
|
506
513
|
return fetch(`${this.config.baseUrl}/api/proxy/email/signin`, {
|
|
507
514
|
method: "POST",
|
|
508
|
-
headers: {
|
|
515
|
+
headers: {
|
|
516
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
517
|
+
"Content-Type": "application/json"
|
|
518
|
+
},
|
|
509
519
|
body: JSON.stringify({
|
|
510
520
|
email: data.email,
|
|
511
521
|
password: data.password,
|
|
512
|
-
project_id: this.config.projectId,
|
|
513
522
|
callback_url: data.callbackUrl
|
|
514
523
|
})
|
|
515
524
|
});
|
|
@@ -523,10 +532,12 @@ var AuthGateClient = class {
|
|
|
523
532
|
async emailForgotPassword(data) {
|
|
524
533
|
return fetch(`${this.config.baseUrl}/api/proxy/email/reset-password`, {
|
|
525
534
|
method: "POST",
|
|
526
|
-
headers: {
|
|
535
|
+
headers: {
|
|
536
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
537
|
+
"Content-Type": "application/json"
|
|
538
|
+
},
|
|
527
539
|
body: JSON.stringify({
|
|
528
540
|
email: data.email,
|
|
529
|
-
project_id: this.config.projectId,
|
|
530
541
|
callback_url: data.callbackUrl
|
|
531
542
|
})
|
|
532
543
|
});
|
|
@@ -542,11 +553,13 @@ var AuthGateClient = class {
|
|
|
542
553
|
`${this.config.baseUrl}/api/proxy/email/reset-password/confirm`,
|
|
543
554
|
{
|
|
544
555
|
method: "POST",
|
|
545
|
-
headers: {
|
|
556
|
+
headers: {
|
|
557
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
558
|
+
"Content-Type": "application/json"
|
|
559
|
+
},
|
|
546
560
|
body: JSON.stringify({
|
|
547
561
|
token: data.token,
|
|
548
|
-
password: data.password
|
|
549
|
-
project_id: this.config.projectId
|
|
562
|
+
password: data.password
|
|
550
563
|
})
|
|
551
564
|
}
|
|
552
565
|
);
|
|
@@ -565,10 +578,12 @@ var AuthGateClient = class {
|
|
|
565
578
|
`${this.config.baseUrl}/api/proxy/email/magic-link/send`,
|
|
566
579
|
{
|
|
567
580
|
method: "POST",
|
|
568
|
-
headers: {
|
|
581
|
+
headers: {
|
|
582
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
583
|
+
"Content-Type": "application/json"
|
|
584
|
+
},
|
|
569
585
|
body: JSON.stringify({
|
|
570
586
|
email: data.email,
|
|
571
|
-
project_id: this.config.projectId,
|
|
572
587
|
callback_url: data.callbackUrl
|
|
573
588
|
})
|
|
574
589
|
}
|
|
@@ -586,11 +601,13 @@ var AuthGateClient = class {
|
|
|
586
601
|
async emailVerifyCode(data) {
|
|
587
602
|
return fetch(`${this.config.baseUrl}/api/proxy/email/verify`, {
|
|
588
603
|
method: "POST",
|
|
589
|
-
headers: {
|
|
604
|
+
headers: {
|
|
605
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
606
|
+
"Content-Type": "application/json"
|
|
607
|
+
},
|
|
590
608
|
body: JSON.stringify({
|
|
591
609
|
email: data.email,
|
|
592
|
-
code: data.code
|
|
593
|
-
project_id: this.config.projectId
|
|
610
|
+
code: data.code
|
|
594
611
|
})
|
|
595
612
|
});
|
|
596
613
|
}
|
|
@@ -606,10 +623,12 @@ var AuthGateClient = class {
|
|
|
606
623
|
async smsSendCode(data) {
|
|
607
624
|
return fetch(`${this.config.baseUrl}/api/proxy/sms/send-code`, {
|
|
608
625
|
method: "POST",
|
|
609
|
-
headers: {
|
|
626
|
+
headers: {
|
|
627
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
628
|
+
"Content-Type": "application/json"
|
|
629
|
+
},
|
|
610
630
|
body: JSON.stringify({
|
|
611
631
|
phone: data.phone,
|
|
612
|
-
project_id: this.config.projectId,
|
|
613
632
|
callback_url: data.callbackUrl
|
|
614
633
|
})
|
|
615
634
|
});
|
|
@@ -623,11 +642,13 @@ var AuthGateClient = class {
|
|
|
623
642
|
async smsVerifyCode(data) {
|
|
624
643
|
return fetch(`${this.config.baseUrl}/api/proxy/sms/verify-code`, {
|
|
625
644
|
method: "POST",
|
|
626
|
-
headers: {
|
|
645
|
+
headers: {
|
|
646
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
647
|
+
"Content-Type": "application/json"
|
|
648
|
+
},
|
|
627
649
|
body: JSON.stringify({
|
|
628
650
|
phone: data.phone,
|
|
629
651
|
code: data.code,
|
|
630
|
-
project_id: this.config.projectId,
|
|
631
652
|
callback_url: data.callbackUrl
|
|
632
653
|
})
|
|
633
654
|
});
|
|
@@ -650,10 +671,12 @@ var AuthGateClient = class {
|
|
|
650
671
|
`${this.config.baseUrl}/api/proxy/token/refresh`,
|
|
651
672
|
{
|
|
652
673
|
method: "POST",
|
|
653
|
-
headers: {
|
|
674
|
+
headers: {
|
|
675
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
676
|
+
"Content-Type": "application/json"
|
|
677
|
+
},
|
|
654
678
|
body: JSON.stringify({
|
|
655
|
-
refresh_token: refreshToken
|
|
656
|
-
project_id: this.config.projectId
|
|
679
|
+
refresh_token: refreshToken
|
|
657
680
|
})
|
|
658
681
|
}
|
|
659
682
|
);
|
|
@@ -678,10 +701,12 @@ var AuthGateClient = class {
|
|
|
678
701
|
`${this.config.baseUrl}/api/proxy/sessions/revoke`,
|
|
679
702
|
{
|
|
680
703
|
method: "POST",
|
|
681
|
-
headers: {
|
|
704
|
+
headers: {
|
|
705
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
706
|
+
"Content-Type": "application/json"
|
|
707
|
+
},
|
|
682
708
|
body: JSON.stringify({
|
|
683
|
-
refresh_token: refreshToken
|
|
684
|
-
project_id: this.config.projectId
|
|
709
|
+
refresh_token: refreshToken
|
|
685
710
|
})
|
|
686
711
|
}
|
|
687
712
|
);
|
|
@@ -704,10 +729,11 @@ var AuthGateClient = class {
|
|
|
704
729
|
async mfaVerify(data) {
|
|
705
730
|
return fetch(`${this.config.baseUrl}/api/proxy/mfa/verify`, {
|
|
706
731
|
method: "POST",
|
|
707
|
-
headers: {
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
}
|
|
732
|
+
headers: {
|
|
733
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
734
|
+
"Content-Type": "application/json"
|
|
735
|
+
},
|
|
736
|
+
body: JSON.stringify(data)
|
|
711
737
|
});
|
|
712
738
|
}
|
|
713
739
|
/**
|
|
@@ -838,11 +864,11 @@ var AuthGateClient = class {
|
|
|
838
864
|
async mfaSmsSendCode(challenge) {
|
|
839
865
|
return fetch(`${this.config.baseUrl}/api/proxy/mfa/sms/send-code`, {
|
|
840
866
|
method: "POST",
|
|
841
|
-
headers: {
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
})
|
|
867
|
+
headers: {
|
|
868
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
869
|
+
"Content-Type": "application/json"
|
|
870
|
+
},
|
|
871
|
+
body: JSON.stringify({ challenge })
|
|
846
872
|
});
|
|
847
873
|
}
|
|
848
874
|
// ---------------------------------------------------------------------------
|
|
@@ -864,10 +890,6 @@ var AuthGateClient = class {
|
|
|
864
890
|
get callbackPath() {
|
|
865
891
|
return this.config.callbackPath;
|
|
866
892
|
}
|
|
867
|
-
/** The AuthGate project ID. */
|
|
868
|
-
get projectId() {
|
|
869
|
-
return this.config.projectId;
|
|
870
|
-
}
|
|
871
893
|
/** The AuthGate service base URL. */
|
|
872
894
|
get baseUrl() {
|
|
873
895
|
return this.config.baseUrl;
|
package/dist/index.d.cts
CHANGED
|
@@ -47,24 +47,15 @@ interface TokenVerifyResult {
|
|
|
47
47
|
*
|
|
48
48
|
* const client = createAuthGateClient({
|
|
49
49
|
* apiKey: process.env.AUTHGATE_API_KEY!,
|
|
50
|
-
* projectId: process.env.AUTHGATE_PROJECT_ID!,
|
|
51
50
|
* baseUrl: "https://authgate.dev",
|
|
52
|
-
* sessionSecret: process.env.SESSION_SECRET!, // min 32 chars
|
|
53
51
|
* });
|
|
54
52
|
* ```
|
|
55
53
|
*/
|
|
56
54
|
interface AuthGateConfig {
|
|
57
|
-
/** Server-side API key for your AuthGate project. Found in the dashboard under API
|
|
55
|
+
/** Server-side API key for your AuthGate project. Found in the dashboard under API Key. */
|
|
58
56
|
apiKey: string;
|
|
59
|
-
/** The project ID from your AuthGate dashboard. */
|
|
60
|
-
projectId: string;
|
|
61
57
|
/** Base URL of the AuthGate service (e.g. `"https://authgate.dev"`). */
|
|
62
58
|
baseUrl: string;
|
|
63
|
-
/**
|
|
64
|
-
* Secret used to derive encryption keys for session cookies and OAuth state.
|
|
65
|
-
* Must be at least 32 characters. Use a cryptographically random string.
|
|
66
|
-
*/
|
|
67
|
-
sessionSecret: string;
|
|
68
59
|
/**
|
|
69
60
|
* Name of the session cookie.
|
|
70
61
|
* @defaultValue `"__authgate"`
|
|
@@ -178,9 +169,7 @@ interface BackupCodesResponse {
|
|
|
178
169
|
* ```ts
|
|
179
170
|
* const client = new AuthGateClient({
|
|
180
171
|
* apiKey: process.env.AUTHGATE_API_KEY!,
|
|
181
|
-
* projectId: process.env.AUTHGATE_PROJECT_ID!,
|
|
182
172
|
* baseUrl: "https://authgate.dev",
|
|
183
|
-
* sessionSecret: process.env.SESSION_SECRET!,
|
|
184
173
|
* });
|
|
185
174
|
* ```
|
|
186
175
|
*/
|
|
@@ -191,7 +180,6 @@ declare class AuthGateClient {
|
|
|
191
180
|
private keysReady;
|
|
192
181
|
/**
|
|
193
182
|
* @param config - Client configuration. See {@link AuthGateConfig}.
|
|
194
|
-
* @throws {@link AuthGateError} if `sessionSecret` is shorter than 32 characters.
|
|
195
183
|
*/
|
|
196
184
|
constructor(config: AuthGateConfig);
|
|
197
185
|
private deriveKeys;
|
|
@@ -270,8 +258,9 @@ declare class AuthGateClient {
|
|
|
270
258
|
* @throws {@link AuthGateError} if the provider is not in {@link SUPPORTED_PROVIDERS}.
|
|
271
259
|
*/
|
|
272
260
|
getOAuthUrl(provider: OAuthProvider, callbackUrl: string, options?: {
|
|
261
|
+
state?: string;
|
|
273
262
|
linkTo?: string;
|
|
274
|
-
}): string
|
|
263
|
+
}): Promise<string>;
|
|
275
264
|
/**
|
|
276
265
|
* Register a new user with email and password.
|
|
277
266
|
*
|
|
@@ -467,8 +456,6 @@ declare class AuthGateClient {
|
|
|
467
456
|
get refreshTokenCookieName(): string;
|
|
468
457
|
/** The callback path where OAuth redirects are handled (default: `"/api/auth/callback"`). */
|
|
469
458
|
get callbackPath(): string;
|
|
470
|
-
/** The AuthGate project ID. */
|
|
471
|
-
get projectId(): string;
|
|
472
459
|
/** The AuthGate service base URL. */
|
|
473
460
|
get baseUrl(): string;
|
|
474
461
|
/** Session lifetime in seconds. */
|
package/dist/index.d.ts
CHANGED
|
@@ -47,24 +47,15 @@ interface TokenVerifyResult {
|
|
|
47
47
|
*
|
|
48
48
|
* const client = createAuthGateClient({
|
|
49
49
|
* apiKey: process.env.AUTHGATE_API_KEY!,
|
|
50
|
-
* projectId: process.env.AUTHGATE_PROJECT_ID!,
|
|
51
50
|
* baseUrl: "https://authgate.dev",
|
|
52
|
-
* sessionSecret: process.env.SESSION_SECRET!, // min 32 chars
|
|
53
51
|
* });
|
|
54
52
|
* ```
|
|
55
53
|
*/
|
|
56
54
|
interface AuthGateConfig {
|
|
57
|
-
/** Server-side API key for your AuthGate project. Found in the dashboard under API
|
|
55
|
+
/** Server-side API key for your AuthGate project. Found in the dashboard under API Key. */
|
|
58
56
|
apiKey: string;
|
|
59
|
-
/** The project ID from your AuthGate dashboard. */
|
|
60
|
-
projectId: string;
|
|
61
57
|
/** Base URL of the AuthGate service (e.g. `"https://authgate.dev"`). */
|
|
62
58
|
baseUrl: string;
|
|
63
|
-
/**
|
|
64
|
-
* Secret used to derive encryption keys for session cookies and OAuth state.
|
|
65
|
-
* Must be at least 32 characters. Use a cryptographically random string.
|
|
66
|
-
*/
|
|
67
|
-
sessionSecret: string;
|
|
68
59
|
/**
|
|
69
60
|
* Name of the session cookie.
|
|
70
61
|
* @defaultValue `"__authgate"`
|
|
@@ -178,9 +169,7 @@ interface BackupCodesResponse {
|
|
|
178
169
|
* ```ts
|
|
179
170
|
* const client = new AuthGateClient({
|
|
180
171
|
* apiKey: process.env.AUTHGATE_API_KEY!,
|
|
181
|
-
* projectId: process.env.AUTHGATE_PROJECT_ID!,
|
|
182
172
|
* baseUrl: "https://authgate.dev",
|
|
183
|
-
* sessionSecret: process.env.SESSION_SECRET!,
|
|
184
173
|
* });
|
|
185
174
|
* ```
|
|
186
175
|
*/
|
|
@@ -191,7 +180,6 @@ declare class AuthGateClient {
|
|
|
191
180
|
private keysReady;
|
|
192
181
|
/**
|
|
193
182
|
* @param config - Client configuration. See {@link AuthGateConfig}.
|
|
194
|
-
* @throws {@link AuthGateError} if `sessionSecret` is shorter than 32 characters.
|
|
195
183
|
*/
|
|
196
184
|
constructor(config: AuthGateConfig);
|
|
197
185
|
private deriveKeys;
|
|
@@ -270,8 +258,9 @@ declare class AuthGateClient {
|
|
|
270
258
|
* @throws {@link AuthGateError} if the provider is not in {@link SUPPORTED_PROVIDERS}.
|
|
271
259
|
*/
|
|
272
260
|
getOAuthUrl(provider: OAuthProvider, callbackUrl: string, options?: {
|
|
261
|
+
state?: string;
|
|
273
262
|
linkTo?: string;
|
|
274
|
-
}): string
|
|
263
|
+
}): Promise<string>;
|
|
275
264
|
/**
|
|
276
265
|
* Register a new user with email and password.
|
|
277
266
|
*
|
|
@@ -467,8 +456,6 @@ declare class AuthGateClient {
|
|
|
467
456
|
get refreshTokenCookieName(): string;
|
|
468
457
|
/** The callback path where OAuth redirects are handled (default: `"/api/auth/callback"`). */
|
|
469
458
|
get callbackPath(): string;
|
|
470
|
-
/** The AuthGate project ID. */
|
|
471
|
-
get projectId(): string;
|
|
472
459
|
/** The AuthGate service base URL. */
|
|
473
460
|
get baseUrl(): string;
|
|
474
461
|
/** Session lifetime in seconds. */
|
package/dist/index.mjs
CHANGED
|
@@ -267,17 +267,11 @@ var InvalidStateError = class extends AuthGateError {
|
|
|
267
267
|
var AuthGateClient = class {
|
|
268
268
|
/**
|
|
269
269
|
* @param config - Client configuration. See {@link AuthGateConfig}.
|
|
270
|
-
* @throws {@link AuthGateError} if `sessionSecret` is shorter than 32 characters.
|
|
271
270
|
*/
|
|
272
271
|
constructor(config) {
|
|
273
272
|
this.encryptionKey = null;
|
|
274
273
|
this.stateKey = null;
|
|
275
274
|
var _a, _b, _c;
|
|
276
|
-
if (!config.sessionSecret || config.sessionSecret.length < 32) {
|
|
277
|
-
throw new AuthGateError(
|
|
278
|
-
"sessionSecret must be at least 32 characters long"
|
|
279
|
-
);
|
|
280
|
-
}
|
|
281
275
|
this.config = __spreadProps(__spreadValues({}, config), {
|
|
282
276
|
cookieName: (_a = config.cookieName) != null ? _a : DEFAULT_COOKIE_NAME,
|
|
283
277
|
sessionMaxAge: (_b = config.sessionMaxAge) != null ? _b : SESSION_MAX_AGE,
|
|
@@ -287,8 +281,8 @@ var AuthGateClient = class {
|
|
|
287
281
|
}
|
|
288
282
|
async deriveKeys() {
|
|
289
283
|
const [encKey, stKey] = await Promise.all([
|
|
290
|
-
deriveEncryptionKey(this.config.
|
|
291
|
-
deriveStateKey(this.config.
|
|
284
|
+
deriveEncryptionKey(this.config.apiKey),
|
|
285
|
+
deriveStateKey(this.config.apiKey)
|
|
292
286
|
]);
|
|
293
287
|
this.encryptionKey = encKey;
|
|
294
288
|
this.stateKey = stKey;
|
|
@@ -425,17 +419,28 @@ var AuthGateClient = class {
|
|
|
425
419
|
* @returns The full authorization URL to redirect the user to.
|
|
426
420
|
* @throws {@link AuthGateError} if the provider is not in {@link SUPPORTED_PROVIDERS}.
|
|
427
421
|
*/
|
|
428
|
-
getOAuthUrl(provider, callbackUrl, options) {
|
|
422
|
+
async getOAuthUrl(provider, callbackUrl, options) {
|
|
429
423
|
if (!SUPPORTED_PROVIDERS.includes(provider)) {
|
|
430
424
|
throw new AuthGateError(`Unsupported provider: ${provider}`);
|
|
431
425
|
}
|
|
432
|
-
const
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
426
|
+
const response = await fetch(`${this.config.baseUrl}/api/proxy/${provider}`, {
|
|
427
|
+
method: "POST",
|
|
428
|
+
headers: {
|
|
429
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
430
|
+
"Content-Type": "application/json"
|
|
431
|
+
},
|
|
432
|
+
body: JSON.stringify({
|
|
433
|
+
callback_url: callbackUrl,
|
|
434
|
+
state: options == null ? void 0 : options.state,
|
|
435
|
+
link_to: options == null ? void 0 : options.linkTo
|
|
436
|
+
})
|
|
437
|
+
});
|
|
438
|
+
if (!response.ok) {
|
|
439
|
+
const error = await response.json().catch(() => ({}));
|
|
440
|
+
throw new AuthGateError(error.error || `OAuth initiation failed: ${response.status}`);
|
|
437
441
|
}
|
|
438
|
-
|
|
442
|
+
const data = await response.json();
|
|
443
|
+
return data.redirect_url;
|
|
439
444
|
}
|
|
440
445
|
// ---------------------------------------------------------------------------
|
|
441
446
|
// Email auth
|
|
@@ -449,12 +454,14 @@ var AuthGateClient = class {
|
|
|
449
454
|
async emailSignup(data) {
|
|
450
455
|
return fetch(`${this.config.baseUrl}/api/proxy/email/signup`, {
|
|
451
456
|
method: "POST",
|
|
452
|
-
headers: {
|
|
457
|
+
headers: {
|
|
458
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
459
|
+
"Content-Type": "application/json"
|
|
460
|
+
},
|
|
453
461
|
body: JSON.stringify({
|
|
454
462
|
email: data.email,
|
|
455
463
|
password: data.password,
|
|
456
464
|
name: data.name,
|
|
457
|
-
project_id: this.config.projectId,
|
|
458
465
|
callback_url: data.callbackUrl
|
|
459
466
|
})
|
|
460
467
|
});
|
|
@@ -471,11 +478,13 @@ var AuthGateClient = class {
|
|
|
471
478
|
async emailSignin(data) {
|
|
472
479
|
return fetch(`${this.config.baseUrl}/api/proxy/email/signin`, {
|
|
473
480
|
method: "POST",
|
|
474
|
-
headers: {
|
|
481
|
+
headers: {
|
|
482
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
483
|
+
"Content-Type": "application/json"
|
|
484
|
+
},
|
|
475
485
|
body: JSON.stringify({
|
|
476
486
|
email: data.email,
|
|
477
487
|
password: data.password,
|
|
478
|
-
project_id: this.config.projectId,
|
|
479
488
|
callback_url: data.callbackUrl
|
|
480
489
|
})
|
|
481
490
|
});
|
|
@@ -489,10 +498,12 @@ var AuthGateClient = class {
|
|
|
489
498
|
async emailForgotPassword(data) {
|
|
490
499
|
return fetch(`${this.config.baseUrl}/api/proxy/email/reset-password`, {
|
|
491
500
|
method: "POST",
|
|
492
|
-
headers: {
|
|
501
|
+
headers: {
|
|
502
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
503
|
+
"Content-Type": "application/json"
|
|
504
|
+
},
|
|
493
505
|
body: JSON.stringify({
|
|
494
506
|
email: data.email,
|
|
495
|
-
project_id: this.config.projectId,
|
|
496
507
|
callback_url: data.callbackUrl
|
|
497
508
|
})
|
|
498
509
|
});
|
|
@@ -508,11 +519,13 @@ var AuthGateClient = class {
|
|
|
508
519
|
`${this.config.baseUrl}/api/proxy/email/reset-password/confirm`,
|
|
509
520
|
{
|
|
510
521
|
method: "POST",
|
|
511
|
-
headers: {
|
|
522
|
+
headers: {
|
|
523
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
524
|
+
"Content-Type": "application/json"
|
|
525
|
+
},
|
|
512
526
|
body: JSON.stringify({
|
|
513
527
|
token: data.token,
|
|
514
|
-
password: data.password
|
|
515
|
-
project_id: this.config.projectId
|
|
528
|
+
password: data.password
|
|
516
529
|
})
|
|
517
530
|
}
|
|
518
531
|
);
|
|
@@ -531,10 +544,12 @@ var AuthGateClient = class {
|
|
|
531
544
|
`${this.config.baseUrl}/api/proxy/email/magic-link/send`,
|
|
532
545
|
{
|
|
533
546
|
method: "POST",
|
|
534
|
-
headers: {
|
|
547
|
+
headers: {
|
|
548
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
549
|
+
"Content-Type": "application/json"
|
|
550
|
+
},
|
|
535
551
|
body: JSON.stringify({
|
|
536
552
|
email: data.email,
|
|
537
|
-
project_id: this.config.projectId,
|
|
538
553
|
callback_url: data.callbackUrl
|
|
539
554
|
})
|
|
540
555
|
}
|
|
@@ -552,11 +567,13 @@ var AuthGateClient = class {
|
|
|
552
567
|
async emailVerifyCode(data) {
|
|
553
568
|
return fetch(`${this.config.baseUrl}/api/proxy/email/verify`, {
|
|
554
569
|
method: "POST",
|
|
555
|
-
headers: {
|
|
570
|
+
headers: {
|
|
571
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
572
|
+
"Content-Type": "application/json"
|
|
573
|
+
},
|
|
556
574
|
body: JSON.stringify({
|
|
557
575
|
email: data.email,
|
|
558
|
-
code: data.code
|
|
559
|
-
project_id: this.config.projectId
|
|
576
|
+
code: data.code
|
|
560
577
|
})
|
|
561
578
|
});
|
|
562
579
|
}
|
|
@@ -572,10 +589,12 @@ var AuthGateClient = class {
|
|
|
572
589
|
async smsSendCode(data) {
|
|
573
590
|
return fetch(`${this.config.baseUrl}/api/proxy/sms/send-code`, {
|
|
574
591
|
method: "POST",
|
|
575
|
-
headers: {
|
|
592
|
+
headers: {
|
|
593
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
594
|
+
"Content-Type": "application/json"
|
|
595
|
+
},
|
|
576
596
|
body: JSON.stringify({
|
|
577
597
|
phone: data.phone,
|
|
578
|
-
project_id: this.config.projectId,
|
|
579
598
|
callback_url: data.callbackUrl
|
|
580
599
|
})
|
|
581
600
|
});
|
|
@@ -589,11 +608,13 @@ var AuthGateClient = class {
|
|
|
589
608
|
async smsVerifyCode(data) {
|
|
590
609
|
return fetch(`${this.config.baseUrl}/api/proxy/sms/verify-code`, {
|
|
591
610
|
method: "POST",
|
|
592
|
-
headers: {
|
|
611
|
+
headers: {
|
|
612
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
613
|
+
"Content-Type": "application/json"
|
|
614
|
+
},
|
|
593
615
|
body: JSON.stringify({
|
|
594
616
|
phone: data.phone,
|
|
595
617
|
code: data.code,
|
|
596
|
-
project_id: this.config.projectId,
|
|
597
618
|
callback_url: data.callbackUrl
|
|
598
619
|
})
|
|
599
620
|
});
|
|
@@ -616,10 +637,12 @@ var AuthGateClient = class {
|
|
|
616
637
|
`${this.config.baseUrl}/api/proxy/token/refresh`,
|
|
617
638
|
{
|
|
618
639
|
method: "POST",
|
|
619
|
-
headers: {
|
|
640
|
+
headers: {
|
|
641
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
642
|
+
"Content-Type": "application/json"
|
|
643
|
+
},
|
|
620
644
|
body: JSON.stringify({
|
|
621
|
-
refresh_token: refreshToken
|
|
622
|
-
project_id: this.config.projectId
|
|
645
|
+
refresh_token: refreshToken
|
|
623
646
|
})
|
|
624
647
|
}
|
|
625
648
|
);
|
|
@@ -644,10 +667,12 @@ var AuthGateClient = class {
|
|
|
644
667
|
`${this.config.baseUrl}/api/proxy/sessions/revoke`,
|
|
645
668
|
{
|
|
646
669
|
method: "POST",
|
|
647
|
-
headers: {
|
|
670
|
+
headers: {
|
|
671
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
672
|
+
"Content-Type": "application/json"
|
|
673
|
+
},
|
|
648
674
|
body: JSON.stringify({
|
|
649
|
-
refresh_token: refreshToken
|
|
650
|
-
project_id: this.config.projectId
|
|
675
|
+
refresh_token: refreshToken
|
|
651
676
|
})
|
|
652
677
|
}
|
|
653
678
|
);
|
|
@@ -670,10 +695,11 @@ var AuthGateClient = class {
|
|
|
670
695
|
async mfaVerify(data) {
|
|
671
696
|
return fetch(`${this.config.baseUrl}/api/proxy/mfa/verify`, {
|
|
672
697
|
method: "POST",
|
|
673
|
-
headers: {
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
}
|
|
698
|
+
headers: {
|
|
699
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
700
|
+
"Content-Type": "application/json"
|
|
701
|
+
},
|
|
702
|
+
body: JSON.stringify(data)
|
|
677
703
|
});
|
|
678
704
|
}
|
|
679
705
|
/**
|
|
@@ -804,11 +830,11 @@ var AuthGateClient = class {
|
|
|
804
830
|
async mfaSmsSendCode(challenge) {
|
|
805
831
|
return fetch(`${this.config.baseUrl}/api/proxy/mfa/sms/send-code`, {
|
|
806
832
|
method: "POST",
|
|
807
|
-
headers: {
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
})
|
|
833
|
+
headers: {
|
|
834
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
835
|
+
"Content-Type": "application/json"
|
|
836
|
+
},
|
|
837
|
+
body: JSON.stringify({ challenge })
|
|
812
838
|
});
|
|
813
839
|
}
|
|
814
840
|
// ---------------------------------------------------------------------------
|
|
@@ -830,10 +856,6 @@ var AuthGateClient = class {
|
|
|
830
856
|
get callbackPath() {
|
|
831
857
|
return this.config.callbackPath;
|
|
832
858
|
}
|
|
833
|
-
/** The AuthGate project ID. */
|
|
834
|
-
get projectId() {
|
|
835
|
-
return this.config.projectId;
|
|
836
|
-
}
|
|
837
859
|
/** The AuthGate service base URL. */
|
|
838
860
|
get baseUrl() {
|
|
839
861
|
return this.config.baseUrl;
|