@c9up/warden 0.1.22 → 0.1.24

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 (81) hide show
  1. package/README.md +1 -1
  2. package/dist/AuthManager.d.ts +14 -3
  3. package/dist/AuthManager.d.ts.map +1 -1
  4. package/dist/AuthManager.js +32 -4
  5. package/dist/AuthManager.js.map +1 -1
  6. package/dist/Authenticator.d.ts +49 -5
  7. package/dist/Authenticator.d.ts.map +1 -1
  8. package/dist/Authenticator.js +235 -26
  9. package/dist/Authenticator.js.map +1 -1
  10. package/dist/WardenProvider.js +1 -1
  11. package/dist/WardenProvider.js.map +1 -1
  12. package/dist/bouncer/Bouncer.d.ts +2 -2
  13. package/dist/bouncer/Bouncer.d.ts.map +1 -1
  14. package/dist/bouncer/Bouncer.js +1 -1
  15. package/dist/bouncer/PolicyAuthorizer.d.ts +1 -1
  16. package/dist/bouncer/PolicyAuthorizer.d.ts.map +1 -1
  17. package/dist/bouncer/PolicyAuthorizer.js +1 -1
  18. package/dist/bouncer/PolicyAuthorizer.js.map +1 -1
  19. package/dist/config.d.ts +8 -1
  20. package/dist/config.d.ts.map +1 -1
  21. package/dist/config.js.map +1 -1
  22. package/dist/errors.d.ts.map +1 -1
  23. package/dist/errors.js +11 -7
  24. package/dist/errors.js.map +1 -1
  25. package/dist/index.d.ts +3 -7
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +1 -4
  28. package/dist/index.js.map +1 -1
  29. package/dist/middleware.d.ts +14 -1
  30. package/dist/middleware.d.ts.map +1 -1
  31. package/dist/middleware.js +7 -5
  32. package/dist/middleware.js.map +1 -1
  33. package/dist/native.js +2 -2
  34. package/dist/native.js.map +1 -1
  35. package/dist/standalone.js +3 -3
  36. package/dist/standalone.js.map +1 -1
  37. package/dist/strategies/JwtStrategy.js +2 -2
  38. package/dist/strategies/JwtStrategy.js.map +1 -1
  39. package/dist/strategies/SessionStrategy.d.ts +37 -25
  40. package/dist/strategies/SessionStrategy.d.ts.map +1 -1
  41. package/dist/strategies/SessionStrategy.js +43 -44
  42. package/dist/strategies/SessionStrategy.js.map +1 -1
  43. package/index.darwin-arm64.node +0 -0
  44. package/index.darwin-x64.node +0 -0
  45. package/index.linux-arm64-gnu.node +0 -0
  46. package/index.linux-x64-gnu.node +0 -0
  47. package/index.win32-x64-msvc.node +0 -0
  48. package/package.json +1 -1
  49. package/src/AuthManager.ts +68 -6
  50. package/src/Authenticator.ts +299 -28
  51. package/src/WardenProvider.ts +1 -1
  52. package/src/bouncer/Bouncer.ts +2 -2
  53. package/src/bouncer/PolicyAuthorizer.ts +1 -1
  54. package/src/config.ts +8 -1
  55. package/src/errors.ts +11 -7
  56. package/src/index.ts +9 -11
  57. package/src/middleware.ts +24 -5
  58. package/src/native.ts +2 -2
  59. package/src/standalone.ts +3 -3
  60. package/src/strategies/JwtStrategy.ts +2 -2
  61. package/src/strategies/SessionStrategy.ts +73 -46
  62. package/dist/firstcontact/FirstContactManager.d.ts +0 -25
  63. package/dist/firstcontact/FirstContactManager.d.ts.map +0 -1
  64. package/dist/firstcontact/FirstContactManager.js +0 -38
  65. package/dist/firstcontact/FirstContactManager.js.map +0 -1
  66. package/dist/firstcontact/drivers/GitHubDriver.d.ts +0 -14
  67. package/dist/firstcontact/drivers/GitHubDriver.d.ts.map +0 -1
  68. package/dist/firstcontact/drivers/GitHubDriver.js +0 -60
  69. package/dist/firstcontact/drivers/GitHubDriver.js.map +0 -1
  70. package/dist/firstcontact/drivers/GoogleDriver.d.ts +0 -14
  71. package/dist/firstcontact/drivers/GoogleDriver.d.ts.map +0 -1
  72. package/dist/firstcontact/drivers/GoogleDriver.js +0 -63
  73. package/dist/firstcontact/drivers/GoogleDriver.js.map +0 -1
  74. package/dist/firstcontact/types.d.ts +0 -44
  75. package/dist/firstcontact/types.d.ts.map +0 -1
  76. package/dist/firstcontact/types.js +0 -22
  77. package/dist/firstcontact/types.js.map +0 -1
  78. package/src/firstcontact/FirstContactManager.ts +0 -54
  79. package/src/firstcontact/drivers/GitHubDriver.ts +0 -77
  80. package/src/firstcontact/drivers/GoogleDriver.ts +0 -80
  81. package/src/firstcontact/types.ts +0 -62
