@eienjs/adonisjs-simple-auth 1.0.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.
- package/LICENSE.md +21 -0
- package/README.md +44 -0
- package/build/chunk-EUXUH3YW.js +15 -0
- package/build/chunk-EUXUH3YW.js.map +1 -0
- package/build/chunk-QSRPDOI7.js +19 -0
- package/build/chunk-QSRPDOI7.js.map +1 -0
- package/build/chunk-UBUHVXKH.js +37 -0
- package/build/chunk-UBUHVXKH.js.map +1 -0
- package/build/commands/commands.json +1 -0
- package/build/commands/generate_api_key.d.ts +8 -0
- package/build/commands/generate_api_key.js +353 -0
- package/build/commands/generate_api_key.js.map +1 -0
- package/build/commands/main.d.ts +4 -0
- package/build/commands/main.js +36 -0
- package/build/config.stub +12 -0
- package/build/configure.d.ts +2 -0
- package/build/index.d.ts +5 -0
- package/build/index.js +49 -0
- package/build/index.js.map +1 -0
- package/build/metafile-esm.json +1 -0
- package/build/middleware/api_key.stub +22 -0
- package/build/providers/simple_auth_provider.d.ts +15 -0
- package/build/providers/simple_auth_provider.js +30 -0
- package/build/providers/simple_auth_provider.js.map +1 -0
- package/build/src/define_config.d.ts +3 -0
- package/build/src/errors.d.ts +5 -0
- package/build/src/helpers/crc32.d.ts +24 -0
- package/build/src/middleware/simple_auth_middleware.d.ts +8 -0
- package/build/src/middleware/simple_auth_middleware.js +9 -0
- package/build/src/middleware/simple_auth_middleware.js.map +1 -0
- package/build/src/plugins/japa/api_client.d.ts +11 -0
- package/build/src/plugins/japa/api_client.js +34 -0
- package/build/src/plugins/japa/api_client.js.map +1 -0
- package/build/src/simple_auth.d.ts +11 -0
- package/build/src/types.d.ts +9 -0
- package/build/src/types.js +1 -0
- package/build/src/types.js.map +1 -0
- package/build/stubs/main.d.ts +5 -0
- package/package.json +143 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 - EienJS
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# @eienjs/adonisjs-simple-auth
|
|
2
|
+
|
|
3
|
+
[![Source Code][badge-source]][source]
|
|
4
|
+
[![Npm Node Version Support][badge-node-version]][node-version]
|
|
5
|
+
[![Latest Version][badge-release]][release]
|
|
6
|
+
[![Software License][badge-license]][license]
|
|
7
|
+
[![Build Status][badge-build]][build]
|
|
8
|
+
[![Total Downloads][badge-downloads]][downloads]
|
|
9
|
+
|
|
10
|
+
<!-- [![Build Status][badge-build]][build] -->
|
|
11
|
+
|
|
12
|
+
> Single authentication key that allows authenticate
|
|
13
|
+
|
|
14
|
+
## Introduction
|
|
15
|
+
|
|
16
|
+
This package enables you to create a single authentication key that allows you to authenticate just by including the key.
|
|
17
|
+
This simplicity also allows a user to make calls easily, with cURL, with interactive docs, or even in their browser.
|
|
18
|
+
|
|
19
|
+
## Documentation
|
|
20
|
+
|
|
21
|
+
The documentation is available on [EienJS](https://eienjs.com/packages/adonisjs-simple-auth/getting-started/).
|
|
22
|
+
|
|
23
|
+
## Contributing
|
|
24
|
+
|
|
25
|
+
Contributions are welcome. Please read [CONTRIBUTING][] for more details and remember to check the [CHANGELOG][] file.
|
|
26
|
+
|
|
27
|
+
## Copyright and License
|
|
28
|
+
|
|
29
|
+
The `@eienjs/adonisjs-simple-auth` library is licensed for use under the MIT License (MIT). Please see [LICENSE][] for more information.
|
|
30
|
+
|
|
31
|
+
[contributing]: https://github.com/eienjs/.github/blob/main/docs/CONTRIBUTING.md
|
|
32
|
+
[changelog]: https://github.com/eienjs/adonisjs-simple-auth/blob/main/CHANGELOG.md
|
|
33
|
+
[source]: https://github.com/eienjs/adonisjs-simple-auth
|
|
34
|
+
[node-version]: https://www.npmjs.com/package/@eienjs/adonisjs-simple-auth
|
|
35
|
+
[release]: https://www.npmjs.com/package/@eienjs/adonisjs-simple-auth
|
|
36
|
+
[license]: https://github.com/eienjs/adonisjs-simple-auth/blob/main/LICENSE.md
|
|
37
|
+
[build]: https://github.com/eienjs/adonisjs-simple-auth/actions/workflows/build.yml?query=branch:main
|
|
38
|
+
[downloads]: https://www.npmjs.com/package/@eienjs/adonisjs-simple-auth
|
|
39
|
+
[badge-source]: https://img.shields.io/badge/source-eienjs/adonisjs--simple--auth-blue.svg?logo=github
|
|
40
|
+
[badge-node-version]: https://img.shields.io/node/v/@eienjs/adonisjs-simple-auth.svg?logo=nodedotjs
|
|
41
|
+
[badge-release]: https://img.shields.io/npm/v/@eienjs/adonisjs-simple-auth.svg?logo=npm
|
|
42
|
+
[badge-license]: https://img.shields.io/github/license/eienjs/adonisjs-simple-auth?logo=open-source-initiative
|
|
43
|
+
[badge-build]: https://img.shields.io/github/actions/workflow/status/eienjs/adonisjs-simple-auth/build.yml?branch=main
|
|
44
|
+
[badge-downloads]: https://img.shields.io/npm/dm/@eienjs/adonisjs-simple-auth.svg?logo=npm
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
4
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
5
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
6
|
+
if (decorator = decorators[i])
|
|
7
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
8
|
+
if (kind && result) __defProp(target, key, result);
|
|
9
|
+
return result;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export {
|
|
13
|
+
__decorateClass
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=chunk-EUXUH3YW.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SimpleAuth
|
|
3
|
+
} from "./chunk-UBUHVXKH.js";
|
|
4
|
+
|
|
5
|
+
// src/middleware/simple_auth_middleware.ts
|
|
6
|
+
var SimpleAuthMiddleware = class {
|
|
7
|
+
constructor(config) {
|
|
8
|
+
this.config = config;
|
|
9
|
+
}
|
|
10
|
+
async handle(ctx, next) {
|
|
11
|
+
ctx.simpleAuth = new SimpleAuth(ctx, this.config);
|
|
12
|
+
return next();
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export {
|
|
17
|
+
SimpleAuthMiddleware
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=chunk-QSRPDOI7.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/middleware/simple_auth_middleware.ts"],"sourcesContent":["import { type HttpContext } from '@adonisjs/core/http';\nimport { type NextFn } from '@adonisjs/core/types/http';\nimport { SimpleAuth } from '../simple_auth.js';\nimport { type ResolvedSimpleAuthConfig } from '../types.js';\n\nexport default class SimpleAuthMiddleware {\n public constructor(protected config: ResolvedSimpleAuthConfig) {}\n\n public async handle(ctx: HttpContext, next: NextFn) {\n ctx.simpleAuth = new SimpleAuth(ctx, this.config);\n\n return next();\n }\n}\n"],"mappings":";;;;;AAKA,IAAqB,uBAArB,MAA0C;AAAA,EACjC,YAAsB,QAAkC;AAAlC;AAAA,EAAmC;AAAA,EAEhE,MAAa,OAAO,KAAkB,MAAc;AAClD,QAAI,aAAa,IAAI,WAAW,KAAK,KAAK,MAAM;AAEhD,WAAO,KAAK;AAAA,EACd;AACF;","names":[]}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
import { Exception } from "@adonisjs/core/exceptions";
|
|
3
|
+
var SimpleAuthException = class extends Exception {
|
|
4
|
+
static code;
|
|
5
|
+
static status;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
// src/simple_auth.ts
|
|
9
|
+
var SimpleAuth = class {
|
|
10
|
+
constructor(ctx, config) {
|
|
11
|
+
this.ctx = ctx;
|
|
12
|
+
this.config = config;
|
|
13
|
+
}
|
|
14
|
+
authenticationFailed() {
|
|
15
|
+
return new SimpleAuthException("Unauthorized access");
|
|
16
|
+
}
|
|
17
|
+
getApiKeyRequest() {
|
|
18
|
+
const apiKey = this.ctx.request.header(this.config.apiKeyHeader, "");
|
|
19
|
+
if (!apiKey.trim()) {
|
|
20
|
+
throw this.authenticationFailed();
|
|
21
|
+
}
|
|
22
|
+
return apiKey;
|
|
23
|
+
}
|
|
24
|
+
authenticate() {
|
|
25
|
+
const apiKeyTokenRequest = this.getApiKeyRequest();
|
|
26
|
+
const apiKeyTokenConfig = this.config.apiKeyValue;
|
|
27
|
+
if (apiKeyTokenRequest !== apiKeyTokenConfig.release()) {
|
|
28
|
+
throw this.authenticationFailed();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export {
|
|
34
|
+
SimpleAuthException,
|
|
35
|
+
SimpleAuth
|
|
36
|
+
};
|
|
37
|
+
//# sourceMappingURL=chunk-UBUHVXKH.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/errors.ts","../src/simple_auth.ts"],"sourcesContent":["import { Exception } from '@adonisjs/core/exceptions';\n\nexport class SimpleAuthException extends Exception {\n public static readonly code: 'E_SIMPLE_UNAUTHORIZED_ACCESS';\n\n public static readonly status: 401;\n}\n","import { type HttpContext } from '@adonisjs/core/http';\nimport { SimpleAuthException } from './errors.js';\nimport { type ResolvedSimpleAuthConfig } from './types.js';\n\nexport class SimpleAuth {\n public constructor(\n protected ctx: HttpContext,\n protected config: ResolvedSimpleAuthConfig,\n ) {}\n\n protected authenticationFailed(): SimpleAuthException {\n return new SimpleAuthException('Unauthorized access');\n }\n\n protected getApiKeyRequest(): string {\n const apiKey = this.ctx.request.header(this.config.apiKeyHeader, '')!;\n if (!apiKey.trim()) {\n throw this.authenticationFailed();\n }\n\n return apiKey;\n }\n\n public authenticate(): void {\n const apiKeyTokenRequest = this.getApiKeyRequest();\n const apiKeyTokenConfig = this.config.apiKeyValue;\n\n if (apiKeyTokenRequest !== apiKeyTokenConfig.release()) {\n throw this.authenticationFailed();\n }\n }\n}\n"],"mappings":";AAAA,SAAS,iBAAiB;AAEnB,IAAM,sBAAN,cAAkC,UAAU;AAAA,EACjD,OAAuB;AAAA,EAEvB,OAAuB;AACzB;;;ACFO,IAAM,aAAN,MAAiB;AAAA,EACf,YACK,KACA,QACV;AAFU;AACA;AAAA,EACT;AAAA,EAEO,uBAA4C;AACpD,WAAO,IAAI,oBAAoB,qBAAqB;AAAA,EACtD;AAAA,EAEU,mBAA2B;AACnC,UAAM,SAAS,KAAK,IAAI,QAAQ,OAAO,KAAK,OAAO,cAAc,EAAE;AACnE,QAAI,CAAC,OAAO,KAAK,GAAG;AAClB,YAAM,KAAK,qBAAqB;AAAA,IAClC;AAEA,WAAO;AAAA,EACT;AAAA,EAEO,eAAqB;AAC1B,UAAM,qBAAqB,KAAK,iBAAiB;AACjD,UAAM,oBAAoB,KAAK,OAAO;AAEtC,QAAI,uBAAuB,kBAAkB,QAAQ,GAAG;AACtD,YAAM,KAAK,qBAAqB;AAAA,IAClC;AAAA,EACF;AACF;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"commands":[{"commandName":"generate:api-key","description":"Generate a a cryptographically secure random value suffixed with a CRC32 checksum","help":"","namespace":"generate","aliases":[],"flags":[{"name":"show","flagName":"show","required":false,"type":"boolean","description":"Display the token on the terminal, instead of writing it to .env file"},{"name":"force","flagName":"force","required":false,"type":"boolean","description":"Force update .env file in production environment"}],"args":[],"options":{},"filePath":"generate_api_key.js"}],"version":1}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { BaseCommand } from '@adonisjs/core/ace';
|
|
2
|
+
export default class GenerateApiKey extends BaseCommand {
|
|
3
|
+
static readonly commandName = "generate:api-key";
|
|
4
|
+
static readonly description = "Generate a a cryptographically secure random value suffixed with a CRC32 checksum";
|
|
5
|
+
show: boolean;
|
|
6
|
+
force: boolean;
|
|
7
|
+
run(): Promise<void>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__decorateClass
|
|
3
|
+
} from "../chunk-EUXUH3YW.js";
|
|
4
|
+
|
|
5
|
+
// commands/generate_api_key.ts
|
|
6
|
+
import process from "node:process";
|
|
7
|
+
import { BaseCommand, flags } from "@adonisjs/core/ace";
|
|
8
|
+
import { EnvEditor } from "@adonisjs/core/env/editor";
|
|
9
|
+
import { base64, Secret } from "@adonisjs/core/helpers";
|
|
10
|
+
import string from "@adonisjs/core/helpers/string";
|
|
11
|
+
|
|
12
|
+
// src/helpers/crc32.ts
|
|
13
|
+
import { TextEncoder } from "node:util";
|
|
14
|
+
var CRC32 = class {
|
|
15
|
+
/**
|
|
16
|
+
* Lookup table calculated for 0xEDB88320 divisor
|
|
17
|
+
*/
|
|
18
|
+
lookupTable = [
|
|
19
|
+
0,
|
|
20
|
+
1996959894,
|
|
21
|
+
3993919788,
|
|
22
|
+
2567524794,
|
|
23
|
+
124634137,
|
|
24
|
+
1886057615,
|
|
25
|
+
3915621685,
|
|
26
|
+
2657392035,
|
|
27
|
+
249268274,
|
|
28
|
+
2044508324,
|
|
29
|
+
3772115230,
|
|
30
|
+
2547177864,
|
|
31
|
+
162941995,
|
|
32
|
+
2125561021,
|
|
33
|
+
3887607047,
|
|
34
|
+
2428444049,
|
|
35
|
+
498536548,
|
|
36
|
+
1789927666,
|
|
37
|
+
4089016648,
|
|
38
|
+
2227061214,
|
|
39
|
+
450548861,
|
|
40
|
+
1843258603,
|
|
41
|
+
4107580753,
|
|
42
|
+
2211677639,
|
|
43
|
+
325883990,
|
|
44
|
+
1684777152,
|
|
45
|
+
4251122042,
|
|
46
|
+
2321926636,
|
|
47
|
+
335633487,
|
|
48
|
+
1661365465,
|
|
49
|
+
4195302755,
|
|
50
|
+
2366115317,
|
|
51
|
+
997073096,
|
|
52
|
+
1281953886,
|
|
53
|
+
3579855332,
|
|
54
|
+
2724688242,
|
|
55
|
+
1006888145,
|
|
56
|
+
1258607687,
|
|
57
|
+
3524101629,
|
|
58
|
+
2768942443,
|
|
59
|
+
901097722,
|
|
60
|
+
1119000684,
|
|
61
|
+
3686517206,
|
|
62
|
+
2898065728,
|
|
63
|
+
853044451,
|
|
64
|
+
1172266101,
|
|
65
|
+
3705015759,
|
|
66
|
+
2882616665,
|
|
67
|
+
651767980,
|
|
68
|
+
1373503546,
|
|
69
|
+
3369554304,
|
|
70
|
+
3218104598,
|
|
71
|
+
565507253,
|
|
72
|
+
1454621731,
|
|
73
|
+
3485111705,
|
|
74
|
+
3099436303,
|
|
75
|
+
671266974,
|
|
76
|
+
1594198024,
|
|
77
|
+
3322730930,
|
|
78
|
+
2970347812,
|
|
79
|
+
795835527,
|
|
80
|
+
1483230225,
|
|
81
|
+
3244367275,
|
|
82
|
+
3060149565,
|
|
83
|
+
1994146192,
|
|
84
|
+
31158534,
|
|
85
|
+
2563907772,
|
|
86
|
+
4023717930,
|
|
87
|
+
1907459465,
|
|
88
|
+
112637215,
|
|
89
|
+
2680153253,
|
|
90
|
+
3904427059,
|
|
91
|
+
2013776290,
|
|
92
|
+
251722036,
|
|
93
|
+
2517215374,
|
|
94
|
+
3775830040,
|
|
95
|
+
2137656763,
|
|
96
|
+
141376813,
|
|
97
|
+
2439277719,
|
|
98
|
+
3865271297,
|
|
99
|
+
1802195444,
|
|
100
|
+
476864866,
|
|
101
|
+
2238001368,
|
|
102
|
+
4066508878,
|
|
103
|
+
1812370925,
|
|
104
|
+
453092731,
|
|
105
|
+
2181625025,
|
|
106
|
+
4111451223,
|
|
107
|
+
1706088902,
|
|
108
|
+
314042704,
|
|
109
|
+
2344532202,
|
|
110
|
+
4240017532,
|
|
111
|
+
1658658271,
|
|
112
|
+
366619977,
|
|
113
|
+
2362670323,
|
|
114
|
+
4224994405,
|
|
115
|
+
1303535960,
|
|
116
|
+
984961486,
|
|
117
|
+
2747007092,
|
|
118
|
+
3569037538,
|
|
119
|
+
1256170817,
|
|
120
|
+
1037604311,
|
|
121
|
+
2765210733,
|
|
122
|
+
3554079995,
|
|
123
|
+
1131014506,
|
|
124
|
+
879679996,
|
|
125
|
+
2909243462,
|
|
126
|
+
3663771856,
|
|
127
|
+
1141124467,
|
|
128
|
+
855842277,
|
|
129
|
+
2852801631,
|
|
130
|
+
3708648649,
|
|
131
|
+
1342533948,
|
|
132
|
+
654459306,
|
|
133
|
+
3188396048,
|
|
134
|
+
3373015174,
|
|
135
|
+
1466479909,
|
|
136
|
+
544179635,
|
|
137
|
+
3110523913,
|
|
138
|
+
3462522015,
|
|
139
|
+
1591671054,
|
|
140
|
+
702138776,
|
|
141
|
+
2966460450,
|
|
142
|
+
3352799412,
|
|
143
|
+
1504918807,
|
|
144
|
+
783551873,
|
|
145
|
+
3082640443,
|
|
146
|
+
3233442989,
|
|
147
|
+
3988292384,
|
|
148
|
+
2596254646,
|
|
149
|
+
62317068,
|
|
150
|
+
1957810842,
|
|
151
|
+
3939845945,
|
|
152
|
+
2647816111,
|
|
153
|
+
81470997,
|
|
154
|
+
1943803523,
|
|
155
|
+
3814918930,
|
|
156
|
+
2489596804,
|
|
157
|
+
225274430,
|
|
158
|
+
2053790376,
|
|
159
|
+
3826175755,
|
|
160
|
+
2466906013,
|
|
161
|
+
167816743,
|
|
162
|
+
2097651377,
|
|
163
|
+
4027552580,
|
|
164
|
+
2265490386,
|
|
165
|
+
503444072,
|
|
166
|
+
1762050814,
|
|
167
|
+
4150417245,
|
|
168
|
+
2154129355,
|
|
169
|
+
426522225,
|
|
170
|
+
1852507879,
|
|
171
|
+
4275313526,
|
|
172
|
+
2312317920,
|
|
173
|
+
282753626,
|
|
174
|
+
1742555852,
|
|
175
|
+
4189708143,
|
|
176
|
+
2394877945,
|
|
177
|
+
397917763,
|
|
178
|
+
1622183637,
|
|
179
|
+
3604390888,
|
|
180
|
+
2714866558,
|
|
181
|
+
953729732,
|
|
182
|
+
1340076626,
|
|
183
|
+
3518719985,
|
|
184
|
+
2797360999,
|
|
185
|
+
1068828381,
|
|
186
|
+
1219638859,
|
|
187
|
+
3624741850,
|
|
188
|
+
2936675148,
|
|
189
|
+
906185462,
|
|
190
|
+
1090812512,
|
|
191
|
+
3747672003,
|
|
192
|
+
2825379669,
|
|
193
|
+
829329135,
|
|
194
|
+
1181335161,
|
|
195
|
+
3412177804,
|
|
196
|
+
3160834842,
|
|
197
|
+
628085408,
|
|
198
|
+
1382605366,
|
|
199
|
+
3423369109,
|
|
200
|
+
3138078467,
|
|
201
|
+
570562233,
|
|
202
|
+
1426400815,
|
|
203
|
+
3317316542,
|
|
204
|
+
2998733608,
|
|
205
|
+
733239954,
|
|
206
|
+
1555261956,
|
|
207
|
+
3268935591,
|
|
208
|
+
3050360625,
|
|
209
|
+
752459403,
|
|
210
|
+
1541320221,
|
|
211
|
+
2607071920,
|
|
212
|
+
3965973030,
|
|
213
|
+
1969922972,
|
|
214
|
+
40735498,
|
|
215
|
+
2617837225,
|
|
216
|
+
3943577151,
|
|
217
|
+
1913087877,
|
|
218
|
+
83908371,
|
|
219
|
+
2512341634,
|
|
220
|
+
3803740692,
|
|
221
|
+
2075208622,
|
|
222
|
+
213261112,
|
|
223
|
+
2463272603,
|
|
224
|
+
3855990285,
|
|
225
|
+
2094854071,
|
|
226
|
+
198958881,
|
|
227
|
+
2262029012,
|
|
228
|
+
4057260610,
|
|
229
|
+
1759359992,
|
|
230
|
+
534414190,
|
|
231
|
+
2176718541,
|
|
232
|
+
4139329115,
|
|
233
|
+
1873836001,
|
|
234
|
+
414664567,
|
|
235
|
+
2282248934,
|
|
236
|
+
4279200368,
|
|
237
|
+
1711684554,
|
|
238
|
+
285281116,
|
|
239
|
+
2405801727,
|
|
240
|
+
4167216745,
|
|
241
|
+
1634467795,
|
|
242
|
+
376229701,
|
|
243
|
+
2685067896,
|
|
244
|
+
3608007406,
|
|
245
|
+
1308918612,
|
|
246
|
+
956543938,
|
|
247
|
+
2808555105,
|
|
248
|
+
3495958263,
|
|
249
|
+
1231636301,
|
|
250
|
+
1047427035,
|
|
251
|
+
2932959818,
|
|
252
|
+
3654703836,
|
|
253
|
+
1088359270,
|
|
254
|
+
936918e3,
|
|
255
|
+
2847714899,
|
|
256
|
+
3736837829,
|
|
257
|
+
1202900863,
|
|
258
|
+
817233897,
|
|
259
|
+
3183342108,
|
|
260
|
+
3401237130,
|
|
261
|
+
1404277552,
|
|
262
|
+
615818150,
|
|
263
|
+
3134207493,
|
|
264
|
+
3453421203,
|
|
265
|
+
1423857449,
|
|
266
|
+
601450431,
|
|
267
|
+
3009837614,
|
|
268
|
+
3294710456,
|
|
269
|
+
1567103746,
|
|
270
|
+
711928724,
|
|
271
|
+
3020668471,
|
|
272
|
+
3272380065,
|
|
273
|
+
1510334235,
|
|
274
|
+
755167117
|
|
275
|
+
];
|
|
276
|
+
initialCRC = 4294967295;
|
|
277
|
+
calculateBytes(bytes, accumulator) {
|
|
278
|
+
let crc = accumulator ?? this.initialCRC;
|
|
279
|
+
for (const byte of bytes) {
|
|
280
|
+
const tableIndex = (crc ^ byte) & 255;
|
|
281
|
+
const tableVal = this.lookupTable[tableIndex];
|
|
282
|
+
crc = crc >>> 8 ^ tableVal;
|
|
283
|
+
}
|
|
284
|
+
return crc;
|
|
285
|
+
}
|
|
286
|
+
crcToUint(crc) {
|
|
287
|
+
return this.toUint32(crc ^ 4294967295);
|
|
288
|
+
}
|
|
289
|
+
strToBytes(input) {
|
|
290
|
+
const encoder = new TextEncoder();
|
|
291
|
+
return encoder.encode(input);
|
|
292
|
+
}
|
|
293
|
+
toUint32(num) {
|
|
294
|
+
if (num >= 0) {
|
|
295
|
+
return num;
|
|
296
|
+
}
|
|
297
|
+
return 4294967295 - num * -1 + 1;
|
|
298
|
+
}
|
|
299
|
+
calculate(input) {
|
|
300
|
+
return this.forString(input);
|
|
301
|
+
}
|
|
302
|
+
forString(input) {
|
|
303
|
+
const bytes = this.strToBytes(input);
|
|
304
|
+
return this.forBytes(bytes);
|
|
305
|
+
}
|
|
306
|
+
forBytes(bytes, accumulator) {
|
|
307
|
+
const crc = this.calculateBytes(bytes, accumulator);
|
|
308
|
+
return this.crcToUint(crc);
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
// commands/generate_api_key.ts
|
|
313
|
+
var GenerateApiKey = class extends BaseCommand {
|
|
314
|
+
static commandName = "generate:api-key";
|
|
315
|
+
static description = "Generate a a cryptographically secure random value suffixed with a CRC32 checksum";
|
|
316
|
+
async run() {
|
|
317
|
+
let writeToFile = process.env.NODE_ENV !== "production";
|
|
318
|
+
if (this.force) {
|
|
319
|
+
writeToFile = true;
|
|
320
|
+
}
|
|
321
|
+
if (this.show) {
|
|
322
|
+
writeToFile = false;
|
|
323
|
+
}
|
|
324
|
+
const seed = string.random(40);
|
|
325
|
+
const tokenSecret = new Secret(`${seed}${new CRC32().calculate(seed)}`);
|
|
326
|
+
const result = new Secret(
|
|
327
|
+
`oat_${base64.urlEncode(String(string.random(2)))}.${base64.urlEncode(tokenSecret.release())}`
|
|
328
|
+
);
|
|
329
|
+
const tokenString = result.release();
|
|
330
|
+
if (writeToFile) {
|
|
331
|
+
const editor = await EnvEditor.create(this.app.appRoot);
|
|
332
|
+
editor.add("API_KEY", tokenString, true);
|
|
333
|
+
await editor.save();
|
|
334
|
+
this.logger.action("add API_KEY to .env").succeeded();
|
|
335
|
+
} else {
|
|
336
|
+
this.logger.log(`API_KEY = ${tokenString}`);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
__decorateClass([
|
|
341
|
+
flags.boolean({
|
|
342
|
+
description: "Display the token on the terminal, instead of writing it to .env file"
|
|
343
|
+
})
|
|
344
|
+
], GenerateApiKey.prototype, "show", 2);
|
|
345
|
+
__decorateClass([
|
|
346
|
+
flags.boolean({
|
|
347
|
+
description: "Force update .env file in production environment"
|
|
348
|
+
})
|
|
349
|
+
], GenerateApiKey.prototype, "force", 2);
|
|
350
|
+
export {
|
|
351
|
+
GenerateApiKey as default
|
|
352
|
+
};
|
|
353
|
+
//# sourceMappingURL=generate_api_key.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../commands/generate_api_key.ts","../../src/helpers/crc32.ts"],"sourcesContent":["import process from 'node:process';\nimport { BaseCommand, flags } from '@adonisjs/core/ace';\nimport { EnvEditor } from '@adonisjs/core/env/editor';\nimport { base64, Secret } from '@adonisjs/core/helpers';\nimport string from '@adonisjs/core/helpers/string';\nimport { CRC32 } from '../src/helpers/crc32.js';\n\nexport default class GenerateApiKey extends BaseCommand {\n public static readonly commandName = 'generate:api-key';\n\n public static readonly description =\n 'Generate a a cryptographically secure random value suffixed with a CRC32 checksum';\n\n @flags.boolean({\n description: 'Display the token on the terminal, instead of writing it to .env file',\n })\n declare public show: boolean;\n\n @flags.boolean({\n description: 'Force update .env file in production environment',\n })\n declare public force: boolean;\n\n public async run() {\n let writeToFile = process.env.NODE_ENV !== 'production';\n if (this.force) {\n writeToFile = true;\n }\n\n if (this.show) {\n writeToFile = false;\n }\n\n const seed = string.random(40);\n const tokenSecret = new Secret(`${seed}${new CRC32().calculate(seed)}`);\n const result = new Secret(\n `oat_${base64.urlEncode(String(string.random(2)))}.${base64.urlEncode(tokenSecret.release())}`,\n );\n const tokenString = result.release();\n\n if (writeToFile) {\n const editor = await EnvEditor.create(this.app.appRoot);\n editor.add('API_KEY', tokenString, true);\n await editor.save();\n this.logger.action('add API_KEY to .env').succeeded();\n } else {\n this.logger.log(`API_KEY = ${tokenString}`);\n }\n }\n}\n","import { TextEncoder } from 'node:util';\n/**\n * We use CRC32 just to add a recognizable checksum to tokens. This helps\n * secret scanning tools like https://docs.github.com/en/github/administering-a-repository/about-secret-scanning easily detect tokens generated by a given program.\n *\n * You can learn more about appending checksum to a hash here in this Github\n * article. https://github.blog/2021-04-05-behind-githubs-new-authentication-token-formats/\n *\n * Code taken from:\n * https://github.com/adonisjs/auth/blob/9.x/modules/access_tokens_guard/crc32.ts\n */\n\nexport class CRC32 {\n /**\n * Lookup table calculated for 0xEDB88320 divisor\n */\n protected readonly lookupTable = [\n 0, 1996959894, 3993919788, 2567524794, 124634137, 1886057615, 3915621685, 2657392035, 249268274, 2044508324,\n 3772115230, 2547177864, 162941995, 2125561021, 3887607047, 2428444049, 498536548, 1789927666, 4089016648,\n 2227061214, 450548861, 1843258603, 4107580753, 2211677639, 325883990, 1684777152, 4251122042, 2321926636, 335633487,\n 1661365465, 4195302755, 2366115317, 997073096, 1281953886, 3579855332, 2724688242, 1006888145, 1258607687,\n 3524101629, 2768942443, 901097722, 1119000684, 3686517206, 2898065728, 853044451, 1172266101, 3705015759,\n 2882616665, 651767980, 1373503546, 3369554304, 3218104598, 565507253, 1454621731, 3485111705, 3099436303, 671266974,\n 1594198024, 3322730930, 2970347812, 795835527, 1483230225, 3244367275, 3060149565, 1994146192, 31158534, 2563907772,\n 4023717930, 1907459465, 112637215, 2680153253, 3904427059, 2013776290, 251722036, 2517215374, 3775830040,\n 2137656763, 141376813, 2439277719, 3865271297, 1802195444, 476864866, 2238001368, 4066508878, 1812370925, 453092731,\n 2181625025, 4111451223, 1706088902, 314042704, 2344532202, 4240017532, 1658658271, 366619977, 2362670323,\n 4224994405, 1303535960, 984961486, 2747007092, 3569037538, 1256170817, 1037604311, 2765210733, 3554079995,\n 1131014506, 879679996, 2909243462, 3663771856, 1141124467, 855842277, 2852801631, 3708648649, 1342533948, 654459306,\n 3188396048, 3373015174, 1466479909, 544179635, 3110523913, 3462522015, 1591671054, 702138776, 2966460450,\n 3352799412, 1504918807, 783551873, 3082640443, 3233442989, 3988292384, 2596254646, 62317068, 1957810842, 3939845945,\n 2647816111, 81470997, 1943803523, 3814918930, 2489596804, 225274430, 2053790376, 3826175755, 2466906013, 167816743,\n 2097651377, 4027552580, 2265490386, 503444072, 1762050814, 4150417245, 2154129355, 426522225, 1852507879,\n 4275313526, 2312317920, 282753626, 1742555852, 4189708143, 2394877945, 397917763, 1622183637, 3604390888,\n 2714866558, 953729732, 1340076626, 3518719985, 2797360999, 1068828381, 1219638859, 3624741850, 2936675148,\n 906185462, 1090812512, 3747672003, 2825379669, 829329135, 1181335161, 3412177804, 3160834842, 628085408, 1382605366,\n 3423369109, 3138078467, 570562233, 1426400815, 3317316542, 2998733608, 733239954, 1555261956, 3268935591,\n 3050360625, 752459403, 1541320221, 2607071920, 3965973030, 1969922972, 40735498, 2617837225, 3943577151, 1913087877,\n 83908371, 2512341634, 3803740692, 2075208622, 213261112, 2463272603, 3855990285, 2094854071, 198958881, 2262029012,\n 4057260610, 1759359992, 534414190, 2176718541, 4139329115, 1873836001, 414664567, 2282248934, 4279200368,\n 1711684554, 285281116, 2405801727, 4167216745, 1634467795, 376229701, 2685067896, 3608007406, 1308918612, 956543938,\n 2808555105, 3495958263, 1231636301, 1047427035, 2932959818, 3654703836, 1088359270, 936918000, 2847714899,\n 3736837829, 1202900863, 817233897, 3183342108, 3401237130, 1404277552, 615818150, 3134207493, 3453421203,\n 1423857449, 601450431, 3009837614, 3294710456, 1567103746, 711928724, 3020668471, 3272380065, 1510334235, 755167117,\n ];\n\n protected readonly initialCRC = 0xffffffff;\n\n protected calculateBytes(bytes: Uint8Array, accumulator?: number): number {\n let crc = accumulator ?? this.initialCRC;\n for (const byte of bytes) {\n const tableIndex = (crc ^ byte) & 0xff;\n const tableVal = this.lookupTable[tableIndex];\n crc = (crc >>> 8) ^ tableVal;\n }\n\n return crc;\n }\n\n protected crcToUint(crc: number): number {\n return this.toUint32(crc ^ 0xffffffff);\n }\n\n protected strToBytes(input: string): Uint8Array {\n const encoder = new TextEncoder();\n\n return encoder.encode(input);\n }\n\n protected toUint32(num: number): number {\n if (num >= 0) {\n return num;\n }\n\n return 0xffffffff - num * -1 + 1;\n }\n\n public calculate(input: string): number {\n return this.forString(input);\n }\n\n public forString(input: string): number {\n const bytes = this.strToBytes(input);\n\n return this.forBytes(bytes);\n }\n\n public forBytes(bytes: Uint8Array, accumulator?: number): number {\n const crc = this.calculateBytes(bytes, accumulator);\n\n return this.crcToUint(crc);\n }\n}\n"],"mappings":";;;;;AAAA,OAAO,aAAa;AACpB,SAAS,aAAa,aAAa;AACnC,SAAS,iBAAiB;AAC1B,SAAS,QAAQ,cAAc;AAC/B,OAAO,YAAY;;;ACJnB,SAAS,mBAAmB;AAYrB,IAAM,QAAN,MAAY;AAAA;AAAA;AAAA;AAAA,EAIE,cAAc;AAAA,IAC/B;AAAA,IAAG;AAAA,IAAY;AAAA,IAAY;AAAA,IAAY;AAAA,IAAW;AAAA,IAAY;AAAA,IAAY;AAAA,IAAY;AAAA,IAAW;AAAA,IACjG;AAAA,IAAY;AAAA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
*/
|
|
6
|
+
let commandsMetaData
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Reads the commands from the "./commands.json" file. Since, the commands.json
|
|
10
|
+
* file is generated automatically, we do not have to validate its contents
|
|
11
|
+
*/
|
|
12
|
+
export async function getMetaData() {
|
|
13
|
+
if (commandsMetaData) {
|
|
14
|
+
return commandsMetaData
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const commandsIndex = await readFile(new URL('./commands.json', import.meta.url), 'utf-8')
|
|
18
|
+
commandsMetaData = JSON.parse(commandsIndex).commands
|
|
19
|
+
|
|
20
|
+
return commandsMetaData
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Imports the command by lookingup its path from the commands
|
|
25
|
+
* metadata
|
|
26
|
+
*/
|
|
27
|
+
export async function getCommand(metaData) {
|
|
28
|
+
const commands = await getMetaData()
|
|
29
|
+
const command = commands.find(({ commandName }) => metaData.commandName === commandName)
|
|
30
|
+
if (!command) {
|
|
31
|
+
return null
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const { default: commandConstructor } = await import(new URL(command.filePath, import.meta.url).href)
|
|
35
|
+
return commandConstructor
|
|
36
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{{{
|
|
2
|
+
exports({ to: app.configPath('simpleauth.ts') })
|
|
3
|
+
}}}
|
|
4
|
+
import env from '#start/env'
|
|
5
|
+
import { defineConfig } from '@eienjs/adonisjs-simple-auth'
|
|
6
|
+
|
|
7
|
+
const simpleAuthConfig = defineConfig({
|
|
8
|
+
apiKeyHeader: 'x-api-key',
|
|
9
|
+
apiKeyValue: env.get('API_KEY'),
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
export default simpleAuthConfig
|
package/build/index.d.ts
ADDED
package/build/index.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SimpleAuth,
|
|
3
|
+
SimpleAuthException
|
|
4
|
+
} from "./chunk-UBUHVXKH.js";
|
|
5
|
+
import "./chunk-EUXUH3YW.js";
|
|
6
|
+
|
|
7
|
+
// stubs/main.ts
|
|
8
|
+
import { getDirname } from "@adonisjs/core/helpers";
|
|
9
|
+
var stubsRoot = getDirname(import.meta.url);
|
|
10
|
+
|
|
11
|
+
// configure.ts
|
|
12
|
+
var configure = async (command) => {
|
|
13
|
+
const codemods = await command.createCodemods();
|
|
14
|
+
await codemods.updateRcFile((rcFile) => {
|
|
15
|
+
rcFile.addCommand("@eienjs/adonisjs-simple-auth/commands");
|
|
16
|
+
rcFile.addProvider("@eienjs/adonisjs-simple-auth/simple_auth_provider");
|
|
17
|
+
});
|
|
18
|
+
await codemods.makeUsingStub(stubsRoot, "config.stub", {});
|
|
19
|
+
await codemods.makeUsingStub(stubsRoot, "middleware/api_key.stub", {
|
|
20
|
+
entity: command.app.generators.createEntity("api_key")
|
|
21
|
+
});
|
|
22
|
+
await codemods.registerMiddleware("router", [{ path: "@eienjs/adonisjs-simple-auth/simple_auth_middleware" }]);
|
|
23
|
+
await codemods.registerMiddleware("named", [
|
|
24
|
+
{
|
|
25
|
+
name: "apiKey",
|
|
26
|
+
path: "#middleware/api_key_middleware"
|
|
27
|
+
}
|
|
28
|
+
]);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// src/define_config.ts
|
|
32
|
+
import { configProvider } from "@adonisjs/core";
|
|
33
|
+
import { Secret } from "@adonisjs/core/helpers";
|
|
34
|
+
var defineConfig = (config) => {
|
|
35
|
+
return configProvider.create(async (_) => {
|
|
36
|
+
return {
|
|
37
|
+
apiKeyHeader: config.apiKeyHeader ?? "x-api-key",
|
|
38
|
+
apiKeyValue: new Secret(config.apiKeyValue)
|
|
39
|
+
};
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
export {
|
|
43
|
+
SimpleAuth,
|
|
44
|
+
SimpleAuthException,
|
|
45
|
+
configure,
|
|
46
|
+
defineConfig,
|
|
47
|
+
stubsRoot
|
|
48
|
+
};
|
|
49
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../stubs/main.ts","../configure.ts","../src/define_config.ts"],"sourcesContent":["import { getDirname } from '@adonisjs/core/helpers';\n\n/**\n * Path to the root directory where the stubs are stored. We use\n * this path within commands and the configure hook\n */\nexport const stubsRoot = getDirname(import.meta.url);\n","import type Configure from '@adonisjs/core/commands/configure';\nimport { stubsRoot } from './stubs/main.js';\n\nexport const configure = async (command: Configure): Promise<void> => {\n const codemods = await command.createCodemods();\n\n // Publish commands and provider\n await codemods.updateRcFile((rcFile) => {\n rcFile.addCommand('@eienjs/adonisjs-simple-auth/commands');\n rcFile.addProvider('@eienjs/adonisjs-simple-auth/simple_auth_provider');\n });\n\n // Publish config file\n await codemods.makeUsingStub(stubsRoot, 'config.stub', {});\n\n // Publish api key middleware\n await codemods.makeUsingStub(stubsRoot, 'middleware/api_key.stub', {\n entity: command.app.generators.createEntity('api_key'),\n });\n\n // Register middleware\n await codemods.registerMiddleware('router', [{ path: '@eienjs/adonisjs-simple-auth/simple_auth_middleware' }]);\n await codemods.registerMiddleware('named', [\n {\n name: 'apiKey',\n path: '#middleware/api_key_middleware',\n },\n ]);\n};\n","import { configProvider } from '@adonisjs/core';\nimport { Secret } from '@adonisjs/core/helpers';\nimport { type ConfigProvider } from '@adonisjs/core/types';\nimport { type ResolvedSimpleAuthConfig, type SimpleAuthConfig } from './types.js';\n\nexport const defineConfig = (config: SimpleAuthConfig): ConfigProvider<ResolvedSimpleAuthConfig> => {\n return configProvider.create(async (_) => {\n return {\n apiKeyHeader: config.apiKeyHeader ?? 'x-api-key',\n apiKeyValue: new Secret(config.apiKeyValue),\n };\n });\n};\n"],"mappings":";;;;;;;AAAA,SAAS,kBAAkB;AAMpB,IAAM,YAAY,WAAW,YAAY,GAAG;;;ACH5C,IAAM,YAAY,OAAO,YAAsC;AACpE,QAAM,WAAW,MAAM,QAAQ,eAAe;AAG9C,QAAM,SAAS,aAAa,CAAC,WAAW;AACtC,WAAO,WAAW,uCAAuC;AACzD,WAAO,YAAY,mDAAmD;AAAA,EACxE,CAAC;AAGD,QAAM,SAAS,cAAc,WAAW,eAAe,CAAC,CAAC;AAGzD,QAAM,SAAS,cAAc,WAAW,2BAA2B;AAAA,IACjE,QAAQ,QAAQ,IAAI,WAAW,aAAa,SAAS;AAAA,EACvD,CAAC;AAGD,QAAM,SAAS,mBAAmB,UAAU,CAAC,EAAE,MAAM,sDAAsD,CAAC,CAAC;AAC7G,QAAM,SAAS,mBAAmB,SAAS;AAAA,IACzC;AAAA,MACE,MAAM;AAAA,MACN,MAAM;AAAA,IACR;AAAA,EACF,CAAC;AACH;;;AC5BA,SAAS,sBAAsB;AAC/B,SAAS,cAAc;AAIhB,IAAM,eAAe,CAAC,WAAuE;AAClG,SAAO,eAAe,OAAO,OAAO,MAAM;AACxC,WAAO;AAAA,MACL,cAAc,OAAO,gBAAgB;AAAA,MACrC,aAAa,IAAI,OAAO,OAAO,WAAW;AAAA,IAC5C;AAAA,EACF,CAAC;AACH;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"inputs":{"stubs/main.ts":{"bytes":233,"imports":[{"path":"@adonisjs/core/helpers","kind":"import-statement","external":true}],"format":"esm"},"configure.ts":{"bytes":995,"imports":[{"path":"stubs/main.ts","kind":"import-statement","original":"./stubs/main.js"}],"format":"esm"},"src/define_config.ts":{"bytes":526,"imports":[{"path":"@adonisjs/core","kind":"import-statement","external":true},{"path":"@adonisjs/core/helpers","kind":"import-statement","external":true},{"path":"@adonisjs/core/types","kind":"import-statement","external":true},{"path":"./types.js","kind":"import-statement","external":true}],"format":"esm"},"src/errors.ts":{"bytes":213,"imports":[{"path":"@adonisjs/core/exceptions","kind":"import-statement","external":true}],"format":"esm"},"src/simple_auth.ts":{"bytes":899,"imports":[{"path":"@adonisjs/core/http","kind":"import-statement","external":true},{"path":"src/errors.ts","kind":"import-statement","original":"./errors.js"},{"path":"./types.js","kind":"import-statement","external":true}],"format":"esm"},"index.ts":{"bytes":250,"imports":[{"path":"configure.ts","kind":"import-statement","original":"./configure.js"},{"path":"src/define_config.ts","kind":"import-statement","original":"./src/define_config.js"},{"path":"src/errors.ts","kind":"import-statement","original":"./src/errors.js"},{"path":"src/simple_auth.ts","kind":"import-statement","original":"./src/simple_auth.js"},{"path":"stubs/main.ts","kind":"import-statement","original":"./stubs/main.js"}],"format":"esm"},"src/helpers/crc32.ts":{"bytes":4896,"imports":[{"path":"node:util","kind":"import-statement","external":true}],"format":"esm"},"commands/generate_api_key.ts":{"bytes":1616,"imports":[{"path":"node:process","kind":"import-statement","external":true},{"path":"@adonisjs/core/ace","kind":"import-statement","external":true},{"path":"@adonisjs/core/env/editor","kind":"import-statement","external":true},{"path":"@adonisjs/core/helpers","kind":"import-statement","external":true},{"path":"@adonisjs/core/helpers/string","kind":"import-statement","external":true},{"path":"src/helpers/crc32.ts","kind":"import-statement","original":"../src/helpers/crc32.js"},{"path":"<runtime>","kind":"import-statement","external":true}],"format":"esm"},"src/middleware/simple_auth_middleware.ts":{"bytes":473,"imports":[{"path":"@adonisjs/core/http","kind":"import-statement","external":true},{"path":"@adonisjs/core/types/http","kind":"import-statement","external":true},{"path":"src/simple_auth.ts","kind":"import-statement","original":"../simple_auth.js"},{"path":"../types.js","kind":"import-statement","external":true}],"format":"esm"},"providers/simple_auth_provider.ts":{"bytes":1187,"imports":[{"path":"@adonisjs/core","kind":"import-statement","external":true},{"path":"@adonisjs/core/exceptions","kind":"import-statement","external":true},{"path":"@adonisjs/core/types","kind":"import-statement","external":true},{"path":"src/middleware/simple_auth_middleware.ts","kind":"import-statement","original":"../src/middleware/simple_auth_middleware.js"},{"path":"../src/simple_auth.js","kind":"import-statement","external":true},{"path":"../src/types.js","kind":"import-statement","external":true}],"format":"esm"},"src/types.ts":{"bytes":241,"imports":[{"path":"@adonisjs/core/helpers","kind":"import-statement","external":true}],"format":"esm"},"src/plugins/japa/api_client.ts":{"bytes":1434,"imports":[{"path":"@adonisjs/core","kind":"import-statement","external":true},{"path":"@adonisjs/core/exceptions","kind":"import-statement","external":true},{"path":"@adonisjs/core/types","kind":"import-statement","external":true},{"path":"@japa/api-client","kind":"import-statement","external":true},{"path":"@japa/runner/types","kind":"import-statement","external":true},{"path":"../../types.js","kind":"import-statement","external":true}],"format":"esm"}},"outputs":{"build/index.js.map":{"imports":[],"exports":[],"inputs":{},"bytes":2665},"build/index.js":{"imports":[{"path":"build/chunk-UBUHVXKH.js","kind":"import-statement"},{"path":"build/chunk-EUXUH3YW.js","kind":"import-statement"},{"path":"@adonisjs/core/helpers","kind":"import-statement","external":true},{"path":"@adonisjs/core","kind":"import-statement","external":true},{"path":"@adonisjs/core/helpers","kind":"import-statement","external":true}],"exports":["SimpleAuth","SimpleAuthException","configure","defineConfig","stubsRoot"],"entryPoint":"index.ts","inputs":{"stubs/main.ts":{"bytesInOutput":98},"configure.ts":{"bytesInOutput":726},"index.ts":{"bytesInOutput":0},"src/define_config.ts":{"bytesInOutput":312}},"bytes":1391},"build/commands/generate_api_key.js.map":{"imports":[],"exports":[],"inputs":{},"bytes":11126},"build/commands/generate_api_key.js":{"imports":[{"path":"build/chunk-EUXUH3YW.js","kind":"import-statement"},{"path":"node:process","kind":"import-statement","external":true},{"path":"@adonisjs/core/ace","kind":"import-statement","external":true},{"path":"@adonisjs/core/env/editor","kind":"import-statement","external":true},{"path":"@adonisjs/core/helpers","kind":"import-statement","external":true},{"path":"@adonisjs/core/helpers/string","kind":"import-statement","external":true},{"path":"node:util","kind":"import-statement","external":true}],"exports":["default"],"entryPoint":"commands/generate_api_key.ts","inputs":{"commands/generate_api_key.ts":{"bytesInOutput":1557},"src/helpers/crc32.ts":{"bytesInOutput":5029}},"bytes":6775},"build/providers/simple_auth_provider.js.map":{"imports":[],"exports":[],"inputs":{},"bytes":1739},"build/providers/simple_auth_provider.js":{"imports":[{"path":"build/chunk-QSRPDOI7.js","kind":"import-statement"},{"path":"build/chunk-UBUHVXKH.js","kind":"import-statement"},{"path":"build/chunk-EUXUH3YW.js","kind":"import-statement"},{"path":"@adonisjs/core","kind":"import-statement","external":true},{"path":"@adonisjs/core/exceptions","kind":"import-statement","external":true}],"exports":["default"],"entryPoint":"providers/simple_auth_provider.ts","inputs":{"providers/simple_auth_provider.ts":{"bytesInOutput":671}},"bytes":878},"build/src/types.js.map":{"imports":[],"exports":[],"inputs":{},"bytes":93},"build/src/types.js":{"imports":[],"exports":[],"entryPoint":"src/types.ts","inputs":{"src/types.ts":{"bytesInOutput":0}},"bytes":0},"build/src/middleware/simple_auth_middleware.js.map":{"imports":[],"exports":[],"inputs":{},"bytes":93},"build/src/middleware/simple_auth_middleware.js":{"imports":[{"path":"build/chunk-QSRPDOI7.js","kind":"import-statement"},{"path":"build/chunk-UBUHVXKH.js","kind":"import-statement"},{"path":"build/chunk-EUXUH3YW.js","kind":"import-statement"}],"exports":["default"],"entryPoint":"src/middleware/simple_auth_middleware.ts","inputs":{},"bytes":180},"build/chunk-QSRPDOI7.js.map":{"imports":[],"exports":[],"inputs":{},"bytes":800},"build/chunk-QSRPDOI7.js":{"imports":[{"path":"build/chunk-UBUHVXKH.js","kind":"import-statement"}],"exports":["SimpleAuthMiddleware"],"inputs":{"src/middleware/simple_auth_middleware.ts":{"bytesInOutput":198}},"bytes":331},"build/chunk-UBUHVXKH.js.map":{"imports":[],"exports":[],"inputs":{},"bytes":1821},"build/chunk-UBUHVXKH.js":{"imports":[{"path":"@adonisjs/core/exceptions","kind":"import-statement","external":true}],"exports":["SimpleAuth","SimpleAuthException"],"inputs":{"src/errors.ts":{"bytesInOutput":142},"src/simple_auth.ts":{"bytesInOutput":633}},"bytes":864},"build/src/plugins/japa/api_client.js.map":{"imports":[],"exports":[],"inputs":{},"bytes":2170},"build/src/plugins/japa/api_client.js":{"imports":[{"path":"build/chunk-EUXUH3YW.js","kind":"import-statement"},{"path":"@adonisjs/core","kind":"import-statement","external":true},{"path":"@adonisjs/core/exceptions","kind":"import-statement","external":true},{"path":"@japa/api-client","kind":"import-statement","external":true}],"exports":["simpleAuthApiClient"],"entryPoint":"src/plugins/japa/api_client.ts","inputs":{"src/plugins/japa/api_client.ts":{"bytesInOutput":979}},"bytes":1085},"build/chunk-EUXUH3YW.js.map":{"imports":[],"exports":[],"inputs":{},"bytes":93},"build/chunk-EUXUH3YW.js":{"imports":[],"exports":["__decorateClass"],"inputs":{},"bytes":524}}}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{{#var middlewareName = generators.middlewareName(entity.name)}}
|
|
2
|
+
{{#var middlewareFileName = generators.middlewareFileName(entity.name)}}
|
|
3
|
+
{{{
|
|
4
|
+
exports({ to: app.middlewarePath(entity.path, middlewareFileName) })
|
|
5
|
+
}}}
|
|
6
|
+
import type { HttpContext } from '@adonisjs/core/http'
|
|
7
|
+
import type { NextFn } from '@adonisjs/core/types/http'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Api Key middleware is used authenticate HTTP requests and deny
|
|
11
|
+
* access to unauthenticated requests.
|
|
12
|
+
*/
|
|
13
|
+
export default class {{ middlewareName }} {
|
|
14
|
+
async handle(
|
|
15
|
+
ctx: HttpContext,
|
|
16
|
+
next: NextFn,
|
|
17
|
+
) {
|
|
18
|
+
ctx.simpleAuth.authenticate()
|
|
19
|
+
|
|
20
|
+
return next()
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type ApplicationService } from '@adonisjs/core/types';
|
|
2
|
+
import { type SimpleAuth } from '../src/simple_auth.js';
|
|
3
|
+
/**
|
|
4
|
+
* HttpContext augmentations
|
|
5
|
+
*/
|
|
6
|
+
declare module '@adonisjs/core/http' {
|
|
7
|
+
interface HttpContext {
|
|
8
|
+
simpleAuth: SimpleAuth;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export default class SimpleAuthProvider {
|
|
12
|
+
protected app: ApplicationService;
|
|
13
|
+
constructor(app: ApplicationService);
|
|
14
|
+
register(): void;
|
|
15
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SimpleAuthMiddleware
|
|
3
|
+
} from "../chunk-QSRPDOI7.js";
|
|
4
|
+
import "../chunk-UBUHVXKH.js";
|
|
5
|
+
import "../chunk-EUXUH3YW.js";
|
|
6
|
+
|
|
7
|
+
// providers/simple_auth_provider.ts
|
|
8
|
+
import { configProvider } from "@adonisjs/core";
|
|
9
|
+
import { RuntimeException } from "@adonisjs/core/exceptions";
|
|
10
|
+
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
|
+
}
|
|
26
|
+
};
|
|
27
|
+
export {
|
|
28
|
+
SimpleAuthProvider as default
|
|
29
|
+
};
|
|
30
|
+
//# sourceMappingURL=simple_auth_provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../providers/simple_auth_provider.ts"],"sourcesContent":["import { configProvider } from '@adonisjs/core';\nimport { RuntimeException } from '@adonisjs/core/exceptions';\nimport { type ApplicationService } from '@adonisjs/core/types';\nimport SimpleAuthMiddleware from '../src/middleware/simple_auth_middleware.js';\nimport { type SimpleAuth } from '../src/simple_auth.js';\nimport { type ResolvedSimpleAuthConfig, type SimpleAuthConfig } from '../src/types.js';\n\n/**\n * HttpContext augmentations\n */\ndeclare module '@adonisjs/core/http' {\n export interface HttpContext {\n simpleAuth: SimpleAuth;\n }\n}\n\nexport default class SimpleAuthProvider {\n public constructor(protected app: ApplicationService) {}\n\n public register(): void {\n this.app.container.singleton(SimpleAuthMiddleware, async () => {\n const simpleAuthConfigProvider = this.app.config.get<SimpleAuthConfig>('simpleauth');\n const config = await configProvider.resolve<ResolvedSimpleAuthConfig>(this.app, simpleAuthConfigProvider);\n if (!config) {\n throw new RuntimeException(\n 'Invalid \"config/simpleauth.ts\" file. Make sure you are using the \"defineConfig\" method',\n );\n }\n\n return new SimpleAuthMiddleware(config);\n });\n }\n}\n"],"mappings":";;;;;;;AAAA,SAAS,sBAAsB;AAC/B,SAAS,wBAAwB;AAejC,IAAqB,qBAArB,MAAwC;AAAA,EAC/B,YAAsB,KAAyB;AAAzB;AAAA,EAA0B;AAAA,EAEhD,WAAiB;AACtB,SAAK,IAAI,UAAU,UAAU,sBAAsB,YAAY;AAC7D,YAAM,2BAA2B,KAAK,IAAI,OAAO,IAAsB,YAAY;AACnF,YAAM,SAAS,MAAM,eAAe,QAAkC,KAAK,KAAK,wBAAwB;AACxG,UAAI,CAAC,QAAQ;AACX,cAAM,IAAI;AAAA,UACR;AAAA,QACF;AAAA,MACF;AAEA,aAAO,IAAI,qBAAqB,MAAM;AAAA,IACxC,CAAC;AAAA,EACH;AACF;","names":[]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* We use CRC32 just to add a recognizable checksum to tokens. This helps
|
|
3
|
+
* secret scanning tools like https://docs.github.com/en/github/administering-a-repository/about-secret-scanning easily detect tokens generated by a given program.
|
|
4
|
+
*
|
|
5
|
+
* You can learn more about appending checksum to a hash here in this Github
|
|
6
|
+
* article. https://github.blog/2021-04-05-behind-githubs-new-authentication-token-formats/
|
|
7
|
+
*
|
|
8
|
+
* Code taken from:
|
|
9
|
+
* https://github.com/adonisjs/auth/blob/9.x/modules/access_tokens_guard/crc32.ts
|
|
10
|
+
*/
|
|
11
|
+
export declare class CRC32 {
|
|
12
|
+
/**
|
|
13
|
+
* Lookup table calculated for 0xEDB88320 divisor
|
|
14
|
+
*/
|
|
15
|
+
protected readonly lookupTable: number[];
|
|
16
|
+
protected readonly initialCRC = 4294967295;
|
|
17
|
+
protected calculateBytes(bytes: Uint8Array, accumulator?: number): number;
|
|
18
|
+
protected crcToUint(crc: number): number;
|
|
19
|
+
protected strToBytes(input: string): Uint8Array;
|
|
20
|
+
protected toUint32(num: number): number;
|
|
21
|
+
calculate(input: string): number;
|
|
22
|
+
forString(input: string): number;
|
|
23
|
+
forBytes(bytes: Uint8Array, accumulator?: number): number;
|
|
24
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type HttpContext } from '@adonisjs/core/http';
|
|
2
|
+
import { type NextFn } from '@adonisjs/core/types/http';
|
|
3
|
+
import { type ResolvedSimpleAuthConfig } from '../types.js';
|
|
4
|
+
export default class SimpleAuthMiddleware {
|
|
5
|
+
protected config: ResolvedSimpleAuthConfig;
|
|
6
|
+
constructor(config: ResolvedSimpleAuthConfig);
|
|
7
|
+
handle(ctx: HttpContext, next: NextFn): Promise<any>;
|
|
8
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type ApplicationService } from '@adonisjs/core/types';
|
|
2
|
+
import { type PluginFn } from '@japa/runner/types';
|
|
3
|
+
declare const API_KEY_ENABLED: unique symbol;
|
|
4
|
+
declare module '@japa/api-client' {
|
|
5
|
+
interface ApiRequest {
|
|
6
|
+
[API_KEY_ENABLED]: boolean;
|
|
7
|
+
withApiKey(): this;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export declare const simpleAuthApiClient: (app: ApplicationService) => PluginFn;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import "../../../chunk-EUXUH3YW.js";
|
|
2
|
+
|
|
3
|
+
// src/plugins/japa/api_client.ts
|
|
4
|
+
import { configProvider } from "@adonisjs/core";
|
|
5
|
+
import { RuntimeException } from "@adonisjs/core/exceptions";
|
|
6
|
+
import { ApiClient, ApiRequest } from "@japa/api-client";
|
|
7
|
+
var API_KEY_ENABLED = Symbol.for("API_KEY_ENABLED");
|
|
8
|
+
var simpleAuthApiClient = (app) => {
|
|
9
|
+
const pluginFn = async function() {
|
|
10
|
+
const simpleAuthConfigProvider = app.config.get("simpleauth");
|
|
11
|
+
const config = await configProvider.resolve(app, simpleAuthConfigProvider);
|
|
12
|
+
if (!config) {
|
|
13
|
+
throw new RuntimeException(
|
|
14
|
+
'Invalid "config/simpleauth.ts" file. Make sure you are using the "defineConfig" method'
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
ApiRequest.macro("withApiKey", function() {
|
|
18
|
+
this[API_KEY_ENABLED] = true;
|
|
19
|
+
return this;
|
|
20
|
+
});
|
|
21
|
+
ApiClient.setup(async (request) => {
|
|
22
|
+
const isApiKeyEnabled = request[API_KEY_ENABLED];
|
|
23
|
+
if (!isApiKeyEnabled) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
request.header(config.apiKeyHeader, config.apiKeyValue.release());
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
return pluginFn;
|
|
30
|
+
};
|
|
31
|
+
export {
|
|
32
|
+
simpleAuthApiClient
|
|
33
|
+
};
|
|
34
|
+
//# sourceMappingURL=api_client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/plugins/japa/api_client.ts"],"sourcesContent":["import { configProvider } from '@adonisjs/core';\nimport { RuntimeException } from '@adonisjs/core/exceptions';\nimport { type ApplicationService } from '@adonisjs/core/types';\nimport { ApiClient, ApiRequest } from '@japa/api-client';\nimport { type PluginFn } from '@japa/runner/types';\nimport { type ResolvedSimpleAuthConfig, type SimpleAuthConfig } from '../../types.js';\n\nconst API_KEY_ENABLED = Symbol.for('API_KEY_ENABLED');\n\ndeclare module '@japa/api-client' {\n export interface ApiRequest {\n [API_KEY_ENABLED]: boolean;\n withApiKey(): this;\n }\n}\n\nexport const simpleAuthApiClient = (app: ApplicationService): PluginFn => {\n const pluginFn: PluginFn = async function () {\n const simpleAuthConfigProvider = app.config.get<SimpleAuthConfig>('simpleauth');\n const config = await configProvider.resolve<ResolvedSimpleAuthConfig>(app, simpleAuthConfigProvider);\n if (!config) {\n throw new RuntimeException(\n 'Invalid \"config/simpleauth.ts\" file. Make sure you are using the \"defineConfig\" method',\n );\n }\n\n ApiRequest.macro('withApiKey', function (this: ApiRequest) {\n this[API_KEY_ENABLED] = true;\n\n return this;\n });\n\n ApiClient.setup(async (request) => {\n const isApiKeyEnabled = request[API_KEY_ENABLED];\n if (!isApiKeyEnabled) {\n return;\n }\n\n request.header(config.apiKeyHeader, config.apiKeyValue.release());\n });\n };\n\n return pluginFn;\n};\n"],"mappings":";;;AAAA,SAAS,sBAAsB;AAC/B,SAAS,wBAAwB;AAEjC,SAAS,WAAW,kBAAkB;AAItC,IAAM,kBAAkB,OAAO,IAAI,iBAAiB;AAS7C,IAAM,sBAAsB,CAAC,QAAsC;AACxE,QAAM,WAAqB,iBAAkB;AAC3C,UAAM,2BAA2B,IAAI,OAAO,IAAsB,YAAY;AAC9E,UAAM,SAAS,MAAM,eAAe,QAAkC,KAAK,wBAAwB;AACnG,QAAI,CAAC,QAAQ;AACX,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAEA,eAAW,MAAM,cAAc,WAA4B;AACzD,WAAK,eAAe,IAAI;AAExB,aAAO;AAAA,IACT,CAAC;AAED,cAAU,MAAM,OAAO,YAAY;AACjC,YAAM,kBAAkB,QAAQ,eAAe;AAC/C,UAAI,CAAC,iBAAiB;AACpB;AAAA,MACF;AAEA,cAAQ,OAAO,OAAO,cAAc,OAAO,YAAY,QAAQ,CAAC;AAAA,IAClE,CAAC;AAAA,EACH;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,11 @@
|
|
|
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;
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@eienjs/adonisjs-simple-auth",
|
|
3
|
+
"description": "Single authentication api key that allows authenticate",
|
|
4
|
+
"version": "1.0.0-0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"module": "build/index.js",
|
|
7
|
+
"types": "build/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"build",
|
|
10
|
+
"!build/bin",
|
|
11
|
+
"!build/tests"
|
|
12
|
+
],
|
|
13
|
+
"exports": {
|
|
14
|
+
".": "./build/index.js",
|
|
15
|
+
"./types": "./build/src/types.js",
|
|
16
|
+
"./commands": "./build/commands/index.js",
|
|
17
|
+
"./commands/*": "./build/commands/*.js",
|
|
18
|
+
"./simple_auth_provider": "./build/providers/simple_auth_provider.js",
|
|
19
|
+
"./plugins/api_client": "./build/src/plugins/japa/api_client.js",
|
|
20
|
+
"./simple_auth_middleware": "./build/src/middleware/simple_auth_middleware.js"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@adonisjs/assembler": "^7.8.2",
|
|
24
|
+
"@adonisjs/core": "^6.17.2",
|
|
25
|
+
"@commitlint/cli": "^19.8.1",
|
|
26
|
+
"@commitlint/config-conventional": "^19.8.1",
|
|
27
|
+
"@japa/api-client": "^3.1.0",
|
|
28
|
+
"@japa/assert": "^4.0.1",
|
|
29
|
+
"@japa/runner": "^4.2.0",
|
|
30
|
+
"@nodecfdi/eslint-config": "^3.2.1",
|
|
31
|
+
"@nodecfdi/prettier-config": "^1.4.1",
|
|
32
|
+
"@nodecfdi/tsconfig": "^1.7.4",
|
|
33
|
+
"@swc/core": "^1.11.24",
|
|
34
|
+
"@types/node": "^22.15.16",
|
|
35
|
+
"auto-changelog": "^2.5.0",
|
|
36
|
+
"c8": "^10.1.3",
|
|
37
|
+
"del-cli": "^6.0.0",
|
|
38
|
+
"eslint": "^9.26.0",
|
|
39
|
+
"husky": "^9.1.7",
|
|
40
|
+
"is-in-ci": "^1.0.0",
|
|
41
|
+
"native-copyfiles": "^0.3.2",
|
|
42
|
+
"np": "^10.2.0",
|
|
43
|
+
"prettier": "^3.5.3",
|
|
44
|
+
"ts-node-maintained": "^10.9.5",
|
|
45
|
+
"tsup": "^8.4.0",
|
|
46
|
+
"typescript": "^5.8.3"
|
|
47
|
+
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"@adonisjs/core": "^6.17.2",
|
|
50
|
+
"@japa/api-client": "^3.1.0"
|
|
51
|
+
},
|
|
52
|
+
"peerDependenciesMeta": {
|
|
53
|
+
"@japa/api-client": {
|
|
54
|
+
"optional": true
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
"author": "Fernando Isidro <luffynando@gmail.com>",
|
|
58
|
+
"license": "MIT",
|
|
59
|
+
"homepage": "https://github.com/eienjs/adonisjs-simple-auth",
|
|
60
|
+
"repository": {
|
|
61
|
+
"type": "git",
|
|
62
|
+
"url": "git+https://github.com/eienjs/adonisjs-simple-auth.git"
|
|
63
|
+
},
|
|
64
|
+
"bugs": {
|
|
65
|
+
"url": "https://github.com/eienjs/adonisjs-simple-auth/issues"
|
|
66
|
+
},
|
|
67
|
+
"keywords": [
|
|
68
|
+
"adonisjs",
|
|
69
|
+
"api",
|
|
70
|
+
"auth",
|
|
71
|
+
"key",
|
|
72
|
+
"headers"
|
|
73
|
+
],
|
|
74
|
+
"engines": {
|
|
75
|
+
"node": ">=20.6.0"
|
|
76
|
+
},
|
|
77
|
+
"c8": {
|
|
78
|
+
"reporter": [
|
|
79
|
+
"text",
|
|
80
|
+
"lcov"
|
|
81
|
+
],
|
|
82
|
+
"exclude": [
|
|
83
|
+
"bin/**",
|
|
84
|
+
"tests/**"
|
|
85
|
+
]
|
|
86
|
+
},
|
|
87
|
+
"commitlint": {
|
|
88
|
+
"extends": [
|
|
89
|
+
"@commitlint/config-conventional"
|
|
90
|
+
]
|
|
91
|
+
},
|
|
92
|
+
"prettier": "@nodecfdi/prettier-config/pkg",
|
|
93
|
+
"publishConfig": {
|
|
94
|
+
"access": "public",
|
|
95
|
+
"tag": "latest"
|
|
96
|
+
},
|
|
97
|
+
"auto-changelog": {
|
|
98
|
+
"template": "keepachangelog",
|
|
99
|
+
"hideCredit": true
|
|
100
|
+
},
|
|
101
|
+
"np": {
|
|
102
|
+
"message": "chore(release): :tada: %s",
|
|
103
|
+
"tag": "latest",
|
|
104
|
+
"branch": "main",
|
|
105
|
+
"testScript": "test"
|
|
106
|
+
},
|
|
107
|
+
"tsup": {
|
|
108
|
+
"entry": [
|
|
109
|
+
"./index.ts",
|
|
110
|
+
"./src/types.ts",
|
|
111
|
+
"./src/middleware/simple_auth_middleware.ts",
|
|
112
|
+
"./providers/simple_auth_provider.ts",
|
|
113
|
+
"./src/plugins/japa/api_client.ts",
|
|
114
|
+
"./commands/generate_api_key.ts"
|
|
115
|
+
],
|
|
116
|
+
"outDir": "./build",
|
|
117
|
+
"clean": true,
|
|
118
|
+
"format": "esm",
|
|
119
|
+
"dts": false,
|
|
120
|
+
"sourcemap": true,
|
|
121
|
+
"target": "esnext"
|
|
122
|
+
},
|
|
123
|
+
"scripts": {
|
|
124
|
+
"clean": "del-cli build",
|
|
125
|
+
"copy:templates": "copyfiles --up 1 \"stubs/**/*.stub\" build",
|
|
126
|
+
"changelog": "auto-changelog -p && git add CHANGELOG.md",
|
|
127
|
+
"typecheck": "tsc --noEmit",
|
|
128
|
+
"lint": "eslint . --fix",
|
|
129
|
+
"lint:check": "eslint .",
|
|
130
|
+
"format": "prettier --write .",
|
|
131
|
+
"format:check": "prettier --check .",
|
|
132
|
+
"test": "node --import ts-node-maintained/register/esm --enable-source-maps bin/test.ts",
|
|
133
|
+
"test:coverage": "c8 pnpm run test",
|
|
134
|
+
"tool:code": "pnpm run lint:check && pnpm run format:check && pnpm run typecheck",
|
|
135
|
+
"tool:build": "pnpm run tool:code && pnpm run test",
|
|
136
|
+
"prebuild": "pnpm run tool:code && pnpm run clean",
|
|
137
|
+
"build": "tsup-node --metafile && tsc --emitDeclarationOnly --declaration",
|
|
138
|
+
"postbuild": "pnpm run copy:templates && pnpm run index:commands",
|
|
139
|
+
"release": "np",
|
|
140
|
+
"version": "pnpm run build && pnpm run changelog",
|
|
141
|
+
"index:commands": "adonis-kit index build/commands"
|
|
142
|
+
}
|
|
143
|
+
}
|