@maximemrf/adonisjs-jwt 0.0.14 → 0.0.16
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 +41 -11
- 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,39 @@ 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
|
+
console.log('token', token);
|
|
93
|
+
if (!token) {
|
|
94
|
+
throw new errors.E_UNAUTHORIZED_ACCESS('Unauthorized access', {
|
|
95
|
+
guardDriverName: this.driverName,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
77
98
|
}
|
|
78
99
|
/**
|
|
79
100
|
* Verify token
|
|
@@ -134,6 +155,15 @@ export class JwtGuard {
|
|
|
134
155
|
*/
|
|
135
156
|
async authenticateAsClient(user) {
|
|
136
157
|
const token = await this.generate(user);
|
|
158
|
+
// if (this.#options.useCookies) {
|
|
159
|
+
// console.log('useCookies')
|
|
160
|
+
// console.log(token.ctx.response.cookie())
|
|
161
|
+
// return {
|
|
162
|
+
// headers: {
|
|
163
|
+
// 'set-cookie': `token=${token}`,
|
|
164
|
+
// },
|
|
165
|
+
// }
|
|
166
|
+
// }
|
|
137
167
|
return {
|
|
138
168
|
headers: {
|
|
139
169
|
authorization: `Bearer ${token.token}`,
|