@fnd-platform/cognito-auth 1.0.0-alpha.1
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/LICENSE +21 -0
- package/README.md +323 -0
- package/lib/authorizer/handler.d.ts +33 -0
- package/lib/authorizer/handler.d.ts.map +1 -0
- package/lib/authorizer/handler.js +106 -0
- package/lib/authorizer/handler.js.map +1 -0
- package/lib/authorizer/index.d.ts +7 -0
- package/lib/authorizer/index.d.ts.map +1 -0
- package/lib/authorizer/index.js +16 -0
- package/lib/authorizer/index.js.map +1 -0
- package/lib/client/auth-client.d.ts +131 -0
- package/lib/client/auth-client.d.ts.map +1 -0
- package/lib/client/auth-client.js +270 -0
- package/lib/client/auth-client.js.map +1 -0
- package/lib/client/errors.d.ts +67 -0
- package/lib/client/errors.d.ts.map +1 -0
- package/lib/client/errors.js +90 -0
- package/lib/client/errors.js.map +1 -0
- package/lib/client/index.d.ts +8 -0
- package/lib/client/index.d.ts.map +1 -0
- package/lib/client/index.js +29 -0
- package/lib/client/index.js.map +1 -0
- package/lib/cognito-construct.d.ts +113 -0
- package/lib/cognito-construct.d.ts.map +1 -0
- package/lib/cognito-construct.js +211 -0
- package/lib/cognito-construct.js.map +1 -0
- package/lib/index.d.ts +30 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +59 -0
- package/lib/index.js.map +1 -0
- package/lib/jwt.d.ts +89 -0
- package/lib/jwt.d.ts.map +1 -0
- package/lib/jwt.js +117 -0
- package/lib/jwt.js.map +1 -0
- package/lib/middleware/auth.d.ts +59 -0
- package/lib/middleware/auth.d.ts.map +1 -0
- package/lib/middleware/auth.js +148 -0
- package/lib/middleware/auth.js.map +1 -0
- package/lib/middleware/index.d.ts +12 -0
- package/lib/middleware/index.d.ts.map +1 -0
- package/lib/middleware/index.js +16 -0
- package/lib/middleware/index.js.map +1 -0
- package/lib/remix/admin.server.d.ts +105 -0
- package/lib/remix/admin.server.d.ts.map +1 -0
- package/lib/remix/admin.server.js +146 -0
- package/lib/remix/admin.server.js.map +1 -0
- package/lib/remix/index.d.ts +17 -0
- package/lib/remix/index.d.ts.map +1 -0
- package/lib/remix/index.js +95 -0
- package/lib/remix/index.js.map +1 -0
- package/lib/remix/session.server.d.ts +177 -0
- package/lib/remix/session.server.d.ts.map +1 -0
- package/lib/remix/session.server.js +287 -0
- package/lib/remix/session.server.js.map +1 -0
- package/lib/types.d.ts +161 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +8 -0
- package/lib/types.js.map +1 -0
- package/lib/utils/index.d.ts +12 -0
- package/lib/utils/index.d.ts.map +1 -0
- package/lib/utils/index.js +22 -0
- package/lib/utils/index.js.map +1 -0
- package/lib/utils/token-refresh.d.ts +62 -0
- package/lib/utils/token-refresh.d.ts.map +1 -0
- package/lib/utils/token-refresh.js +84 -0
- package/lib/utils/token-refresh.js.map +1 -0
- package/package.json +70 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.FndCognitoAuth = exports.VALID_STAGES = void 0;
|
|
37
|
+
exports.validateStage = validateStage;
|
|
38
|
+
const constructs_1 = require("constructs");
|
|
39
|
+
const cognito = __importStar(require("aws-cdk-lib/aws-cognito"));
|
|
40
|
+
const aws_cdk_lib_1 = require("aws-cdk-lib");
|
|
41
|
+
/**
|
|
42
|
+
* Valid deployment stages for fnd-platform applications.
|
|
43
|
+
*/
|
|
44
|
+
exports.VALID_STAGES = ['dev', 'staging', 'prod'];
|
|
45
|
+
/**
|
|
46
|
+
* Validates that the provided stage is a valid deployment stage.
|
|
47
|
+
* @param stage - The stage to validate
|
|
48
|
+
* @throws Error if stage is not valid
|
|
49
|
+
*/
|
|
50
|
+
function validateStage(stage) {
|
|
51
|
+
if (!exports.VALID_STAGES.includes(stage)) {
|
|
52
|
+
throw new Error(`Invalid stage "${stage}". Valid stages are: ${exports.VALID_STAGES.join(', ')}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* CDK construct for AWS Cognito User Pool with app clients and user groups.
|
|
57
|
+
*
|
|
58
|
+
* Creates a fully configured User Pool with:
|
|
59
|
+
* - Secure password policy
|
|
60
|
+
* - Email verification
|
|
61
|
+
* - MFA support (optional, enabled by default in prod)
|
|
62
|
+
* - User groups (admin, editor, viewer)
|
|
63
|
+
* - Web client for frontend OAuth flows
|
|
64
|
+
* - Admin client for CMS direct authentication
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* const auth = new FndCognitoAuth(this, 'Auth', {
|
|
69
|
+
* appName: 'my-app',
|
|
70
|
+
* stage: 'dev',
|
|
71
|
+
* callbackUrls: ['http://localhost:3000'],
|
|
72
|
+
* });
|
|
73
|
+
*
|
|
74
|
+
* // Access the User Pool ID
|
|
75
|
+
* console.log(auth.userPoolId);
|
|
76
|
+
*
|
|
77
|
+
* // Use the web client for frontend
|
|
78
|
+
* console.log(auth.webClientId);
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
class FndCognitoAuth extends constructs_1.Construct {
|
|
82
|
+
/**
|
|
83
|
+
* The Cognito User Pool.
|
|
84
|
+
*/
|
|
85
|
+
userPool;
|
|
86
|
+
/**
|
|
87
|
+
* The web client for frontend OAuth authentication.
|
|
88
|
+
*/
|
|
89
|
+
webClient;
|
|
90
|
+
/**
|
|
91
|
+
* The admin client for CMS direct authentication.
|
|
92
|
+
*/
|
|
93
|
+
adminClient;
|
|
94
|
+
/**
|
|
95
|
+
* The User Pool ID.
|
|
96
|
+
*/
|
|
97
|
+
userPoolId;
|
|
98
|
+
/**
|
|
99
|
+
* The web client ID.
|
|
100
|
+
*/
|
|
101
|
+
webClientId;
|
|
102
|
+
/**
|
|
103
|
+
* The admin client ID.
|
|
104
|
+
*/
|
|
105
|
+
adminClientId;
|
|
106
|
+
/**
|
|
107
|
+
* The deployment stage.
|
|
108
|
+
*/
|
|
109
|
+
stage;
|
|
110
|
+
constructor(scope, id, props) {
|
|
111
|
+
super(scope, id);
|
|
112
|
+
// Validate and store stage
|
|
113
|
+
validateStage(props.stage);
|
|
114
|
+
this.stage = props.stage;
|
|
115
|
+
const isProd = this.stage === 'prod';
|
|
116
|
+
// Determine MFA setting
|
|
117
|
+
const mfaEnabled = props.mfaEnabled ?? isProd;
|
|
118
|
+
// Determine removal policy
|
|
119
|
+
const removalPolicy = props.removalPolicy ?? (isProd ? aws_cdk_lib_1.RemovalPolicy.RETAIN : aws_cdk_lib_1.RemovalPolicy.DESTROY);
|
|
120
|
+
// Create User Pool
|
|
121
|
+
this.userPool = new cognito.UserPool(this, 'UserPool', {
|
|
122
|
+
userPoolName: `${props.appName}-${this.stage}`,
|
|
123
|
+
selfSignUpEnabled: true,
|
|
124
|
+
signInAliases: { email: true },
|
|
125
|
+
autoVerify: { email: true },
|
|
126
|
+
passwordPolicy: {
|
|
127
|
+
minLength: 8,
|
|
128
|
+
requireLowercase: true,
|
|
129
|
+
requireUppercase: true,
|
|
130
|
+
requireDigits: true,
|
|
131
|
+
requireSymbols: isProd,
|
|
132
|
+
},
|
|
133
|
+
mfa: mfaEnabled ? cognito.Mfa.OPTIONAL : cognito.Mfa.OFF,
|
|
134
|
+
mfaSecondFactor: mfaEnabled
|
|
135
|
+
? {
|
|
136
|
+
sms: false,
|
|
137
|
+
otp: true,
|
|
138
|
+
}
|
|
139
|
+
: undefined,
|
|
140
|
+
accountRecovery: cognito.AccountRecovery.EMAIL_ONLY,
|
|
141
|
+
removalPolicy: removalPolicy,
|
|
142
|
+
standardAttributes: {
|
|
143
|
+
email: { required: true, mutable: true },
|
|
144
|
+
fullname: { required: false, mutable: true },
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
// Create user groups
|
|
148
|
+
this.createUserGroups();
|
|
149
|
+
// Create web client (for frontend with OAuth)
|
|
150
|
+
this.webClient = this.userPool.addClient('WebClient', {
|
|
151
|
+
userPoolClientName: `${props.appName}-web`,
|
|
152
|
+
authFlows: {
|
|
153
|
+
userPassword: true,
|
|
154
|
+
userSrp: true,
|
|
155
|
+
},
|
|
156
|
+
oAuth: {
|
|
157
|
+
flows: { authorizationCodeGrant: true },
|
|
158
|
+
callbackUrls: props.callbackUrls,
|
|
159
|
+
logoutUrls: props.callbackUrls,
|
|
160
|
+
scopes: [cognito.OAuthScope.EMAIL, cognito.OAuthScope.OPENID, cognito.OAuthScope.PROFILE],
|
|
161
|
+
},
|
|
162
|
+
accessTokenValidity: aws_cdk_lib_1.Duration.hours(1),
|
|
163
|
+
idTokenValidity: aws_cdk_lib_1.Duration.hours(1),
|
|
164
|
+
refreshTokenValidity: aws_cdk_lib_1.Duration.days(30),
|
|
165
|
+
});
|
|
166
|
+
// Create admin client (for CMS, no OAuth)
|
|
167
|
+
this.adminClient = this.userPool.addClient('AdminClient', {
|
|
168
|
+
userPoolClientName: `${props.appName}-admin`,
|
|
169
|
+
authFlows: {
|
|
170
|
+
userPassword: true,
|
|
171
|
+
userSrp: true,
|
|
172
|
+
adminUserPassword: true,
|
|
173
|
+
},
|
|
174
|
+
accessTokenValidity: aws_cdk_lib_1.Duration.hours(1),
|
|
175
|
+
idTokenValidity: aws_cdk_lib_1.Duration.hours(1),
|
|
176
|
+
refreshTokenValidity: aws_cdk_lib_1.Duration.days(7),
|
|
177
|
+
});
|
|
178
|
+
// Store IDs for easy access
|
|
179
|
+
this.userPoolId = this.userPool.userPoolId;
|
|
180
|
+
this.webClientId = this.webClient.userPoolClientId;
|
|
181
|
+
this.adminClientId = this.adminClient.userPoolClientId;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Creates the standard user groups for role-based access control.
|
|
185
|
+
* - admin: Full access to CMS and API
|
|
186
|
+
* - editor: Can create/edit content, no admin settings
|
|
187
|
+
* - viewer: Read-only access
|
|
188
|
+
*/
|
|
189
|
+
createUserGroups() {
|
|
190
|
+
new cognito.CfnUserPoolGroup(this, 'AdminGroup', {
|
|
191
|
+
userPoolId: this.userPool.userPoolId,
|
|
192
|
+
groupName: 'admin',
|
|
193
|
+
description: 'Administrators with full access',
|
|
194
|
+
precedence: 1,
|
|
195
|
+
});
|
|
196
|
+
new cognito.CfnUserPoolGroup(this, 'EditorGroup', {
|
|
197
|
+
userPoolId: this.userPool.userPoolId,
|
|
198
|
+
groupName: 'editor',
|
|
199
|
+
description: 'Content editors',
|
|
200
|
+
precedence: 2,
|
|
201
|
+
});
|
|
202
|
+
new cognito.CfnUserPoolGroup(this, 'ViewerGroup', {
|
|
203
|
+
userPoolId: this.userPool.userPoolId,
|
|
204
|
+
groupName: 'viewer',
|
|
205
|
+
description: 'Read-only users',
|
|
206
|
+
precedence: 3,
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
exports.FndCognitoAuth = FndCognitoAuth;
|
|
211
|
+
//# sourceMappingURL=cognito-construct.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cognito-construct.js","sourceRoot":"","sources":["../src/cognito-construct.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmBA,sCAIC;AAvBD,2CAAuC;AACvC,iEAAmD;AACnD,6CAAsD;AAEtD;;GAEG;AACU,QAAA,YAAY,GAAG,CAAC,KAAK,EAAE,SAAS,EAAE,MAAM,CAAU,CAAC;AAOhE;;;;GAIG;AACH,SAAgB,aAAa,CAAC,KAAa;IACzC,IAAI,CAAC,oBAAY,CAAC,QAAQ,CAAC,KAAc,CAAC,EAAE,CAAC;QAC3C,MAAM,IAAI,KAAK,CAAC,kBAAkB,KAAK,wBAAwB,oBAAY,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC5F,CAAC;AACH,CAAC;AAsCD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAa,cAAe,SAAQ,sBAAS;IAC3C;;OAEG;IACa,QAAQ,CAAmB;IAE3C;;OAEG;IACa,SAAS,CAAyB;IAElD;;OAEG;IACa,WAAW,CAAyB;IAEpD;;OAEG;IACa,UAAU,CAAS;IAEnC;;OAEG;IACa,WAAW,CAAS;IAEpC;;OAEG;IACa,aAAa,CAAS;IAEtC;;OAEG;IACa,KAA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|
|
4
|
+
* AWS Cognito authentication constructs and middleware for fnd-platform applications.
|
|
5
|
+
*
|
|
6
|
+
* Provides:
|
|
7
|
+
* - Fully configured Cognito User Pool CDK construct
|
|
8
|
+
* - JWT token validation middleware
|
|
9
|
+
* - Lambda authorizer for API Gateway
|
|
10
|
+
* - Token refresh utilities
|
|
11
|
+
*
|
|
12
|
+
* @packageDocumentation
|
|
13
|
+
*/
|
|
14
|
+
export { FndCognitoAuth } from './cognito-construct.js';
|
|
15
|
+
export type { FndCognitoAuthProps, Stage } from './cognito-construct.js';
|
|
16
|
+
export { validateStage, VALID_STAGES } from './cognito-construct.js';
|
|
17
|
+
export type { CognitoAccessTokenPayload, CognitoIdTokenPayload, CognitoAuthOptions, JwtVerifierConfig, TokenVerificationResult, } from './types.js';
|
|
18
|
+
export { verifyToken, verifyAndExtract, getVerifier, clearVerifierCache } from './jwt.js';
|
|
19
|
+
export { withCognitoAuth } from './middleware/auth.js';
|
|
20
|
+
export type { CognitoAuthenticatedEvent, Middleware as CognitoMiddleware, MiddlewareHandler as CognitoMiddlewareHandler, } from './middleware/auth.js';
|
|
21
|
+
export { handler as authorizerHandler } from './authorizer/handler.js';
|
|
22
|
+
export { refreshAccessToken, clearClientCache } from './utils/token-refresh.js';
|
|
23
|
+
export type { TokenRefreshConfig, RefreshResult } from './utils/token-refresh.js';
|
|
24
|
+
export { FndAuthClient, clearClientCache as clearAuthClientCache } from './client/auth-client.js';
|
|
25
|
+
export { AuthError } from './client/errors.js';
|
|
26
|
+
export type { AuthErrorCode } from './client/errors.js';
|
|
27
|
+
export { createSessionStorage, getSession, createUserSession, requireAuth, getOptionalUser, getUserSession, logout, } from './remix/session.server.js';
|
|
28
|
+
export { requireAdmin, requireRole, hasRole, hasAnyRole } from './remix/admin.server.js';
|
|
29
|
+
export type { AuthClientConfig, AuthTokens, SignUpResult, SessionData, SessionUser, } from './types.js';
|
|
30
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
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YAAY,EACV,gBAAgB,EAChB,UAAU,EACV,YAAY,EACZ,WAAW,EACX,WAAW,GACZ,MAAM,YAAY,CAAC"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @fnd-platform/cognito-auth
|
|
4
|
+
*
|
|
5
|
+
* AWS Cognito authentication constructs and middleware for fnd-platform applications.
|
|
6
|
+
*
|
|
7
|
+
* Provides:
|
|
8
|
+
* - Fully configured Cognito User Pool CDK construct
|
|
9
|
+
* - JWT token validation middleware
|
|
10
|
+
* - Lambda authorizer for API Gateway
|
|
11
|
+
* - Token refresh utilities
|
|
12
|
+
*
|
|
13
|
+
* @packageDocumentation
|
|
14
|
+
*/
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.hasAnyRole = exports.hasRole = exports.requireRole = exports.requireAdmin = exports.logout = exports.getUserSession = exports.getOptionalUser = exports.requireAuth = exports.createUserSession = exports.getSession = exports.createSessionStorage = exports.AuthError = exports.clearAuthClientCache = exports.FndAuthClient = exports.clearClientCache = exports.refreshAccessToken = exports.authorizerHandler = exports.withCognitoAuth = exports.clearVerifierCache = exports.getVerifier = exports.verifyAndExtract = exports.verifyToken = exports.VALID_STAGES = exports.validateStage = exports.FndCognitoAuth = void 0;
|
|
17
|
+
// ===== CDK Constructs =====
|
|
18
|
+
var cognito_construct_js_1 = require("./cognito-construct.js");
|
|
19
|
+
Object.defineProperty(exports, "FndCognitoAuth", { enumerable: true, get: function () { return cognito_construct_js_1.FndCognitoAuth; } });
|
|
20
|
+
var cognito_construct_js_2 = require("./cognito-construct.js");
|
|
21
|
+
Object.defineProperty(exports, "validateStage", { enumerable: true, get: function () { return cognito_construct_js_2.validateStage; } });
|
|
22
|
+
Object.defineProperty(exports, "VALID_STAGES", { enumerable: true, get: function () { return cognito_construct_js_2.VALID_STAGES; } });
|
|
23
|
+
// ===== JWT Utilities =====
|
|
24
|
+
var jwt_js_1 = require("./jwt.js");
|
|
25
|
+
Object.defineProperty(exports, "verifyToken", { enumerable: true, get: function () { return jwt_js_1.verifyToken; } });
|
|
26
|
+
Object.defineProperty(exports, "verifyAndExtract", { enumerable: true, get: function () { return jwt_js_1.verifyAndExtract; } });
|
|
27
|
+
Object.defineProperty(exports, "getVerifier", { enumerable: true, get: function () { return jwt_js_1.getVerifier; } });
|
|
28
|
+
Object.defineProperty(exports, "clearVerifierCache", { enumerable: true, get: function () { return jwt_js_1.clearVerifierCache; } });
|
|
29
|
+
// ===== Middleware =====
|
|
30
|
+
var auth_js_1 = require("./middleware/auth.js");
|
|
31
|
+
Object.defineProperty(exports, "withCognitoAuth", { enumerable: true, get: function () { return auth_js_1.withCognitoAuth; } });
|
|
32
|
+
// ===== Lambda Authorizer =====
|
|
33
|
+
var handler_js_1 = require("./authorizer/handler.js");
|
|
34
|
+
Object.defineProperty(exports, "authorizerHandler", { enumerable: true, get: function () { return handler_js_1.handler; } });
|
|
35
|
+
// ===== Token Utilities =====
|
|
36
|
+
var token_refresh_js_1 = require("./utils/token-refresh.js");
|
|
37
|
+
Object.defineProperty(exports, "refreshAccessToken", { enumerable: true, get: function () { return token_refresh_js_1.refreshAccessToken; } });
|
|
38
|
+
Object.defineProperty(exports, "clearClientCache", { enumerable: true, get: function () { return token_refresh_js_1.clearClientCache; } });
|
|
39
|
+
// ===== Auth Client =====
|
|
40
|
+
var auth_client_js_1 = require("./client/auth-client.js");
|
|
41
|
+
Object.defineProperty(exports, "FndAuthClient", { enumerable: true, get: function () { return auth_client_js_1.FndAuthClient; } });
|
|
42
|
+
Object.defineProperty(exports, "clearAuthClientCache", { enumerable: true, get: function () { return auth_client_js_1.clearClientCache; } });
|
|
43
|
+
var errors_js_1 = require("./client/errors.js");
|
|
44
|
+
Object.defineProperty(exports, "AuthError", { enumerable: true, get: function () { return errors_js_1.AuthError; } });
|
|
45
|
+
// ===== Remix Utilities =====
|
|
46
|
+
var session_server_js_1 = require("./remix/session.server.js");
|
|
47
|
+
Object.defineProperty(exports, "createSessionStorage", { enumerable: true, get: function () { return session_server_js_1.createSessionStorage; } });
|
|
48
|
+
Object.defineProperty(exports, "getSession", { enumerable: true, get: function () { return session_server_js_1.getSession; } });
|
|
49
|
+
Object.defineProperty(exports, "createUserSession", { enumerable: true, get: function () { return session_server_js_1.createUserSession; } });
|
|
50
|
+
Object.defineProperty(exports, "requireAuth", { enumerable: true, get: function () { return session_server_js_1.requireAuth; } });
|
|
51
|
+
Object.defineProperty(exports, "getOptionalUser", { enumerable: true, get: function () { return session_server_js_1.getOptionalUser; } });
|
|
52
|
+
Object.defineProperty(exports, "getUserSession", { enumerable: true, get: function () { return session_server_js_1.getUserSession; } });
|
|
53
|
+
Object.defineProperty(exports, "logout", { enumerable: true, get: function () { return session_server_js_1.logout; } });
|
|
54
|
+
var admin_server_js_1 = require("./remix/admin.server.js");
|
|
55
|
+
Object.defineProperty(exports, "requireAdmin", { enumerable: true, get: function () { return admin_server_js_1.requireAdmin; } });
|
|
56
|
+
Object.defineProperty(exports, "requireRole", { enumerable: true, get: function () { return admin_server_js_1.requireRole; } });
|
|
57
|
+
Object.defineProperty(exports, "hasRole", { enumerable: true, get: function () { return admin_server_js_1.hasRole; } });
|
|
58
|
+
Object.defineProperty(exports, "hasAnyRole", { enumerable: true, get: function () { return admin_server_js_1.hasAnyRole; } });
|
|
59
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;GAYG;;;AAEH,6BAA6B;AAC7B,+DAAwD;AAA/C,sHAAA,cAAc,OAAA;AAEvB,+DAAqE;AAA5D,qHAAA,aAAa,OAAA;AAAE,oHAAA,YAAY,OAAA;AAWpC,4BAA4B;AAC5B,mCAA0F;AAAjF,qGAAA,WAAW,OAAA;AAAE,0GAAA,gBAAgB,OAAA;AAAE,qGAAA,WAAW,OAAA;AAAE,4GAAA,kBAAkB,OAAA;AAEvE,yBAAyB;AACzB,gDAAuD;AAA9C,0GAAA,eAAe,OAAA;AAOxB,gCAAgC;AAChC,sDAAuE;AAA9D,+GAAA,OAAO,OAAqB;AAErC,8BAA8B;AAC9B,6DAAgF;AAAvE,sHAAA,kBAAkB,OAAA;AAAE,oHAAA,gBAAgB,OAAA;AAG7C,0BAA0B;AAC1B,0DAAkG;AAAzF,+GAAA,aAAa,OAAA;AAAE,sHAAA,gBAAgB,OAAwB;AAChE,gDAA+C;AAAtC,sGAAA,SAAS,OAAA;AAGlB,8BAA8B;AAC9B,+DAQmC;AAPjC,yHAAA,oBAAoB,OAAA;AACpB,+GAAA,UAAU,OAAA;AACV,sHAAA,iBAAiB,OAAA;AACjB,gHAAA,WAAW,OAAA;AACX,oHAAA,eAAe,OAAA;AACf,mHAAA,cAAc,OAAA;AACd,2GAAA,MAAM,OAAA;AAGR,2DAAyF;AAAhF,+GAAA,YAAY,OAAA;AAAE,8GAAA,WAAW,OAAA;AAAE,0GAAA,OAAO,OAAA;AAAE,6GAAA,UAAU,OAAA"}
|
package/lib/jwt.d.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JWT verification utilities using aws-jwt-verify.
|
|
3
|
+
*
|
|
4
|
+
* Uses a singleton pattern for the verifier to avoid re-fetching
|
|
5
|
+
* JWKS on every request.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
import { CognitoJwtVerifier } from 'aws-jwt-verify';
|
|
10
|
+
import type {
|
|
11
|
+
CognitoAccessTokenPayload,
|
|
12
|
+
CognitoIdTokenPayload,
|
|
13
|
+
JwtVerifierConfig,
|
|
14
|
+
TokenVerificationResult,
|
|
15
|
+
} from './types.js';
|
|
16
|
+
/** Verifier instance type */
|
|
17
|
+
type VerifierInstance = ReturnType<typeof CognitoJwtVerifier.create>;
|
|
18
|
+
/**
|
|
19
|
+
* Gets or creates a verifier for the given configuration.
|
|
20
|
+
* Verifiers are cached to avoid re-fetching JWKS.
|
|
21
|
+
*
|
|
22
|
+
* @param config - Verifier configuration
|
|
23
|
+
* @returns Cached or new verifier instance
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* const verifier = getVerifier({
|
|
28
|
+
* userPoolId: 'us-east-1_abc123',
|
|
29
|
+
* clientId: '1234567890abcdef',
|
|
30
|
+
* });
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare function getVerifier(config: JwtVerifierConfig): VerifierInstance;
|
|
34
|
+
/**
|
|
35
|
+
* Verifies a JWT token from Cognito.
|
|
36
|
+
*
|
|
37
|
+
* @param token - The JWT token string
|
|
38
|
+
* @param config - Verifier configuration
|
|
39
|
+
* @returns The verified token payload
|
|
40
|
+
* @throws Error if token is invalid or expired
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* const payload = await verifyToken(token, {
|
|
45
|
+
* userPoolId: process.env.COGNITO_USER_POOL_ID!,
|
|
46
|
+
* clientId: process.env.COGNITO_CLIENT_ID!,
|
|
47
|
+
* });
|
|
48
|
+
* console.log(payload.sub); // User ID
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare function verifyToken(
|
|
52
|
+
token: string,
|
|
53
|
+
config: JwtVerifierConfig
|
|
54
|
+
): Promise<CognitoAccessTokenPayload | CognitoIdTokenPayload>;
|
|
55
|
+
/**
|
|
56
|
+
* Verifies a token and returns a normalized result.
|
|
57
|
+
*
|
|
58
|
+
* @param token - The JWT token string
|
|
59
|
+
* @param config - Verifier configuration
|
|
60
|
+
* @returns Normalized verification result
|
|
61
|
+
* @throws Error if token is invalid or expired
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* const result = await verifyAndExtract(token, {
|
|
66
|
+
* userPoolId: process.env.COGNITO_USER_POOL_ID!,
|
|
67
|
+
* clientId: process.env.COGNITO_CLIENT_ID!,
|
|
68
|
+
* });
|
|
69
|
+
* console.log(result.userId); // User ID
|
|
70
|
+
* console.log(result.groups); // ['admin', 'editor']
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export declare function verifyAndExtract(
|
|
74
|
+
token: string,
|
|
75
|
+
config: JwtVerifierConfig
|
|
76
|
+
): Promise<TokenVerificationResult>;
|
|
77
|
+
/**
|
|
78
|
+
* Clears the verifier cache. Useful for testing.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* beforeEach(() => {
|
|
83
|
+
* clearVerifierCache();
|
|
84
|
+
* });
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export declare function clearVerifierCache(): void;
|
|
88
|
+
export {};
|
|
89
|
+
//# sourceMappingURL=jwt.d.ts.map
|
package/lib/jwt.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jwt.d.ts","sourceRoot":"","sources":["../src/jwt.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,KAAK,EACV,yBAAyB,EACzB,qBAAqB,EACrB,iBAAiB,EACjB,uBAAuB,EACxB,MAAM,YAAY,CAAC;AAEpB,6BAA6B;AAC7B,KAAK,gBAAgB,GAAG,UAAU,CAAC,OAAO,kBAAkB,CAAC,MAAM,CAAC,CAAC;AAerE;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,iBAAiB,GAAG,gBAAgB,CAavE;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,WAAW,CAC/B,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,iBAAiB,GACxB,OAAO,CAAC,yBAAyB,GAAG,qBAAqB,CAAC,CAK5D;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,gBAAgB,CACpC,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,iBAAiB,GACxB,OAAO,CAAC,uBAAuB,CAAC,CASlC;AAED;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,IAAI,IAAI,CAEzC"}
|
package/lib/jwt.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* JWT verification utilities using aws-jwt-verify.
|
|
4
|
+
*
|
|
5
|
+
* Uses a singleton pattern for the verifier to avoid re-fetching
|
|
6
|
+
* JWKS on every request.
|
|
7
|
+
*
|
|
8
|
+
* @packageDocumentation
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
11
|
+
exports.getVerifier = getVerifier;
|
|
12
|
+
exports.verifyToken = verifyToken;
|
|
13
|
+
exports.verifyAndExtract = verifyAndExtract;
|
|
14
|
+
exports.clearVerifierCache = clearVerifierCache;
|
|
15
|
+
const aws_jwt_verify_1 = require('aws-jwt-verify');
|
|
16
|
+
/** Verifier cache keyed by userPoolId+clientId+tokenUse */
|
|
17
|
+
const verifierCache = new Map();
|
|
18
|
+
/**
|
|
19
|
+
* Creates a cache key for the verifier.
|
|
20
|
+
*
|
|
21
|
+
* @param config - Verifier configuration
|
|
22
|
+
* @returns Cache key string
|
|
23
|
+
*/
|
|
24
|
+
function getCacheKey(config) {
|
|
25
|
+
return `${config.userPoolId}:${config.clientId}:${config.tokenUse ?? 'access'}`;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Gets or creates a verifier for the given configuration.
|
|
29
|
+
* Verifiers are cached to avoid re-fetching JWKS.
|
|
30
|
+
*
|
|
31
|
+
* @param config - Verifier configuration
|
|
32
|
+
* @returns Cached or new verifier instance
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* const verifier = getVerifier({
|
|
37
|
+
* userPoolId: 'us-east-1_abc123',
|
|
38
|
+
* clientId: '1234567890abcdef',
|
|
39
|
+
* });
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
function getVerifier(config) {
|
|
43
|
+
const key = getCacheKey(config);
|
|
44
|
+
if (!verifierCache.has(key)) {
|
|
45
|
+
const verifier = aws_jwt_verify_1.CognitoJwtVerifier.create({
|
|
46
|
+
userPoolId: config.userPoolId,
|
|
47
|
+
clientId: config.clientId,
|
|
48
|
+
tokenUse: config.tokenUse ?? 'access',
|
|
49
|
+
});
|
|
50
|
+
verifierCache.set(key, verifier);
|
|
51
|
+
}
|
|
52
|
+
return verifierCache.get(key);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Verifies a JWT token from Cognito.
|
|
56
|
+
*
|
|
57
|
+
* @param token - The JWT token string
|
|
58
|
+
* @param config - Verifier configuration
|
|
59
|
+
* @returns The verified token payload
|
|
60
|
+
* @throws Error if token is invalid or expired
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* const payload = await verifyToken(token, {
|
|
65
|
+
* userPoolId: process.env.COGNITO_USER_POOL_ID!,
|
|
66
|
+
* clientId: process.env.COGNITO_CLIENT_ID!,
|
|
67
|
+
* });
|
|
68
|
+
* console.log(payload.sub); // User ID
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
async function verifyToken(token, config) {
|
|
72
|
+
const verifier = getVerifier(config);
|
|
73
|
+
const payload = await verifier.verify(token);
|
|
74
|
+
// Cast through unknown to handle aws-jwt-verify's generic payload type
|
|
75
|
+
return payload;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Verifies a token and returns a normalized result.
|
|
79
|
+
*
|
|
80
|
+
* @param token - The JWT token string
|
|
81
|
+
* @param config - Verifier configuration
|
|
82
|
+
* @returns Normalized verification result
|
|
83
|
+
* @throws Error if token is invalid or expired
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* const result = await verifyAndExtract(token, {
|
|
88
|
+
* userPoolId: process.env.COGNITO_USER_POOL_ID!,
|
|
89
|
+
* clientId: process.env.COGNITO_CLIENT_ID!,
|
|
90
|
+
* });
|
|
91
|
+
* console.log(result.userId); // User ID
|
|
92
|
+
* console.log(result.groups); // ['admin', 'editor']
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
async function verifyAndExtract(token, config) {
|
|
96
|
+
const payload = await verifyToken(token, config);
|
|
97
|
+
return {
|
|
98
|
+
userId: payload.sub,
|
|
99
|
+
email: 'email' in payload ? payload.email : undefined,
|
|
100
|
+
groups: payload['cognito:groups'] ?? [],
|
|
101
|
+
payload,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Clears the verifier cache. Useful for testing.
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```typescript
|
|
109
|
+
* beforeEach(() => {
|
|
110
|
+
* clearVerifierCache();
|
|
111
|
+
* });
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
function clearVerifierCache() {
|
|
115
|
+
verifierCache.clear();
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=jwt.js.map
|
package/lib/jwt.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jwt.js","sourceRoot":"","sources":["../src/jwt.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;AAyCH,kCAaC;AAmBD,kCAQC;AAoBD,4CAYC;AAYD,gDAEC;AA7HD,mDAAoD;AAWpD,2DAA2D;AAC3D,MAAM,aAAa,GAAG,IAAI,GAAG,EAA4B,CAAC;AAE1D;;;;;GAKG;AACH,SAAS,WAAW,CAAC,MAAyB;IAC5C,OAAO,GAAG,MAAM,CAAC,UAAU,IAAI,MAAM,CAAC,QAAQ,IAAI,MAAM,CAAC,QAAQ,IAAI,QAAQ,EAAE,CAAC;AAClF,CAAC;AAED;;;;;;;;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B,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cognito authentication middleware with real JWT validation.
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
import type { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda';
|
|
7
|
+
import type { CognitoAuthOptions, TokenVerificationResult } from '../types.js';
|
|
8
|
+
/**
|
|
9
|
+
* Authenticated event with verified Cognito claims.
|
|
10
|
+
*/
|
|
11
|
+
export interface CognitoAuthenticatedEvent extends APIGatewayProxyEvent {
|
|
12
|
+
/** Verified auth information */
|
|
13
|
+
auth: TokenVerificationResult;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Middleware handler type.
|
|
17
|
+
*/
|
|
18
|
+
export type MiddlewareHandler<TEvent = APIGatewayProxyEvent> = (
|
|
19
|
+
event: TEvent,
|
|
20
|
+
context: Context
|
|
21
|
+
) => Promise<APIGatewayProxyResult>;
|
|
22
|
+
/**
|
|
23
|
+
* Middleware function type.
|
|
24
|
+
*/
|
|
25
|
+
export type Middleware<TEventIn = APIGatewayProxyEvent, TEventOut = TEventIn> = (
|
|
26
|
+
handler: MiddlewareHandler<TEventOut>
|
|
27
|
+
) => MiddlewareHandler<TEventIn>;
|
|
28
|
+
/**
|
|
29
|
+
* Middleware that validates JWT tokens from Cognito.
|
|
30
|
+
*
|
|
31
|
+
* Unlike the API package's withAuth (which expects API Gateway to validate),
|
|
32
|
+
* this middleware performs actual JWT verification using aws-jwt-verify.
|
|
33
|
+
*
|
|
34
|
+
* @param options - Authentication configuration
|
|
35
|
+
* @returns Middleware that validates tokens and adds auth info to event
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* // Basic auth - validates token
|
|
40
|
+
* const handler = withCognitoAuth()(async (event) => {
|
|
41
|
+
* const userId = event.auth.userId;
|
|
42
|
+
* return { statusCode: 200, body: JSON.stringify({ userId }) };
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* // With role requirement
|
|
46
|
+
* const adminHandler = withCognitoAuth({ roles: ['admin'] })(async (event) => {
|
|
47
|
+
* return { statusCode: 200, body: 'Admin access granted' };
|
|
48
|
+
* });
|
|
49
|
+
*
|
|
50
|
+
* // Skip auth for certain paths
|
|
51
|
+
* const handler = withCognitoAuth({ skipPaths: ['/health'] })(async (event) => {
|
|
52
|
+
* return { statusCode: 200, body: 'OK' };
|
|
53
|
+
* });
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare function withCognitoAuth(
|
|
57
|
+
options?: CognitoAuthOptions
|
|
58
|
+
): Middleware<APIGatewayProxyEvent, CognitoAuthenticatedEvent>;
|
|
59
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/middleware/auth.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAEvF,OAAO,KAAK,EAAE,kBAAkB,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AAE/E;;GAEG;AACH,MAAM,WAAW,yBAA0B,SAAQ,oBAAoB;IACrE,gCAAgC;IAChC,IAAI,EAAE,uBAAuB,CAAC;CAC/B;AAED;;GAEG;AACH,MAAM,MAAM,iBAAiB,CAAC,MAAM,GAAG,oBAAoB,IAAI,CAC7D,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,OAAO,KACb,OAAO,CAAC,qBAAqB,CAAC,CAAC;AAEpC;;GAEG;AACH,MAAM,MAAM,UAAU,CAAC,QAAQ,GAAG,oBAAoB,EAAE,SAAS,GAAG,QAAQ,IAAI,CAC9E,OAAO,EAAE,iBAAiB,CAAC,SAAS,CAAC,KAClC,iBAAiB,CAAC,QAAQ,CAAC,CAAC;AAgEjC;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,eAAe,CAC7B,OAAO,GAAE,kBAAuB,GAC/B,UAAU,CAAC,oBAAoB,EAAE,yBAAyB,CAAC,CAkE7D"}
|