@adonisjs/auth 9.5.0 → 10.0.0-next.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/build/{chunk-SAW2RPCA.js → chunk-MSPAYMZE.js} +94 -4
- package/build/{chunk-MUPAP5IP.js → chunk-S5G5RTJX.js} +40 -0
- package/build/index.d.ts +8 -8
- package/build/index.js +2 -2
- package/build/modules/access_tokens_guard/access_token.d.ts +103 -0
- package/build/modules/access_tokens_guard/crc32.d.ts +39 -0
- package/build/modules/access_tokens_guard/define_config.d.ts +4 -4
- package/build/modules/access_tokens_guard/guard.d.ts +82 -5
- package/build/modules/access_tokens_guard/main.d.ts +5 -5
- package/build/modules/access_tokens_guard/main.js +339 -4
- package/build/modules/access_tokens_guard/token_providers/db.d.ts +102 -4
- package/build/modules/access_tokens_guard/types.d.ts +2 -2
- package/build/modules/access_tokens_guard/user_providers/lucid.d.ts +77 -4
- package/build/modules/basic_auth_guard/define_config.d.ts +4 -4
- package/build/modules/basic_auth_guard/guard.d.ts +16 -3
- package/build/modules/basic_auth_guard/main.d.ts +3 -3
- package/build/modules/basic_auth_guard/main.js +11 -1
- package/build/modules/basic_auth_guard/types.d.ts +1 -1
- package/build/modules/basic_auth_guard/user_providers/lucid.d.ts +19 -2
- package/build/modules/session_guard/define_config.d.ts +4 -4
- package/build/modules/session_guard/guard.d.ts +42 -3
- package/build/modules/session_guard/main.d.ts +5 -5
- package/build/modules/session_guard/main.js +349 -124
- package/build/modules/session_guard/remember_me_token.d.ts +64 -0
- package/build/modules/session_guard/token_providers/db.d.ts +92 -2
- package/build/modules/session_guard/types.d.ts +2 -2
- package/build/modules/session_guard/user_providers/lucid.d.ts +78 -5
- package/build/providers/auth_provider.d.ts +22 -1
- package/build/providers/auth_provider.js +16 -3
- package/build/services/auth.d.ts +1 -1
- package/build/src/auth_manager.d.ts +25 -4
- package/build/src/authenticator.d.ts +55 -3
- package/build/src/authenticator_client.d.ts +18 -1
- package/build/src/define_config.d.ts +19 -1
- package/build/src/errors.d.ts +31 -0
- package/build/src/middleware/initialize_auth_middleware.d.ts +15 -2
- package/build/src/middleware/initialize_auth_middleware.js +10 -0
- package/build/src/mixins/lucid.d.ts +13 -0
- package/build/src/mixins/lucid.js +27 -2
- package/build/src/plugins/japa/api_client.d.ts +10 -1
- package/build/src/plugins/japa/browser_client.d.ts +10 -1
- package/build/src/types.d.ts +141 -29
- package/package.json +23 -22
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ConfigProvider } from '@adonisjs/core/types';
|
|
2
|
-
import type { GuardConfigProvider, GuardFactory } from './types.
|
|
2
|
+
import type { GuardConfigProvider, GuardFactory } from './types.ts';
|
|
3
3
|
/**
|
|
4
4
|
* Config resolved by the "defineConfig" method
|
|
5
5
|
*/
|
|
@@ -13,6 +13,24 @@ export type ResolvedAuthConfig<KnownGuards extends Record<string, GuardFactory |
|
|
|
13
13
|
* Define configuration for the auth package. The function returns
|
|
14
14
|
* a config provider that is invoked inside the auth service
|
|
15
15
|
* provider
|
|
16
|
+
*
|
|
17
|
+
* @param config - Configuration object with default guard and available guards
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* import { defineConfig } from '@adonisjs/auth'
|
|
21
|
+
* import { sessionGuard, sessionUserProvider } from '@adonisjs/auth/session'
|
|
22
|
+
*
|
|
23
|
+
* const authConfig = defineConfig({
|
|
24
|
+
* default: 'web',
|
|
25
|
+
* guards: {
|
|
26
|
+
* web: sessionGuard({
|
|
27
|
+
* useRememberMeTokens: false,
|
|
28
|
+
* provider: sessionUserProvider({
|
|
29
|
+
* model: () => import('#models/user')
|
|
30
|
+
* })
|
|
31
|
+
* })
|
|
32
|
+
* }
|
|
33
|
+
* })
|
|
16
34
|
*/
|
|
17
35
|
export declare function defineConfig<KnownGuards extends Record<string, GuardFactory | GuardConfigProvider<GuardFactory>>>(config: {
|
|
18
36
|
default: keyof KnownGuards;
|
package/build/src/errors.d.ts
CHANGED
|
@@ -38,10 +38,24 @@ export declare const E_UNAUTHORIZED_ACCESS: {
|
|
|
38
38
|
* Returns the message to be sent in the HTTP response.
|
|
39
39
|
* Feel free to override this method and return a custom
|
|
40
40
|
* response.
|
|
41
|
+
*
|
|
42
|
+
* @param error - The error instance
|
|
43
|
+
* @param ctx - The HTTP context
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* const message = error.getResponseMessage(error, ctx)
|
|
47
|
+
* console.log('Error message:', message)
|
|
41
48
|
*/
|
|
42
49
|
getResponseMessage(error: /*elided*/ any, ctx: HttpContext): string;
|
|
43
50
|
/**
|
|
44
51
|
* Converts exception to an HTTP response
|
|
52
|
+
*
|
|
53
|
+
* @param error - The error instance
|
|
54
|
+
* @param ctx - The HTTP context
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* // This method is called automatically by AdonisJS
|
|
58
|
+
* await error.handle(error, ctx)
|
|
45
59
|
*/
|
|
46
60
|
handle(error: /*elided*/ any, ctx: HttpContext): Promise<void>;
|
|
47
61
|
name: string;
|
|
@@ -65,6 +79,9 @@ export declare const E_UNAUTHORIZED_ACCESS: {
|
|
|
65
79
|
};
|
|
66
80
|
/**
|
|
67
81
|
* Exception is raised when user credentials are invalid
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* throw new E_INVALID_CREDENTIALS('Invalid email or password')
|
|
68
85
|
*/
|
|
69
86
|
export declare const E_INVALID_CREDENTIALS: {
|
|
70
87
|
new (message?: string, options?: ErrorOptions & {
|
|
@@ -79,10 +96,24 @@ export declare const E_INVALID_CREDENTIALS: {
|
|
|
79
96
|
* Returns the message to be sent in the HTTP response.
|
|
80
97
|
* Feel free to override this method and return a custom
|
|
81
98
|
* response.
|
|
99
|
+
*
|
|
100
|
+
* @param error - The error instance
|
|
101
|
+
* @param ctx - The HTTP context
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* const message = error.getResponseMessage(error, ctx)
|
|
105
|
+
* console.log('Error message:', message)
|
|
82
106
|
*/
|
|
83
107
|
getResponseMessage(error: /*elided*/ any, ctx: HttpContext): string;
|
|
84
108
|
/**
|
|
85
109
|
* Converts exception to an HTTP response
|
|
110
|
+
*
|
|
111
|
+
* @param error - The error instance
|
|
112
|
+
* @param ctx - The HTTP context
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* // This method is called automatically by AdonisJS
|
|
116
|
+
* await error.handle(error, ctx)
|
|
86
117
|
*/
|
|
87
118
|
handle(error: /*elided*/ any, ctx: HttpContext): Promise<void>;
|
|
88
119
|
name: string;
|
|
@@ -1,15 +1,28 @@
|
|
|
1
1
|
import type { HttpContext } from '@adonisjs/core/http';
|
|
2
2
|
import type { NextFn } from '@adonisjs/core/types/http';
|
|
3
|
-
import type { Authenticator } from '../authenticator.
|
|
4
|
-
import type { Authenticators, GuardFactory } from '../types.
|
|
3
|
+
import type { Authenticator } from '../authenticator.ts';
|
|
4
|
+
import type { Authenticators, GuardFactory } from '../types.ts';
|
|
5
5
|
/**
|
|
6
6
|
* The "InitializeAuthMiddleware" is used to create a request
|
|
7
7
|
* specific authenticator instance for every HTTP request.
|
|
8
8
|
*
|
|
9
9
|
* This middleware does not protect routes from unauthenticated
|
|
10
10
|
* users. Please use the "auth" middleware for that.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* router.use([() => import('#middleware/initialize_auth_middleware')])
|
|
11
14
|
*/
|
|
12
15
|
export default class InitializeAuthMiddleware {
|
|
16
|
+
/**
|
|
17
|
+
* Handle the HTTP request by initializing the authenticator
|
|
18
|
+
*
|
|
19
|
+
* @param ctx - The HTTP context
|
|
20
|
+
* @param next - The next function to call in the middleware chain
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* // This middleware runs automatically when registered
|
|
24
|
+
* // It adds ctx.auth to every HTTP request
|
|
25
|
+
*/
|
|
13
26
|
handle(ctx: HttpContext, next: NextFn): Promise<any>;
|
|
14
27
|
}
|
|
15
28
|
declare module '@adonisjs/core/http' {
|
|
@@ -2,6 +2,16 @@ import "../../chunk-UXA4FHST.js";
|
|
|
2
2
|
|
|
3
3
|
// src/middleware/initialize_auth_middleware.ts
|
|
4
4
|
var InitializeAuthMiddleware = class {
|
|
5
|
+
/**
|
|
6
|
+
* Handle the HTTP request by initializing the authenticator
|
|
7
|
+
*
|
|
8
|
+
* @param ctx - The HTTP context
|
|
9
|
+
* @param next - The next function to call in the middleware chain
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* // This middleware runs automatically when registered
|
|
13
|
+
* // It adds ctx.auth to every HTTP request
|
|
14
|
+
*/
|
|
5
15
|
async handle(ctx, next) {
|
|
6
16
|
const auth = await ctx.containerResolver.make("auth.manager");
|
|
7
17
|
ctx.auth = auth.createAuthenticator(ctx);
|
|
@@ -20,6 +20,19 @@ type UserWithUserFinderClass<Model extends NormalizeConstructor<typeof BaseModel
|
|
|
20
20
|
* - findForAuth method to find a user during authentication
|
|
21
21
|
* - verifyCredentials method to verify user credentials and prevent
|
|
22
22
|
* timing attacks.
|
|
23
|
+
*
|
|
24
|
+
* @param hash - Function that returns a Hash instance for password hashing
|
|
25
|
+
* @param options - Configuration options with uids and password column name
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* import { withAuthFinder } from '@adonisjs/auth/mixins/lucid'
|
|
29
|
+
*
|
|
30
|
+
* class User extends withAuthFinder(hash, {
|
|
31
|
+
* uids: ['email', 'username'],
|
|
32
|
+
* passwordColumnName: 'password'
|
|
33
|
+
* })(BaseModel) {
|
|
34
|
+
* // User model implementation
|
|
35
|
+
* }
|
|
23
36
|
*/
|
|
24
37
|
export declare function withAuthFinder(hash: () => Hash, options: {
|
|
25
38
|
uids: string[];
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
E_INVALID_CREDENTIALS
|
|
3
|
-
} from "../../chunk-
|
|
3
|
+
} from "../../chunk-S5G5RTJX.js";
|
|
4
4
|
import {
|
|
5
5
|
__decorateClass
|
|
6
6
|
} from "../../chunk-UXA4FHST.js";
|
|
@@ -21,8 +21,14 @@ function withAuthFinder(hash, options) {
|
|
|
21
21
|
}
|
|
22
22
|
/**
|
|
23
23
|
* Finds the user for authentication via "verifyCredentials".
|
|
24
|
-
* Feel free to override this method customize the user
|
|
24
|
+
* Feel free to override this method to customize the user
|
|
25
25
|
* lookup behavior.
|
|
26
|
+
*
|
|
27
|
+
* @param uids - Array of column names to search in
|
|
28
|
+
* @param value - The value to search for
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* const user = await User.findForAuth(['email', 'username'], 'john@example.com')
|
|
26
32
|
*/
|
|
27
33
|
static findForAuth(uids, value) {
|
|
28
34
|
const query = this.query();
|
|
@@ -32,6 +38,15 @@ function withAuthFinder(hash, options) {
|
|
|
32
38
|
/**
|
|
33
39
|
* Find a user by uid and verify their password. This method is
|
|
34
40
|
* safe from timing attacks.
|
|
41
|
+
*
|
|
42
|
+
* @param uid - The user identifier (email, username, etc.)
|
|
43
|
+
* @param password - The plain text password to verify
|
|
44
|
+
*
|
|
45
|
+
* @throws {E_INVALID_CREDENTIALS} When credentials are invalid
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* const user = await User.verifyCredentials('john@example.com', 'password123')
|
|
49
|
+
* console.log('Authenticated user:', user.email)
|
|
35
50
|
*/
|
|
36
51
|
static async verifyCredentials(uid, password) {
|
|
37
52
|
if (!uid || !password) {
|
|
@@ -50,6 +65,16 @@ function withAuthFinder(hash, options) {
|
|
|
50
65
|
/**
|
|
51
66
|
* Verifies the plain password against the user's password
|
|
52
67
|
* hash
|
|
68
|
+
*
|
|
69
|
+
* @param plainPassword - The plain text password to verify
|
|
70
|
+
*
|
|
71
|
+
* @throws {RuntimeException} When password column value is undefined or null
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* const isValid = await user.verifyPassword('password123')
|
|
75
|
+
* if (isValid) {
|
|
76
|
+
* console.log('Password is correct')
|
|
77
|
+
* }
|
|
53
78
|
*/
|
|
54
79
|
verifyPassword(plainPassword) {
|
|
55
80
|
const passwordHash = this[options.passwordColumnName];
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { PluginFn } from '@japa/runner/types';
|
|
2
2
|
import type { ApplicationService } from '@adonisjs/core/types';
|
|
3
|
-
import type { Authenticators, GuardContract, GuardFactory } from '../../types.
|
|
3
|
+
import type { Authenticators, GuardContract, GuardFactory } from '../../types.ts';
|
|
4
4
|
declare module '@japa/api-client' {
|
|
5
5
|
interface ApiRequest {
|
|
6
6
|
authData: {
|
|
@@ -28,5 +28,14 @@ declare module '@japa/api-client' {
|
|
|
28
28
|
/**
|
|
29
29
|
* Auth API client to authenticate users when making
|
|
30
30
|
* HTTP requests using the Japa API client
|
|
31
|
+
*
|
|
32
|
+
* @param app - The AdonisJS application service instance
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* import { authApiClient } from '@adonisjs/auth/plugins/japa/api_client'
|
|
36
|
+
*
|
|
37
|
+
* export const plugins: PluginFn[] = [
|
|
38
|
+
* authApiClient(app)
|
|
39
|
+
* ]
|
|
31
40
|
*/
|
|
32
41
|
export declare const authApiClient: (app: ApplicationService) => PluginFn;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { PluginFn } from '@japa/runner/types';
|
|
2
2
|
import type { ApplicationService } from '@adonisjs/core/types';
|
|
3
|
-
import type { Authenticators, GuardContract, GuardFactory } from '../../types.
|
|
3
|
+
import type { Authenticators, GuardContract, GuardFactory } from '../../types.ts';
|
|
4
4
|
declare module 'playwright' {
|
|
5
5
|
interface BrowserContext {
|
|
6
6
|
/**
|
|
@@ -24,5 +24,14 @@ declare module 'playwright' {
|
|
|
24
24
|
/**
|
|
25
25
|
* Browser API client to authenticate users when making
|
|
26
26
|
* HTTP requests using the Japa Browser client.
|
|
27
|
+
*
|
|
28
|
+
* @param app - The AdonisJS application service instance
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* import { authBrowserClient } from '@adonisjs/auth/plugins/japa/browser_client'
|
|
32
|
+
*
|
|
33
|
+
* export const plugins: PluginFn[] = [
|
|
34
|
+
* authBrowserClient(app)
|
|
35
|
+
* ]
|
|
27
36
|
*/
|
|
28
37
|
export declare const authBrowserClient: (app: ApplicationService) => PluginFn;
|
package/build/src/types.d.ts
CHANGED
|
@@ -1,108 +1,220 @@
|
|
|
1
1
|
import type { HttpContext } from '@adonisjs/core/http';
|
|
2
2
|
import type { ApplicationService, ConfigProvider } from '@adonisjs/core/types';
|
|
3
|
-
import type { AuthManager } from './auth_manager.
|
|
4
|
-
import type { GUARD_KNOWN_EVENTS } from './symbols.
|
|
3
|
+
import type { AuthManager } from './auth_manager.ts';
|
|
4
|
+
import type { GUARD_KNOWN_EVENTS } from './symbols.ts';
|
|
5
5
|
/**
|
|
6
6
|
* Authentication response to login a user as a client.
|
|
7
|
-
* This response is used by Japa plugins
|
|
7
|
+
* This response is used by Japa plugins to set up authentication
|
|
8
|
+
* state during testing.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const response: AuthClientResponse = {
|
|
12
|
+
* cookies: { 'auth-token': 'abc123' },
|
|
13
|
+
* session: { userId: 1 },
|
|
14
|
+
* headers: { 'Authorization': 'Bearer token' }
|
|
15
|
+
* }
|
|
8
16
|
*/
|
|
9
17
|
export interface AuthClientResponse {
|
|
18
|
+
/**
|
|
19
|
+
* HTTP headers to be set for authentication
|
|
20
|
+
*/
|
|
10
21
|
headers?: Record<string, any>;
|
|
22
|
+
/**
|
|
23
|
+
* Cookies to be set for authentication
|
|
24
|
+
*/
|
|
11
25
|
cookies?: Record<string, any>;
|
|
26
|
+
/**
|
|
27
|
+
* Session data to be set for authentication
|
|
28
|
+
*/
|
|
12
29
|
session?: Record<string, any>;
|
|
13
30
|
}
|
|
14
31
|
/**
|
|
15
32
|
* A set of properties a guard must implement to authenticate
|
|
16
|
-
* incoming HTTP requests
|
|
33
|
+
* incoming HTTP requests. This is the core contract that all
|
|
34
|
+
* authentication guards must follow.
|
|
35
|
+
*
|
|
36
|
+
* @template User - The type of the authenticated user
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* class SessionGuard implements GuardContract<User> {
|
|
40
|
+
* readonly driverName = 'session'
|
|
41
|
+
* user?: User
|
|
42
|
+
* isAuthenticated = false
|
|
43
|
+
* authenticationAttempted = false
|
|
44
|
+
*
|
|
45
|
+
* async authenticate(): Promise<User> {
|
|
46
|
+
* // Implementation
|
|
47
|
+
* }
|
|
48
|
+
* }
|
|
17
49
|
*/
|
|
18
50
|
export interface GuardContract<User> {
|
|
19
51
|
/**
|
|
20
|
-
* A unique name for the guard driver
|
|
52
|
+
* A unique name for the guard driver (e.g., 'session', 'api', 'basic')
|
|
21
53
|
*/
|
|
22
54
|
readonly driverName: string;
|
|
23
55
|
/**
|
|
24
|
-
* Reference to the currently authenticated user
|
|
56
|
+
* Reference to the currently authenticated user.
|
|
57
|
+
* Undefined when not authenticated.
|
|
25
58
|
*/
|
|
26
59
|
user?: User;
|
|
27
60
|
/**
|
|
28
|
-
* Returns logged-in user or throws an exception
|
|
61
|
+
* Returns logged-in user or throws an exception.
|
|
62
|
+
* Use this when you need to ensure the user is authenticated.
|
|
63
|
+
*
|
|
64
|
+
* @throws {RuntimeException} When user is not authenticated
|
|
29
65
|
*/
|
|
30
66
|
getUserOrFail(): User;
|
|
31
67
|
/**
|
|
32
|
-
* A boolean to know if the current request has
|
|
33
|
-
*
|
|
68
|
+
* A boolean to know if the current request has been authenticated.
|
|
69
|
+
* Returns false if authentication was not attempted.
|
|
34
70
|
*/
|
|
35
71
|
isAuthenticated: boolean;
|
|
36
72
|
/**
|
|
37
73
|
* Whether or not the authentication has been attempted
|
|
38
|
-
* during the current request
|
|
74
|
+
* during the current request. Used to differentiate between
|
|
75
|
+
* "not attempted" and "attempted but failed".
|
|
39
76
|
*/
|
|
40
77
|
authenticationAttempted: boolean;
|
|
41
78
|
/**
|
|
42
79
|
* Authenticates the current request and throws an
|
|
43
80
|
* exception if the request is not authenticated.
|
|
81
|
+
*
|
|
82
|
+
* @throws {E_UNAUTHORIZED_ACCESS} When authentication fails
|
|
44
83
|
*/
|
|
45
84
|
authenticate(): Promise<User>;
|
|
46
85
|
/**
|
|
47
86
|
* Check if the current request has been authenticated
|
|
48
|
-
* without throwing an exception.
|
|
87
|
+
* without throwing an exception. Use this for optional
|
|
88
|
+
* authentication scenarios.
|
|
49
89
|
*/
|
|
50
90
|
check(): Promise<boolean>;
|
|
51
91
|
/**
|
|
52
|
-
* The method is used to authenticate the user as client
|
|
53
|
-
* This method should return cookies, headers,
|
|
54
|
-
* session state.
|
|
92
|
+
* The method is used to authenticate the user as client
|
|
93
|
+
* during testing. This method should return cookies, headers,
|
|
94
|
+
* or session state that can be used to simulate authentication.
|
|
55
95
|
*
|
|
56
|
-
*
|
|
57
|
-
* to
|
|
96
|
+
* @param user - The user to authenticate as
|
|
97
|
+
* @param args - Additional arguments specific to the guard
|
|
58
98
|
*/
|
|
59
99
|
authenticateAsClient(user: User, ...args: any[]): Promise<AuthClientResponse>;
|
|
60
100
|
/**
|
|
61
|
-
*
|
|
62
|
-
* guard
|
|
101
|
+
* Symbol for inferring the events emitted by a specific guard.
|
|
102
|
+
* Used internally for type inference of guard events.
|
|
63
103
|
*/
|
|
64
104
|
[GUARD_KNOWN_EVENTS]: unknown;
|
|
65
105
|
}
|
|
66
106
|
/**
|
|
67
|
-
* The guard factory method is called
|
|
68
|
-
* of a guard during an HTTP request
|
|
107
|
+
* The guard factory method is called to create an instance
|
|
108
|
+
* of a guard during an HTTP request. Each guard factory receives
|
|
109
|
+
* the HTTP context and returns a configured guard instance.
|
|
110
|
+
*
|
|
111
|
+
* @param ctx - The HTTP context for the current request
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* const sessionGuardFactory: GuardFactory = (ctx) => {
|
|
115
|
+
* return new SessionGuard(ctx)
|
|
116
|
+
* }
|
|
69
117
|
*/
|
|
70
118
|
export type GuardFactory = (ctx: HttpContext) => GuardContract<unknown>;
|
|
71
119
|
/**
|
|
72
|
-
* Config provider for registering guards.
|
|
73
|
-
*
|
|
74
|
-
*
|
|
120
|
+
* Config provider for registering guards. Used for lazy loading
|
|
121
|
+
* of guard factories. The resolver function is called with the
|
|
122
|
+
* guard name and application instance to create the factory.
|
|
123
|
+
*
|
|
124
|
+
* @template Factory - The guard factory type
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* const guardProvider: GuardConfigProvider<SessionGuardFactory> = {
|
|
128
|
+
* resolver: async (name, app) => {
|
|
129
|
+
* const sessionConfig = await app.container.make('session.config')
|
|
130
|
+
* return (ctx) => new SessionGuard(ctx, sessionConfig)
|
|
131
|
+
* }
|
|
132
|
+
* }
|
|
75
133
|
*/
|
|
76
134
|
export type GuardConfigProvider<Factory extends GuardFactory> = {
|
|
135
|
+
/**
|
|
136
|
+
* Resolver function that creates the guard factory
|
|
137
|
+
*
|
|
138
|
+
* @param name - The name of the guard being resolved
|
|
139
|
+
* @param app - The application service instance
|
|
140
|
+
*/
|
|
77
141
|
resolver: (name: string, app: ApplicationService) => Promise<Factory>;
|
|
78
142
|
};
|
|
79
143
|
/**
|
|
80
144
|
* Authenticators are inferred inside the user application
|
|
81
|
-
* from the config file
|
|
145
|
+
* from the config file. This interface is augmented automatically
|
|
146
|
+
* when you define your auth configuration.
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* // After defining your auth config, this interface will be augmented:
|
|
150
|
+
* declare module '@adonisjs/auth/types' {
|
|
151
|
+
* interface Authenticators {
|
|
152
|
+
* web: SessionGuardFactory
|
|
153
|
+
* api: TokenGuardFactory
|
|
154
|
+
* }
|
|
155
|
+
* }
|
|
82
156
|
*/
|
|
83
157
|
export interface Authenticators {
|
|
84
158
|
}
|
|
85
159
|
/**
|
|
86
|
-
* Infer authenticators from the auth config
|
|
160
|
+
* Infer authenticators from the auth config. This utility type
|
|
161
|
+
* extracts the guards configuration from a config provider.
|
|
162
|
+
*
|
|
163
|
+
* @template Config - The auth config provider type
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* type MyAuthenticators = InferAuthenticators<typeof authConfig>
|
|
167
|
+
* // Results in: { web: SessionGuardFactory, api: TokenGuardFactory }
|
|
87
168
|
*/
|
|
88
169
|
export type InferAuthenticators<Config extends ConfigProvider<{
|
|
89
170
|
default: unknown;
|
|
90
171
|
guards: unknown;
|
|
91
172
|
}>> = Awaited<ReturnType<Config['resolver']>>['guards'];
|
|
92
173
|
/**
|
|
93
|
-
* Helper to convert union to intersection
|
|
174
|
+
* Helper to convert union to intersection. This utility type
|
|
175
|
+
* transforms a union of types into an intersection of types.
|
|
176
|
+
* Used internally for merging guard events.
|
|
177
|
+
*
|
|
178
|
+
* @template U - The union type to convert
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* type Union = { a: string } | { b: number }
|
|
182
|
+
* type Intersection = UnionToIntersection<Union>
|
|
183
|
+
* // Results in: { a: string } & { b: number }
|
|
94
184
|
*/
|
|
95
185
|
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
96
186
|
/**
|
|
97
|
-
* Infer events based upon the
|
|
187
|
+
* Infer events based upon the configured authenticators.
|
|
188
|
+
* This type extracts and merges all events from all configured
|
|
189
|
+
* guards into a single intersection type.
|
|
190
|
+
*
|
|
191
|
+
* @template KnownAuthenticators - Record of guard names to guard factories
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* type AuthEvents = InferAuthEvents<{
|
|
195
|
+
* web: SessionGuardFactory
|
|
196
|
+
* api: TokenGuardFactory
|
|
197
|
+
* }>
|
|
198
|
+
* // Results in intersection of all guard events
|
|
98
199
|
*/
|
|
99
200
|
export type InferAuthEvents<KnownAuthenticators extends Record<string, GuardFactory>> = UnionToIntersection<{
|
|
100
201
|
[K in keyof KnownAuthenticators]: ReturnType<KnownAuthenticators[K]>[typeof GUARD_KNOWN_EVENTS];
|
|
101
202
|
}[keyof KnownAuthenticators]>;
|
|
102
203
|
/**
|
|
103
204
|
* Auth service is a singleton instance of the AuthManager
|
|
104
|
-
* configured using the config stored within the user
|
|
105
|
-
*
|
|
205
|
+
* configured using the config stored within the user app.
|
|
206
|
+
* This interface is used for dependency injection and provides
|
|
207
|
+
* type-safe access to the auth manager.
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* // In a service or controller:
|
|
211
|
+
* class UserController {
|
|
212
|
+
* constructor(private auth: AuthService) {}
|
|
213
|
+
*
|
|
214
|
+
* async login() {
|
|
215
|
+
* const user = await this.auth.createAuthenticator(ctx).authenticate()
|
|
216
|
+
* }
|
|
217
|
+
* }
|
|
106
218
|
*/
|
|
107
219
|
export interface AuthService extends AuthManager<Authenticators extends Record<string, GuardFactory> ? Authenticators : never> {
|
|
108
220
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adonisjs/auth",
|
|
3
3
|
"description": "Official authentication provider for Adonis framework",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "10.0.0-next.0",
|
|
5
5
|
"engines": {
|
|
6
|
-
"node": ">=
|
|
6
|
+
"node": ">=24.0.0"
|
|
7
7
|
},
|
|
8
8
|
"main": "build/index.js",
|
|
9
9
|
"type": "module",
|
|
@@ -48,28 +48,27 @@
|
|
|
48
48
|
"prepublishOnly": "npm run build",
|
|
49
49
|
"release": "release-it",
|
|
50
50
|
"version": "npm run build",
|
|
51
|
-
"quick:test": "cross-env NODE_DEBUG=\"adonisjs:auth:*\" node --enable-source-maps --import
|
|
51
|
+
"quick:test": "cross-env NODE_DEBUG=\"adonisjs:auth:*\" node --enable-source-maps --import=@poppinss/ts-exec ./bin/test.js"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
|
-
"@adonisjs/assembler": "^
|
|
55
|
-
"@adonisjs/core": "^
|
|
56
|
-
"@adonisjs/eslint-config": "^
|
|
57
|
-
"@adonisjs/
|
|
58
|
-
"@adonisjs/
|
|
59
|
-
"@adonisjs/lucid": "^21.8.0",
|
|
54
|
+
"@adonisjs/assembler": "^8.0.0-next.9",
|
|
55
|
+
"@adonisjs/core": "^7.0.0-next.1",
|
|
56
|
+
"@adonisjs/eslint-config": "^3.0.0-next.1",
|
|
57
|
+
"@adonisjs/i18n": "^3.0.0-next.0",
|
|
58
|
+
"@adonisjs/lucid": "^22.0.0-next.0",
|
|
60
59
|
"@adonisjs/prettier-config": "^1.4.5",
|
|
61
|
-
"@adonisjs/session": "^
|
|
62
|
-
"@adonisjs/tsconfig": "^
|
|
60
|
+
"@adonisjs/session": "^8.0.0-next.0",
|
|
61
|
+
"@adonisjs/tsconfig": "^2.0.0-next.0",
|
|
63
62
|
"@japa/api-client": "^3.1.0",
|
|
64
63
|
"@japa/assert": "^4.1.1",
|
|
65
64
|
"@japa/browser-client": "^2.1.1",
|
|
66
65
|
"@japa/expect-type": "^2.0.3",
|
|
67
66
|
"@japa/file-system": "^2.3.2",
|
|
68
|
-
"@japa/plugin-adonisjs": "^
|
|
67
|
+
"@japa/plugin-adonisjs": "^5.0.0-next.0",
|
|
69
68
|
"@japa/runner": "^4.4.0",
|
|
70
69
|
"@japa/snapshot": "^2.0.9",
|
|
70
|
+
"@poppinss/ts-exec": "^1.4.1",
|
|
71
71
|
"@release-it/conventional-changelog": "^10.0.1",
|
|
72
|
-
"@swc/core": "1.13.5",
|
|
73
72
|
"@types/basic-auth": "^1.1.8",
|
|
74
73
|
"@types/luxon": "^3.7.1",
|
|
75
74
|
"@types/node": "^24.5.2",
|
|
@@ -94,24 +93,26 @@
|
|
|
94
93
|
"sqlite3": "^5.1.7",
|
|
95
94
|
"tedious": "^18.6.1",
|
|
96
95
|
"timekeeper": "^2.3.1",
|
|
97
|
-
"ts-node-maintained": "^10.9.6",
|
|
98
96
|
"tsup": "^8.5.0",
|
|
99
97
|
"typescript": "^5.9.2"
|
|
100
98
|
},
|
|
101
99
|
"dependencies": {
|
|
102
|
-
"@adonisjs/presets": "^
|
|
103
|
-
"@poppinss/utils": "^6.10.1",
|
|
100
|
+
"@adonisjs/presets": "^3.0.0-next.0",
|
|
104
101
|
"basic-auth": "^2.0.1"
|
|
105
102
|
},
|
|
106
103
|
"peerDependencies": {
|
|
107
|
-
"@adonisjs/
|
|
108
|
-
"@adonisjs/
|
|
109
|
-
"@adonisjs/
|
|
110
|
-
"@
|
|
111
|
-
"@japa/
|
|
112
|
-
"@japa/
|
|
104
|
+
"@adonisjs/assembler": "^8.0.0-next.9",
|
|
105
|
+
"@adonisjs/core": "^7.0.0-next.1",
|
|
106
|
+
"@adonisjs/lucid": "^22.0.0-next.0",
|
|
107
|
+
"@adonisjs/session": "^8.0.0-next.0",
|
|
108
|
+
"@japa/api-client": "^3.1.0",
|
|
109
|
+
"@japa/browser-client": "^2.1.1",
|
|
110
|
+
"@japa/plugin-adonisjs": "^5.0.0-next.0"
|
|
113
111
|
},
|
|
114
112
|
"peerDependenciesMeta": {
|
|
113
|
+
"@adonisjs/assembler": {
|
|
114
|
+
"optional": true
|
|
115
|
+
},
|
|
115
116
|
"@adonisjs/lucid": {
|
|
116
117
|
"optional": true
|
|
117
118
|
},
|