@eienjs/adonisjs-simple-auth 1.0.1 → 2.0.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.
Files changed (49) hide show
  1. package/{LICENSE.md → LICENSE} +1 -1
  2. package/README.md +26 -3
  3. package/build/_virtual/_@oxc-project_runtime@0.137.0/helpers/esm/decorate.js +9 -0
  4. package/build/commands/generate_api_key.d.ts +11 -7
  5. package/build/commands/generate_api_key.js +23 -347
  6. package/build/configure.d.ts +6 -2
  7. package/build/configure.js +18 -0
  8. package/build/index.d.ts +6 -5
  9. package/build/index.js +6 -49
  10. package/build/middleware/simple_auth_middleware.d.ts +12 -0
  11. package/build/middleware/simple_auth_middleware.js +14 -0
  12. package/build/plugins/api_client.d.ts +14 -0
  13. package/build/plugins/api_client.js +23 -0
  14. package/build/providers/simple_auth_provider.d.ts +13 -9
  15. package/build/providers/simple_auth_provider.js +16 -26
  16. package/build/src/define_config.d.ts +7 -3
  17. package/build/src/define_config.js +13 -0
  18. package/build/src/errors.d.ts +20 -16
  19. package/build/src/errors.js +41 -0
  20. package/build/src/helpers/crc32.js +308 -0
  21. package/build/src/simple_auth.d.ts +14 -10
  22. package/build/src/simple_auth.js +23 -0
  23. package/build/stubs/main.d.ts +4 -5
  24. package/build/stubs/main.js +4 -0
  25. package/build/types.d.ts +13 -0
  26. package/build/types.js +1 -0
  27. package/package.json +91 -92
  28. package/build/chunk-EUXUH3YW.js +0 -15
  29. package/build/chunk-EUXUH3YW.js.map +0 -1
  30. package/build/chunk-JUF7K25A.js +0 -80
  31. package/build/chunk-JUF7K25A.js.map +0 -1
  32. package/build/chunk-TVVNPJ4M.js +0 -19
  33. package/build/chunk-TVVNPJ4M.js.map +0 -1
  34. package/build/commands/generate_api_key.js.map +0 -1
  35. package/build/index.js.map +0 -1
  36. package/build/metafile-esm.json +0 -1
  37. package/build/providers/simple_auth_provider.js.map +0 -1
  38. package/build/src/helpers/crc32.d.ts +0 -24
  39. package/build/src/middleware/simple_auth_middleware.d.ts +0 -8
  40. package/build/src/middleware/simple_auth_middleware.js +0 -9
  41. package/build/src/middleware/simple_auth_middleware.js.map +0 -1
  42. package/build/src/plugins/japa/api_client.d.ts +0 -11
  43. package/build/src/plugins/japa/api_client.js +0 -34
  44. package/build/src/plugins/japa/api_client.js.map +0 -1
  45. package/build/src/types.d.ts +0 -9
  46. package/build/src/types.js +0 -1
  47. package/build/src/types.js.map +0 -1
  48. /package/build/{config.stub → stubs/config.stub} +0 -0
  49. /package/build/{middleware → stubs/middleware}/api_key.stub +0 -0
@@ -1,30 +1,20 @@
1
- import {
2
- SimpleAuthMiddleware
3
- } from "../chunk-TVVNPJ4M.js";
4
- import "../chunk-JUF7K25A.js";
5
- import "../chunk-EUXUH3YW.js";
6
-
7
- // providers/simple_auth_provider.ts
1
+ import SimpleAuthMiddleware from "../middleware/simple_auth_middleware.js";
8
2
  import { configProvider } from "@adonisjs/core";
9
3
  import { RuntimeException } from "@adonisjs/core/exceptions";
4
+ //#region providers/simple_auth_provider.ts
10
5
  var SimpleAuthProvider = class {
11
- constructor(app) {
12
- this.app = app;
13
- }
14
- register() {
15
- this.app.container.singleton(SimpleAuthMiddleware, async () => {
16
- const simpleAuthConfigProvider = this.app.config.get("simpleauth");
17
- const config = await configProvider.resolve(this.app, simpleAuthConfigProvider);
18
- if (!config) {
19
- throw new RuntimeException(
20
- 'Invalid "config/simpleauth.ts" file. Make sure you are using the "defineConfig" method'
21
- );
22
- }
23
- return new SimpleAuthMiddleware(config);
24
- });
25
- }
6
+ app;
7
+ constructor(app) {
8
+ this.app = app;
9
+ }
10
+ register() {
11
+ this.app.container.singleton(SimpleAuthMiddleware, async () => {
12
+ const simpleAuthConfigProvider = this.app.config.get("simpleauth");
13
+ const config = await configProvider.resolve(this.app, simpleAuthConfigProvider);
14
+ if (!config) throw new RuntimeException("Invalid \"config/simpleauth.ts\" file. Make sure you are using the \"defineConfig\" method");
15
+ return new SimpleAuthMiddleware(config);
16
+ });
17
+ }
26
18
  };
