@maximemrf/adonisjs-jwt 1.0.0-rc.1 → 1.0.0-rc.2
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/configure.js +8 -2
- package/build/src/define_config.d.ts +3 -3
- package/build/src/guard.d.ts +3 -3
- package/build/src/guard.js +3 -0
- package/build/src/types.d.ts +9 -3
- package/package.json +1 -1
package/build/configure.js
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
| Flags: --refresh-tokens / --no-refresh-tokens to skip the question.
|
|
11
11
|
|
|
|
12
12
|
*/
|
|
13
|
-
import { existsSync, readdirSync } from 'node:fs';
|
|
13
|
+
import { appendFileSync, existsSync, readdirSync } from 'node:fs';
|
|
14
14
|
import { stubsRoot } from './stubs/main.js';
|
|
15
15
|
const DOCS_URL = 'https://maximemrf.github.io/adonisjs-jwt';
|
|
16
16
|
const MIGRATION_SUFFIX = '_create_jwt_refresh_tokens_table.ts';
|
|
@@ -127,11 +127,17 @@ async function createMigration(command, codemods) {
|
|
|
127
127
|
.skipped('the migration already exists');
|
|
128
128
|
return;
|
|
129
129
|
}
|
|
130
|
-
await codemods.makeUsingStub(stubsRoot, 'migration.stub', {
|
|
130
|
+
const output = await codemods.makeUsingStub(stubsRoot, 'migration.stub', {
|
|
131
131
|
entity: {
|
|
132
132
|
filename: `${Date.now()}${MIGRATION_SUFFIX}`,
|
|
133
133
|
},
|
|
134
134
|
});
|
|
135
|
+
/**
|
|
136
|
+
* The stub renderer trims the final newline, which prettier reports
|
|
137
|
+
*/
|
|
138
|
+
if (output.status !== 'skipped' && !output.contents.endsWith('\n')) {
|
|
139
|
+
appendFileSync(output.destination, '\n');
|
|
140
|
+
}
|
|
135
141
|
}
|
|
136
142
|
function getSourceFile(project, path) {
|
|
137
143
|
return project.getSourceFile(path) ?? project.addSourceFileAtPath(path);
|
|
@@ -6,9 +6,9 @@ import { JwtGuard } from './guard.js';
|
|
|
6
6
|
import type { StringValue } from 'ms';
|
|
7
7
|
import type { AccessTokensUserProviderContract } from '@adonisjs/auth/types/access_tokens';
|
|
8
8
|
import type { Options } from 'jwks-rsa';
|
|
9
|
-
export declare function jwtGuard<UserProvider extends JwtUserProviderContract<unknown
|
|
9
|
+
export declare function jwtGuard<UserProvider extends JwtUserProviderContract<unknown>, RefreshTokenUserProvider extends AccessTokensUserProviderContract<unknown> | undefined = undefined>(config: {
|
|
10
10
|
provider: UserProvider;
|
|
11
|
-
refreshTokenUserProvider?:
|
|
11
|
+
refreshTokenUserProvider?: RefreshTokenUserProvider;
|
|
12
12
|
tokenName?: string;
|
|
13
13
|
refreshTokenName?: string;
|
|
14
14
|
tokenExpiresIn?: number | StringValue;
|
|
@@ -29,4 +29,4 @@ export declare function jwtGuard<UserProvider extends JwtUserProviderContract<un
|
|
|
29
29
|
clockTolerance?: number;
|
|
30
30
|
getUserId?: JwtGetUserId;
|
|
31
31
|
verifyPayload?: JwtVerifyPayload;
|
|
32
|
-
}): GuardConfigProvider<(ctx: HttpContext) => JwtGuard<UserProvider>>;
|
|
32
|
+
}): GuardConfigProvider<(ctx: HttpContext) => JwtGuard<UserProvider, [RefreshTokenUserProvider] extends [undefined] ? false : true>>;
|
package/build/src/guard.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { symbols } from '@adonisjs/auth';
|
|
|
2
2
|
import type { AuthClientResponse, GuardContract } from '@adonisjs/auth/types';
|
|
3
3
|
import type { HttpContext } from '@adonisjs/core/http';
|
|
4
4
|
import { type JwtUserProviderContract, type JwtGuardOptions, type JwtGenerateResult } from './types.js';
|
|
5
|
-
export declare class JwtGuard<UserProvider extends JwtUserProviderContract<unknown
|
|
5
|
+
export declare class JwtGuard<UserProvider extends JwtUserProviderContract<unknown>, WithRefreshToken extends boolean = boolean> implements GuardContract<UserProvider[typeof symbols.PROVIDER_REAL_USER]> {
|
|
6
6
|
#private;
|
|
7
7
|
constructor(ctx: HttpContext, userProvider: UserProvider, option: JwtGuardOptions<UserProvider[typeof symbols.PROVIDER_REAL_USER]>);
|
|
8
8
|
/**
|
|
@@ -33,7 +33,7 @@ export declare class JwtGuard<UserProvider extends JwtUserProviderContract<unkno
|
|
|
33
33
|
/**
|
|
34
34
|
* Generate a JWT token for a given user.
|
|
35
35
|
*/
|
|
36
|
-
generate(user: UserProvider[typeof symbols.PROVIDER_REAL_USER]): Promise<JwtGenerateResult
|
|
36
|
+
generate(user: UserProvider[typeof symbols.PROVIDER_REAL_USER]): Promise<JwtGenerateResult<WithRefreshToken>>;
|
|
37
37
|
/**
|
|
38
38
|
* Authenticate the current HTTP request and return
|
|
39
39
|
* the user instance if there is a valid JWT token
|
|
@@ -42,7 +42,7 @@ export declare class JwtGuard<UserProvider extends JwtUserProviderContract<unkno
|
|
|
42
42
|
authenticate(): Promise<UserProvider[typeof symbols.PROVIDER_REAL_USER] & {
|
|
43
43
|
currentToken: string;
|
|
44
44
|
}>;
|
|
45
|
-
generateWithRefreshToken(refreshToken?: string): Promise<JwtGenerateResult
|
|
45
|
+
generateWithRefreshToken(refreshToken?: string): Promise<JwtGenerateResult<true>>;
|
|
46
46
|
/**
|
|
47
47
|
* Revoke the refresh token
|
|
48
48
|
*/
|
package/build/src/guard.js
CHANGED
|
@@ -244,6 +244,9 @@ export class JwtGuard {
|
|
|
244
244
|
this.isAuthenticated = true;
|
|
245
245
|
this.user = user;
|
|
246
246
|
this.user.currentToken = result.token;
|
|
247
|
+
/**
|
|
248
|
+
* The guard has a refresh token provider, so `generate()` issued a refresh token
|
|
249
|
+
*/
|
|
247
250
|
return result;
|
|
248
251
|
}
|
|
249
252
|
async #findRefreshToken() {
|
package/build/src/types.d.ts
CHANGED
|
@@ -56,13 +56,19 @@ export declare const ALLOWED_ASYMMETRIC_ALGORITHMS: readonly ["RS256", "RS384",
|
|
|
56
56
|
export type JwtAsymmetricAlgorithm = (typeof ALLOWED_ASYMMETRIC_ALGORITHMS)[number];
|
|
57
57
|
export declare const ALLOWED_JWKS_ALGORITHMS: readonly ["RS256", "RS384", "RS512", "ES256", "ES384", "ES512", "PS256", "PS384", "PS512"];
|
|
58
58
|
export type JwtJwksAlgorithm = (typeof ALLOWED_JWKS_ALGORITHMS)[number];
|
|
59
|
-
|
|
59
|
+
/**
|
|
60
|
+
* `refreshToken` is a string when the guard has a `refreshTokenUserProvider`
|
|
61
|
+
*/
|
|
62
|
+
export type JwtGenerateResult<WithRefreshToken extends boolean = boolean> = {
|
|
60
63
|
type: string;
|
|
61
64
|
token: string;
|
|
62
65
|
expiresIn?: number | StringValue;
|
|
63
|
-
refreshToken?: string;
|
|
64
66
|
refreshTokenExpiresIn?: number | StringValue;
|
|
65
|
-
}
|
|
67
|
+
} & ([WithRefreshToken] extends [true] ? {
|
|
68
|
+
refreshToken: string;
|
|
69
|
+
} : {
|
|
70
|
+
refreshToken?: string;
|
|
71
|
+
});
|
|
66
72
|
export type JwtGuardOptions<RealUser extends any = unknown> = {
|
|
67
73
|
/**
|
|
68
74
|
* Driver resolved by `jwtGuard()`. Not part of the public API: custom
|