@maximemrf/adonisjs-jwt 0.0.13 → 0.0.15
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/build/src/define_config.d.ts +1 -0
- package/build/src/define_config.js +5 -1
- package/build/src/jwt.d.ts +2 -1
- package/build/src/jwt.js +37 -15
- package/package.json +2 -1
|
@@ -4,4 +4,5 @@ import { JwtGuard, JwtUserProviderContract } from './jwt.js';
|
|
|
4
4
|
export declare function jwtGuard<UserProvider extends JwtUserProviderContract<unknown>>(config: {
|
|
5
5
|
provider: UserProvider;
|
|
6
6
|
tokenExpiresIn: number | string;
|
|
7
|
+
useCookies?: boolean;
|
|
7
8
|
}): GuardConfigProvider<(ctx: HttpContext) => JwtGuard<UserProvider>>;
|
|
@@ -3,7 +3,11 @@ export function jwtGuard(config) {
|
|
|
3
3
|
return {
|
|
4
4
|
async resolver(_, app) {
|
|
5
5
|
const appKey = app.config.get('app.appKey').release();
|
|
6
|
-
const options = {
|
|
6
|
+
const options = {
|
|
7
|
+
secret: appKey,
|
|
8
|
+
expiresIn: config.tokenExpiresIn,
|
|
9
|
+
useCookies: config.useCookies,
|
|
10
|
+
};
|
|
7
11
|
return (ctx) => new JwtGuard(ctx, config.provider, options);
|
|
8
12
|
},
|
|
9
13
|
};
|
package/build/src/jwt.d.ts
CHANGED
|
@@ -38,6 +38,7 @@ export interface JwtUserProviderContract<RealUser> {
|
|
|
38
38
|
export type JwtGuardOptions = {
|
|
39
39
|
secret: string;
|
|
40
40
|
expiresIn?: number | string;
|
|
41
|
+
useCookies?: boolean;
|
|
41
42
|
};
|
|
42
43
|
export declare class JwtGuard<UserProvider extends JwtUserProviderContract<unknown>> implements GuardContract<UserProvider[typeof symbols.PROVIDER_REAL_USER]> {
|
|
43
44
|
#private;
|
|
@@ -68,7 +69,7 @@ export declare class JwtGuard<UserProvider extends JwtUserProviderContract<unkno
|
|
|
68
69
|
/**
|
|
69
70
|
* Generate a JWT token for a given user.
|
|
70
71
|
*/
|
|
71
|
-
generate(user: UserProvider[typeof symbols.PROVIDER_REAL_USER]): Promise<{
|
|
72
|
+
generate(user: UserProvider[typeof symbols.PROVIDER_REAL_USER]): Promise<import("@adonisjs/core/http").Response | {
|
|
72
73
|
type: string;
|
|
73
74
|
token: string;
|
|
74
75
|
expiresIn: string | number | undefined;
|
package/build/src/jwt.js
CHANGED
|
@@ -32,9 +32,16 @@ export class JwtGuard {
|
|
|
32
32
|
*/
|
|
33
33
|
async generate(user) {
|
|
34
34
|
const providerUser = await this.#userProvider.createUserForGuard(user);
|
|
35
|
-
const token = jwt.sign({ userId: providerUser.getId() }, this.#options.secret,
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
const token = jwt.sign({ userId: providerUser.getId() }, this.#options.secret, this.#options.expiresIn
|
|
36
|
+
? {
|
|
37
|
+
expiresIn: this.#options.expiresIn,
|
|
38
|
+
}
|
|
39
|
+
: {});
|
|
40
|
+
if (this.#options.useCookies) {
|
|
41
|
+
return this.#ctx.response.cookie('token', token, {
|
|
42
|
+
httpOnly: true,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
38
45
|
return {
|
|
39
46
|
type: 'bearer',
|
|
40
47
|
token: token,
|
|
@@ -55,23 +62,38 @@ export class JwtGuard {
|
|
|
55
62
|
return this.getUserOrFail();
|
|
56
63
|
}
|
|
57
64
|
this.authenticationAttempted = true;
|
|
65
|
+
const cookieHeader = this.#ctx.request.request.headers.cookie;
|
|
66
|
+
let token;
|
|
58
67
|
/**
|
|
59
|
-
*
|
|
68
|
+
* If cookies are enabled, then read the token from the cookies
|
|
60
69
|
*/
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
guardDriverName: this.driverName,
|
|
65
|
-
});
|
|
70
|
+
if (cookieHeader) {
|
|
71
|
+
;
|
|
72
|
+
[, token] = cookieHeader.split('=');
|
|
66
73
|
}
|
|
67
74
|
/**
|
|
68
|
-
*
|
|
75
|
+
* If token is missing on cookies, then try to read it from the header authorization
|
|
69
76
|
*/
|
|
70
|
-
const [, token] = authHeader.split('Bearer ');
|
|
71
77
|
if (!token) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
78
|
+
/**
|
|
79
|
+
* Ensure the auth header exists
|
|
80
|
+
*/
|
|
81
|
+
const authHeader = this.#ctx.request.header('authorization');
|
|
82
|
+
if (!authHeader) {
|
|
83
|
+
throw new errors.E_UNAUTHORIZED_ACCESS('Unauthorized access', {
|
|
84
|
+
guardDriverName: this.driverName,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Split the header value and read the token from it
|
|
89
|
+
*/
|
|
90
|
+
;
|
|
91
|
+
[, token] = authHeader.split('Bearer ');
|
|
92
|
+
if (!token) {
|
|
93
|
+
throw new errors.E_UNAUTHORIZED_ACCESS('Unauthorized access', {
|
|
94
|
+
guardDriverName: this.driverName,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
75
97
|
}
|
|
76
98
|
/**
|
|
77
99
|
* Verify token
|
|
@@ -134,7 +156,7 @@ export class JwtGuard {
|
|
|
134
156
|
const token = await this.generate(user);
|
|
135
157
|
return {
|
|
136
158
|
headers: {
|
|
137
|
-
authorization: `Bearer ${token.token}`,
|
|
159
|
+
authorization: `Bearer ${this.#options.useCookies ? token : token.token}`,
|
|
138
160
|
},
|
|
139
161
|
};
|
|
140
162
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@maximemrf/adonisjs-jwt",
|
|
3
3
|
"description": "",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.15",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=20.6.0"
|
|
7
7
|
},
|
|
@@ -55,6 +55,7 @@
|
|
|
55
55
|
"luxon": "^3.4.4",
|
|
56
56
|
"np": "^10.0.0",
|
|
57
57
|
"prettier": "^3.2.5",
|
|
58
|
+
"timekeeper": "^2.3.1",
|
|
58
59
|
"ts-node": "^10.9.2",
|
|
59
60
|
"typescript": "^5.4.2"
|
|
60
61
|
},
|