nextjs-secure 0.2.0 → 0.5.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/README.md +414 -8
- package/dist/auth.cjs +500 -6
- package/dist/auth.cjs.map +1 -1
- package/dist/auth.d.cts +180 -19
- package/dist/auth.d.ts +180 -19
- package/dist/auth.js +493 -6
- package/dist/auth.js.map +1 -1
- package/dist/headers.cjs +277 -7
- package/dist/headers.cjs.map +1 -1
- package/dist/headers.d.cts +162 -25
- package/dist/headers.d.ts +162 -25
- package/dist/headers.js +267 -6
- package/dist/headers.js.map +1 -1
- package/dist/index.cjs +2685 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +2634 -2
- package/dist/index.js.map +1 -1
- package/dist/path-BVbunPfR.d.cts +534 -0
- package/dist/path-BVbunPfR.d.ts +534 -0
- package/dist/validation.cjs +2031 -0
- package/dist/validation.cjs.map +1 -0
- package/dist/validation.d.cts +42 -0
- package/dist/validation.d.ts +42 -0
- package/dist/validation.js +1964 -0
- package/dist/validation.js.map +1 -0
- package/package.json +14 -1
package/dist/auth.d.cts
CHANGED
|
@@ -1,21 +1,182 @@
|
|
|
1
|
+
import { NextRequest } from 'next/server';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* User object attached to request after authentication
|
|
5
|
+
*/
|
|
6
|
+
interface AuthUser {
|
|
7
|
+
id: string;
|
|
8
|
+
email?: string;
|
|
9
|
+
name?: string;
|
|
10
|
+
roles?: string[];
|
|
11
|
+
permissions?: string[];
|
|
12
|
+
[key: string]: unknown;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* JWT payload structure
|
|
16
|
+
*/
|
|
17
|
+
interface JWTPayload {
|
|
18
|
+
sub?: string;
|
|
19
|
+
iss?: string;
|
|
20
|
+
aud?: string | string[];
|
|
21
|
+
exp?: number;
|
|
22
|
+
iat?: number;
|
|
23
|
+
nbf?: number;
|
|
24
|
+
jti?: string;
|
|
25
|
+
[key: string]: unknown;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* JWT verification options
|
|
29
|
+
*/
|
|
30
|
+
interface JWTConfig {
|
|
31
|
+
/** Secret key for HS256/HS384/HS512 */
|
|
32
|
+
secret?: string;
|
|
33
|
+
/** Public key for RS256/RS384/RS512/ES256/ES384/ES512 */
|
|
34
|
+
publicKey?: string;
|
|
35
|
+
/** JWKS endpoint URL */
|
|
36
|
+
jwksUri?: string;
|
|
37
|
+
/** Expected issuer */
|
|
38
|
+
issuer?: string | string[];
|
|
39
|
+
/** Expected audience */
|
|
40
|
+
audience?: string | string[];
|
|
41
|
+
/** Algorithms to accept */
|
|
42
|
+
algorithms?: string[];
|
|
43
|
+
/** Clock tolerance in seconds */
|
|
44
|
+
clockTolerance?: number;
|
|
45
|
+
/** Extract token from request (default: Authorization header) */
|
|
46
|
+
getToken?: (req: NextRequest) => string | null | Promise<string | null>;
|
|
47
|
+
/** Map JWT payload to user object */
|
|
48
|
+
mapUser?: (payload: JWTPayload) => AuthUser | Promise<AuthUser>;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* API Key authentication config
|
|
52
|
+
*/
|
|
53
|
+
interface APIKeyConfig {
|
|
54
|
+
/** Header name to check (default: x-api-key) */
|
|
55
|
+
headerName?: string;
|
|
56
|
+
/** Query parameter name (default: api_key) */
|
|
57
|
+
queryParam?: string;
|
|
58
|
+
/** Validate API key and return user */
|
|
59
|
+
validate: (key: string, req: NextRequest) => AuthUser | null | Promise<AuthUser | null>;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Session/Cookie authentication config
|
|
63
|
+
*/
|
|
64
|
+
interface SessionConfig {
|
|
65
|
+
/** Cookie name (default: session) */
|
|
66
|
+
cookieName?: string;
|
|
67
|
+
/** Validate session and return user */
|
|
68
|
+
validate: (sessionId: string, req: NextRequest) => AuthUser | null | Promise<AuthUser | null>;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Role-based access control config
|
|
72
|
+
*/
|
|
73
|
+
interface RBACConfig {
|
|
74
|
+
/** Required roles (user must have at least one) */
|
|
75
|
+
roles?: string[];
|
|
76
|
+
/** Required permissions (user must have all) */
|
|
77
|
+
permissions?: string[];
|
|
78
|
+
/** Get user roles from request */
|
|
79
|
+
getUserRoles?: (user: AuthUser) => string[];
|
|
80
|
+
/** Get user permissions from request */
|
|
81
|
+
getUserPermissions?: (user: AuthUser) => string[];
|
|
82
|
+
/** Custom authorization check */
|
|
83
|
+
authorize?: (user: AuthUser, req: NextRequest) => boolean | Promise<boolean>;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Combined auth configuration
|
|
87
|
+
*/
|
|
88
|
+
interface AuthConfig {
|
|
89
|
+
/** JWT authentication */
|
|
90
|
+
jwt?: JWTConfig;
|
|
91
|
+
/** API Key authentication */
|
|
92
|
+
apiKey?: APIKeyConfig;
|
|
93
|
+
/** Session/Cookie authentication */
|
|
94
|
+
session?: SessionConfig;
|
|
95
|
+
/** Role-based access control */
|
|
96
|
+
rbac?: RBACConfig;
|
|
97
|
+
/** Custom error response */
|
|
98
|
+
onError?: (req: NextRequest, error: AuthError) => Response | Promise<Response>;
|
|
99
|
+
/** Called on successful auth */
|
|
100
|
+
onSuccess?: (req: NextRequest, user: AuthUser) => void | Promise<void>;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Auth error types
|
|
104
|
+
*/
|
|
105
|
+
type AuthErrorCode = 'missing_token' | 'invalid_token' | 'expired_token' | 'invalid_signature' | 'missing_api_key' | 'invalid_api_key' | 'missing_session' | 'invalid_session' | 'insufficient_roles' | 'insufficient_permissions' | 'unauthorized';
|
|
106
|
+
interface AuthError {
|
|
107
|
+
code: AuthErrorCode;
|
|
108
|
+
message: string;
|
|
109
|
+
status: number;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Extended request with auth context
|
|
113
|
+
*/
|
|
114
|
+
interface AuthenticatedRequest extends NextRequest {
|
|
115
|
+
auth: {
|
|
116
|
+
user: AuthUser;
|
|
117
|
+
token?: string;
|
|
118
|
+
method: 'jwt' | 'apiKey' | 'session';
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
type RouteHandler = (req: NextRequest) => Response | Promise<Response>;
|
|
123
|
+
type AuthenticatedHandler = (req: NextRequest, ctx: {
|
|
124
|
+
user: AuthUser;
|
|
125
|
+
token?: string;
|
|
126
|
+
}) => Response | Promise<Response>;
|
|
127
|
+
/**
|
|
128
|
+
* JWT Authentication middleware
|
|
129
|
+
*/
|
|
130
|
+
declare function withJWT(handler: AuthenticatedHandler, config: JWTConfig): RouteHandler;
|
|
131
|
+
/**
|
|
132
|
+
* API Key Authentication middleware
|
|
133
|
+
*/
|
|
134
|
+
declare function withAPIKey(handler: AuthenticatedHandler, config: APIKeyConfig): RouteHandler;
|
|
1
135
|
/**
|
|
2
|
-
* Authentication
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
*
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
136
|
+
* Session/Cookie Authentication middleware
|
|
137
|
+
*/
|
|
138
|
+
declare function withSession(handler: AuthenticatedHandler, config: SessionConfig): RouteHandler;
|
|
139
|
+
/**
|
|
140
|
+
* Role-based access control middleware
|
|
141
|
+
* Must be used after authentication middleware
|
|
142
|
+
*/
|
|
143
|
+
declare function withRoles(handler: AuthenticatedHandler, config: RBACConfig): (req: NextRequest, ctx: {
|
|
144
|
+
user: AuthUser;
|
|
145
|
+
token?: string;
|
|
146
|
+
}) => Promise<Response>;
|
|
147
|
+
/**
|
|
148
|
+
* Combined auth middleware with multiple strategies
|
|
149
|
+
*/
|
|
150
|
+
declare function withAuth(handler: AuthenticatedHandler, config: AuthConfig): RouteHandler;
|
|
151
|
+
/**
|
|
152
|
+
* Optional auth - doesn't fail if no auth present
|
|
153
|
+
*/
|
|
154
|
+
declare function withOptionalAuth(handler: (req: NextRequest, ctx: {
|
|
155
|
+
user: AuthUser | null;
|
|
156
|
+
token?: string;
|
|
157
|
+
}) => Response | Promise<Response>, config: Omit<AuthConfig, 'rbac'>): RouteHandler;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Parse JWT without verification (for header inspection)
|
|
161
|
+
*/
|
|
162
|
+
declare function decodeJWT(token: string): {
|
|
163
|
+
header: Record<string, unknown>;
|
|
164
|
+
payload: JWTPayload;
|
|
165
|
+
signature: Uint8Array;
|
|
166
|
+
} | null;
|
|
167
|
+
/**
|
|
168
|
+
* Verify and decode JWT
|
|
169
|
+
*/
|
|
170
|
+
declare function verifyJWT(token: string, config: JWTConfig): Promise<{
|
|
171
|
+
payload: JWTPayload;
|
|
172
|
+
error: null;
|
|
173
|
+
} | {
|
|
174
|
+
payload: null;
|
|
175
|
+
error: AuthError;
|
|
176
|
+
}>;
|
|
177
|
+
/**
|
|
178
|
+
* Extract token from Authorization header
|
|
179
|
+
*/
|
|
180
|
+
declare function extractBearerToken(authHeader: string | null): string | null;
|
|
20
181
|
|
|
21
|
-
export {
|
|
182
|
+
export { type APIKeyConfig, type AuthConfig, type AuthError, type AuthErrorCode, type AuthUser, type AuthenticatedRequest, type JWTConfig, type JWTPayload, type RBACConfig, type SessionConfig, decodeJWT, extractBearerToken, verifyJWT, withAPIKey, withAuth, withJWT, withOptionalAuth, withRoles, withSession };
|
package/dist/auth.d.ts
CHANGED
|
@@ -1,21 +1,182 @@
|
|
|
1
|
+
import { NextRequest } from 'next/server';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* User object attached to request after authentication
|
|
5
|
+
*/
|
|
6
|
+
interface AuthUser {
|
|
7
|
+
id: string;
|
|
8
|
+
email?: string;
|
|
9
|
+
name?: string;
|
|
10
|
+
roles?: string[];
|
|
11
|
+
permissions?: string[];
|
|
12
|
+
[key: string]: unknown;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* JWT payload structure
|
|
16
|
+
*/
|
|
17
|
+
interface JWTPayload {
|
|
18
|
+
sub?: string;
|
|
19
|
+
iss?: string;
|
|
20
|
+
aud?: string | string[];
|
|
21
|
+
exp?: number;
|
|
22
|
+
iat?: number;
|
|
23
|
+
nbf?: number;
|
|
24
|
+
jti?: string;
|
|
25
|
+
[key: string]: unknown;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* JWT verification options
|
|
29
|
+
*/
|
|
30
|
+
interface JWTConfig {
|
|
31
|
+
/** Secret key for HS256/HS384/HS512 */
|
|
32
|
+
secret?: string;
|
|
33
|
+
/** Public key for RS256/RS384/RS512/ES256/ES384/ES512 */
|
|
34
|
+
publicKey?: string;
|
|
35
|
+
/** JWKS endpoint URL */
|
|
36
|
+
jwksUri?: string;
|
|
37
|
+
/** Expected issuer */
|
|
38
|
+
issuer?: string | string[];
|
|
39
|
+
/** Expected audience */
|
|
40
|
+
audience?: string | string[];
|
|
41
|
+
/** Algorithms to accept */
|
|
42
|
+
algorithms?: string[];
|
|
43
|
+
/** Clock tolerance in seconds */
|
|
44
|
+
clockTolerance?: number;
|
|
45
|
+
/** Extract token from request (default: Authorization header) */
|
|
46
|
+
getToken?: (req: NextRequest) => string | null | Promise<string | null>;
|
|
47
|
+
/** Map JWT payload to user object */
|
|
48
|
+
mapUser?: (payload: JWTPayload) => AuthUser | Promise<AuthUser>;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* API Key authentication config
|
|
52
|
+
*/
|
|
53
|
+
interface APIKeyConfig {
|
|
54
|
+
/** Header name to check (default: x-api-key) */
|
|
55
|
+
headerName?: string;
|
|
56
|
+
/** Query parameter name (default: api_key) */
|
|
57
|
+
queryParam?: string;
|
|
58
|
+
/** Validate API key and return user */
|
|
59
|
+
validate: (key: string, req: NextRequest) => AuthUser | null | Promise<AuthUser | null>;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Session/Cookie authentication config
|
|
63
|
+
*/
|
|
64
|
+
interface SessionConfig {
|
|
65
|
+
/** Cookie name (default: session) */
|
|
66
|
+
cookieName?: string;
|
|
67
|
+
/** Validate session and return user */
|
|
68
|
+
validate: (sessionId: string, req: NextRequest) => AuthUser | null | Promise<AuthUser | null>;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Role-based access control config
|
|
72
|
+
*/
|
|
73
|
+
interface RBACConfig {
|
|
74
|
+
/** Required roles (user must have at least one) */
|
|
75
|
+
roles?: string[];
|
|
76
|
+
/** Required permissions (user must have all) */
|
|
77
|
+
permissions?: string[];
|
|
78
|
+
/** Get user roles from request */
|
|
79
|
+
getUserRoles?: (user: AuthUser) => string[];
|
|
80
|
+
/** Get user permissions from request */
|
|
81
|
+
getUserPermissions?: (user: AuthUser) => string[];
|
|
82
|
+
/** Custom authorization check */
|
|
83
|
+
authorize?: (user: AuthUser, req: NextRequest) => boolean | Promise<boolean>;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Combined auth configuration
|
|
87
|
+
*/
|
|
88
|
+
interface AuthConfig {
|
|
89
|
+
/** JWT authentication */
|
|
90
|
+
jwt?: JWTConfig;
|
|
91
|
+
/** API Key authentication */
|
|
92
|
+
apiKey?: APIKeyConfig;
|
|
93
|
+
/** Session/Cookie authentication */
|
|
94
|
+
session?: SessionConfig;
|
|
95
|
+
/** Role-based access control */
|
|
96
|
+
rbac?: RBACConfig;
|
|
97
|
+
/** Custom error response */
|
|
98
|
+
onError?: (req: NextRequest, error: AuthError) => Response | Promise<Response>;
|
|
99
|
+
/** Called on successful auth */
|
|
100
|
+
onSuccess?: (req: NextRequest, user: AuthUser) => void | Promise<void>;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Auth error types
|
|
104
|
+
*/
|
|
105
|
+
type AuthErrorCode = 'missing_token' | 'invalid_token' | 'expired_token' | 'invalid_signature' | 'missing_api_key' | 'invalid_api_key' | 'missing_session' | 'invalid_session' | 'insufficient_roles' | 'insufficient_permissions' | 'unauthorized';
|
|
106
|
+
interface AuthError {
|
|
107
|
+
code: AuthErrorCode;
|
|
108
|
+
message: string;
|
|
109
|
+
status: number;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Extended request with auth context
|
|
113
|
+
*/
|
|
114
|
+
interface AuthenticatedRequest extends NextRequest {
|
|
115
|
+
auth: {
|
|
116
|
+
user: AuthUser;
|
|
117
|
+
token?: string;
|
|
118
|
+
method: 'jwt' | 'apiKey' | 'session';
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
type RouteHandler = (req: NextRequest) => Response | Promise<Response>;
|
|
123
|
+
type AuthenticatedHandler = (req: NextRequest, ctx: {
|
|
124
|
+
user: AuthUser;
|
|
125
|
+
token?: string;
|
|
126
|
+
}) => Response | Promise<Response>;
|
|
127
|
+
/**
|
|
128
|
+
* JWT Authentication middleware
|
|
129
|
+
*/
|
|
130
|
+
declare function withJWT(handler: AuthenticatedHandler, config: JWTConfig): RouteHandler;
|
|
131
|
+
/**
|
|
132
|
+
* API Key Authentication middleware
|
|
133
|
+
*/
|
|
134
|
+
declare function withAPIKey(handler: AuthenticatedHandler, config: APIKeyConfig): RouteHandler;
|
|
1
135
|
/**
|
|
2
|
-
* Authentication
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
*
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
136
|
+
* Session/Cookie Authentication middleware
|
|
137
|
+
*/
|
|
138
|
+
declare function withSession(handler: AuthenticatedHandler, config: SessionConfig): RouteHandler;
|
|
139
|
+
/**
|
|
140
|
+
* Role-based access control middleware
|
|
141
|
+
* Must be used after authentication middleware
|
|
142
|
+
*/
|
|
143
|
+
declare function withRoles(handler: AuthenticatedHandler, config: RBACConfig): (req: NextRequest, ctx: {
|
|
144
|
+
user: AuthUser;
|
|
145
|
+
token?: string;
|
|
146
|
+
}) => Promise<Response>;
|
|
147
|
+
/**
|
|
148
|
+
* Combined auth middleware with multiple strategies
|
|
149
|
+
*/
|
|
150
|
+
declare function withAuth(handler: AuthenticatedHandler, config: AuthConfig): RouteHandler;
|
|
151
|
+
/**
|
|
152
|
+
* Optional auth - doesn't fail if no auth present
|
|
153
|
+
*/
|
|
154
|
+
declare function withOptionalAuth(handler: (req: NextRequest, ctx: {
|
|
155
|
+
user: AuthUser | null;
|
|
156
|
+
token?: string;
|
|
157
|
+
}) => Response | Promise<Response>, config: Omit<AuthConfig, 'rbac'>): RouteHandler;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Parse JWT without verification (for header inspection)
|
|
161
|
+
*/
|
|
162
|
+
declare function decodeJWT(token: string): {
|
|
163
|
+
header: Record<string, unknown>;
|
|
164
|
+
payload: JWTPayload;
|
|
165
|
+
signature: Uint8Array;
|
|
166
|
+
} | null;
|
|
167
|
+
/**
|
|
168
|
+
* Verify and decode JWT
|
|
169
|
+
*/
|
|
170
|
+
declare function verifyJWT(token: string, config: JWTConfig): Promise<{
|
|
171
|
+
payload: JWTPayload;
|
|
172
|
+
error: null;
|
|
173
|
+
} | {
|
|
174
|
+
payload: null;
|
|
175
|
+
error: AuthError;
|
|
176
|
+
}>;
|
|
177
|
+
/**
|
|
178
|
+
* Extract token from Authorization header
|
|
179
|
+
*/
|
|
180
|
+
declare function extractBearerToken(authHeader: string | null): string | null;
|
|
20
181
|
|
|
21
|
-
export {
|
|
182
|
+
export { type APIKeyConfig, type AuthConfig, type AuthError, type AuthErrorCode, type AuthUser, type AuthenticatedRequest, type JWTConfig, type JWTPayload, type RBACConfig, type SessionConfig, decodeJWT, extractBearerToken, verifyJWT, withAPIKey, withAuth, withJWT, withOptionalAuth, withRoles, withSession };
|