@maximemrf/adonisjs-jwt 0.0.14 → 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 +32 -12
- package/package.json +1 -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
|
@@ -37,6 +37,11 @@ export class JwtGuard {
|
|
|
37
37
|
expiresIn: this.#options.expiresIn,
|
|
38
38
|
}
|
|
39
39
|
: {});
|
|
40
|
+
if (this.#options.useCookies) {
|
|
41
|
+
return this.#ctx.response.cookie('token', token, {
|
|
42
|
+
httpOnly: true,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
40
45
|
return {
|
|
41
46
|
type: 'bearer',
|
|
42
47
|
token: token,
|
|
@@ -57,23 +62,38 @@ export class JwtGuard {
|
|
|
57
62
|
return this.getUserOrFail();
|
|
58
63
|
}
|
|
59
64
|
this.authenticationAttempted = true;
|
|
65
|
+
const cookieHeader = this.#ctx.request.request.headers.cookie;
|
|
66
|
+
let token;
|
|
60
67
|
/**
|
|
61
|
-
*
|
|
68
|
+
* If cookies are enabled, then read the token from the cookies
|
|
62
69
|
*/
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
guardDriverName: this.driverName,
|
|
67
|
-
});
|
|
70
|
+
if (cookieHeader) {
|
|
71
|
+
;
|
|
72
|
+
[, token] = cookieHeader.split('=');
|
|
68
73
|
}
|
|
69
74
|
/**
|
|
70
|
-
*
|
|
75
|
+
* If token is missing on cookies, then try to read it from the header authorization
|
|
71
76
|
*/
|
|
72
|
-
const [, token] = authHeader.split('Bearer ');
|
|
73
77
|
if (!token) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
+
}
|
|
77
97
|
}
|
|
78
98
|
/**
|
|
79
99
|
* Verify token
|
|
@@ -136,7 +156,7 @@ export class JwtGuard {
|
|
|
136
156
|
const token = await this.generate(user);
|
|
137
157
|
return {
|
|
138
158
|
headers: {
|
|
139
|
-
authorization: `Bearer ${token.token}`,
|
|
159
|
+
authorization: `Bearer ${this.#options.useCookies ? token : token.token}`,
|
|
140
160
|
},
|
|
141
161
|
};
|
|
142
162
|
}
|