@antelopejs/auth-jwt 1.0.4 → 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.
- package/README.md +37 -29
- package/dist/implementations/auth/index.d.ts +3 -6
- package/dist/implementations/auth/index.js.map +1 -1
- package/dist/jwt.d.ts +3 -3
- package/dist/jwt.js +35 -4
- package/dist/jwt.js.map +1 -1
- package/dist/test/payload.test.d.ts +1 -0
- package/dist/test/payload.test.js +57 -0
- package/dist/test/payload.test.js.map +1 -0
- package/package.json +21 -9
package/README.md
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@antelopejs/auth-jwt`
|
|
2
2
|
|
|
3
3
|
<div align="center">
|
|
4
|
-
<a href="https://www.npmjs.com/package/@antelopejs/auth-jwt"><img alt="npm" src="https://img.shields.io/npm/v/@antelopejs/auth-jwt?style=for-the-badge&labelColor=000000&color=000000"></a>
|
|
5
|
-
<a href="./LICENSE"><img alt="License" src="https://img.shields.io/npm/l/@antelopejs/auth-jwt?style=for-the-badge&labelColor=000000&color=000000"></a>
|
|
6
|
-
<a href="https://discord.gg/
|
|
7
|
-
<a href="https://interfaces.antelopejs.com/docs/auth/introduction"><img src="https://img.shields.io/badge/Docs-18181B?style=for-the-badge&color=000000" alt="Documentation"></a>
|
|
4
|
+
<a href="https://www.npmjs.com/package/@antelopejs/auth-jwt"><img data-illustration alt="npm" src="https://img.shields.io/npm/v/@antelopejs/auth-jwt?style=for-the-badge&labelColor=000000&color=000000"></a>
|
|
5
|
+
<a href="./LICENSE"><img data-illustration alt="License" src="https://img.shields.io/npm/l/@antelopejs/auth-jwt?style=for-the-badge&labelColor=000000&color=000000"></a>
|
|
6
|
+
<a href="https://discord.gg/g4pFt6eXqu"><img data-illustration src="https://img.shields.io/badge/Discord-18181B?logo=discord&style=for-the-badge&color=000000" alt="Discord"></a>
|
|
7
|
+
<a href="https://interfaces.antelopejs.com/docs/auth/introduction"><img data-illustration src="https://img.shields.io/badge/Docs-18181B?style=for-the-badge&color=000000" alt="Documentation"></a>
|
|
8
8
|
</div>
|
|
9
9
|
|
|
10
|
-
The AntelopeJS authentication module
|
|
10
|
+
The AntelopeJS authentication module. It implements `@antelopejs/interface-auth` and signs and verifies its tokens as JSON Web Tokens with [`jsonwebtoken`](https://github.com/auth0/node-jsonwebtoken).
|
|
11
|
+
|
|
12
|
+
Part of [AntelopeJS](https://github.com/AntelopeJS/antelopejs). Used by [AntelopeJS DMS](https://github.com/AntelopeJS/dms), the open-source back office for your PostgreSQL, MongoDB or RethinkDB database.
|
|
11
13
|
|
|
12
14
|
## Interfaces
|
|
13
15
|
|
|
@@ -15,7 +17,7 @@ The AntelopeJS authentication module: it signs and verifies the auth interface's
|
|
|
15
17
|
| ---------------------------------------------------------------------------- | ---------------------------------------------------------------- |
|
|
16
18
|
| [`@antelopejs/interface-auth`](https://github.com/AntelopeJS/interface-auth) | [Auth](https://interfaces.antelopejs.com/docs/auth/introduction) |
|
|
17
19
|
|
|
18
|
-
The `@Authentication` decorator, custom auth decorators, token sources and cookies
|
|
20
|
+
The interface pages document the `@Authentication` decorator, custom auth decorators, token sources, and cookies. The module also depends on [`@antelopejs/interface-api`](https://interfaces.antelopejs.com/docs/api/introduction), which [`@antelopejs/api`](https://github.com/AntelopeJS/api) implements.
|
|
19
21
|
|
|
20
22
|
## Install
|
|
21
23
|
|
|
@@ -40,7 +42,7 @@ export default defineConfig({
|
|
|
40
42
|
source: {
|
|
41
43
|
type: "package",
|
|
42
44
|
package: "@antelopejs/auth-jwt",
|
|
43
|
-
version: "^
|
|
45
|
+
version: "^2.0.0",
|
|
44
46
|
},
|
|
45
47
|
config: {
|
|
46
48
|
secret: "change-me",
|
|
@@ -55,9 +57,11 @@ export default defineConfig({
|
|
|
55
57
|
});
|
|
56
58
|
```
|
|
57
59
|
|
|
58
|
-
|
|
60
|
+
When the `JWT_SECRET` environment variable is set, `envOverrides` writes its value into `secret`.
|
|
61
|
+
|
|
62
|
+
To sign with a key pair instead, give the paths of the two key files. The module reads both files when it constructs.
|
|
59
63
|
|
|
60
|
-
```typescript
|
|
64
|
+
```typescript fragment
|
|
61
65
|
config: {
|
|
62
66
|
signKey: "./keys/private.pem",
|
|
63
67
|
verifyKey: "./keys/public.pem",
|
|
@@ -66,25 +70,25 @@ config: {
|
|
|
66
70
|
},
|
|
67
71
|
```
|
|
68
72
|
|
|
69
|
-
| Key | Description
|
|
70
|
-
| --------------- |
|
|
71
|
-
| `secret` | Shared secret that signs and verifies tokens. Takes precedence over `signKey` and `verifyKey`.
|
|
72
|
-
| `signKey` | Path of the private key file.
|
|
73
|
-
| `verifyKey` | Path of the public key file.
|
|
74
|
-
| `signOptions` | Default [`jsonwebtoken` sign options](https://github.com/auth0/node-jsonwebtoken#jwtsignpayload-secretorprivatekey-options-callback). `
|
|
75
|
-
| `verifyOptions` | Default [`jsonwebtoken` verify options](https://github.com/auth0/node-jsonwebtoken#jwtverifytoken-secretorpublickey-options-callback).
|
|
73
|
+
| Key | Description |
|
|
74
|
+
| --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
75
|
+
| `secret` | Shared secret that signs and verifies tokens. Takes precedence over `signKey` and `verifyKey`. |
|
|
76
|
+
| `signKey` | Path of the private key file. The module uses it with `verifyKey` when `secret` is not set. |
|
|
77
|
+
| `verifyKey` | Path of the public key file. |
|
|
78
|
+
| `signOptions` | Default [`jsonwebtoken` sign options](https://github.com/auth0/node-jsonwebtoken#jwtsignpayload-secretorprivatekey-options-callback). The `expiresIn` and `notBefore` that `SignRaw` or `SignServerResponse` receive override them. |
|
|
79
|
+
| `verifyOptions` | Default [`jsonwebtoken` verify options](https://github.com/auth0/node-jsonwebtoken#jwtverifytoken-secretorpublickey-options-callback). Options passed to `ValidateRaw` override them. |
|
|
76
80
|
|
|
77
|
-
The module refuses to start without `secret` or both `signKey` and `verifyKey`. `ajs project modules add` writes `secret: "your-secret-key"
|
|
81
|
+
The module refuses to start without `secret` or both `signKey` and `verifyKey`, and throws `Invalid JWT config`. `ajs project modules add` writes `secret: "your-secret-key"`. Replace it before you deploy.
|
|
78
82
|
|
|
79
83
|
## Quick start
|
|
80
84
|
|
|
81
|
-
In the module that serves the routes, add both interfaces
|
|
85
|
+
In the module that serves the routes, add both interfaces.
|
|
82
86
|
|
|
83
87
|
```bash
|
|
84
88
|
pnpm add @antelopejs/interface-api @antelopejs/interface-auth
|
|
85
89
|
```
|
|
86
90
|
|
|
87
|
-
Issue a token as a cookie, then read it back on a protected route
|
|
91
|
+
Issue a token as a cookie, then read it back on a protected route.
|
|
88
92
|
|
|
89
93
|
```typescript [src/index.ts]
|
|
90
94
|
import {
|
|
@@ -119,22 +123,26 @@ export class SessionController extends Controller("/session") {
|
|
|
119
123
|
}
|
|
120
124
|
```
|
|
121
125
|
|
|
122
|
-
`GET /session/me` reads the token from the `x-antelopejs-auth` header or the `ANTELOPEJS_AUTH` cookie. It answers `401` when the token is missing
|
|
126
|
+
`GET /session/me` reads the token from the `x-antelopejs-auth` header, or from the `ANTELOPEJS_AUTH` cookie when the header is absent. It answers `401` when the token is missing. It answers `403` when the token is invalid or expired, or when its payload is neither a string nor a JSON object, such as an array or a number.
|
|
123
127
|
|
|
124
128
|
## Documentation
|
|
125
129
|
|
|
126
|
-
- [Auth](https://interfaces.antelopejs.com/docs/auth/introduction)
|
|
127
|
-
- [Configuration](https://framework.antelopejs.com/docs/concepts/configuration)
|
|
130
|
+
- [Auth](https://interfaces.antelopejs.com/docs/auth/introduction) documents the `@antelopejs/interface-auth` contract that this module implements.
|
|
131
|
+
- [Configuration](https://framework.antelopejs.com/docs/concepts/configuration) documents module sources, `config` blocks, and config variables in `antelope.config.ts`.
|
|
128
132
|
|
|
129
133
|
## Compatibility
|
|
130
134
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
|
134
|
-
|
|
|
135
|
-
| `@antelopejs/interface-
|
|
135
|
+
<!-- antelopejs-docs-compat:start -->
|
|
136
|
+
|
|
137
|
+
| Package | Supported | Tested with |
|
|
138
|
+
| ---------------------------- | ---------------- | ----------- |
|
|
139
|
+
| `@antelopejs/interface-api` | `>=0.2.0 <1.0.0` | `0.2.0` |
|
|
140
|
+
| `@antelopejs/interface-auth` | `>=0.2.0 <0.3.0` | `0.2.0` |
|
|
141
|
+
| `@antelopejs/interface-core` | `>=0.4.0 <1.0.0` | `0.4.0` |
|
|
142
|
+
|
|
143
|
+
<!-- antelopejs-docs-compat:end -->
|
|
136
144
|
|
|
137
|
-
The
|
|
145
|
+
The generated table lists the interface packages and ranges this module loads, read from `package.json`.
|
|
138
146
|
|
|
139
147
|
## Contributing
|
|
140
148
|
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
import type { SignOptions } from "@antelopejs/interface-auth";
|
|
1
|
+
import type { AuthClaims, AuthPayload, SignOptions } from "@antelopejs/interface-auth";
|
|
2
2
|
import type { VerifyOptions as JWTVerifyOptions } from "jsonwebtoken";
|
|
3
|
-
import type { JWTSignPayload } from "../../jwt";
|
|
4
|
-
type AuthPayload = unknown;
|
|
5
3
|
export declare namespace internal {
|
|
6
|
-
function Verify
|
|
7
|
-
function Sign(data:
|
|
4
|
+
function Verify(data?: string, options?: JWTVerifyOptions): Promise<AuthClaims>;
|
|
5
|
+
function Sign(data: AuthPayload, options?: SignOptions): Promise<string>;
|
|
8
6
|
}
|
|
9
|
-
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/implementations/auth/index.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/implementations/auth/index.ts"],"names":[],"mappings":";;;AAUA,uCAA4C;AAE5C,SAAS,oBAAoB,CAC3B,OAAqB;IAErB,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,MAAM,cAAc,GAAmB,EAAE,CAAC;IAE1C,IAAI,OAAO,CAAC,SAAS,KAAK,SAAS,EAAE,CAAC;QACpC,cAAc,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC;IAC/C,CAAC;IAED,IAAI,OAAO,CAAC,SAAS,KAAK,SAAS,EAAE,CAAC;QACpC,cAAc,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC;IAC/C,CAAC;IAED,OAAO,cAAc,CAAC;AACxB,CAAC;AAED,IAAiB,QAAQ,CAcxB;AAdD,WAAiB,QAAQ;IACvB,SAAgB,MAAM,CACpB,IAAa,EACb,OAA0B;QAE1B,OAAO,IAAA,qBAAa,GAAE,CAAC,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IAC/C,CAAC;IALe,eAAM,SAKrB,CAAA;IAED,SAAgB,IAAI,CAClB,IAAiB,EACjB,OAAqB;QAErB,OAAO,IAAA,qBAAa,GAAE,CAAC,IAAI,CAAC,IAAI,EAAE,oBAAoB,CAAC,OAAO,CAAC,CAAC,CAAC;IACnE,CAAC;IALe,aAAI,OAKnB,CAAA;AACH,CAAC,EAdgB,QAAQ,wBAAR,QAAQ,QAcxB"}
|
package/dist/jwt.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { AuthClaims, AuthPayload } from "@antelopejs/interface-auth";
|
|
1
2
|
import { type SignOptions, type VerifyOptions } from "jsonwebtoken";
|
|
2
3
|
export interface JWTHandlerConfig {
|
|
3
4
|
secret?: string;
|
|
@@ -6,7 +7,6 @@ export interface JWTHandlerConfig {
|
|
|
6
7
|
signOptions?: SignOptions;
|
|
7
8
|
verifyOptions?: VerifyOptions;
|
|
8
9
|
}
|
|
9
|
-
export type JWTSignPayload = string | Buffer | object;
|
|
10
10
|
export declare class JWTHandler {
|
|
11
11
|
private loadFromDisk;
|
|
12
12
|
private signKey;
|
|
@@ -15,6 +15,6 @@ export declare class JWTHandler {
|
|
|
15
15
|
private verifyOptions;
|
|
16
16
|
constructor(config: JWTHandlerConfig);
|
|
17
17
|
loadKeys(): Promise<void>;
|
|
18
|
-
verify
|
|
19
|
-
sign(data:
|
|
18
|
+
verify(data: string | undefined, options?: VerifyOptions): Promise<AuthClaims>;
|
|
19
|
+
sign(data: AuthPayload, options?: SignOptions): Promise<string>;
|
|
20
20
|
}
|
package/dist/jwt.js
CHANGED
|
@@ -9,13 +9,40 @@ const UnauthorizedStatusCode = 401;
|
|
|
9
9
|
const UnauthorizedMessage = "Unauthorized";
|
|
10
10
|
const ForbiddenMessagePrefix = "Forbidden";
|
|
11
11
|
const JWTSignFailureMessage = "Unable to sign JWT";
|
|
12
|
+
function isJsonValue(value) {
|
|
13
|
+
if (value === null ||
|
|
14
|
+
typeof value === "string" ||
|
|
15
|
+
typeof value === "number" ||
|
|
16
|
+
typeof value === "boolean") {
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
if (Array.isArray(value)) {
|
|
20
|
+
return value.every(isJsonValue);
|
|
21
|
+
}
|
|
22
|
+
return isJsonObject(value);
|
|
23
|
+
}
|
|
24
|
+
function isJsonObject(value) {
|
|
25
|
+
return (typeof value === "object" &&
|
|
26
|
+
value !== null &&
|
|
27
|
+
!Array.isArray(value) &&
|
|
28
|
+
Object.values(value).every(isJsonValue));
|
|
29
|
+
}
|
|
30
|
+
function isAuthClaims(payload) {
|
|
31
|
+
return typeof payload === "string" || isJsonObject(payload);
|
|
32
|
+
}
|
|
33
|
+
function hasSecret(config) {
|
|
34
|
+
return typeof config.secret === "string";
|
|
35
|
+
}
|
|
36
|
+
function hasSignAndVerifyKeys(config) {
|
|
37
|
+
return (typeof config.signKey === "string" && typeof config.verifyKey === "string");
|
|
38
|
+
}
|
|
12
39
|
function resolveJWTHandlerConfig(config) {
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
if (!
|
|
40
|
+
const usesSecret = hasSecret(config);
|
|
41
|
+
const usesSignAndVerifyKeys = hasSignAndVerifyKeys(config);
|
|
42
|
+
if (!usesSecret && !usesSignAndVerifyKeys) {
|
|
16
43
|
throw new Error("Invalid JWT config");
|
|
17
44
|
}
|
|
18
|
-
if (
|
|
45
|
+
if (usesSecret) {
|
|
19
46
|
return {
|
|
20
47
|
loadFromDisk: false,
|
|
21
48
|
signKey: config.secret,
|
|
@@ -79,6 +106,10 @@ class JWTHandler {
|
|
|
79
106
|
reject(new interface_api_1.HTTPResult(ForbiddenStatusCode, `${ForbiddenMessagePrefix} (${error.message})`));
|
|
80
107
|
return;
|
|
81
108
|
}
|
|
109
|
+
if (!isAuthClaims(payload)) {
|
|
110
|
+
reject(new interface_api_1.HTTPResult(ForbiddenStatusCode, `${ForbiddenMessagePrefix} (the payload is not a JSON object)`));
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
82
113
|
resolve(payload);
|
|
83
114
|
});
|
|
84
115
|
});
|
package/dist/jwt.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jwt.js","sourceRoot":"","sources":["../src/jwt.ts"],"names":[],"mappings":";;;AAAA,+CAA4C;AAC5C,6DAAuD;
|
|
1
|
+
{"version":3,"file":"jwt.js","sourceRoot":"","sources":["../src/jwt.ts"],"names":[],"mappings":";;;AAAA,+CAA4C;AAC5C,6DAAuD;AAOvD,+CAKsB;AAEtB,MAAM,mBAAmB,GAAG,GAAG,CAAC;AAChC,MAAM,sBAAsB,GAAG,GAAG,CAAC;AACnC,MAAM,mBAAmB,GAAG,cAAc,CAAC;AAC3C,MAAM,sBAAsB,GAAG,WAAW,CAAC;AAC3C,MAAM,qBAAqB,GAAG,oBAAoB,CAAC;AAUnD,SAAS,WAAW,CAAC,KAAc;IACjC,IACE,KAAK,KAAK,IAAI;QACd,OAAO,KAAK,KAAK,QAAQ;QACzB,OAAO,KAAK,KAAK,QAAQ;QACzB,OAAO,KAAK,KAAK,SAAS,EAC1B,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IACD,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QACzB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ACJ,IAAI,CAAC,OAAO,EACZ,gBAAgB,CAAC,IAAI,CAAC,WAAW,EAAE,OAAO,CAAC,EAC3C,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE;gBACf,IAAI,KAAK,IAAI,CAAC,KAAK,EAAE,CAAC;oBACpB,MAAM,CAAC,KAAK,IAAI,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC,CAAC;oBAClD,OAAO;gBACT,CAAC;gBAED,OAAO,CAAC,KAAK,CAAC,CAAC;YACjB,CAAC,CACF,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAxFD,gCAwFC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const node_assert_1 = __importDefault(require("node:assert"));
|
|
7
|
+
const node_crypto_1 = require("node:crypto");
|
|
8
|
+
const interface_api_1 = require("@antelopejs/interface-api");
|
|
9
|
+
const jwt_1 = require("../jwt");
|
|
10
|
+
const SECRET = "test-secret-key-for-jwt";
|
|
11
|
+
const FORBIDDEN_STATUS = 403;
|
|
12
|
+
function base64Url(value) {
|
|
13
|
+
return Buffer.from(value).toString("base64url");
|
|
14
|
+
}
|
|
15
|
+
function signRaw(payload) {
|
|
16
|
+
const header = base64Url(JSON.stringify({ alg: "HS256", typ: "JWT" }));
|
|
17
|
+
const signingInput = `${header}.${base64Url(payload)}`;
|
|
18
|
+
const signature = (0, node_crypto_1.createHmac)("sha256", SECRET)
|
|
19
|
+
.update(signingInput)
|
|
20
|
+
.digest("base64url");
|
|
21
|
+
return `${signingInput}.${signature}`;
|
|
22
|
+
}
|
|
23
|
+
function assertForbidden(message) {
|
|
24
|
+
return (error) => {
|
|
25
|
+
node_assert_1.default.ok(error instanceof interface_api_1.HTTPResult);
|
|
26
|
+
node_assert_1.default.strictEqual(error.getStatus(), FORBIDDEN_STATUS);
|
|
27
|
+
if (message !== undefined) {
|
|
28
|
+
node_assert_1.default.strictEqual(error.getBody(), message);
|
|
29
|
+
}
|
|
30
|
+
return true;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
describe("JWTHandler.verify payloads", () => {
|
|
34
|
+
const handler = new jwt_1.JWTHandler({ secret: SECRET });
|
|
35
|
+
it("resolves the claims object of a token it signed", async () => {
|
|
36
|
+
const token = await handler.sign({ userId: 123, role: "admin" });
|
|
37
|
+
const claims = await handler.verify(token);
|
|
38
|
+
node_assert_1.default.ok(claims instanceof Object);
|
|
39
|
+
node_assert_1.default.strictEqual(claims["userId"], 123);
|
|
40
|
+
node_assert_1.default.strictEqual(claims["role"], "admin");
|
|
41
|
+
});
|
|
42
|
+
it("resolves a string payload as the string", async () => {
|
|
43
|
+
const token = await handler.sign("plain-data");
|
|
44
|
+
node_assert_1.default.strictEqual(await handler.verify(token), "plain-data");
|
|
45
|
+
});
|
|
46
|
+
it("answers 403 for a validly signed JSON array payload", async () => {
|
|
47
|
+
const token = signRaw(JSON.stringify([1, 2]));
|
|
48
|
+
await node_assert_1.default.rejects(handler.verify(token), assertForbidden("Forbidden (the payload is not a JSON object)"));
|
|
49
|
+
});
|
|
50
|
+
it("answers 403 for a token signed with another secret", async () => {
|
|
51
|
+
const token = await new jwt_1.JWTHandler({ secret: "other-secret" }).sign({
|
|
52
|
+
userId: 1,
|
|
53
|
+
});
|
|
54
|
+
await node_assert_1.default.rejects(handler.verify(token), assertForbidden());
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
//# sourceMappingURL=payload.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"payload.test.js","sourceRoot":"","sources":["../../src/test/payload.test.ts"],"names":[],"mappings":";;;;;AAAA,8DAAiC;AACjC,6CAAyC;AACzC,6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CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,eAAe,EAAE,CAAC,CAAC;IACjE,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@antelopejs/auth-jwt",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "JWT authentication module for AntelopeJS, implementing the Auth interface with jsonwebtoken",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"antelopejs",
|
|
@@ -32,23 +32,31 @@
|
|
|
32
32
|
"dev": "ajs project dev -w -p playground",
|
|
33
33
|
"format": "oxfmt .",
|
|
34
34
|
"lint:fix": "oxlint --fix",
|
|
35
|
-
"lint": "oxlint",
|
|
35
|
+
"lint": "oxlint && antelopejs-check-interface-ranges",
|
|
36
36
|
"prepack": "pnpm run build",
|
|
37
37
|
"release": "pnpm run lint && pnpm run test && pnpm run prepack && release-it",
|
|
38
38
|
"test": "pnpm run build && ajs module test",
|
|
39
39
|
"format:check": "oxfmt --check .",
|
|
40
|
-
"knip": "knip"
|
|
40
|
+
"knip": "knip",
|
|
41
|
+
"prepare": "if git rev-parse --is-inside-work-tree >/dev/null 2>&1 && command -v lefthook >/dev/null 2>&1; then lefthook install; fi",
|
|
42
|
+
"checks": "antelopejs-checks",
|
|
43
|
+
"size": "antelopejs-check-size",
|
|
44
|
+
"sync": "antelopejs-sync",
|
|
45
|
+
"sync:check": "antelopejs-sync --check",
|
|
46
|
+
"typecheck": "tsc --noEmit",
|
|
47
|
+
"suppressions": "antelopejs-suppressions",
|
|
48
|
+
"suppressions:update": "antelopejs-suppressions --update"
|
|
41
49
|
},
|
|
42
50
|
"dependencies": {
|
|
43
|
-
"@antelopejs/interface-api": ">=0.0
|
|
44
|
-
"@antelopejs/interface-auth": ">=0.0
|
|
45
|
-
"@antelopejs/interface-core": ">=0.
|
|
51
|
+
"@antelopejs/interface-api": ">=0.2.0 <1.0.0",
|
|
52
|
+
"@antelopejs/interface-auth": ">=0.2.0 <0.3.0",
|
|
53
|
+
"@antelopejs/interface-core": ">=0.4.0 <1.0.0",
|
|
46
54
|
"chai": "^4.5.0",
|
|
47
55
|
"jsonwebtoken": "^9.0.3"
|
|
48
56
|
},
|
|
49
57
|
"devDependencies": {
|
|
50
|
-
"@antelopejs/core": "^1.
|
|
51
|
-
"@antelopejs/tooling-configs": "^0.
|
|
58
|
+
"@antelopejs/core": "^1.20.0",
|
|
59
|
+
"@antelopejs/tooling-configs": "^0.2.17",
|
|
52
60
|
"@types/chai": "^4.3.20",
|
|
53
61
|
"@types/jsonwebtoken": "^9.0.10",
|
|
54
62
|
"@types/mocha": "^10.0.10",
|
|
@@ -56,6 +64,7 @@
|
|
|
56
64
|
"@types/semver": "^7.7.1",
|
|
57
65
|
"eslint-plugin-perfectionist": "^5.11.0",
|
|
58
66
|
"knip": "^6.34.0",
|
|
67
|
+
"lefthook": "^2.2.2",
|
|
59
68
|
"oxfmt": "^0.66.0",
|
|
60
69
|
"oxlint": "1.81.0",
|
|
61
70
|
"oxlint-tsgolint": "^7.0.2001",
|
|
@@ -72,6 +81,9 @@
|
|
|
72
81
|
"defaultConfig": {
|
|
73
82
|
"secret": "your-secret-key"
|
|
74
83
|
},
|
|
75
|
-
"test": "src/test/antelope.test.ts"
|
|
84
|
+
"test": "src/test/antelope.test.ts",
|
|
85
|
+
"docs": {
|
|
86
|
+
"standard": true
|
|
87
|
+
}
|
|
76
88
|
}
|
|
77
89
|
}
|