@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.
Files changed (67) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +323 -0
  3. package/lib/authorizer/handler.d.ts +33 -0
  4. package/lib/authorizer/handler.d.ts.map +1 -0
  5. package/lib/authorizer/handler.js +106 -0
  6. package/lib/authorizer/handler.js.map +1 -0
  7. package/lib/authorizer/index.d.ts +7 -0
  8. package/lib/authorizer/index.d.ts.map +1 -0
  9. package/lib/authorizer/index.js +16 -0
  10. package/lib/authorizer/index.js.map +1 -0
  11. package/lib/client/auth-client.d.ts +131 -0
  12. package/lib/client/auth-client.d.ts.map +1 -0
  13. package/lib/client/auth-client.js +270 -0
  14. package/lib/client/auth-client.js.map +1 -0
  15. package/lib/client/errors.d.ts +67 -0
  16. package/lib/client/errors.d.ts.map +1 -0
  17. package/lib/client/errors.js +90 -0
  18. package/lib/client/errors.js.map +1 -0
  19. package/lib/client/index.d.ts +8 -0
  20. package/lib/client/index.d.ts.map +1 -0
  21. package/lib/client/index.js +29 -0
  22. package/lib/client/index.js.map +1 -0
  23. package/lib/cognito-construct.d.ts +113 -0
  24. package/lib/cognito-construct.d.ts.map +1 -0
  25. package/lib/cognito-construct.js +211 -0
  26. package/lib/cognito-construct.js.map +1 -0
  27. package/lib/index.d.ts +30 -0
  28. package/lib/index.d.ts.map +1 -0
  29. package/lib/index.js +59 -0
  30. package/lib/index.js.map +1 -0
  31. package/lib/jwt.d.ts +89 -0
  32. package/lib/jwt.d.ts.map +1 -0
  33. package/lib/jwt.js +117 -0
  34. package/lib/jwt.js.map +1 -0
  35. package/lib/middleware/auth.d.ts +59 -0
  36. package/lib/middleware/auth.d.ts.map +1 -0
  37. package/lib/middleware/auth.js +148 -0
  38. package/lib/middleware/auth.js.map +1 -0
  39. package/lib/middleware/index.d.ts +12 -0
  40. package/lib/middleware/index.d.ts.map +1 -0
  41. package/lib/middleware/index.js +16 -0
  42. package/lib/middleware/index.js.map +1 -0
  43. package/lib/remix/admin.server.d.ts +105 -0
  44. package/lib/remix/admin.server.d.ts.map +1 -0
  45. package/lib/remix/admin.server.js +146 -0
  46. package/lib/remix/admin.server.js.map +1 -0
  47. package/lib/remix/index.d.ts +17 -0
  48. package/lib/remix/index.d.ts.map +1 -0
  49. package/lib/remix/index.js +95 -0
  50. package/lib/remix/index.js.map +1 -0
  51. package/lib/remix/session.server.d.ts +177 -0
  52. package/lib/remix/session.server.d.ts.map +1 -0
  53. package/lib/remix/session.server.js +287 -0
  54. package/lib/remix/session.server.js.map +1 -0
  55. package/lib/types.d.ts +161 -0
  56. package/lib/types.d.ts.map +1 -0
  57. package/lib/types.js +8 -0
  58. package/lib/types.js.map +1 -0
  59. package/lib/utils/index.d.ts +12 -0
  60. package/lib/utils/index.d.ts.map +1 -0
  61. package/lib/utils/index.js +22 -0
  62. package/lib/utils/index.js.map +1 -0
  63. package/lib/utils/token-refresh.d.ts +62 -0
  64. package/lib/utils/token-refresh.d.ts.map +1 -0
  65. package/lib/utils/token-refresh.js +84 -0
  66. package/lib/utils/token-refresh.js.map +1 -0
  67. package/package.json +70 -0
