@antelopejs/auth-jwt 1.0.3 → 1.1.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 +132 -50
- package/dist/implementations/auth/index.d.ts +3 -6
- package/dist/implementations/auth/index.js.map +1 -1
- package/dist/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 +55 -30
package/README.md
CHANGED
|
@@ -1,80 +1,162 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
# @antelopejs/auth-jwt
|
|
1
|
+
# `@antelopejs/auth-jwt`
|
|
4
2
|
|
|
5
3
|
<div align="center">
|
|
6
|
-
<a href="https://www.npmjs.com/package/@antelopejs/auth-jwt"><img alt="
|
|
7
|
-
<a href="./LICENSE"><img alt="License" src="https://img.shields.io/npm/l/@antelopejs/auth-jwt
|
|
8
|
-
<a href="https://discord.gg/
|
|
9
|
-
<a href="https://antelopejs.com/
|
|
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>
|
|
10
8
|
</div>
|
|
11
9
|
|
|
12
|
-
|
|
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.
|
|
13
|
+
|
|
14
|
+
## Interfaces
|
|
15
|
+
|
|
16
|
+
| Interface | Documentation |
|
|
17
|
+
| ---------------------------------------------------------------------------- | ---------------------------------------------------------------- |
|
|
18
|
+
| [`@antelopejs/interface-auth`](https://github.com/AntelopeJS/interface-auth) | [Auth](https://interfaces.antelopejs.com/docs/auth/introduction) |
|
|
13
19
|
|
|
14
|
-
|
|
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.
|
|
21
|
+
|
|
22
|
+
## Install
|
|
15
23
|
|
|
16
24
|
```bash
|
|
17
25
|
ajs project modules add @antelopejs/auth-jwt
|
|
26
|
+
ajs project modules install
|
|
18
27
|
```
|
|
19
28
|
|
|
20
|
-
|
|
29
|
+
`ajs project modules install` adds `@antelopejs/api` when no module of the project implements the API interface yet.
|
|
21
30
|
|
|
22
|
-
|
|
31
|
+
## Configuration
|
|
23
32
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
33
|
+
Sign with a shared secret:
|
|
34
|
+
|
|
35
|
+
```typescript [antelope.config.ts]
|
|
36
|
+
import { defineConfig } from "@antelopejs/interface-core/config";
|
|
37
|
+
|
|
38
|
+
export default defineConfig({
|
|
39
|
+
name: "my-project",
|
|
40
|
+
modules: {
|
|
41
|
+
"auth-jwt": {
|
|
42
|
+
source: {
|
|
43
|
+
type: "package",
|
|
44
|
+
package: "@antelopejs/auth-jwt",
|
|
45
|
+
version: "^2.0.0",
|
|
46
|
+
},
|
|
47
|
+
config: {
|
|
48
|
+
secret: "change-me",
|
|
49
|
+
signOptions: { expiresIn: "1h" },
|
|
50
|
+
verifyOptions: { ignoreExpiration: false },
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
envOverrides: {
|
|
55
|
+
JWT_SECRET: "modules.auth-jwt.config.secret",
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
```
|
|
27
59
|
|
|
28
|
-
|
|
60
|
+
When the `JWT_SECRET` environment variable is set, `envOverrides` writes its value into `secret`.
|
|
29
61
|
|
|
30
|
-
|
|
62
|
+
To sign with a key pair instead, give the paths of the two key files. The module reads both files when it constructs.
|
|
31
63
|
|
|
32
|
-
```
|
|
33
|
-
{
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
|
|
39
|
-
"algorithm": "RS256"
|
|
40
|
-
},
|
|
41
|
-
"verifyOptions": {
|
|
42
|
-
"ignoreExpiration": false,
|
|
43
|
-
"algorithms": ["RS256"]
|
|
44
|
-
}
|
|
45
|
-
}
|
|
64
|
+
```typescript fragment
|
|
65
|
+
config: {
|
|
66
|
+
signKey: "./keys/private.pem",
|
|
67
|
+
verifyKey: "./keys/public.pem",
|
|
68
|
+
signOptions: { algorithm: "RS256", expiresIn: "1h" },
|
|
69
|
+
verifyOptions: { algorithms: ["RS256"] },
|
|
70
|
+
},
|
|
46
71
|
```
|
|
47
72
|
|
|
48
|
-
|
|
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. |
|
|
80
|
+
|
|
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.
|
|
82
|
+
|
|
83
|
+
## Quick start
|
|
49
84
|
|
|
50
|
-
|
|
51
|
-
- `signKey`: Path to a private key file (for asymmetric signing)
|
|
52
|
-
- `verifyKey`: Path to a public key file (for asymmetric verification)
|
|
53
|
-
- `signOptions`: Options for token signing (follows jsonwebtoken's SignOptions)
|
|
54
|
-
- `verifyOptions`: Options for token verification (follows jsonwebtoken's VerifyOptions)
|
|
85
|
+
In the module that serves the routes, add both interfaces.
|
|
55
86
|
|
|
56
|
-
|
|
87
|
+
```bash
|
|
88
|
+
pnpm add @antelopejs/interface-api @antelopejs/interface-auth
|
|
89
|
+
```
|
|
57
90
|
|
|
58
|
-
|
|
91
|
+
Issue a token as a cookie, then read it back on a protected route.
|
|
59
92
|
|
|
60
|
-
|
|
93
|
+
```typescript [src/index.ts]
|
|
94
|
+
import {
|
|
95
|
+
Context,
|
|
96
|
+
Controller,
|
|
97
|
+
Get,
|
|
98
|
+
Post,
|
|
99
|
+
type RequestContext,
|
|
100
|
+
} from "@antelopejs/interface-api";
|
|
101
|
+
import { Authentication, SignServerResponse } from "@antelopejs/interface-auth";
|
|
61
102
|
|
|
62
|
-
|
|
103
|
+
interface Session {
|
|
104
|
+
userId: string;
|
|
105
|
+
}
|
|
63
106
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
107
|
+
export class SessionController extends Controller("/session") {
|
|
108
|
+
@Post("/login")
|
|
109
|
+
async login(@Context() context: RequestContext) {
|
|
110
|
+
await SignServerResponse(
|
|
111
|
+
context.rawResponse,
|
|
112
|
+
{ userId: "42" },
|
|
113
|
+
{ expiresIn: "1h" },
|
|
114
|
+
{ httpOnly: true, path: "/" },
|
|
115
|
+
);
|
|
116
|
+
return { ok: true };
|
|
117
|
+
}
|
|
67
118
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
async listUsers(@Authentication() user: any) {
|
|
72
|
-
// List users
|
|
73
|
-
return { users: [] };
|
|
119
|
+
@Get("/me")
|
|
120
|
+
me(@Authentication() session: Session) {
|
|
121
|
+
return session;
|
|
74
122
|
}
|
|
75
123
|
}
|
|
76
124
|
```
|
|
77
125
|
|
|
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.
|
|
127
|
+
|
|
128
|
+
## Documentation
|
|
129
|
+
|
|
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`.
|
|
132
|
+
|
|
133
|
+
## Compatibility
|
|
134
|
+
|
|
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 -->
|
|
144
|
+
|
|
145
|
+
The generated table lists the interface packages and ranges this module loads, read from `package.json`.
|
|
146
|
+
|
|
147
|
+
## Contributing
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
pnpm install
|
|
151
|
+
pnpm dev # run the playground/ project in watch mode
|
|
152
|
+
pnpm lint
|
|
153
|
+
pnpm format:check
|
|
154
|
+
pnpm knip
|
|
155
|
+
pnpm test # build, then ajs module test
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
Read the [contribution guidelines](CONTRIBUTING.md) before opening a pull request.
|
|
159
|
+
|
|
78
160
|
## License
|
|
79
161
|
|
|
80
|
-
|
|
162
|
+
[Apache-2.0](LICENSE)
|
|
@@ -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/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQA,8BAOC;AAED,sCAMC;AAED,0BAEC;AAED,sBAAgC;AAEhC,oBAA+B;AA/B/B,+DAAgE;AAEhE,+BAA0D;AAE1D,MAAM,6BAA6B,GAAG,gCAAgC,CAAC;AAEvE,IAAI,IAA4B,CAAC;AAE1B,KAAK,UAAU,SAAS,CAAC,MAAwB;IACtD,IAAI,GAAG,IAAI,gBAAU,CAAC,MAAM,CAAC,CAAC;IAC9B,MAAM,IAAI,CAAC,QAAQ,EAAE,CAAC;IACtB,KAAK,IAAA,mCAAkB,EACrB,wDAAa,4BAA4B,GAAC,EAC1C,wDAAa,wBAAwB,GAAC,CACvC,CAAC;AACJ,CAAC;AAED,SAAgB,aAAa;IAC3B,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;IACjD,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAgB,OAAO;IACrB,IAAI,GAAG,SAAS,CAAC;AACnB,CAAC;AAED,SAAgB,KAAK,KAAU,CAAC;AAEhC,SAAgB,IAAI,KAAU,CAAC"}
|
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,OAAO,KAAK,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;IAClC,CAAC;IACD,OAAO,YAAY,CAAC,KAAK,CAAC,CAAC;AAC7B,CAAC;AAED,SAAS,YAAY,CAAC,KAAc;IAClC,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QACrB,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,WAAW,CAAC,CACxC,CAAC;A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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,6DAAuD;AACvD,gCAAoC;AAEpC,MAAM,MAAM,GAAG,yBAAyB,CAAC;AACzC,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAE7B,SAAS,SAAS,CAAC,KAAsB;IACvC,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;AAClD,CAAC;AAED,SAAS,OAAO,CAAC,OAAe;IAC9B,MAAM,MAAM,GAAG,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,GAAG,EAAE,OAAO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AC,CAAC;QAC1C,qBAAM,CAAC,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,CAAC;IAC9C,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,yCAAyC,EAAE,KAAK,IAAI,EAAE;QACvD,MAAM,KAAK,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;QAE/C,qBAAM,CAAC,WAAW,CAAC,MAAM,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,YAAY,CAAC,CAAC;IAChE,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,qDAAqD,EAAE,KAAK,IAAI,EAAE;QACnE,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;QAE9C,MAAM,qBAAM,CAAC,OAAO,CAClB,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,EACrB,eAAe,CAAC,8CAA8C,CAAC,CAChE,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,oDAAoD,EAAE,KAAK,IAAI,EAAE;QAClE,MAAM,KAAK,GAAG,MAAM,IAAI,gBAAU,CAAC,EAAE,MAAM,EAAE,cAAc,EAAE,CAAC,CAAC,IAAI,CAAC;YAClE,MAAM,EAAE,CAAC;SACV,CAAC,CAAC;QAEH,MAAM,qBAAM,CAAC,OAAO,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,64 +1,89 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@antelopejs/auth-jwt",
|
|
3
|
-
"version": "1.0
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "JWT authentication module for AntelopeJS, implementing the Auth interface with jsonwebtoken",
|
|
5
5
|
"keywords": [
|
|
6
|
-
"
|
|
7
|
-
"jwt",
|
|
6
|
+
"antelopejs",
|
|
8
7
|
"auth",
|
|
9
8
|
"authentication",
|
|
10
|
-
"authorization"
|
|
9
|
+
"authorization",
|
|
10
|
+
"jsonwebtoken",
|
|
11
|
+
"jwt"
|
|
11
12
|
],
|
|
13
|
+
"homepage": "https://interfaces.antelopejs.com/docs/auth/introduction",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/AntelopeJS/auth-jwt/issues"
|
|
16
|
+
},
|
|
17
|
+
"license": "Apache-2.0",
|
|
12
18
|
"repository": {
|
|
13
19
|
"type": "git",
|
|
14
|
-
"url": "git+https://github.com/AntelopeJS/auth.git"
|
|
20
|
+
"url": "git+https://github.com/AntelopeJS/auth-jwt.git"
|
|
15
21
|
},
|
|
16
|
-
"main": "dist/index.js",
|
|
17
22
|
"files": [
|
|
18
23
|
"dist"
|
|
19
24
|
],
|
|
25
|
+
"main": "dist/index.js",
|
|
20
26
|
"publishConfig": {
|
|
21
|
-
"access": "public"
|
|
27
|
+
"access": "public",
|
|
28
|
+
"provenance": true
|
|
22
29
|
},
|
|
23
|
-
"license": "Apache-2.0",
|
|
24
|
-
"packageManager": "pnpm@10.6.5",
|
|
25
30
|
"scripts": {
|
|
26
31
|
"build": "rimraf dist && tsc",
|
|
27
|
-
"dev": "ajs project
|
|
28
|
-
"format": "
|
|
29
|
-
"lint:fix": "
|
|
30
|
-
"lint": "
|
|
32
|
+
"dev": "ajs project dev -w -p playground",
|
|
33
|
+
"format": "oxfmt .",
|
|
34
|
+
"lint:fix": "oxlint --fix",
|
|
35
|
+
"lint": "oxlint && antelopejs-check-interface-ranges",
|
|
31
36
|
"prepack": "pnpm run build",
|
|
32
37
|
"release": "pnpm run lint && pnpm run test && pnpm run prepack && release-it",
|
|
33
|
-
"test": "pnpm run build && ajs module test"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
"
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
"
|
|
38
|
+
"test": "pnpm run build && ajs module test",
|
|
39
|
+
"format:check": "oxfmt --check .",
|
|
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"
|
|
43
49
|
},
|
|
44
50
|
"dependencies": {
|
|
45
|
-
"@antelopejs/interface-api": ">=0.0
|
|
46
|
-
"@antelopejs/interface-auth": ">=0.0
|
|
47
|
-
"@antelopejs/interface-core": ">=0.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",
|
|
48
54
|
"chai": "^4.5.0",
|
|
49
|
-
"jsonwebtoken": "^9.0.3"
|
|
50
|
-
"mocha": "^11.7.5"
|
|
55
|
+
"jsonwebtoken": "^9.0.3"
|
|
51
56
|
},
|
|
52
57
|
"devDependencies": {
|
|
53
|
-
"@
|
|
58
|
+
"@antelopejs/core": "^1.20.0",
|
|
59
|
+
"@antelopejs/tooling-configs": "^0.2.17",
|
|
54
60
|
"@types/chai": "^4.3.20",
|
|
55
61
|
"@types/jsonwebtoken": "^9.0.10",
|
|
56
62
|
"@types/mocha": "^10.0.10",
|
|
57
63
|
"@types/node": "^22.19.15",
|
|
58
64
|
"@types/semver": "^7.7.1",
|
|
65
|
+
"eslint-plugin-perfectionist": "^5.11.0",
|
|
66
|
+
"knip": "^6.34.0",
|
|
67
|
+
"lefthook": "^2.2.2",
|
|
68
|
+
"oxfmt": "^0.66.0",
|
|
69
|
+
"oxlint": "1.81.0",
|
|
70
|
+
"oxlint-tsgolint": "^7.0.2001",
|
|
59
71
|
"release-it": "^19.2.4",
|
|
60
72
|
"release-it-changelogen": "^0.1.0",
|
|
61
73
|
"rimraf": "^6.1.3",
|
|
62
74
|
"typescript": "^5.9.3"
|
|
75
|
+
},
|
|
76
|
+
"packageManager": "pnpm@10.6.5",
|
|
77
|
+
"antelopeJs": {
|
|
78
|
+
"implements": [
|
|
79
|
+
"@antelopejs/interface-auth"
|
|
80
|
+
],
|
|
81
|
+
"defaultConfig": {
|
|
82
|
+
"secret": "your-secret-key"
|
|
83
|
+
},
|
|
84
|
+
"test": "src/test/antelope.test.ts",
|
|
85
|
+
"docs": {
|
|
86
|
+
"standard": true
|
|
87
|
+
}
|
|
63
88
|
}
|
|
64
89
|
}
|