27
- export {
28
- SimpleAuthProvider as default
29
- };
30
- //# sourceMappingURL=simple_auth_provider.js.map
19
+ //#endregion
20
+ export { SimpleAuthProvider as default };
@@ -1,3 +1,7 @@
1
- import { type ConfigProvider } from '@adonisjs/core/types';
2
- import { type ResolvedSimpleAuthConfig, type SimpleAuthConfig } from './types.js';
3
- export declare const defineConfig: (config: SimpleAuthConfig) => ConfigProvider<ResolvedSimpleAuthConfig>;
1
+ import { ResolvedSimpleAuthConfig, SimpleAuthConfig } from "../types.js";
2
+ import { ConfigProvider } from "@adonisjs/core/types";
3
+
4
+ //#region src/define_config.d.ts
5
+ declare const defineConfig: (config: SimpleAuthConfig) => ConfigProvider<ResolvedSimpleAuthConfig>;
6
+ //#endregion
7
+ export { defineConfig };
@@ -0,0 +1,13 @@
1
+ import { configProvider } from "@adonisjs/core";
2
+ import { Secret } from "@adonisjs/core/helpers";
3
+ //#region src/define_config.ts
4
+ const defineConfig = (config) => {
5
+ return configProvider.create(async (_) => {
6
+ return {
7
+ apiKeyHeader: config.apiKeyHeader ?? "x-api-key",
8
+ apiKeyValue: new Secret(config.apiKeyValue)
9
+ };
10
+ });
11
+ };
12
+ //#endregion
13
+ export { defineConfig };
@@ -1,17 +1,21 @@
1
- import { Exception } from '@adonisjs/core/exceptions';
2
- import { type HttpContext } from '@adonisjs/core/http';
3
- export declare class SimpleAuthException extends Exception {
4
- static readonly code = "E_SIMPLE_UNAUTHORIZED_ACCESS";
5
- static readonly status = 401;
6
- /**
7
- * Translation identifier. Can be customized
8
- */
9
- identifier: string;
10
- /**
11
- * Returns the message to be sent in the HTTP response.
12
- * Feel free to override this method and return a custom
13
- * response.
14
- */
15
- getResponseMessage(error: this, ctx: HttpContext): string;
16
- handle(error: this, ctx: HttpContext): Promise<void>;
1
+ import { Exception } from "@adonisjs/core/exceptions";
2
+ import { HttpContext } from "@adonisjs/core/http";
3
+
4
+ //#region src/errors.d.ts
5
+ declare class SimpleAuthException extends Exception {
6
+ static readonly code = "E_SIMPLE_UNAUTHORIZED_ACCESS";
7
+ static readonly status = 401;
8
+ /**
9
+ * Translation identifier. Can be customized
10
+ */
11
+ identifier: string;
12
+ /**
13
+ * Returns the message to be sent in the HTTP response.
14
+ * Feel free to override this method and return a custom
15
+ * response.
16
+ */
17
+ getResponseMessage(error: this, ctx: HttpContext): string;
18
+ handle(error: this, ctx: HttpContext): Promise<void>;
17
19
  }
20
+ //#endregion
21
+ export { SimpleAuthException };
@@ -0,0 +1,41 @@
1
+ import { Exception } from "@adonisjs/core/exceptions";
2
+ //#region src/errors.ts
3
+ var SimpleAuthException = class SimpleAuthException extends Exception {
4
+ static code = "E_SIMPLE_UNAUTHORIZED_ACCESS";
5
+ static status = 401;
6
+ /**
7
+ * Translation identifier. Can be customized
8
+ */
9
+ identifier = `errors.${SimpleAuthException.code}`;
10
+ /**
11
+ * Returns the message to be sent in the HTTP response.
12
+ * Feel free to override this method and return a custom
13
+ * response.
14
+ */
15
+ getResponseMessage(error, ctx) {
16
+ if ("i18n" in ctx) return ctx.i18n.t(error.identifier, {}, error.message);
17
+ return error.message;
18
+ }
19
+ async handle(error, ctx) {
20
+ const message = this.getResponseMessage(error, ctx);
21
+ switch (ctx.request.accepts([
22
+ "html",
23
+ "application/vnd.api+json",
24
+ "json"
25
+ ])) {
26
+ case "html":
27
+ case null:
28
+ case "json":
29
+ ctx.response.status(error.status).send({ errors: [{ message }] });
30
+ break;
31
+ case "application/vnd.api+json":
32
+ ctx.response.status(error.status).send({ errors: [{
33
+ code: error.code,
34
+ title: message
35
+ }] });
36
+ break;
37
+ }
38
+ }
39
+ };
40
+ //#endregion
41
+ export { SimpleAuthException };
@@ -0,0 +1,308 @@
1
+ import { TextEncoder } from "node:util";
2
+ //#region src/helpers/crc32.ts
3
+ /**
4
+ * We use CRC32 just to add a recognizable checksum to tokens. This helps
5
+ * secret scanning tools like https://docs.github.com/en/github/administering-a-repository/about-secret-scanning easily detect tokens generated by a given program.
6
+ *
7
+ * You can learn more about appending checksum to a hash here in this Github
8
+ * article. https://github.blog/2021-04-05-behind-githubs-new-authentication-token-formats/
9
+ *
10
+ * Code taken from:
11
+ * https://github.com/adonisjs/auth/blob/9.x/modules/access_tokens_guard/crc32.ts
12
+ */
13
+ var CRC32 = class {
14
+ /**
15
+ * Lookup table calculated for 0xEDB88320 divisor
16
+ */
17
+ lookupTable = [
18
+ 0,
19
+ 1996959894,
20
+ 3993919788,
21
+ 2567524794,
22
+ 124634137,
23
+ 1886057615,
24
+ 3915621685,
25
+ 2657392035,
26
+ 249268274,
27
+ 2044508324,
28
+ 3772115230,
29
+ 2547177864,
30
+ 162941995,
31
+ 2125561021,
32
+ 3887607047,
33
+ 2428444049,
34
+ 498536548,
35
+ 1789927666,
36
+ 4089016648,
37
+ 2227061214,
38
+ 450548861,
39
+ 1843258603,
40
+ 4107580753,
41
+ 2211677639,
42
+ 325883990,
43
+ 1684777152,
44
+ 4251122042,
45
+ 2321926636,
46
+ 335633487,
47
+ 1661365465,
48
+ 4195302755,
49
+ 2366115317,
50
+ 997073096,
51
+ 1281953886,
52
+ 3579855332,
53
+ 2724688242,
54
+ 1006888145,
55
+ 1258607687,
56
+ 3524101629,
57
+ 2768942443,
58
+ 901097722,
59
+ 1119000684,
60
+ 3686517206,
61
+ 2898065728,
62
+ 853044451,
63
+ 1172266101,
64
+ 3705015759,
65
+ 2882616665,
66
+ 651767980,
67
+ 1373503546,
68
+ 3369554304,
69
+ 3218104598,
70
+ 565507253,
71
+ 1454621731,
72
+ 3485111705,
73
+ 3099436303,
74
+ 671266974,
75
+ 1594198024,
76
+ 3322730930,
77
+ 2970347812,
78
+ 795835527,
79
+ 1483230225,
80
+ 3244367275,
81
+ 3060149565,
82
+ 1994146192,
83
+ 31158534,
84
+ 2563907772,
85
+ 4023717930,
86
+ 1907459465,
87
+ 112637215,
88
+ 2680153253,
89
+ 3904427059,
90
+ 2013776290,
91
+ 251722036,
92
+ 2517215374,
93
+ 3775830040,
94
+ 2137656763,
95
+ 141376813,
96
+ 2439277719,
97
+ 3865271297,
98
+ 1802195444,
99
+ 476864866,
100
+ 2238001368,
101
+ 4066508878,
102
+ 1812370925,
103
+ 453092731,
104
+ 2181625025,
105
+ 4111451223,
106
+ 1706088902,
107
+ 314042704,
108
+ 2344532202,
109
+ 4240017532,
110
+ 1658658271,
111
+ 366619977,
112
+ 2362670323,
113
+ 4224994405,
114
+ 1303535960,
115
+ 984961486,
116
+ 2747007092,
117
+ 3569037538,
118
+ 1256170817,
119
+ 1037604311,
120
+ 2765210733,
121
+ 3554079995,
122
+ 1131014506,
123
+ 879679996,
124
+ 2909243462,
125
+ 3663771856,
126
+ 1141124467,
127
+ 855842277,
128
+ 2852801631,
129
+ 3708648649,
130
+ 1342533948,
131
+ 654459306,
132
+ 3188396048,
133
+ 3373015174,
134
+ 1466479909,
135
+ 544179635,
136
+ 3110523913,
137
+ 3462522015,
138
+ 1591671054,
139
+ 702138776,
140
+ 2966460450,
141
+ 3352799412,
142
+ 1504918807,
143
+ 783551873,
144
+ 3082640443,
145
+ 3233442989,
146
+ 3988292384,
147
+ 2596254646,
148
+ 62317068,
149
+ 1957810842,
150
+ 3939845945,
151
+ 2647816111,
152
+ 81470997,
153
+ 1943803523,
154
+ 3814918930,
155
+ 2489596804,
156
+ 225274430,
157
+ 2053790376,
158
+ 3826175755,
159
+ 2466906013,
160
+ 167816743,
161
+ 2097651377,
162
+ 4027552580,
163
+ 2265490386,
164
+ 503444072,
165
+ 1762050814,
166
+ 4150417245,
167
+ 2154129355,
168
+ 426522225,
169
+ 1852507879,
170
+ 4275313526,
171
+ 2312317920,
172
+ 282753626,
173
+ 1742555852,
174
+ 4189708143,
175
+ 2394877945,
176
+ 397917763,
177
+ 1622183637,
178
+ 3604390888,
179
+ 2714866558,
180
+ 953729732,
181
+ 1340076626,
182
+ 3518719985,
183
+ 2797360999,
184
+ 1068828381,
185
+ 1219638859,
186
+ 3624741850,
187
+ 2936675148,
188
+ 906185462,
189
+ 1090812512,
190
+ 3747672003,
191
+ 2825379669,
192
+ 829329135,
193
+ 1181335161,
194
+ 3412177804,
195
+ 3160834842,
196
+ 628085408,
197
+ 1382605366,
198
+ 3423369109,
199
+ 3138078467,
200
+ 570562233,
201
+ 1426400815,
202
+ 3317316542,
203
+ 2998733608,
204
+ 733239954,
205
+ 1555261956,
206
+ 3268935591,
207
+ 3050360625,
208
+ 752459403,
209
+ 1541320221,
210
+ 2607071920,
211
+ 3965973030,
212
+ 1969922972,
213
+ 40735498,
214
+ 2617837225,
215
+ 3943577151,
216
+ 1913087877,
217
+ 83908371,
218
+ 2512341634,
219
+ 3803740692,
220
+ 2075208622,
221
+ 213261112,
222
+ 2463272603,
223
+ 3855990285,
224
+ 2094854071,
225
+ 198958881,
226
+ 2262029012,
227
+ 4057260610,
228
+ 1759359992,
229
+ 534414190,
230
+ 2176718541,
231
+ 4139329115,
232
+ 1873836001,
233
+ 414664567,
234
+ 2282248934,
235
+ 4279200368,
236
+ 1711684554,
237
+ 285281116,
238
+ 2405801727,
239
+ 4167216745,
240
+ 1634467795,
241
+ 376229701,
242
+ 2685067896,
243
+ 3608007406,
244
+ 1308918612,
245
+ 956543938,
246
+ 2808555105,
247
+ 3495958263,
248
+ 1231636301,
249
+ 1047427035,
250
+ 2932959818,
251
+ 3654703836,
252
+ 1088359270,
253
+ 936918e3,
254
+ 2847714899,
255
+ 3736837829,
256
+ 1202900863,
257
+ 817233897,
258
+ 3183342108,
259
+ 3401237130,
260
+ 1404277552,
261
+ 615818150,
262
+ 3134207493,
263
+ 3453421203,
264
+ 1423857449,
265
+ 601450431,
266
+ 3009837614,
267
+ 3294710456,
268
+ 1567103746,
269
+ 711928724,
270
+ 3020668471,
271
+ 3272380065,
272
+ 1510334235,
273
+ 755167117
274
+ ];
275
+ initialCRC = 4294967295;
276
+ calculateBytes(bytes, accumulator) {
277
+ let crc = accumulator ?? this.initialCRC;
278
+ for (const byte of bytes) {
279
+ const tableIndex = (crc ^ byte) & 255;
280
+ const tableVal = this.lookupTable[tableIndex];
281
+ crc = crc >>> 8 ^ tableVal;
282
+ }
283
+ return crc;
284
+ }
285
+ crcToUint(crc) {
286
+ return this.toUint32(crc ^ 4294967295);
287
+ }
288
+ strToBytes(input) {
289
+ return new TextEncoder().encode(input);
290
+ }
291
+ toUint32(num) {
292
+ if (num >= 0) return num;
293
+ return 4294967295 - -num + 1;
294
+ }
295
+ calculate(input) {
296
+ return this.forString(input);
297
+ }
298
+ forString(input) {
299
+ const bytes = this.strToBytes(input);
300
+ return this.forBytes(bytes);
301
+ }
302
+ forBytes(bytes, accumulator) {
303
+ const crc = this.calculateBytes(bytes, accumulator);
304
+ return this.crcToUint(crc);
305
+ }
306
+ };
307
+ //#endregion
308
+ export { CRC32 };
@@ -1,11 +1,15 @@
1
- import { type HttpContext } from '@adonisjs/core/http';
2
- import { SimpleAuthException } from './errors.js';
3
- import { type ResolvedSimpleAuthConfig } from './types.js';
4
- export declare class SimpleAuth {
5
- protected ctx: HttpContext;
6
- protected config: ResolvedSimpleAuthConfig;
7
- constructor(ctx: HttpContext, config: ResolvedSimpleAuthConfig);
8
- protected authenticationFailed(): SimpleAuthException;
9
- protected getApiKeyRequest(): string;
10
- authenticate(): void;
1
+ import { ResolvedSimpleAuthConfig } from "../types.js";
2
+ import { SimpleAuthException } from "./errors.js";
3
+ import { HttpContext } from "@adonisjs/core/http";
4
+
5
+ //#region src/simple_auth.d.ts
6
+ declare class SimpleAuth {
7
+ protected ctx: HttpContext;
8
+ protected config: ResolvedSimpleAuthConfig;
9
+ constructor(ctx: HttpContext, config: ResolvedSimpleAuthConfig);
10
+ protected authenticationFailed(): SimpleAuthException;
11
+ protected getApiKeyRequest(): string;
12
+ authenticate(): void;
11
13
  }
14
+ //#endregion
15
+ export { SimpleAuth };
@@ -0,0 +1,23 @@
1
+ import { SimpleAuthException } from "./errors.js";
2
+ //#region src/simple_auth.ts
3
+ var SimpleAuth = class {
4
+ ctx;
5
+ config;
6
+ constructor(ctx, config) {
7
+ this.ctx = ctx;
8
+ this.config = config;
9
+ }
10
+ authenticationFailed() {
11
+ return new SimpleAuthException("Unauthorized access");
12
+ }
13
+ getApiKeyRequest() {
14
+ const apiKey = this.ctx.request.header(this.config.apiKeyHeader, "");
15
+ if (!apiKey.trim()) throw this.authenticationFailed();
16
+ return apiKey;
17
+ }
18
+ authenticate() {
19
+ if (this.getApiKeyRequest() !== this.config.apiKeyValue.release()) throw this.authenticationFailed();
20
+ }
21
+ };
22
+ //#endregion
23
+ export { SimpleAuth };
@@ -1,5 +1,4 @@
1
- /**
2
- * Path to the root directory where the stubs are stored. We use
3
- * this path within commands and the configure hook
4
- */
5
- export declare const stubsRoot: string;
1
+ //#region stubs/main.d.ts
2
+ declare const stubsRoot: string;
3
+ //#endregion
4
+ export { stubsRoot };
@@ -0,0 +1,4 @@
1
+ //#region stubs/main.ts
2
+ const stubsRoot = import.meta.dirname;
3
+ //#endregion
4
+ export { stubsRoot };
@@ -0,0 +1,13 @@
1
+ import { Secret } from "@adonisjs/core/helpers";
2
+
3
+ //#region src/types.d.ts
4
+ interface SimpleAuthConfig {
5
+ apiKeyHeader?: string;
6
+ apiKeyValue: string;
7
+ }
8
+ interface ResolvedSimpleAuthConfig {
9
+ apiKeyHeader: string;
10
+ apiKeyValue: Secret<string>;
11
+ }
12
+ //#endregion
13
+ export { ResolvedSimpleAuthConfig, SimpleAuthConfig };
package/build/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};