@zephkelly/nuxt-authkit 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 (75) hide show
  1. package/README.md +170 -0
  2. package/dist/module.d.mts +6 -0
  3. package/dist/module.json +9 -0
  4. package/dist/module.mjs +80 -0
  5. package/dist/runtime/composables/useAuthkitToken.d.ts +25 -0
  6. package/dist/runtime/composables/useAuthkitToken.js +31 -0
  7. package/dist/runtime/helpers/request-middleware.d.ts +22 -0
  8. package/dist/runtime/helpers/request-middleware.js +82 -0
  9. package/dist/runtime/index.d.ts +8 -0
  10. package/dist/runtime/index.js +2 -0
  11. package/dist/runtime/plugins/auth-fetch.client.d.ts +13 -0
  12. package/dist/runtime/plugins/auth-fetch.client.js +91 -0
  13. package/dist/runtime/plugins/internal/request-target.d.ts +16 -0
  14. package/dist/runtime/plugins/internal/request-target.js +17 -0
  15. package/dist/runtime/roles/has-role.d.ts +21 -0
  16. package/dist/runtime/roles/has-role.js +51 -0
  17. package/dist/runtime/server/api/_auth/refresh-handler.d.ts +6 -0
  18. package/dist/runtime/server/api/_auth/refresh-handler.js +44 -0
  19. package/dist/runtime/server/api/_auth/refresh.post.d.ts +6 -0
  20. package/dist/runtime/server/api/_auth/refresh.post.js +5 -0
  21. package/dist/runtime/server/tsconfig.json +3 -0
  22. package/dist/runtime/service/index.d.ts +119 -0
  23. package/dist/runtime/service/index.js +170 -0
  24. package/dist/runtime/service/new-index.d.ts +24 -0
  25. package/dist/runtime/service/new-index.js +0 -0
  26. package/dist/runtime/service/types/async-service.d.ts +59 -0
  27. package/dist/runtime/service/types/async-service.js +0 -0
  28. package/dist/runtime/service/types/sync-service.d.ts +28 -0
  29. package/dist/runtime/service/types/sync-service.js +0 -0
  30. package/dist/runtime/service/utils/getJwtAsyncService.d.ts +11 -0
  31. package/dist/runtime/service/utils/getJwtAsyncService.js +16 -0
  32. package/dist/runtime/storage/nitro.d.ts +61 -0
  33. package/dist/runtime/storage/nitro.js +124 -0
  34. package/dist/runtime/storage/types.d.ts +27 -0
  35. package/dist/runtime/storage/types.js +0 -0
  36. package/dist/runtime/strategy/base.d.ts +40 -0
  37. package/dist/runtime/strategy/base.js +88 -0
  38. package/dist/runtime/strategy/jwt/callback.d.ts +60 -0
  39. package/dist/runtime/strategy/jwt/callback.js +0 -0
  40. package/dist/runtime/strategy/jwt/index.d.ts +137 -0
  41. package/dist/runtime/strategy/jwt/index.js +748 -0
  42. package/dist/runtime/strategy/jwt/jwt-crypto.d.ts +122 -0
  43. package/dist/runtime/strategy/jwt/jwt-crypto.js +298 -0
  44. package/dist/runtime/strategy/jwt/types.d.ts +68 -0
  45. package/dist/runtime/strategy/jwt/types.js +0 -0
  46. package/dist/runtime/strategy/types.d.ts +68 -0
  47. package/dist/runtime/strategy/types.js +0 -0
  48. package/dist/runtime/types/2fac.d.ts +32 -0
  49. package/dist/runtime/types/2fac.js +0 -0
  50. package/dist/runtime/types/authkit-types.d.ts +6 -0
  51. package/dist/runtime/types/authkit-types.js +0 -0
  52. package/dist/runtime/types/callbacks.d.ts +133 -0
  53. package/dist/runtime/types/callbacks.js +0 -0
  54. package/dist/runtime/types/cookie.d.ts +47 -0
  55. package/dist/runtime/types/cookie.js +0 -0
  56. package/dist/runtime/types/expand.d.ts +3 -0
  57. package/dist/runtime/types/expand.js +0 -0
  58. package/dist/runtime/types/index.d.ts +52 -0
  59. package/dist/runtime/types/index.js +0 -0
  60. package/dist/runtime/types/module-options.d.ts +33 -0
  61. package/dist/runtime/types/module-options.js +0 -0
  62. package/dist/runtime/types/runtime-config.d.ts +2 -0
  63. package/dist/runtime/types/runtime-config.js +47 -0
  64. package/dist/runtime/types/token.d.ts +42 -0
  65. package/dist/runtime/types/token.js +12 -0
  66. package/dist/runtime/utils/context-helpers.d.ts +4 -0
  67. package/dist/runtime/utils/context-helpers.js +10 -0
  68. package/dist/runtime/utils/get-module-options.d.ts +2 -0
  69. package/dist/runtime/utils/get-module-options.js +18 -0
  70. package/dist/runtime/utils/password.d.ts +23 -0
  71. package/dist/runtime/utils/password.js +18 -0
  72. package/dist/runtime/utils/uuid.d.ts +187 -0
  73. package/dist/runtime/utils/uuid.js +345 -0
  74. package/dist/types.d.mts +7 -0
  75. package/package.json +65 -0
