@draftlab/auth 0.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.
Files changed (86) hide show
  1. package/dist/adapters/node.d.ts +18 -0
  2. package/dist/adapters/node.js +71 -0
  3. package/dist/allow-CixonwTW.d.ts +59 -0
  4. package/dist/allow-DX5cehSc.js +63 -0
  5. package/dist/allow.d.ts +2 -0
  6. package/dist/allow.js +4 -0
  7. package/dist/base-DRutbxgL.js +422 -0
  8. package/dist/client.d.ts +413 -0
  9. package/dist/client.js +209 -0
  10. package/dist/code-l_uvMR1j.d.ts +212 -0
  11. package/dist/core-8WTqfnb4.d.ts +129 -0
  12. package/dist/core-CncE5rPg.js +498 -0
  13. package/dist/core.d.ts +9 -0
  14. package/dist/core.js +14 -0
  15. package/dist/error-CWAdNAzm.d.ts +243 -0
  16. package/dist/error-DgAKK7b2.js +237 -0
  17. package/dist/error.d.ts +2 -0
  18. package/dist/error.js +3 -0
  19. package/dist/form-6XKM_cOk.js +61 -0
  20. package/dist/icon-Ci5uqGB_.js +192 -0
  21. package/dist/index.d.ts +9 -0
  22. package/dist/index.js +14 -0
  23. package/dist/keys-EEfxEGfO.js +140 -0
  24. package/dist/keys.d.ts +67 -0
  25. package/dist/keys.js +5 -0
  26. package/dist/oauth2-B7-6Z7Lc.js +155 -0
  27. package/dist/oauth2-DtKwtl8p.d.ts +176 -0
  28. package/dist/password-Cm0dRMwa.d.ts +385 -0
  29. package/dist/pkce-276Za_rZ.js +162 -0
  30. package/dist/pkce.d.ts +72 -0
  31. package/dist/pkce.js +3 -0
  32. package/dist/provider/code.d.ts +4 -0
  33. package/dist/provider/code.js +145 -0
  34. package/dist/provider/facebook.d.ts +137 -0
  35. package/dist/provider/facebook.js +85 -0
  36. package/dist/provider/github.d.ts +141 -0
  37. package/dist/provider/github.js +88 -0
  38. package/dist/provider/google.d.ts +113 -0
  39. package/dist/provider/google.js +62 -0
  40. package/dist/provider/oauth2.d.ts +4 -0
  41. package/dist/provider/oauth2.js +7 -0
  42. package/dist/provider/password.d.ts +4 -0
  43. package/dist/provider/password.js +366 -0
  44. package/dist/provider/provider.d.ts +3 -0
  45. package/dist/provider/provider.js +44 -0
  46. package/dist/provider-CwWMG-1l.d.ts +227 -0
  47. package/dist/random-SXMYlaVr.js +87 -0
  48. package/dist/random.d.ts +66 -0
  49. package/dist/random.js +3 -0
  50. package/dist/select-BjySLL8I.js +280 -0
  51. package/dist/storage/memory.d.ts +82 -0
  52. package/dist/storage/memory.js +127 -0
  53. package/dist/storage/storage.d.ts +2 -0
  54. package/dist/storage/storage.js +3 -0
  55. package/dist/storage/turso.d.ts +31 -0
  56. package/dist/storage/turso.js +117 -0
  57. package/dist/storage/unstorage.d.ts +38 -0
  58. package/dist/storage/unstorage.js +97 -0
  59. package/dist/storage-BEaqEPNQ.js +62 -0
  60. package/dist/storage-CxKerLlc.d.ts +162 -0
  61. package/dist/subject-DiQdRWGt.d.ts +62 -0
  62. package/dist/subject.d.ts +3 -0
  63. package/dist/subject.js +36 -0
  64. package/dist/theme-C9by7VXf.d.ts +209 -0
  65. package/dist/theme-CswaLtbW.js +120 -0
  66. package/dist/themes/theme.d.ts +2 -0
  67. package/dist/themes/theme.js +3 -0
  68. package/dist/types.d.ts +94 -0
  69. package/dist/types.js +0 -0
  70. package/dist/ui/base.d.ts +43 -0
  71. package/dist/ui/base.js +4 -0
  72. package/dist/ui/code.d.ts +158 -0
  73. package/dist/ui/code.js +197 -0
  74. package/dist/ui/form.d.ts +31 -0
  75. package/dist/ui/form.js +3 -0
  76. package/dist/ui/icon.d.ts +98 -0
  77. package/dist/ui/icon.js +3 -0
  78. package/dist/ui/password.d.ts +54 -0
  79. package/dist/ui/password.js +300 -0
  80. package/dist/ui/select.d.ts +233 -0
  81. package/dist/ui/select.js +6 -0
  82. package/dist/util-CSdHUFOo.js +108 -0
  83. package/dist/util-ChlgVqPN.d.ts +72 -0
  84. package/dist/util.d.ts +2 -0
  85. package/dist/util.js +3 -0
  86. package/package.json +63 -0