@@ -1,80 +0,0 @@
1
- /**
2
- * Google OAuth2 driver for FirstContact.
3
- */
4
-
5
- import type {
6
- FirstContactDriver,
7
- OAuthConfig,
8
- OAuthToken,
9
- OAuthUser,
10
- } from "../types.js";
11
- import { assertOAuthState } from "../types.js";
12
-
13
- export class GoogleDriver implements FirstContactDriver {
14
- constructor(private config: OAuthConfig) {}
15
-
16
- redirectUrl(state?: string): string {
17
- const params = new URLSearchParams({
18
- client_id: this.config.clientId,
19
- redirect_uri: this.config.callbackUrl,
20
- response_type: "code",
21
- scope: (this.config.scopes ?? ["openid", "email", "profile"]).join(" "),
22
- access_type: "offline",
23
- ...(state ? { state } : {}),
24
- });
25
- return `https://accounts.google.com/o/oauth2/v2/auth?${params}`;
26
- }
27
-
28
- async callback(
29
- code: string,
30
- state?: string,
31
- expectedState?: string,
32
- ): Promise<{ user: OAuthUser; token: OAuthToken }> {
33
- // CSRF protection: validate state matches what we sent in redirectUrl().
34
- assertOAuthState(state, expectedState);
35
- const tokenRes = await fetch("https://oauth2.googleapis.com/token", {
36
- method: "POST",
37
- headers: { "Content-Type": "application/x-www-form-urlencoded" },
38
- body: new URLSearchParams({
39
- code,
40
- client_id: this.config.clientId,
41
- client_secret: this.config.clientSecret,
42
- redirect_uri: this.config.callbackUrl,
43
- grant_type: "authorization_code",
44
- }),
45
- });
46
- if (!tokenRes.ok) {
47
- throw new Error(
48
- `Google OAuth token exchange failed (HTTP ${tokenRes.status})`,
49
- );
50
- }
51
- const tokens = (await tokenRes.json()) as Record<string, unknown>;
52
- if (!tokens.access_token)
53
- throw new Error("Google OAuth: no access_token in response");
54
-
55
- const userRes = await fetch(
56
- "https://www.googleapis.com/oauth2/v2/userinfo",
57
- {
58
- headers: { Authorization: `Bearer ${tokens.access_token}` },
59
- },
60
- );
61
- if (!userRes.ok)
62
- throw new Error(`Google userinfo failed (${userRes.status})`);
63
- const raw = (await userRes.json()) as Record<string, unknown>;
64
-
65
- return {
66
- user: {
67
- id: String(raw.id ?? ""),
68
- email: String(raw.email ?? ""),
69
- name: String(raw.name ?? ""),
70
- avatarUrl: raw.picture as string | undefined,
71
- raw,
72
- },
73
- token: {
74
- accessToken: String(tokens.access_token),
75
- refreshToken: tokens.refresh_token as string | undefined,
76
- expiresIn: tokens.expires_in as number | undefined,
77
- },
78
- };
79
- }
80
- }
@@ -1,62 +0,0 @@
1
- /**
2
- * FirstContact — OAuth2 social authentication types.
3
- */
4
-
5
- export interface OAuthConfig {
6
- clientId: string;
7
- clientSecret: string;
8
- callbackUrl: string;
9
- scopes?: string[];
10
- }
11
-
12
- export interface OAuthUser {
13
- id: string;
14
- email: string;
15
- name: string;
16
- avatarUrl?: string;
17
- raw: Record<string, unknown>;
18
- }
19
-
20
- export interface OAuthToken {
21
- accessToken: string;
22
- refreshToken?: string;
23
- expiresIn?: number;
24
- }
25
-
26
- export interface FirstContactDriver {
27
- redirectUrl(state?: string): string;
28
- /**
29
- * Handle the OAuth callback. `state` MUST be validated against the value
30
- * originally passed to `redirectUrl()` to prevent CSRF login attacks.
31
- * Throws if state is missing or mismatched.
32
- */
33
- callback(
34
- code: string,
35
- state?: string,
36
- expectedState?: string,
37
- ): Promise<{ user: OAuthUser; token: OAuthToken }>;
38
- }
39
-
40
- /**
41
- * Check the OAuth `state` round-trip, failing CLOSED.
42
- *
43
- * The check used to be `if (expectedState && state !== expectedState)`, so a
44
- * caller that passed no expected state got NO CSRF protection and no sign that
45
- * it was missing. A driver used directly — without the manager, which already
46
- * refuses — was therefore open by default. Anything that cannot be verified is
47
- * refused instead.
48
- */
49
- export function assertOAuthState(
50
- state: string | undefined,
51
- expectedState: string | undefined,
52
- ): void {
53
- if (!expectedState) {
54
- throw new Error(
55
- "[warden] OAuth callback requires expectedState for CSRF protection. " +
56
- "Store the state given to redirectUrl() in the session and pass it here.",
57
- );
58
- }
59
- if (state !== expectedState) {
60
- throw new Error("OAuth state mismatch — possible CSRF attack");
61
- }
62
- }