@@ -0,0 +1,345 @@
1
+ /**
2
+ * uuidv7: A JavaScript implementation of UUID version 7
3
+ *
4
+ * Copyright 2021-2024 LiosK
5
+ *
6
+ * @license Apache-2.0
7
+ * @packageDocumentation
8
+ */
9
+ const DIGITS = "0123456789abcdef";
10
+ export class UUID {
11
+ /** @param bytes - The 16-byte byte array representation. */
12
+ constructor(bytes) {
13
+ this.bytes = bytes;
14
+ }
15
+ /**
16
+ * Creates an object from the internal representation, a 16-byte byte array
17
+ * containing the binary UUID representation in the big-endian byte order.
18
+ *
19
+ * This method does NOT shallow-copy the argument, and thus the created object
20
+ * holds the reference to the underlying buffer.
21
+ *
22
+ * @throws TypeError if the length of the argument is not 16.
23
+ */
24
+ static ofInner(bytes) {
25
+ if (bytes.length !== 16) {
26
+ throw new TypeError("not 128-bit length");
27
+ } else {
28
+ return new UUID(bytes);
29
+ }
30
+ }
31
+ /**
32
+ * Builds a byte array from UUIDv7 field values.
33
+ *
34
+ * @param unixTsMs - A 48-bit `unix_ts_ms` field value.
35
+ * @param randA - A 12-bit `rand_a` field value.
36
+ * @param randBHi - The higher 30 bits of 62-bit `rand_b` field value.
37
+ * @param randBLo - The lower 32 bits of 62-bit `rand_b` field value.
38
+ * @throws RangeError if any field value is out of the specified range.
39
+ */
40
+ static fromFieldsV7(unixTsMs, randA, randBHi, randBLo) {
41
+ if (!Number.isInteger(unixTsMs) || !Number.isInteger(randA) || !Number.isInteger(randBHi) || !Number.isInteger(randBLo) || unixTsMs < 0 || randA < 0 || randBHi < 0 || randBLo < 0 || unixTsMs > 281474976710655 || randA > 4095 || randBHi > 1073741823 || randBLo > 4294967295) {
42
+ throw new RangeError("invalid field value");
43
+ }
44
+ const bytes = new Uint8Array(16);
45
+ bytes[0] = unixTsMs / 2 ** 40;
46
+ bytes[1] = unixTsMs / 2 ** 32;
47
+ bytes[2] = unixTsMs / 2 ** 24;
48
+ bytes[3] = unixTsMs / 2 ** 16;
49
+ bytes[4] = unixTsMs / 2 ** 8;
50
+ bytes[5] = unixTsMs;
51
+ bytes[6] = 112 | randA >>> 8;
52
+ bytes[7] = randA;
53
+ bytes[8] = 128 | randBHi >>> 24;
54
+ bytes[9] = randBHi >>> 16;
55
+ bytes[10] = randBHi >>> 8;
56
+ bytes[11] = randBHi;
57
+ bytes[12] = randBLo >>> 24;
58
+ bytes[13] = randBLo >>> 16;
59
+ bytes[14] = randBLo >>> 8;
60
+ bytes[15] = randBLo;
61
+ return new UUID(bytes);
62
+ }
63
+ /**
64
+ * Builds a byte array from a string representation.
65
+ *
66
+ * This method accepts the following formats:
67
+ *
68
+ * - 32-digit hexadecimal format without hyphens: `0189dcd553117d408db09496a2eef37b`
69
+ * - 8-4-4-4-12 hyphenated format: `0189dcd5-5311-7d40-8db0-9496a2eef37b`
70
+ * - Hyphenated format with surrounding braces: `{0189dcd5-5311-7d40-8db0-9496a2eef37b}`
71
+ * - RFC 9562 URN format: `urn:uuid:0189dcd5-5311-7d40-8db0-9496a2eef37b`
72
+ *
73
+ * Leading and trailing whitespaces represents an error.
74
+ *
75
+ * @throws SyntaxError if the argument could not parse as a valid UUID string.
76
+ */
77
+ static parse(uuid) {
78
+ let hex = void 0;
79
+ switch (uuid.length) {
80
+ case 32:
81
+ hex = /^[0-9a-f]{32}$/i.exec(uuid)?.[0];
82
+ break;
83
+ case 36:
84
+ hex = /^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$/i.exec(uuid)?.slice(1, 6).join("");
85
+ break;
86
+ case 38:
87
+ hex = /^\{([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})\}$/i.exec(uuid)?.slice(1, 6).join("");
88
+ break;
89
+ case 45:
90
+ hex = /^urn:uuid:([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$/i.exec(uuid)?.slice(1, 6).join("");
91
+ break;
92
+ default:
93
+ break;
94
+ }
95
+ if (hex) {
96
+ const inner = new Uint8Array(16);
97
+ for (let i = 0; i < 16; i += 4) {
98
+ const n = parseInt(hex.substring(2 * i, 2 * i + 8), 16);
99
+ inner[i + 0] = n >>> 24;
100
+ inner[i + 1] = n >>> 16;
101
+ inner[i + 2] = n >>> 8;
102
+ inner[i + 3] = n;
103
+ }
104
+ return new UUID(inner);
105
+ } else {
106
+ throw new SyntaxError("could not parse UUID string");
107
+ }
108
+ }
109
+ /**
110
+ * @returns The 8-4-4-4-12 canonical hexadecimal string representation
111
+ * (`0189dcd5-5311-7d40-8db0-9496a2eef37b`).
112
+ */
113
+ toString() {
114
+ let text = "";
115
+ for (let i = 0; i < this.bytes.length; i++) {
116
+ text += DIGITS.charAt(this.bytes[i] >>> 4);
117
+ text += DIGITS.charAt(this.bytes[i] & 15);
118
+ if (i === 3 || i === 5 || i === 7 || i === 9) {
119
+ text += "-";
120
+ }
121
+ }
122
+ return text;
123
+ }
124
+ /**
125
+ * @returns The 32-digit hexadecimal representation without hyphens
126
+ * (`0189dcd553117d408db09496a2eef37b`).
127
+ */
128
+ toHex() {
129
+ let text = "";
130
+ for (let i = 0; i < this.bytes.length; i++) {
131
+ text += DIGITS.charAt(this.bytes[i] >>> 4);
132
+ text += DIGITS.charAt(this.bytes[i] & 15);
133
+ }
134
+ return text;
135
+ }
136
+ /** @returns The 8-4-4-4-12 canonical hexadecimal string representation. */
137
+ toJSON() {
138
+ return this.toString();
139
+ }
140
+ /**
141
+ * Reports the variant field value of the UUID or, if appropriate, "NIL" or
142
+ * "MAX".
143
+ *
144
+ * For convenience, this method reports "NIL" or "MAX" if `this` represents
145
+ * the Nil or Max UUID, although the Nil and Max UUIDs are technically
146
+ * subsumed under the variants `0b0` and `0b111`, respectively.
147
+ */
148
+ getVariant() {
149
+ const n = this.bytes[8] >>> 4;
150
+ if (n < 0) {
151
+ throw new Error("unreachable");
152
+ } else if (n <= 7) {
153
+ return this.bytes.every((e) => e === 0) ? "NIL" : "VAR_0";
154
+ } else if (n <= 11) {
155
+ return "VAR_10";
156
+ } else if (n <= 13) {
157
+ return "VAR_110";
158
+ } else if (n <= 15) {
159
+ return this.bytes.every((e) => e === 255) ? "MAX" : "VAR_RESERVED";
160
+ } else {
161
+ throw new Error("unreachable");
162
+ }
163
+ }
164
+ /**
165
+ * Returns the version field value of the UUID or `undefined` if the UUID does
166
+ * not have the variant field value of `0b10`.
167
+ */
168
+ getVersion() {
169
+ return this.getVariant() === "VAR_10" ? this.bytes[6] >>> 4 : void 0;
170
+ }
171
+ /** Creates an object from `this`. */
172
+ clone() {
173
+ return new UUID(this.bytes.slice(0));
174
+ }
175
+ /** Returns true if `this` is equivalent to `other`. */
176
+ equals(other) {
177
+ return this.compareTo(other) === 0;
178
+ }
179
+ /**
180
+ * Returns a negative integer, zero, or positive integer if `this` is less
181
+ * than, equal to, or greater than `other`, respectively.
182
+ */
183
+ compareTo(other) {
184
+ for (let i = 0; i < 16; i++) {
185
+ const diff = this.bytes[i] - other.bytes[i];
186
+ if (diff !== 0) {
187
+ return Math.sign(diff);
188
+ }
189
+ }
190
+ return 0;
191
+ }
192
+ }
193
+ export class V7Generator {
194
+ timestamp = 0;
195
+ counter = 0;
196
+ /** The random number generator used by the generator. */
197
+ random;
198
+ /**
199
+ * Creates a generator object with the default random number generator, or
200
+ * with the specified one if passed as an argument. The specified random
201
+ * number generator should be cryptographically strong and securely seeded.
202
+ */
203
+ constructor(randomNumberGenerator) {
204
+ this.random = randomNumberGenerator ?? getDefaultRandom();
205
+ }
206
+ /**
207
+ * Generates a new UUIDv7 object from the current timestamp, or resets the
208
+ * generator upon significant timestamp rollback.
209
+ *
210
+ * This method returns a monotonically increasing UUID by reusing the previous
211
+ * timestamp even if the up-to-date timestamp is smaller than the immediately
212
+ * preceding UUID's. However, when such a clock rollback is considered
213
+ * significant (i.e., by more than ten seconds), this method resets the
214
+ * generator and returns a new UUID based on the given timestamp, breaking the
215
+ * increasing order of UUIDs.
216
+ *
217
+ * See {@link generateOrAbort} for the other mode of generation and
218
+ * {@link generateOrResetCore} for the low-level primitive.
219
+ */
220
+ generate() {
221
+ return this.generateOrResetCore(Date.now(), 1e4);
222
+ }
223
+ /**
224
+ * Generates a new UUIDv7 object from the current timestamp, or returns
225
+ * `undefined` upon significant timestamp rollback.
226
+ *
227
+ * This method returns a monotonically increasing UUID by reusing the previous
228
+ * timestamp even if the up-to-date timestamp is smaller than the immediately
229
+ * preceding UUID's. However, when such a clock rollback is considered
230
+ * significant (i.e., by more than ten seconds), this method aborts and
231
+ * returns `undefined` immediately.
232
+ *
233
+ * See {@link generate} for the other mode of generation and
234
+ * {@link generateOrAbortCore} for the low-level primitive.
235
+ */
236
+ generateOrAbort() {
237
+ return this.generateOrAbortCore(Date.now(), 1e4);
238
+ }
239
+ /**
240
+ * Generates a new UUIDv7 object from the `unixTsMs` passed, or resets the
241
+ * generator upon significant timestamp rollback.
242
+ *
243
+ * This method is equivalent to {@link generate} except that it takes a custom
244
+ * timestamp and clock rollback allowance.
245
+ *
246
+ * @param rollbackAllowance - The amount of `unixTsMs` rollback that is
247
+ * considered significant. A suggested value is `10_000` (milliseconds).
248
+ * @throws RangeError if `unixTsMs` is not a 48-bit positive integer.
249
+ */
250
+ generateOrResetCore(unixTsMs, rollbackAllowance) {
251
+ let value = this.generateOrAbortCore(unixTsMs, rollbackAllowance);
252
+ if (value === void 0) {
253
+ this.timestamp = 0;
254
+ value = this.generateOrAbortCore(unixTsMs, rollbackAllowance);
255
+ }
256
+ return value;
257
+ }
258
+ /**
259
+ * Generates a new UUIDv7 object from the `unixTsMs` passed, or returns
260
+ * `undefined` upon significant timestamp rollback.
261
+ *
262
+ * This method is equivalent to {@link generateOrAbort} except that it takes a
263
+ * custom timestamp and clock rollback allowance.
264
+ *
265
+ * @param rollbackAllowance - The amount of `unixTsMs` rollback that is
266
+ * considered significant. A suggested value is `10_000` (milliseconds).
267
+ * @throws RangeError if `unixTsMs` is not a 48-bit positive integer.
268
+ */
269
+ generateOrAbortCore(unixTsMs, rollbackAllowance) {
270
+ const MAX_COUNTER = 4398046511103;
271
+ if (!Number.isInteger(unixTsMs) || unixTsMs < 1 || unixTsMs > 281474976710655) {
272
+ throw new RangeError("`unixTsMs` must be a 48-bit positive integer");
273
+ } else if (rollbackAllowance < 0 || rollbackAllowance > 281474976710655) {
274
+ throw new RangeError("`rollbackAllowance` out of reasonable range");
275
+ }
276
+ if (unixTsMs > this.timestamp) {
277
+ this.timestamp = unixTsMs;
278
+ this.resetCounter();
279
+ } else if (unixTsMs + rollbackAllowance >= this.timestamp) {
280
+ this.counter++;
281
+ if (this.counter > MAX_COUNTER) {
282
+ this.timestamp++;
283
+ this.resetCounter();
284
+ }
285
+ } else {
286
+ return void 0;
287
+ }
288
+ return UUID.fromFieldsV7(
289
+ this.timestamp,
290
+ Math.trunc(this.counter / 2 ** 30),
291
+ this.counter & 2 ** 30 - 1,
292
+ this.random.nextUint32()
293
+ );
294
+ }
295
+ /** Initializes the counter at a 42-bit random integer. */
296
+ resetCounter() {
297
+ this.counter = this.random.nextUint32() * 1024 + (this.random.nextUint32() & 1023);
298
+ }
299
+ /**
300
+ * Generates a new UUIDv4 object utilizing the random number generator inside.
301
+ *
302
+ * @internal
303
+ */
304
+ generateV4() {
305
+ const bytes = new Uint8Array(
306
+ Uint32Array.of(
307
+ this.random.nextUint32(),
308
+ this.random.nextUint32(),
309
+ this.random.nextUint32(),
310
+ this.random.nextUint32()
311
+ ).buffer
312
+ );
313
+ bytes[6] = 64 | bytes[6] >>> 4;
314
+ bytes[8] = 128 | bytes[8] >>> 2;
315
+ return UUID.ofInner(bytes);
316
+ }
317
+ }
318
+ const getDefaultRandom = () => {
319
+ if (typeof crypto !== "undefined" && typeof crypto.getRandomValues !== "undefined") {
320
+ return new BufferedCryptoRandom();
321
+ } else {
322
+ if (typeof UUIDV7_DENY_WEAK_RNG !== "undefined" && UUIDV7_DENY_WEAK_RNG) {
323
+ throw new Error("no cryptographically strong RNG available");
324
+ }
325
+ return {
326
+ nextUint32: () => Math.trunc(Math.random() * 65536) * 65536 + Math.trunc(Math.random() * 65536)
327
+ };
328
+ }
329
+ };
330
+ class BufferedCryptoRandom {
331
+ buffer = new Uint32Array(8);
332
+ cursor = 65535;
333
+ nextUint32() {
334
+ if (this.cursor >= this.buffer.length) {
335
+ crypto.getRandomValues(this.buffer);
336
+ this.cursor = 0;
337
+ }
338
+ return this.buffer[this.cursor++];
339
+ }
340
+ }
341
+ let defaultGenerator;
342
+ export const uuidv7 = () => uuidv7obj().toString();
343
+ export const uuidv7obj = () => (defaultGenerator || (defaultGenerator = new V7Generator())).generate();
344
+ export const uuidv4 = () => uuidv4obj().toString();
345
+ export const uuidv4obj = () => (defaultGenerator || (defaultGenerator = new V7Generator())).generateV4();
@@ -0,0 +1,7 @@
1
+ import type { NuxtModule } from '@nuxt/schema'
2
+
3
+ import type { default as Module } from './module.mjs'
4
+
5
+ export type ModuleOptions = typeof Module extends NuxtModule<infer O> ? Partial<O> : Record<string, any>
6
+
7
+ export { default } from './module.mjs'
package/package.json ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "@zephkelly/nuxt-authkit",
3
+ "version": "0.0.1",
4
+ "description": "An authentication toolkit for Nuxt applications",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/zephkelly/nuxt-authkit.git"
8
+ },
9
+ "bugs": {
10
+ "url": "https://github.com/zephkelly/nuxt-authkit/issues"
11
+ },
12
+ "license": "MIT",
13
+ "type": "module",
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/types.d.mts",
17
+ "import": "./dist/module.mjs"
18
+ }
19
+ },
20
+ "main": "./dist/module.mjs",
21
+ "typesVersions": {
22
+ "*": {
23
+ ".": [
24
+ "./dist/types.d.mts"
25
+ ]
26
+ }
27
+ },
28
+ "files": [
29
+ "dist"
30
+ ],
31
+ "scripts": {
32
+ "prepack": "nuxt-module-build build",
33
+ "dev": "npm run dev:prepare && nuxi dev playground",
34
+ "dev:build": "nuxi build playground",
35
+ "dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
36
+ "release": "npm run lint && npm run test && npm run prepack && changelogen --release && npm publish --access public && git push --follow-tags",
37
+ "lint": "eslint .",
38
+ "test": "vitest run",
39
+ "test:watch": "vitest watch",
40
+ "test:types": "vue-tsc --noEmit && cd playground && vue-tsc --noEmit"
41
+ },
42
+ "dependencies": {
43
+ "@nuxt/kit": "^4.2.0",
44
+ "scrypt": "^6.0.3"
45
+ },
46
+ "devDependencies": {
47
+ "@nuxt/devtools": "^2.7.0",
48
+ "@nuxt/eslint-config": "^1.10.0",
49
+ "@nuxt/module-builder": "^1.0.2",
50
+ "@nuxt/schema": "^4.2.0",
51
+ "@nuxt/test-utils": "^3.20.1",
52
+ "@types/node": "latest",
53
+ "@vue/test-utils": "^2.4.6",
54
+ "changelogen": "^0.6.2",
55
+ "eslint": "^9.39.1",
56
+ "happy-dom": "^20.0.10",
57
+ "nuxt": "^4.2.0",
58
+ "playwright-core": "^1.56.1",
59
+ "typescript": "~5.9.3",
60
+ "vitest": "^4.0.6",
61
+ "vue-tsc": "^3.1.3",
62
+ "@adonisjs/hash": "^9.1.1"
63
+ },
64
+ "author": "zephkelly"
65
+ }