@@ -0,0 +1,270 @@
1
+ 'use strict';
2
+ /**
3
+ * Cognito authentication client for frontend applications.
4
+ *
5
+ * Provides methods for sign-in, sign-up, sign-out, and token refresh
6
+ * using AWS Cognito User Pools.
7
+ *
8
+ * @packageDocumentation
9
+ */
10
+ Object.defineProperty(exports, '__esModule', { value: true });
11
+ exports.FndAuthClient = void 0;
12
+ exports.clearClientCache = clearClientCache;
13
+ const client_cognito_identity_provider_1 = require('@aws-sdk/client-cognito-identity-provider');
14
+ const errors_js_1 = require('./errors.js');
15
+ /**
16
+ * Cache for Cognito clients keyed by configuration.
17
+ */
18
+ const clientCache = new Map();
19
+ /**
20
+ * Generates a cache key for the given configuration.
21
+ */
22
+ function getCacheKey(config) {
23
+ const region = config.region ?? process.env.AWS_REGION ?? 'us-east-1';
24
+ return `${config.userPoolId}:${config.clientId}:${region}`;
25
+ }
26
+ /**
27
+ * Gets or creates a Cognito client for the given configuration.
28
+ */
29
+ function getClient(config) {
30
+ const key = getCacheKey(config);
31
+ let client = clientCache.get(key);
32
+ if (!client) {
33
+ const region = config.region ?? process.env.AWS_REGION ?? 'us-east-1';
34
+ client = new client_cognito_identity_provider_1.CognitoIdentityProviderClient({ region });
35
+ clientCache.set(key, client);
36
+ }
37
+ return client;
38
+ }
39
+ /**
40
+ * Clears the client cache. Useful for testing.
41
+ */
42
+ function clearClientCache() {
43
+ clientCache.clear();
44
+ }
45
+ /**
46
+ * Cognito authentication client for frontend applications.
47
+ *
48
+ * Provides methods for user authentication including sign-in, sign-up,
49
+ * email confirmation, token refresh, and sign-out.
50
+ *
51
+ * @example
52
+ * ```typescript
53
+ * const authClient = new FndAuthClient({
54
+ * userPoolId: process.env.COGNITO_USER_POOL_ID!,
55
+ * clientId: process.env.COGNITO_CLIENT_ID!,
56
+ * region: process.env.AWS_REGION,
57
+ * });
58
+ *
59
+ * // Sign in
60
+ * const tokens = await authClient.signIn('user@example.com', 'password');
61
+ *
62
+ * // Use access token for API calls
63
+ * const response = await fetch('/api/data', {
64
+ * headers: { Authorization: `Bearer ${tokens.accessToken}` },
65
+ * });
66
+ * ```
67
+ */
68
+ class FndAuthClient {
69
+ client;
70
+ clientId;
71
+ /**
72
+ * Creates a new FndAuthClient.
73
+ *
74
+ * @param config - Configuration for the auth client
75
+ */
76
+ constructor(config) {
77
+ this.client = getClient(config);
78
+ this.clientId = config.clientId;
79
+ }
80
+ /**
81
+ * Signs in a user with email and password.
82
+ *
83
+ * @param email - User's email address
84
+ * @param password - User's password
85
+ * @returns Authentication tokens
86
+ * @throws {AuthError} If authentication fails
87
+ *
88
+ * @example
89
+ * ```typescript
90
+ * try {
91
+ * const tokens = await authClient.signIn('user@example.com', 'password');
92
+ * console.log('Logged in!', tokens.accessToken);
93
+ * } catch (error) {
94
+ * if (error instanceof AuthError && error.code === 'USER_NOT_CONFIRMED') {
95
+ * // Redirect to confirmation page
96
+ * }
97
+ * }
98
+ * ```
99
+ */
100
+ async signIn(email, password) {
101
+ try {
102
+ const result = await this.client.send(
103
+ new client_cognito_identity_provider_1.InitiateAuthCommand({
104
+ AuthFlow: client_cognito_identity_provider_1.AuthFlowType.USER_PASSWORD_AUTH,
105
+ ClientId: this.clientId,
106
+ AuthParameters: {
107
+ USERNAME: email,
108
+ PASSWORD: password,
109
+ },
110
+ })
111
+ );
112
+ if (!result.AuthenticationResult) {
113
+ throw new Error('Authentication failed - no result returned');
114
+ }
115
+ const { AccessToken, IdToken, RefreshToken, ExpiresIn } = result.AuthenticationResult;
116
+ if (!AccessToken || !IdToken || !RefreshToken) {
117
+ throw new Error('Authentication failed - missing tokens');
118
+ }
119
+ return {
120
+ accessToken: AccessToken,
121
+ idToken: IdToken,
122
+ refreshToken: RefreshToken,
123
+ expiresIn: ExpiresIn ?? 3600,
124
+ };
125
+ } catch (error) {
126
+ throw (0, errors_js_1.mapCognitoError)(error, 'Sign in failed');
127
+ }
128
+ }
129
+ /**
130
+ * Signs up a new user.
131
+ *
132
+ * @param email - User's email address
133
+ * @param password - User's password
134
+ * @param name - Optional user's name
135
+ * @returns Sign-up result with confirmation status
136
+ * @throws {AuthError} If sign-up fails
137
+ *
138
+ * @example
139
+ * ```typescript
140
+ * const result = await authClient.signUp('user@example.com', 'password', 'John Doe');
141
+ * if (!result.userConfirmed) {
142
+ * // Show confirmation code input
143
+ * console.log(`Code sent to ${result.codeDeliveryDetails?.destination}`);
144
+ * }
145
+ * ```
146
+ */
147
+ async signUp(email, password, name) {
148
+ try {
149
+ const userAttributes = [{ Name: 'email', Value: email }];
150
+ if (name) {
151
+ userAttributes.push({ Name: 'name', Value: name });
152
+ }
153
+ const result = await this.client.send(
154
+ new client_cognito_identity_provider_1.SignUpCommand({
155
+ ClientId: this.clientId,
156
+ Username: email,
157
+ Password: password,
158
+ UserAttributes: userAttributes,
159
+ })
160
+ );
161
+ return {
162
+ userConfirmed: result.UserConfirmed ?? false,
163
+ codeDeliveryDetails: result.CodeDeliveryDetails
164
+ ? {
165
+ destination: result.CodeDeliveryDetails.Destination,
166
+ deliveryMedium: result.CodeDeliveryDetails.DeliveryMedium,
167
+ }
168
+ : undefined,
169
+ };
170
+ } catch (error) {
171
+ throw (0, errors_js_1.mapCognitoError)(error, 'Sign up failed');
172
+ }
173
+ }
174
+ /**
175
+ * Confirms a user's sign-up with the verification code.
176
+ *
177
+ * @param email - User's email address
178
+ * @param code - Verification code from email/SMS
179
+ * @throws {AuthError} If confirmation fails
180
+ *
181
+ * @example
182
+ * ```typescript
183
+ * await authClient.confirmSignUp('user@example.com', '123456');
184
+ * // User is now confirmed, can sign in
185
+ * ```
186
+ */
187
+ async confirmSignUp(email, code) {
188
+ try {
189
+ await this.client.send(
190
+ new client_cognito_identity_provider_1.ConfirmSignUpCommand({
191
+ ClientId: this.clientId,
192
+ Username: email,
193
+ ConfirmationCode: code,
194
+ })
195
+ );
196
+ } catch (error) {
197
+ throw (0, errors_js_1.mapCognitoError)(error, 'Confirmation failed');
198
+ }
199
+ }
200
+ /**
201
+ * Refreshes authentication tokens using a refresh token.
202
+ *
203
+ * @param refreshToken - The refresh token from a previous authentication
204
+ * @returns New authentication tokens
205
+ * @throws {AuthError} If refresh fails
206
+ *
207
+ * @example
208
+ * ```typescript
209
+ * // When access token is about to expire
210
+ * const newTokens = await authClient.refreshTokens(tokens.refreshToken);
211
+ * ```
212
+ */
213
+ async refreshTokens(refreshToken) {
214
+ try {
215
+ const result = await this.client.send(
216
+ new client_cognito_identity_provider_1.InitiateAuthCommand({
217
+ AuthFlow: client_cognito_identity_provider_1.AuthFlowType.REFRESH_TOKEN_AUTH,
218
+ ClientId: this.clientId,
219
+ AuthParameters: {
220
+ REFRESH_TOKEN: refreshToken,
221
+ },
222
+ })
223
+ );
224
+ if (!result.AuthenticationResult) {
225
+ throw new Error('Token refresh failed - no result returned');
226
+ }
227
+ const { AccessToken, IdToken, ExpiresIn } = result.AuthenticationResult;
228
+ if (!AccessToken || !IdToken) {
229
+ throw new Error('Token refresh failed - missing tokens');
230
+ }
231
+ return {
232
+ accessToken: AccessToken,
233
+ idToken: IdToken,
234
+ // Refresh token doesn't change on refresh
235
+ refreshToken: refreshToken,
236
+ expiresIn: ExpiresIn ?? 3600,
237
+ };
238
+ } catch (error) {
239
+ throw (0, errors_js_1.mapCognitoError)(error, 'Token refresh failed');
240
+ }
241
+ }
242
+ /**
243
+ * Signs out the user from all devices.
244
+ *
245
+ * This invalidates all refresh tokens for the user, effectively
246
+ * signing them out from all devices.
247
+ *
248
+ * @param accessToken - The user's current access token
249
+ * @throws {AuthError} If sign-out fails
250
+ *
251
+ * @example
252
+ * ```typescript
253
+ * await authClient.signOut(tokens.accessToken);
254
+ * // User is now signed out from all devices
255
+ * ```
256
+ */
257
+ async signOut(accessToken) {
258
+ try {
259
+ await this.client.send(
260
+ new client_cognito_identity_provider_1.GlobalSignOutCommand({
261
+ AccessToken: accessToken,
262
+ })
263
+ );
264
+ } catch (error) {
265
+ throw (0, errors_js_1.mapCognitoError)(error, 'Sign out failed');
266
+ }
267
+ }
268
+ }
269
+ exports.FndAuthClient = FndAuthClient;
270
+ //# sourceMappingURL=auth-client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth-client.js","sourceRoot":"","sources":["../../src/client/auth-client.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;AA2CH,4CAEC;AA3CD,gGAOmD;AAEnD,2CAA8C;AAE9C;;GAEG;AACH,MAAM,WAAW,GAAG,IAAI,GAAG,EAAyC,CAAC;AAErE;;GAEG;AACH,SAAS,WAAW,CAAC,MAAwB;IAC3C,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC,UAAU,IAAI,WAAW,CAAC;IACtE,OAAO,GAAG,MAAM,CAAC,UAAU,IAAI,MAAM,CAAC,QAAQ,IAAI,MAAM,EAAE,CA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;YAExE,IAAI,CAAC,WAAW,IAAI,CAAC,OAAO,EAAE,CAAC;gBAC7B,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC;YAC3D,CAAC;YAED,OAAO;gBACL,WAAW,EAAE,WAAW;gBACxB,OAAO,EAAE,OAAO;gBAChB,0CAA0C;gBAC1C,YAAY,EAAE,YAAY;gBAC1B,SAAS,EAAE,SAAS,IAAI,IAAI;aAC7B,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,IAAA,2BAAe,EAAC,KAAK,EAAE,sBAAsB,CAAC,CAAC;QACvD,CAAC;IACH,CAAC;IAED;;;;;;;;;;;;;;OAcG;IACH,KAAK,CAAC,OAAO,CAAC,WAAmB;QAC/B,IAAI,CAAC;YACH,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CACpB,IAAI,uDAAoB,CAAC;gBACvB,WAAW,EAAE,WAAW;aACzB,CAAC,CACH,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,IAAA,2BAAe,EAAC,KAAK,EAAE,iBAAiB,CAAC,CAAC;QAClD,CAAC;IACH,CAAC;CACF;AAzND,sCAyNC"}
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Authentication error types for FndAuthClient.
3
+ *
4
+ * Provides structured error handling with error codes for common
5
+ * Cognito authentication failures.
6
+ *
7
+ * @packageDocumentation
8
+ */
9
+ /**
10
+ * Error codes for authentication failures.
11
+ */
12
+ export type AuthErrorCode =
13
+ | 'INVALID_CREDENTIALS'
14
+ | 'USER_NOT_FOUND'
15
+ | 'USER_NOT_CONFIRMED'
16
+ | 'CODE_MISMATCH'
17
+ | 'CODE_EXPIRED'
18
+ | 'TOKEN_EXPIRED'
19
+ | 'INVALID_TOKEN'
20
+ | 'USER_EXISTS'
21
+ | 'PASSWORD_POLICY'
22
+ | 'RATE_LIMITED'
23
+ | 'SERVICE_ERROR';
24
+ /**
25
+ * Authentication error with structured error code.
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * try {
30
+ * await authClient.signIn(email, password);
31
+ * } catch (error) {
32
+ * if (error instanceof AuthError) {
33
+ * switch (error.code) {
34
+ * case 'INVALID_CREDENTIALS':
35
+ * // Handle invalid credentials
36
+ * break;
37
+ * case 'USER_NOT_CONFIRMED':
38
+ * // Redirect to confirmation page
39
+ * break;
40
+ * }
41
+ * }
42
+ * }
43
+ * ```
44
+ */
45
+ export declare class AuthError extends Error {
46
+ readonly code: AuthErrorCode;
47
+ readonly cause?: Error | undefined;
48
+ /**
49
+ * Creates a new AuthError.
50
+ *
51
+ * @param message - Human-readable error message
52
+ * @param code - Structured error code for programmatic handling
53
+ * @param cause - Original error that caused this error
54
+ */
55
+ constructor(message: string, code: AuthErrorCode, cause?: Error | undefined);
56
+ }
57
+ /**
58
+ * Maps a Cognito SDK error to an AuthError.
59
+ *
60
+ * @param error - Error from Cognito SDK
61
+ * @param defaultMessage - Default message if error message is not available
62
+ * @returns AuthError with appropriate code
63
+ *
64
+ * @internal
65
+ */
66
+ export declare function mapCognitoError(error: unknown, defaultMessage: string): AuthError;
67
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/client/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;GAEG;AACH,MAAM,MAAM,aAAa,GACrB,qBAAqB,GACrB,gBAAgB,GAChB,oBAAoB,GACpB,eAAe,GACf,cAAc,GACd,eAAe,GACf,eAAe,GACf,aAAa,GACb,iBAAiB,GACjB,cAAc,GACd,eAAe,CAAC;AAEpB;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBAAa,SAAU,SAAQ,KAAK;aAUhB,IAAI,EAAE,aAAa;aACnB,KAAK,CAAC,EAAE,KAAK;IAV/B;;;;;;OAMG;gBAED,OAAO,EAAE,MAAM,EACC,IAAI,EAAE,aAAa,EACnB,KAAK,CAAC,EAAE,KAAK,YAAA;CAShC;AAoBD;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,GAAG,SAAS,CAOjF"}
@@ -0,0 +1,90 @@
1
+ 'use strict';
2
+ /**
3
+ * Authentication error types for FndAuthClient.
4
+ *
5
+ * Provides structured error handling with error codes for common
6
+ * Cognito authentication failures.
7
+ *
8
+ * @packageDocumentation
9
+ */
10
+ Object.defineProperty(exports, '__esModule', { value: true });
11
+ exports.AuthError = void 0;
12
+ exports.mapCognitoError = mapCognitoError;
13
+ /**
14
+ * Authentication error with structured error code.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * try {
19
+ * await authClient.signIn(email, password);
20
+ * } catch (error) {
21
+ * if (error instanceof AuthError) {
22
+ * switch (error.code) {
23
+ * case 'INVALID_CREDENTIALS':
24
+ * // Handle invalid credentials
25
+ * break;
26
+ * case 'USER_NOT_CONFIRMED':
27
+ * // Redirect to confirmation page
28
+ * break;
29
+ * }
30
+ * }
31
+ * }
32
+ * ```
33
+ */
34
+ class AuthError extends Error {
35
+ code;
36
+ cause;
37
+ /**
38
+ * Creates a new AuthError.
39
+ *
40
+ * @param message - Human-readable error message
41
+ * @param code - Structured error code for programmatic handling
42
+ * @param cause - Original error that caused this error
43
+ */
44
+ constructor(message, code, cause) {
45
+ super(message);
46
+ this.code = code;
47
+ this.cause = cause;
48
+ this.name = 'AuthError';
49
+ // Maintains proper stack trace in V8 environments
50
+ if (Error.captureStackTrace) {
51
+ Error.captureStackTrace(this, AuthError);
52
+ }
53
+ }
54
+ }
55
+ exports.AuthError = AuthError;
56
+ /**
57
+ * Maps Cognito exception names to AuthErrorCode.
58
+ *
59
+ * @internal
60
+ */
61
+ const COGNITO_ERROR_MAP = {
62
+ NotAuthorizedException: 'INVALID_CREDENTIALS',
63
+ UserNotFoundException: 'USER_NOT_FOUND',
64
+ UserNotConfirmedException: 'USER_NOT_CONFIRMED',
65
+ CodeMismatchException: 'CODE_MISMATCH',
66
+ ExpiredCodeException: 'CODE_EXPIRED',
67
+ UsernameExistsException: 'USER_EXISTS',
68
+ InvalidPasswordException: 'PASSWORD_POLICY',
69
+ InvalidParameterException: 'PASSWORD_POLICY',
70
+ TooManyRequestsException: 'RATE_LIMITED',
71
+ LimitExceededException: 'RATE_LIMITED',
72
+ };
73
+ /**
74
+ * Maps a Cognito SDK error to an AuthError.
75
+ *
76
+ * @param error - Error from Cognito SDK
77
+ * @param defaultMessage - Default message if error message is not available
78
+ * @returns AuthError with appropriate code
79
+ *
80
+ * @internal
81
+ */
82
+ function mapCognitoError(error, defaultMessage) {
83
+ if (error instanceof Error) {
84
+ const errorName = error.name;
85
+ const code = COGNITO_ERROR_MAP[errorName] ?? 'SERVICE_ERROR';
86
+ return new AuthError(error.message || defaultMessage, code, error);
87
+ }
88
+ return new AuthError(defaultMessage, 'SERVICE_ERROR');
89
+ }
90
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/client/errors.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;AAwFH,0CAOC;AA7ED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAa,SAAU,SAAQ,KAAK;IAUhB;IACA;IAVlB;;;;;;OAMG;IACH,YACE,OAAe,EACC,IAAmB,EACnB,KAAa;QAE7B,KAAK,CAAC,OAAO,CAAC,CAAC;QAHC,SAAI,GAAJ,IAAI,CAAe;QACnB,UAAK,GAAL,KAAK,CAAQ;QAG7B,IAAI,CAAC,IAAI,GAAG,WAAW,CAAC;QACxB,kDAAkD;QAClD,IAAI,KAAK,CAAC,iBAAiB,EAAE,CAAC;YAC5B,KAAK,CAAC,iBAAiB,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;QAC3C,CAAC;IACH,CAAC;CACF;AApBD,8BAoBC;AAED;;;;GAIG;AACH,MAAM,iBAAiB,GAAkC;IACvD,sBAAsB,EAAE,qBAAqB;IAC7C,qBAAqB,EAAE,gBAAgB;IACvC,yBAAyB,EAAE,oBAAoB;IAC/C,qBAAqB,EAAE,eAAe;IACtC,oBAAoB,EAAE,cAAc;IACpC,uBAAuB,EAAE,aAAa;IACtC,wBAAwB,EAAE,iBAAiB;IAC3C,yBAAyB,EAAE,iBAAiB;IAC5C,wBAAwB,EAAE,cAAc;IACxC,sBAAsB,EAAE,cAAc;CACvC,CAAC;AAEF;;;;;;;;GAQG;AACH,SAAgB,eAAe,CAAC,KAAc,EAAE,cAAsB;IACpE,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;QAC3B,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAC;QAC7B,MAAM,IAAI,GAAG,iBAAiB,CAAC,SAAS,CAAC,IAAI,eAAe,CAAC;QAC7D,OAAO,IAAI,SAAS,CAAC,KAAK,CAAC,OAAO,IAAI,cAAc,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;IACrE,CAAC;IACD,OAAO,IAAI,SAAS,CAAC,cAAc,EAAE,eAAe,CAAC,CAAC;AACxD,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Auth client exports.
3
+ *
4
+ * @packageDocumentation
5
+ */
6
+ export { FndAuthClient, clearClientCache } from './auth-client.js';
7
+ export { AuthError, type AuthErrorCode } from './errors.js';
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACnE,OAAO,EAAE,SAAS,EAAE,KAAK,aAAa,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,29 @@
1
+ 'use strict';
2
+ /**
3
+ * Auth client exports.
4
+ *
5
+ * @packageDocumentation
6
+ */
7
+ Object.defineProperty(exports, '__esModule', { value: true });
8
+ exports.AuthError = exports.clearClientCache = exports.FndAuthClient = void 0;
9
+ var auth_client_js_1 = require('./auth-client.js');
10
+ Object.defineProperty(exports, 'FndAuthClient', {
11
+ enumerable: true,
12
+ get: function () {
13
+ return auth_client_js_1.FndAuthClient;
14
+ },
15
+ });
16
+ Object.defineProperty(exports, 'clearClientCache', {
17
+ enumerable: true,
18
+ get: function () {
19
+ return auth_client_js_1.clearClientCache;
20
+ },
21
+ });
22
+ var errors_js_1 = require('./errors.js');
23
+ Object.defineProperty(exports, 'AuthError', {
24
+ enumerable: true,
25
+ get: function () {
26
+ return errors_js_1.AuthError;
27
+ },
28
+ });
29
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AAEH,mDAAmE;AAA1D,+GAAA,aAAa,OAAA;AAAE,kHAAA,gBAAgB,OAAA;AACxC,yCAA4D;AAAnD,sGAAA,SAAS,OAAA"}
@@ -0,0 +1,113 @@
1
+ import { Construct } from 'constructs';
2
+ import * as cognito from 'aws-cdk-lib/aws-cognito';
3
+ import { RemovalPolicy } from 'aws-cdk-lib';
4
+ /**
5
+ * Valid deployment stages for fnd-platform applications.
6
+ */
7
+ export declare const VALID_STAGES: readonly ['dev', 'staging', 'prod'];
8
+ /**
9
+ * Deployment stage type.
10
+ */
11
+ export type Stage = (typeof VALID_STAGES)[number];
12
+ /**
13
+ * Validates that the provided stage is a valid deployment stage.
14
+ * @param stage - The stage to validate
15
+ * @throws Error if stage is not valid
16
+ */
17
+ export declare function validateStage(stage: string): asserts stage is Stage;
18
+ /**
19
+ * Configuration options for FndCognitoAuth construct.
20
+ */
21
+ export interface FndCognitoAuthProps {
22
+ /**
23
+ * Application name used in User Pool naming.
24
+ * The User Pool will be named `{appName}-{stage}`.
25
+ */
26
+ appName: string;
27
+ /**
28
+ * Deployment stage (dev, staging, prod).
29
+ * Affects security settings like MFA and password policy.
30
+ */
31
+ stage: Stage | string;
32
+ /**
33
+ * Frontend callback URLs for OAuth flows.
34
+ * These URLs will be allowed for OAuth redirects.
35
+ * @example ['http://localhost:3000', 'https://myapp.com']
36
+ */
37
+ callbackUrls: string[];
38
+ /**
39
+ * Enable Multi-Factor Authentication.
40
+ * @default false for dev/staging, true for prod
41
+ */
42
+ mfaEnabled?: boolean;
43
+ /**
44
+ * Removal policy for the User Pool.
45
+ * @default RemovalPolicy.DESTROY for dev/staging, RemovalPolicy.RETAIN for prod
46
+ */
47
+ removalPolicy?: RemovalPolicy;
48
+ }
49
+ /**
50
+ * CDK construct for AWS Cognito User Pool with app clients and user groups.
51
+ *
52
+ * Creates a fully configured User Pool with:
53
+ * - Secure password policy
54
+ * - Email verification
55
+ * - MFA support (optional, enabled by default in prod)
56
+ * - User groups (admin, editor, viewer)
57
+ * - Web client for frontend OAuth flows
58
+ * - Admin client for CMS direct authentication
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * const auth = new FndCognitoAuth(this, 'Auth', {
63
+ * appName: 'my-app',
64
+ * stage: 'dev',
65
+ * callbackUrls: ['http://localhost:3000'],
66
+ * });
67
+ *
68
+ * // Access the User Pool ID
69
+ * console.log(auth.userPoolId);
70
+ *
71
+ * // Use the web client for frontend
72
+ * console.log(auth.webClientId);
73
+ * ```
74
+ */
75
+ export declare class FndCognitoAuth extends Construct {
76
+ /**
77
+ * The Cognito User Pool.
78
+ */
79
+ readonly userPool: cognito.UserPool;
80
+ /**
81
+ * The web client for frontend OAuth authentication.
82
+ */
83
+ readonly webClient: cognito.UserPoolClient;
84
+ /**
85
+ * The admin client for CMS direct authentication.
86
+ */
87
+ readonly adminClient: cognito.UserPoolClient;
88
+ /**
89
+ * The User Pool ID.
90
+ */
91
+ readonly userPoolId: string;
92
+ /**
93
+ * The web client ID.
94
+ */
95
+ readonly webClientId: string;
96
+ /**
97
+ * The admin client ID.
98
+ */
99
+ readonly adminClientId: string;
100
+ /**
101
+ * The deployment stage.
102
+ */
103
+ readonly stage: Stage;
104
+ constructor(scope: Construct, id: string, props: FndCognitoAuthProps);
105
+ /**
106
+ * Creates the standard user groups for role-based access control.
107
+ * - admin: Full access to CMS and API
108
+ * - editor: Can create/edit content, no admin settings
109
+ * - viewer: Read-only access
110
+ */
111
+ private createUserGroups;
112
+ }
113
+ //# sourceMappingURL=cognito-construct.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cognito-construct.d.ts","sourceRoot":"","sources":["../src/cognito-construct.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,KAAK,OAAO,MAAM,yBAAyB,CAAC;AACnD,OAAO,EAAE,aAAa,EAAY,MAAM,aAAa,CAAC;AAEtD;;GAEG;AACH,eAAO,MAAM,YAAY,qCAAsC,CAAC;AAEhE;;GAEG;AACH,MAAM,MAAM,KAAK,GAAG,CAAC,OAAO,YAAY,CAAC,CAAC,MAAM,CAAC,CAAC;AAElD;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,IAAI,KAAK,CAInE;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;;OAGG;IACH,KAAK,EAAE,KAAK,GAAG,MAAM,CAAC;IAEtB;;;;OAIG;IACH,YAAY,EAAE,MAAM,EAAE,CAAC;IAEvB;;;OAGG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IAErB;;;OAGG;IACH,aAAa,CAAC,EAAE,aAAa,CAAC;CAC/B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,qBAAa,cAAe,SAAQ,SAAS;IAC3C;;OAEG;IACH,SAAgB,QAAQ,EAAE,OAAO,CAAC,QAAQ,CAAC;IAE3C;;OAEG;IACH,SAAgB,SAAS,EAAE,OAAO,CAAC,cAAc,CAAC;IAElD;;OAEG;IACH,SAAgB,WAAW,EAAE,OAAO,CAAC,cAAc,CAAC;IA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