@maximemrf/adonisjs-jwt 0.7.0-alpha.2 → 0.7.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 +21 -0
- package/build/src/define_config.js +0 -2
- package/build/src/jwt.js +14 -3
- package/build/src/types.d.ts +0 -2
- package/package.json +7 -5
package/README.md
CHANGED
|
@@ -116,6 +116,27 @@ useCookies: true
|
|
|
116
116
|
|
|
117
117
|
If you just want to use jwt with the bearer token no need to set `useCookies` to `false` you can just remove it.
|
|
118
118
|
|
|
119
|
+
## JWKS
|
|
120
|
+
|
|
121
|
+
You can use JWKS to verify the token by setting the `jwks` option in the guard configuration.
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
// ...
|
|
125
|
+
jwt: jwtGuard({
|
|
126
|
+
// ...
|
|
127
|
+
jwks: {
|
|
128
|
+
jwksUri: 'https://your-auth-server/.well-known/jwks.json',
|
|
129
|
+
// you can pass any options accepted by jwks-rsa package
|
|
130
|
+
cache: true,
|
|
131
|
+
rateLimit: true,
|
|
132
|
+
},
|
|
133
|
+
}),
|
|
134
|
+
// ...
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
> [!WARNING]
|
|
138
|
+
> If you enable JWKS, you cannot use the `auth.use('jwt').generate(user)` and `auth.use('jwt').generateWithRefreshToken()` method because the token is signed by an external provider. You can only use the `authenticate` (or `check` / `getUserOrFail`) method to verify the token.
|
|
139
|
+
|
|
119
140
|
## Refresh Tokens
|
|
120
141
|
|
|
121
142
|
To use refresh tokens, you have to set the `refreshTokenUserProvider` in the guard configuration, see the example above.
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { JwtGuard } from './jwt.js';
|
|
2
|
-
import { JwksManager } from './jwks.js';
|
|
3
2
|
export function jwtGuard(config) {
|
|
4
3
|
return {
|
|
5
4
|
async resolver(_, app) {
|
|
@@ -15,7 +14,6 @@ export function jwtGuard(config) {
|
|
|
15
14
|
refreshTokenAbilities: config.refreshTokenAbilities,
|
|
16
15
|
content: config.content,
|
|
17
16
|
jwks: config.jwks,
|
|
18
|
-
jwksClient: config.jwks ? new JwksManager(config.jwks).getClient() : undefined,
|
|
19
17
|
};
|
|
20
18
|
return (ctx) => new JwtGuard(ctx, config.provider, options);
|
|
21
19
|
},
|
package/build/src/jwt.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { errors } from '@adonisjs/auth';
|
|
2
2
|
import jwt from 'jsonwebtoken';
|
|
3
3
|
import { Secret } from '@adonisjs/core/helpers';
|
|
4
|
+
import { JwksManager } from './jwks.js';
|
|
4
5
|
export class JwtGuard {
|
|
5
6
|
#ctx;
|
|
6
7
|
#userProvider;
|
|
@@ -17,7 +18,7 @@ export class JwtGuard {
|
|
|
17
18
|
if (!this.#options.content)
|
|
18
19
|
this.#options.content = (user) => ({ userId: user.getId() });
|
|
19
20
|
this.#tokenName = this.#options.tokenName ?? 'token';
|
|
20
|
-
this.#jwksManager = this.#options.
|
|
21
|
+
this.#jwksManager = this.#options.jwks ? new JwksManager(this.#options.jwks) : undefined;
|
|
21
22
|
}
|
|
22
23
|
/**
|
|
23
24
|
* A unique name for the guard driver
|
|
@@ -41,6 +42,11 @@ export class JwtGuard {
|
|
|
41
42
|
* Generate a JWT token for a given user.
|
|
42
43
|
*/
|
|
43
44
|
async generate(user) {
|
|
45
|
+
if (this.#options.jwks) {
|
|
46
|
+
throw new errors.E_UNAUTHORIZED_ACCESS("You can't use the auth.generate method with jwks", {
|
|
47
|
+
guardDriverName: this.driverName,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
44
50
|
const providerUser = await this.#userProvider.createUserForGuard(user);
|
|
45
51
|
const token = jwt.sign(this.#options.content(providerUser), this.#options.secret, this.#options.expiresIn
|
|
46
52
|
? {
|
|
@@ -142,7 +148,7 @@ export class JwtGuard {
|
|
|
142
148
|
payload = jwt.verify(token, this.#options.secret);
|
|
143
149
|
}
|
|
144
150
|
}
|
|
145
|
-
catch
|
|
151
|
+
catch {
|
|
146
152
|
throw new errors.E_UNAUTHORIZED_ACCESS('Unauthorized access', {
|
|
147
153
|
guardDriverName: this.driverName,
|
|
148
154
|
});
|
|
@@ -168,6 +174,11 @@ export class JwtGuard {
|
|
|
168
174
|
}
|
|
169
175
|
async generateWithRefreshToken(refreshToken) {
|
|
170
176
|
this.authenticationAttempted = true;
|
|
177
|
+
if (this.#options.jwks) {
|
|
178
|
+
throw new errors.E_UNAUTHORIZED_ACCESS('JWKS is not supported for refresh token', {
|
|
179
|
+
guardDriverName: this.driverName,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
171
182
|
if (!this.#refreshTokenUserProvider) {
|
|
172
183
|
throw new errors.E_UNAUTHORIZED_ACCESS('Unauthorized access', {
|
|
173
184
|
guardDriverName: this.driverName,
|
|
@@ -278,7 +289,7 @@ export class JwtGuard {
|
|
|
278
289
|
const token = await this.generate(user);
|
|
279
290
|
return {
|
|
280
291
|
headers: {
|
|
281
|
-
authorization: `Bearer ${
|
|
292
|
+
authorization: `Bearer ${token.token}`,
|
|
282
293
|
},
|
|
283
294
|
};
|
|
284
295
|
}
|
package/build/src/types.d.ts
CHANGED
|
@@ -2,7 +2,6 @@ import { symbols } from '@adonisjs/auth';
|
|
|
2
2
|
import type { StringValue } from 'ms';
|
|
3
3
|
import { AccessTokensUserProviderContract } from '@adonisjs/auth/types/access_tokens';
|
|
4
4
|
import { Options } from 'jwks-rsa';
|
|
5
|
-
import { JwksManager } from './jwks.js';
|
|
6
5
|
/**
|
|
7
6
|
* The bridge between the User provider and the
|
|
8
7
|
* Guard
|
|
@@ -43,7 +42,6 @@ export type BaseJwtContent = {
|
|
|
43
42
|
export type JwtGuardOptions<RealUser extends any = unknown> = {
|
|
44
43
|
secret: string;
|
|
45
44
|
jwks?: Options;
|
|
46
|
-
jwksManager?: JwksManager;
|
|
47
45
|
refreshTokenUserProvider?: AccessTokensUserProviderContract<RealUser>;
|
|
48
46
|
tokenName?: string;
|
|
49
47
|
expiresIn?: number | StringValue;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@maximemrf/adonisjs-jwt",
|
|
3
3
|
"description": "Adonisjs v6 package for authentication with jwt token",
|
|
4
|
-
"version": "0.7.0
|
|
4
|
+
"version": "0.7.0",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=20.6.0"
|
|
7
7
|
},
|
|
@@ -44,15 +44,17 @@
|
|
|
44
44
|
"@japa/assert": "^4.2.0",
|
|
45
45
|
"@japa/file-system": "^3.0.0",
|
|
46
46
|
"@japa/runner": "^5.3.0",
|
|
47
|
-
"@
|
|
47
|
+
"@types/nock": "^10.0.3",
|
|
48
|
+
"@swc/core": "^1.15.10",
|
|
48
49
|
"@types/node": "^22.15.18",
|
|
49
50
|
"c8": "^10.1.3",
|
|
50
51
|
"copyfiles": "^2.4.1",
|
|
51
52
|
"del-cli": "^7.0.0",
|
|
52
53
|
"eslint": "^9.39.2",
|
|
53
54
|
"luxon": "^3.7.2",
|
|
54
|
-
"
|
|
55
|
-
"
|
|
55
|
+
"nock": "^14.0.10",
|
|
56
|
+
"np": "^11.0.1",
|
|
57
|
+
"prettier": "^3.8.1",
|
|
56
58
|
"sqlite3": "^5.1.7",
|
|
57
59
|
"timekeeper": "^2.3.1",
|
|
58
60
|
"ts-node-maintained": "^10.9.6",
|
|
@@ -98,4 +100,4 @@
|
|
|
98
100
|
"tests/**"
|
|
99
101
|
]
|
|
100
102
|
}
|
|
101
|
-
}
|
|
103
|
+
}
|