@@ -0,0 +1,192 @@
1
+ //#region src/ui/icon.ts
2
+ /**
3
+ * Icon components for Draft Auth UI.
4
+ * Provides SVG icons for various authentication providers and common UI elements.
5
+ *
6
+ * ## Usage
7
+ *
8
+ * ```ts
9
+ * import { ICON_GITHUB, ICON_GOOGLE, ICON_EMAIL } from "./icon"
10
+ *
11
+ * // In provider buttons
12
+ * const buttonHtml = `
13
+ * <button>
14
+ * ${ICON_GITHUB}
15
+ * Continue with GitHub
16
+ * </button>
17
+ * `
18
+ *
19
+ * // In form elements
20
+ * const formHtml = `
21
+ * <div>
22
+ * ${ICON_EMAIL}
23
+ * Email verification
24
+ * </div>
25
+ * `
26
+ * ```
27
+ *
28
+ * ## Features
29
+ *
30
+ * - **Consistent sizing**: All icons are properly sized and scalable
31
+ * - **Accessibility**: Proper ARIA labels and semantic markup
32
+ * - **Current color**: Icons inherit text color for easy theming
33
+ * - **Optimized SVGs**: Clean, minimal SVG markup for performance
34
+ *
35
+ * @packageDocumentation
36
+ */
37
+ /**
38
+ * GitHub brand icon with official logo design.
39
+ * Used for GitHub authentication provider buttons and references.
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * const buttonHtml = `
44
+ * <button data-component="provider-button">
45
+ * ${ICON_GITHUB}
46
+ * Sign in with GitHub
47
+ * </button>
48
+ * `
49
+ * ```
50
+ */
51
+ const ICON_GITHUB = `
52
+ <svg
53
+ aria-label="GitHub"
54
+ fill="currentColor"
55
+ height="250"
56
+ preserveAspectRatio="xMidYMid"
57
+ role="img"
58
+ viewBox="0 0 256 250"
59
+ width="256"
60
+ xmlns="http://www.w3.org/2000/svg"
61
+ >
62
+ <path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46 6.397 1.185 8.746-2.777 8.746-6.158 0-3.052-.12-13.135-.174-23.83-35.61 7.742-43.124-15.103-43.124-15.103-5.823-14.795-14.213-18.73-14.213-18.73-11.613-7.944.876-7.78.876-7.78 12.853.902 19.621 13.19 19.621 13.19 11.417 19.568 29.945 13.911 37.249 10.64 1.149-8.272 4.466-13.92 8.127-17.116-28.431-3.236-58.318-14.212-58.318-63.258 0-13.975 5-25.394 13.188-34.358-1.329-3.224-5.71-16.242 1.24-33.874 0 0 10.749-3.44 35.21 13.121 10.21-2.836 21.16-4.258 32.038-4.307 10.878.049 21.837 1.47 32.066 4.307 24.431-16.56 35.165-13.12 35.165-13.12 6.967 17.63 2.584 30.65 1.255 33.873 8.207 8.964 13.173 20.383 13.173 34.358 0 49.163-29.944 59.988-58.447 63.157 4.591 3.972 8.682 11.762 8.682 23.704 0 17.126-.148 30.91-.148 35.126 0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002 256 57.307 198.691 0 128.001 0Zm-80.06 182.34c-.282.636-1.283.827-2.194.39-.929-.417-1.45-1.284-1.15-1.922.276-.655 1.279-.838 2.205-.399.93.418 1.46 1.293 1.139 1.931Zm6.296 5.618c-.61.566-1.804.303-2.614-.591-.837-.892-.994-2.086-.375-2.66.63-.566 1.787-.301 2.626.591.838.903 1 2.088.363 2.66Zm4.32 7.188c-.785.545-2.067.034-2.86-1.104-.784-1.138-.784-2.503.017-3.05.795-.547 2.058-.055 2.861 1.075.782 1.157.782 2.522-.019 3.08Zm7.304 8.325c-.701.774-2.196.566-3.29-.49-1.119-1.032-1.43-2.496-.726-3.27.71-.776 2.213-.558 3.315.49 1.11 1.03 1.45 2.505.701 3.27Zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033-1.448-.439-2.395-1.613-2.103-2.626.301-1.01 1.747-1.484 3.207-1.028 1.446.436 2.396 1.602 2.095 2.622Zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95-1.53.034-2.769-.82-2.786-1.86 0-1.065 1.202-1.932 2.733-1.958 1.522-.03 2.768.818 2.768 1.868Zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37-1.485.271-2.861-.365-3.05-1.386-.184-1.056.893-2.114 2.376-2.387 1.514-.263 2.868.356 3.061 1.403Z" />
63
+ </svg>
64
+ `;
65
+ /**
66
+ * Google brand icon with official multicolor logo design.
67
+ * Used for Google authentication provider buttons and references.
68
+ *
69
+ * @example
70
+ * ```ts
71
+ * const buttonHtml = `
72
+ * <button data-component="provider-button">
73
+ * ${ICON_GOOGLE}
74
+ * Continue with Google
75
+ * </button>
76
+ * `
77
+ * ```
78
+ */
79
+ const ICON_GOOGLE = `
80
+ <svg
81
+ aria-label="Google"
82
+ height="262"
83
+ preserveAspectRatio="xMidYMid"
84
+ role="img"
85
+ viewBox="0 0 256 262"
86
+ width="256"
87
+ xmlns="http://www.w3.org/2000/svg"
88
+ >
89
+ <path
90
+ d="M255.878 133.451c0-10.734-.871-18.567-2.756-26.69H130.55v48.448h71.947c-1.45 12.04-9.283 30.172-26.69 42.356l-.244 1.622 38.755 30.023 2.685.268c24.659-22.774 38.875-56.282 38.875-96.027"
91
+ fill="#4285F4"
92
+ />
93
+ <path
94
+ d="M130.55 261.1c35.248 0 64.839-11.605 86.453-31.622l-41.196-31.913c-11.024 7.688-25.82 13.055-45.257 13.055-34.523 0-63.824-22.773-74.269-54.25l-1.531.13-40.298 31.187-.527 1.465C35.393 231.798 79.49 261.1 130.55 261.1"
95
+ fill="#34A853"
96
+ />
97
+ <path
98
+ d="M56.281 156.37c-2.756-8.123-4.351-16.827-4.351-25.82 0-8.994 1.595-17.697 4.206-25.82l-.073-1.73L15.26 71.312l-1.335.635C5.077 89.644 0 109.517 0 130.55s5.077 40.905 13.925 58.602l42.356-32.782"
99
+ fill="#FBBC05"
100
+ />
101
+ <path
102
+ d="M130.55 50.479c24.514 0 41.05 10.589 50.479 19.438l36.844-35.974C195.245 12.91 165.798 0 130.55 0 79.49 0 35.393 29.301 13.925 71.947l42.211 32.783c10.59-31.477 39.891-54.251 74.414-54.251"
103
+ fill="#EB4335"
104
+ />
105
+ </svg>
106
+ `;
107
+ /**
108
+ * Email envelope icon for email-related authentication flows.
109
+ * Used in email verification, password reset, and contact forms.
110
+ *
111
+ * @example
112
+ * ```ts
113
+ * const inputGroupHtml = `
114
+ * <div data-component="input-group">
115
+ * ${ICON_EMAIL}
116
+ * <input type="email" placeholder="Enter your email" />
117
+ * </div>
118
+ * `
119
+ * ```
120
+ */
121
+ const ICON_EMAIL = `
122
+ <svg
123
+ aria-label="Email"
124
+ fill="none"
125
+ role="img"
126
+ stroke="currentColor"
127
+ stroke-width="1.5"
128
+ viewBox="0 0 24 24"
129
+ xmlns="http://www.w3.org/2000/svg"
130
+ >
131
+ <path
132
+ d="M21.75 6.75v10.5a2.25 2.25 0 0 1-2.25 2.25h-15a2.25 2.25 0 0 1-2.25-2.25V6.75m19.5 0A2.25 2.25 0 0 0 19.5 4.5h-15a2.25 2.25 0 0 0-2.25 2.25m19.5 0v.243a2.25 2.25 0 0 1-1.07 1.916l-7.5 4.615a2.25 2.25 0 0 1-2.36 0L3.32 8.91a2.25 2.25 0 0 1-1.07-1.916V6.75"
133
+ stroke-linecap="round"
134
+ stroke-linejoin="round"
135
+ />
136
+ </svg>
137
+ `;
138
+ /**
139
+ * Slack brand icon with official logo design.
140
+ * Used for Slack authentication provider buttons and workplace integrations.
141
+ *
142
+ * @example
143
+ * ```ts
144
+ * const buttonHtml = `
145
+ * <button data-component="provider-button">
146
+ * ${ICON_SLACK}
147
+ * Continue with Slack
148
+ * </button>
149
+ * `
150
+ * ```
151
+ */
152
+ const ICON_SLACK = `
153
+ <svg
154
+ aria-label="Slack"
155
+ fill="none"
156
+ height="24"
157
+ role="img"
158
+ viewBox="0 0 24 24"
159
+ width="24"
160
+ xmlns="http://www.w3.org/2000/svg"
161
+ >
162
+ <g>
163
+ <path
164
+ clip-rule="evenodd"
165
+ d="M8.79948 0C7.47279 0.000978593 6.39909 1.07547 6.40007 2.39951C6.39909 3.72355 7.47377 4.79804 8.80046 4.79902H11.2009V2.40049C11.2018 1.07645 10.1271 0.00195719 8.79948 0ZM8.79948 6.4H2.40039C1.07371 6.40098 -0.000977873 7.47547 2.67973e-06 8.79951C-0.00195842 10.1235 1.07273 11.198 2.39941 11.2H8.79948C10.1262 11.199 11.2009 10.1245 11.1999 8.80049C11.2009 7.47547 10.1262 6.40098 8.79948 6.4Z"
166
+ fill="currentColor"
167
+ fill-rule="evenodd"
168
+ />
169
+ <path
170
+ clip-rule="evenodd"
171
+ d="M24.0007 8.79951C24.0016 7.47547 22.9269 6.40098 21.6003 6.4C20.2736 6.40098 19.1989 7.47547 19.1999 8.79951V11.2H21.6003C22.9269 11.199 24.0016 10.1245 24.0007 8.79951ZM17.6006 8.79951V2.39951C17.6016 1.07645 16.5279 0.00195719 15.2012 0C13.8745 0.000978593 12.7998 1.07547 12.8008 2.39951V8.79951C12.7988 10.1235 13.8735 11.198 15.2002 11.2C16.5269 11.199 17.6016 10.1245 17.6006 8.79951Z"
172
+ fill="currentColor"
173
+ fill-rule="evenodd"
174
+ />
175
+ <path
176
+ clip-rule="evenodd"
177
+ d="M15.1992 23.9998C16.5259 23.9988 17.6006 22.9243 17.5996 21.6003C17.6006 20.2763 16.5259 19.2018 15.1992 19.2008H12.7988V21.6003C12.7978 22.9234 13.8725 23.9978 15.1992 23.9998ZM15.1992 17.5988H21.5993C22.926 17.5978 24.0007 16.5234 23.9997 15.1993C24.0016 13.8753 22.927 12.8008 21.6003 12.7988H15.2002C13.8735 12.7998 12.7988 13.8743 12.7998 15.1983C12.7988 16.5234 13.8725 17.5978 15.1992 17.5988Z"
178
+ fill="currentColor"
179
+ fill-rule="evenodd"
180
+ />
181
+ <path
182
+ clip-rule="evenodd"
183
+ d="M0 15.1993C-0.000979882 16.5234 1.07371 17.5978 2.40039 17.5988C3.72708 17.5978 4.80177 16.5234 4.80079 15.1993V12.7998H2.40039C1.07371 12.8008 -0.000979882 13.8753 0 15.1993ZM6.40007 15.1993V21.5993C6.3981 22.9234 7.47279 23.9978 8.79948 23.9998C10.1262 23.9988 11.2009 22.9243 11.1999 21.6003V15.2013C11.2018 13.8772 10.1271 12.8027 8.80046 12.8008C7.47279 12.8008 6.39909 13.8753 6.40007 15.1993Z"
184
+ fill="currentColor"
185
+ fill-rule="evenodd"
186
+ />
187
+ </g>
188
+ </svg>
189
+ `;
190
+
191
+ //#endregion
192
+ export { ICON_EMAIL, ICON_GITHUB, ICON_GOOGLE, ICON_SLACK };
@@ -0,0 +1,9 @@
1
+ import "./allow-CixonwTW.js";
2
+ import "./error-CWAdNAzm.js";
3
+ import "./util-ChlgVqPN.js";
4
+ import "./subject-DiQdRWGt.js";
5
+ import "./storage-CxKerLlc.js";
6
+ import "./provider-CwWMG-1l.js";
7
+ import "./theme-C9by7VXf.js";
8
+ import { issuer } from "./core-8WTqfnb4.js";
9
+ export { issuer };
package/dist/index.js ADDED
@@ -0,0 +1,14 @@
1
+ import "./util-CSdHUFOo.js";
2
+ import "./allow-DX5cehSc.js";
3
+ import "./error-DgAKK7b2.js";
4
+ import "./pkce-276Za_rZ.js";
5
+ import "./random-SXMYlaVr.js";
6
+ import "./storage-BEaqEPNQ.js";
7
+ import "./keys-EEfxEGfO.js";
8
+ import "./theme-CswaLtbW.js";
9
+ import "./base-DRutbxgL.js";
10
+ import "./icon-Ci5uqGB_.js";
11
+ import "./select-BjySLL8I.js";
12
+ import { issuer } from "./core-CncE5rPg.js";
13
+
14
+ export { issuer };
@@ -0,0 +1,140 @@
1
+ import { generateSecureToken } from "./random-SXMYlaVr.js";
2
+ import { Storage } from "./storage-BEaqEPNQ.js";
3
+ import { exportJWK, exportPKCS8, exportSPKI, generateKeyPair, importPKCS8, importSPKI } from "jose";
4
+
5
+ //#region src/keys.ts
6
+ /**
7
+ * Cryptographic key management for JWT signing and encryption operations.
8
+ * Handles automatic key generation, rotation, and storage for OAuth operations.
9
+ */
10
+ /** Elliptic Curve algorithm used for JWT signing operations */
11
+ const signingAlg = "ES256";
12
+ /** RSA algorithm used for token encryption operations */
13
+ const encryptionAlg = "RSA-OAEP-512";
14
+ /**
15
+ * Loads or generates signing keys for JWT operations.
16
+ * Returns existing valid keys, or generates new ones if none are available.
17
+ * Keys are automatically sorted by creation date (newest first).
18
+ *
19
+ * @param storage - Storage adapter for persistent key storage
20
+ * @returns Promise resolving to array of available signing key pairs
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * const keys = await signingKeys(storage)
25
+ * const currentKey = keys[0] // Most recent key
26
+ *
27
+ * // Use for JWT signing
28
+ * const jwt = await new SignJWT(payload)
29
+ * .setProtectedHeader({ alg: currentKey.alg, kid: currentKey.id })
30
+ * .sign(currentKey.private)
31
+ * ```
32
+ */
33
+ const signingKeys = async (storage) => {
34
+ const results = [];
35
+ const scanner = Storage.scan(storage, ["signing:key"]);
36
+ for await (const [, value] of scanner) try {
37
+ const publicKey = await importSPKI(value.publicKey, value.alg, { extractable: true });
38
+ const privateKey = await importPKCS8(value.privateKey, value.alg);
39
+ const jwk$1 = await exportJWK(publicKey);
40
+ jwk$1.kid = value.id;
41
+ jwk$1.use = "sig";
42
+ results.push({
43
+ id: value.id,
44
+ alg: signingAlg,
45
+ created: new Date(value.created),
46
+ expired: value.expired ? new Date(value.expired) : void 0,
47
+ public: publicKey,
48
+ private: privateKey,
49
+ jwk: jwk$1
50
+ });
51
+ } catch {}
52
+ results.sort((a, b) => b.created.getTime() - a.created.getTime());
53
+ if (results.filter((item) => !item.expired).length) return results;
54
+ const key = await generateKeyPair(signingAlg, { extractable: true });
55
+ const serialized = {
56
+ id: generateSecureToken(16),
57
+ publicKey: await exportSPKI(key.publicKey),
58
+ privateKey: await exportPKCS8(key.privateKey),
59
+ created: Date.now(),
60
+ alg: signingAlg
61
+ };
62
+ await Storage.set(storage, ["signing:key", serialized.id], serialized);
63
+ const jwk = await exportJWK(key.publicKey);
64
+ jwk.kid = serialized.id;
65
+ jwk.use = "sig";
66
+ const newKeyPair = {
67
+ id: serialized.id,
68
+ alg: signingAlg,
69
+ created: new Date(serialized.created),
70
+ expired: serialized.expired ? new Date(serialized.expired) : void 0,
71
+ public: key.publicKey,
72
+ private: key.privateKey,
73
+ jwk
74
+ };
75
+ return [newKeyPair, ...results];
76
+ };
77
+ /**
78
+ * Loads or generates encryption keys for token encryption operations.
79
+ * Returns existing valid keys, or generates new ones if none are available.
80
+ * Keys are automatically sorted by creation date (newest first).
81
+ *
82
+ * @param storage - Storage adapter for persistent key storage
83
+ * @returns Promise resolving to array of available encryption key pairs
84
+ *
85
+ * @example
86
+ * ```ts
87
+ * const keys = await encryptionKeys(storage)
88
+ * const currentKey = keys[0] // Most recent key
89
+ *
90
+ * // Use for token encryption
91
+ * const encrypted = await new EncryptJWT(payload)
92
+ * .setProtectedHeader({ alg: 'RSA-OAEP-512', enc: 'A256GCM' })
93
+ * .encrypt(currentKey.public)
94
+ * ```
95
+ */
96
+ const encryptionKeys = async (storage) => {
97
+ const results = [];
98
+ const scanner = Storage.scan(storage, ["encryption:key"]);
99
+ for await (const [, value] of scanner) try {
100
+ const publicKey = await importSPKI(value.publicKey, value.alg, { extractable: true });
101
+ const privateKey = await importPKCS8(value.privateKey, value.alg);
102
+ const jwk$1 = await exportJWK(publicKey);
103
+ jwk$1.kid = value.id;
104
+ results.push({
105
+ id: value.id,
106
+ alg: encryptionAlg,
107
+ created: new Date(value.created),
108
+ expired: value.expired ? new Date(value.expired) : void 0,
109
+ public: publicKey,
110
+ private: privateKey,
111
+ jwk: jwk$1
112
+ });
113
+ } catch {}
114
+ results.sort((a, b) => b.created.getTime() - a.created.getTime());
115
+ if (results.filter((item) => !item.expired).length) return results;
116
+ const key = await generateKeyPair(encryptionAlg, { extractable: true });
117
+ const serialized = {
118
+ id: generateSecureToken(16),
119
+ publicKey: await exportSPKI(key.publicKey),
120
+ privateKey: await exportPKCS8(key.privateKey),
121
+ created: Date.now(),
122
+ alg: encryptionAlg
123
+ };
124
+ await Storage.set(storage, ["encryption:key", serialized.id], serialized);
125
+ const jwk = await exportJWK(key.publicKey);
126
+ jwk.kid = serialized.id;
127
+ const newKeyPair = {
128
+ id: serialized.id,
129
+ alg: encryptionAlg,
130
+ created: new Date(serialized.created),
131
+ expired: serialized.expired ? new Date(serialized.expired) : void 0,
132
+ public: key.publicKey,
133
+ private: key.privateKey,
134
+ jwk
135
+ };
136
+ return [newKeyPair, ...results];
137
+ };
138
+
139
+ //#endregion
140
+ export { encryptionKeys, signingKeys };
package/dist/keys.d.ts ADDED
@@ -0,0 +1,67 @@
1
+ import { StorageAdapter } from "./storage-CxKerLlc.js";
2
+ import { CryptoKey, JWK } from "jose";
3
+
4
+ //#region src/keys.d.ts
5
+
6
+ /**
7
+ * Runtime key pair with loaded cryptographic keys and metadata.
8
+ * Ready for immediate use in signing and encryption operations.
9
+ */
10
+ interface KeyPair {
11
+ /** Unique identifier for the key pair */
12
+ readonly id: string;
13
+ /** Algorithm used for this key pair */
14
+ readonly alg: string;
15
+ /** Loaded public key for verification/encryption */
16
+ readonly public: CryptoKey;
17
+ /** Loaded private key for signing/decryption */
18
+ readonly private: CryptoKey;
19
+ /** Date when the key was created */
20
+ readonly created: Date;
21
+ /** Optional expiration date */
22
+ readonly expired?: Date;
23
+ /** JSON Web Key representation for JWKS endpoints */
24
+ readonly jwk: JWK;
25
+ }
26
+ /**
27
+ * Loads or generates signing keys for JWT operations.
28
+ * Returns existing valid keys, or generates new ones if none are available.
29
+ * Keys are automatically sorted by creation date (newest first).
30
+ *
31
+ * @param storage - Storage adapter for persistent key storage
32
+ * @returns Promise resolving to array of available signing key pairs
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * const keys = await signingKeys(storage)
37
+ * const currentKey = keys[0] // Most recent key
38
+ *
39
+ * // Use for JWT signing
40
+ * const jwt = await new SignJWT(payload)
41
+ * .setProtectedHeader({ alg: currentKey.alg, kid: currentKey.id })
42
+ * .sign(currentKey.private)
43
+ * ```
44
+ */
45
+ declare const signingKeys: (storage: StorageAdapter) => Promise<KeyPair[]>;
46
+ /**
47
+ * Loads or generates encryption keys for token encryption operations.
48
+ * Returns existing valid keys, or generates new ones if none are available.
49
+ * Keys are automatically sorted by creation date (newest first).
50
+ *
51
+ * @param storage - Storage adapter for persistent key storage
52
+ * @returns Promise resolving to array of available encryption key pairs
53
+ *
54
+ * @example
55
+ * ```ts
56
+ * const keys = await encryptionKeys(storage)
57
+ * const currentKey = keys[0] // Most recent key
58
+ *
59
+ * // Use for token encryption
60
+ * const encrypted = await new EncryptJWT(payload)
61
+ * .setProtectedHeader({ alg: 'RSA-OAEP-512', enc: 'A256GCM' })
62
+ * .encrypt(currentKey.public)
63
+ * ```
64
+ */
65
+ declare const encryptionKeys: (storage: StorageAdapter) => Promise<KeyPair[]>;
66
+ //#endregion
67
+ export { KeyPair, encryptionKeys, signingKeys };
package/dist/keys.js ADDED
@@ -0,0 +1,5 @@
1
+ import "./random-SXMYlaVr.js";
2
+ import "./storage-BEaqEPNQ.js";
3
+ import { encryptionKeys, signingKeys } from "./keys-EEfxEGfO.js";
4
+
5
+ export { encryptionKeys, signingKeys };
@@ -0,0 +1,155 @@
1
+ import { getRelativeUrl } from "./util-CSdHUFOo.js";
2
+ import { OauthError } from "./error-DgAKK7b2.js";
3
+ import { generatePKCE } from "./pkce-276Za_rZ.js";
4
+ import { generateSecureToken, timingSafeCompare } from "./random-SXMYlaVr.js";
5
+
6
+ //#region src/provider/oauth2.ts
7
+ /**
8
+ * Creates an OAuth 2.0 authentication provider.
9
+ * Implements the Authorization Code Grant flow with optional PKCE support.
10
+ *
11
+ * @param config - OAuth 2.0 provider configuration
12
+ * @returns Provider instance implementing OAuth 2.0 authentication
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * // GitHub provider with basic configuration
17
+ * const githubProvider = Oauth2Provider({
18
+ * clientID: process.env.GITHUB_CLIENT_ID,
19
+ * clientSecret: process.env.GITHUB_CLIENT_SECRET,
20
+ * endpoint: {
21
+ * authorization: "https://github.com/login/oauth/authorize",
22
+ * token: "https://github.com/login/oauth/access_token"
23
+ * },
24
+ * scopes: ["user:email", "read:user"]
25
+ * })
26
+ *
27
+ * // Provider with PKCE and custom parameters
28
+ * const customProvider = Oauth2Provider({
29
+ * clientID: "my-client-id",
30
+ * clientSecret: "my-client-secret",
31
+ * endpoint: {
32
+ * authorization: "https://provider.com/oauth/authorize",
33
+ * token: "https://provider.com/oauth/token",
34
+ * jwks: "https://provider.com/.well-known/jwks.json"
35
+ * },
36
+ * scopes: ["read", "write"],
37
+ * pkce: true,
38
+ * query: {
39
+ * prompt: "consent",
40
+ * access_type: "offline"
41
+ * }
42
+ * })
43
+ * ```
44
+ */
45
+ const Oauth2Provider = (config) => {
46
+ const authQuery = config.query || {};
47
+ /**
48
+ * Handles the OAuth 2.0 callback logic for both GET and POST requests.
49
+ * Exchanges the authorization code for tokens and processes the response.
50
+ */
51
+ const handleCallbackLogic = async (c, ctx, provider, code) => {
52
+ if (!(provider && code)) return c.redirect(getRelativeUrl(c, "./authorize"));
53
+ const tokenRequestBody = new URLSearchParams({
54
+ client_id: config.clientID,
55
+ client_secret: config.clientSecret,
56
+ code,
57
+ grant_type: "authorization_code",
58
+ redirect_uri: provider.redirect,
59
+ ...provider.codeVerifier ? { code_verifier: provider.codeVerifier } : {}
60
+ });
61
+ try {
62
+ const response = await fetch(config.endpoint.token, {
63
+ method: "POST",
64
+ headers: {
65
+ "Content-Type": "application/x-www-form-urlencoded",
66
+ Accept: "application/json"
67
+ },
68
+ body: tokenRequestBody.toString()
69
+ });
70
+ if (!response.ok) throw new Error(`Token request failed with status ${response.status}`);
71
+ const tokenData = await response.json();
72
+ if (tokenData.error) throw new OauthError(tokenData.error, tokenData.error_description || "");
73
+ return await ctx.success(c, {
74
+ clientID: config.clientID,
75
+ tokenset: {
76
+ get access() {
77
+ return tokenData.access_token;
78
+ },
79
+ get refresh() {
80
+ return tokenData.refresh_token || "";
81
+ },
82
+ get expiry() {
83
+ return tokenData.expires_in || 0;
84
+ },
85
+ get raw() {
86
+ return tokenData;
87
+ }
88
+ }
89
+ });
90
+ } catch (error) {
91
+ if (error instanceof OauthError) throw error;
92
+ throw new OauthError("server_error", `Token exchange failed: ${error instanceof Error ? error.message : "Unknown error"}`);
93
+ }
94
+ };
95
+ return {
96
+ type: config.type || "oauth2",
97
+ init(routes, ctx) {
98
+ /**
99
+ * Initiates OAuth 2.0 authorization flow.
100
+ * Redirects user to the provider's authorization endpoint with proper parameters.
101
+ */
102
+ routes.get("/authorize", async (c) => {
103
+ const state = generateSecureToken();
104
+ const pkce = config.pkce ? await generatePKCE() : void 0;
105
+ await ctx.set(c, "provider", 60 * 10, {
106
+ state,
107
+ redirect: getRelativeUrl(c, "./callback"),
108
+ codeVerifier: pkce?.verifier
109
+ });
110
+ const authorizationUrl = new URL(config.endpoint.authorization);
111
+ authorizationUrl.searchParams.set("client_id", config.clientID);
112
+ authorizationUrl.searchParams.set("redirect_uri", getRelativeUrl(c, "./callback"));
113
+ authorizationUrl.searchParams.set("response_type", "code");
114
+ authorizationUrl.searchParams.set("state", state);
115
+ authorizationUrl.searchParams.set("scope", config.scopes.join(" "));
116
+ if (pkce) {
117
+ authorizationUrl.searchParams.set("code_challenge", pkce.challenge);
118
+ authorizationUrl.searchParams.set("code_challenge_method", pkce.method);
119
+ }
120
+ for (const [key, value] of Object.entries(authQuery)) authorizationUrl.searchParams.set(key, value);
121
+ return c.redirect(authorizationUrl.toString());
122
+ });
123
+ /**
124
+ * Handles OAuth 2.0 callback via query parameters (GET request).
125
+ * Standard OAuth 2.0 callback method for most providers.
126
+ */
127
+ routes.get("/callback", async (c) => {
128
+ const provider = await ctx.get(c, "provider");
129
+ const code = c.query("code");
130
+ const state = c.query("state");
131
+ const error = c.query("error");
132
+ if (error) throw new OauthError(error, c.query("error_description") || "");
133
+ if (!(provider && code) || provider.state && !timingSafeCompare(state || "", provider.state)) return c.redirect(getRelativeUrl(c, "./authorize"));
134
+ return await handleCallbackLogic(c, ctx, provider, code);
135
+ });
136
+ /**
137
+ * Handles OAuth 2.0 callback via form data (POST request).
138
+ * Alternative callback method supported by some providers.
139
+ */
140
+ routes.post("/callback", async (c) => {
141
+ const provider = await ctx.get(c, "provider");
142
+ const formData = await c.formData();
143
+ const code = formData.get("code")?.toString();
144
+ const state = formData.get("state")?.toString();
145
+ const error = formData.get("error")?.toString();
146
+ if (error) throw new OauthError(error, formData.get("error_description")?.toString() || "");
147
+ if (!(provider && code) || provider.state && !timingSafeCompare(state || "", provider.state)) return c.redirect(getRelativeUrl(c, "./authorize"));
148
+ return await handleCallbackLogic(c, ctx, provider, code);
149
+ });
150
+ }
151
+ };
152
+ };
153
+
154
+ //#endregion
155
+ export { Oauth2Provider };