ts-ag 1.2.0 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser.d.ts +1 -2
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js +3 -3
- package/dist/browser.js.map +1 -1
- package/dist/index.d.mts +48 -39
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +325 -268
- package/dist/index.mjs.map +1 -1
- package/dist/scripts/ts-build-config.mjs +0 -1
- package/dist/worker.d.mts +48 -39
- package/dist/worker.d.mts.map +1 -1
- package/dist/worker.mjs +325 -268
- package/dist/worker.mjs.map +1 -1
- package/package.json +3 -2
package/dist/worker.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { parse, stringify } from "devalue";
|
|
2
|
-
import
|
|
3
|
-
import { parse as parse$
|
|
2
|
+
import { parse as parse$1, parseAsync } from "valibot";
|
|
3
|
+
import { parse as parse$2 } from "cookie-es";
|
|
4
4
|
import { Result, ResultAsync } from "neverthrow";
|
|
5
|
-
import { AdminGetUserCommand, AdminInitiateAuthCommand, AdminListGroupsForUserCommand, ChangePasswordCommand, CognitoIdentityProviderClient, ConfirmForgotPasswordCommand, ConfirmSignUpCommand, ForgotPasswordCommand, GlobalSignOutCommand, RespondToAuthChallengeCommand, SignUpCommand } from "@aws-sdk/client-cognito-identity-provider";
|
|
5
|
+
import { AdminGetUserCommand, AdminInitiateAuthCommand, AdminListGroupsForUserCommand, ChangePasswordCommand, CognitoIdentityProviderClient, ConfirmForgotPasswordCommand, ConfirmSignUpCommand, ForgotPasswordCommand, GetTokensFromRefreshTokenCommand, GlobalSignOutCommand, RespondToAuthChallengeCommand, SignUpCommand } from "@aws-sdk/client-cognito-identity-provider";
|
|
6
6
|
import { createHmac } from "node:crypto";
|
|
7
7
|
import { GetObjectCommand, HeadObjectCommand, S3Client } from "@aws-sdk/client-s3";
|
|
8
8
|
import { getSignedUrl as getSignedUrl$1 } from "@aws-sdk/s3-request-presigner";
|
|
@@ -32,8 +32,8 @@ const bodyMethods = [
|
|
|
32
32
|
];
|
|
33
33
|
const queryMethods = ["GET", "DELETE"];
|
|
34
34
|
async function _apiRequest(path, method, input, schema, environment, apiUrl, headers) {
|
|
35
|
-
if (schema) if (schema.async === true) await
|
|
36
|
-
else
|
|
35
|
+
if (schema) if (schema.async === true) await parseAsync(schema, input);
|
|
36
|
+
else parse$1(schema, input);
|
|
37
37
|
let url = `${apiUrl}${apiUrl.endsWith("/") ? "" : "/"}${path}`;
|
|
38
38
|
if (queryMethods.includes(method)) {
|
|
39
39
|
const params = new URLSearchParams();
|
|
@@ -115,36 +115,48 @@ function wrapHandler(handler) {
|
|
|
115
115
|
* The separation means that they can be returned from functions that are certainly run inside a lambda fucntion but theyre not the actual return of the lambda.
|
|
116
116
|
* Im not sure it this is optimal behaviour and if not we will migrate to only using the errorResponse function
|
|
117
117
|
*/
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
118
|
+
function error_lambda_badRequest(message, fieldName, fieldValue) {
|
|
119
|
+
return {
|
|
120
|
+
type: "badRequest",
|
|
121
|
+
message,
|
|
122
|
+
fieldName,
|
|
123
|
+
fieldValue
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function error_lambda_unauthorized(message) {
|
|
127
|
+
return {
|
|
128
|
+
type: "unauthorized",
|
|
129
|
+
message
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
function error_lambda_forbidden(message) {
|
|
133
|
+
return {
|
|
134
|
+
type: "forbidden",
|
|
135
|
+
message
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
function error_lambda_notFound(message, fieldName, fieldValue) {
|
|
139
|
+
return {
|
|
140
|
+
type: "notFound",
|
|
141
|
+
message,
|
|
142
|
+
fieldName,
|
|
143
|
+
fieldValue
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
function error_lambda_conflict(message, fieldName, fieldValue) {
|
|
147
|
+
return {
|
|
148
|
+
type: "conflict",
|
|
149
|
+
message,
|
|
150
|
+
fieldName,
|
|
151
|
+
fieldValue
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
function error_lambda_internal(message) {
|
|
155
|
+
return {
|
|
156
|
+
type: "internal",
|
|
157
|
+
message
|
|
158
|
+
};
|
|
159
|
+
}
|
|
148
160
|
//#endregion
|
|
149
161
|
//#region src/lambda/response.ts
|
|
150
162
|
function field(obj) {
|
|
@@ -245,15 +257,17 @@ function response_ok(body, headers, cookies) {
|
|
|
245
257
|
/**
|
|
246
258
|
* Wraps cookies parse along with the api gateway event with neverthrow
|
|
247
259
|
*/
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
260
|
+
function getCookies(event) {
|
|
261
|
+
return Result.fromThrowable(() => {
|
|
262
|
+
if (!("headers" in event) || !event.headers) throw new Error("No headers in event");
|
|
263
|
+
const cookieString = Array.isArray(event.cookies) && event.cookies.length > 0 ? event.cookies.join("; ") : event.headers.Cookie || event.headers.cookie;
|
|
264
|
+
if (!cookieString) throw new Error("No cookies found in event");
|
|
265
|
+
return parse$2(cookieString);
|
|
266
|
+
}, (e) => {
|
|
267
|
+
if (e instanceof Error) return error_lambda_unauthorized(e.message);
|
|
268
|
+
return error_lambda_unauthorized("Invalid Cookies");
|
|
269
|
+
})();
|
|
270
|
+
}
|
|
257
271
|
//#endregion
|
|
258
272
|
//#region src/cognito/client.ts
|
|
259
273
|
/**
|
|
@@ -448,20 +462,22 @@ function isRecord$2(value) {
|
|
|
448
462
|
/**
|
|
449
463
|
* Performs an AdminGetUserCommand and extracts the user attributes into an object
|
|
450
464
|
*/
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
+
function getUserDetails(a) {
|
|
466
|
+
return ResultAsync.fromThrowable(async () => {
|
|
467
|
+
console.log("getUserDetails: Getting details for user: ", a.username);
|
|
468
|
+
const res = await getCognitoClient().send(new AdminGetUserCommand({
|
|
469
|
+
UserPoolId: a.userPoolId,
|
|
470
|
+
Username: a.username
|
|
471
|
+
}));
|
|
472
|
+
return {
|
|
473
|
+
...res,
|
|
474
|
+
UserAttributes: extractAttributes(res.UserAttributes)
|
|
475
|
+
};
|
|
476
|
+
}, (e) => {
|
|
477
|
+
console.error("getUserDetails:error:", e);
|
|
478
|
+
return error_cognito(e);
|
|
479
|
+
})();
|
|
480
|
+
}
|
|
465
481
|
/**
|
|
466
482
|
* @returns An object of attributes with their names as keys and values as values.
|
|
467
483
|
*/
|
|
@@ -475,16 +491,18 @@ function extractAttributes(attrs) {
|
|
|
475
491
|
/**
|
|
476
492
|
* Performs an AdminGetUserCommand and extracts the user attributes into an object
|
|
477
493
|
*/
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
494
|
+
function getUserGroups(a) {
|
|
495
|
+
return ResultAsync.fromThrowable(async () => {
|
|
496
|
+
console.log("getUserGroups: Getting groups for user: ", a.username);
|
|
497
|
+
return await getCognitoClient().send(new AdminListGroupsForUserCommand({
|
|
498
|
+
UserPoolId: a.userPoolId,
|
|
499
|
+
Username: a.username
|
|
500
|
+
}));
|
|
501
|
+
}, (e) => {
|
|
502
|
+
console.error("getUserGroups:error:", e);
|
|
503
|
+
return error_cognito(e);
|
|
504
|
+
})();
|
|
505
|
+
}
|
|
488
506
|
//#endregion
|
|
489
507
|
//#region src/cognito/password.ts
|
|
490
508
|
/**
|
|
@@ -505,16 +523,18 @@ function computeSecretHash(username, clientId, clientSecret) {
|
|
|
505
523
|
* @param oldPassword - Current password.
|
|
506
524
|
* @param newPassword - New password to set.
|
|
507
525
|
*/
|
|
508
|
-
|
|
509
|
-
return
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
526
|
+
function changePassword(accessToken, oldPassword, newPassword) {
|
|
527
|
+
return ResultAsync.fromThrowable(async () => {
|
|
528
|
+
return getCognitoClient().send(new ChangePasswordCommand({
|
|
529
|
+
AccessToken: accessToken,
|
|
530
|
+
PreviousPassword: oldPassword,
|
|
531
|
+
ProposedPassword: newPassword
|
|
532
|
+
}));
|
|
533
|
+
}, (e) => {
|
|
534
|
+
console.error("ChangePasswordCommand error", e);
|
|
535
|
+
return error_cognito(e);
|
|
536
|
+
})();
|
|
537
|
+
}
|
|
518
538
|
/**
|
|
519
539
|
* Completes a forgot-password flow by submitting the confirmation code and new password.
|
|
520
540
|
*
|
|
@@ -524,18 +544,20 @@ const changePassword = ResultAsync.fromThrowable(async (accessToken, oldPassword
|
|
|
524
544
|
* @param a.clientId - Cognito app client ID.
|
|
525
545
|
* @param a.clientSecret - Cognito app client secret.
|
|
526
546
|
*/
|
|
527
|
-
|
|
528
|
-
return
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
547
|
+
function confirmForgotPassword(a) {
|
|
548
|
+
return ResultAsync.fromThrowable(() => {
|
|
549
|
+
return getCognitoClient().send(new ConfirmForgotPasswordCommand({
|
|
550
|
+
ClientId: a.clientId,
|
|
551
|
+
Username: a.username,
|
|
552
|
+
ConfirmationCode: a.confirmationCode,
|
|
553
|
+
Password: a.newPassword,
|
|
554
|
+
SecretHash: computeSecretHash(a.username, a.clientId, a.clientSecret)
|
|
555
|
+
}));
|
|
556
|
+
}, (e) => {
|
|
557
|
+
console.error("ConfirmForgotPasswordCommand error", e);
|
|
558
|
+
return error_cognito(e);
|
|
559
|
+
})();
|
|
560
|
+
}
|
|
539
561
|
/**
|
|
540
562
|
* Confirms a user's signup using the confirmation code sent by Cognito.
|
|
541
563
|
*
|
|
@@ -544,17 +566,19 @@ const confirmForgotPassword = ResultAsync.fromThrowable((a) => {
|
|
|
544
566
|
* @param a.clientId - Cognito app client ID.
|
|
545
567
|
* @param a.clientSecret - Cognito app client secret.
|
|
546
568
|
*/
|
|
547
|
-
|
|
548
|
-
return
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
569
|
+
function confirmSignup(a) {
|
|
570
|
+
return ResultAsync.fromThrowable(() => {
|
|
571
|
+
return getCognitoClient().send(new ConfirmSignUpCommand({
|
|
572
|
+
ClientId: a.clientId,
|
|
573
|
+
Username: a.username,
|
|
574
|
+
ConfirmationCode: a.confirmationCode,
|
|
575
|
+
SecretHash: computeSecretHash(a.username, a.clientId, a.clientSecret)
|
|
576
|
+
}));
|
|
577
|
+
}, (e) => {
|
|
578
|
+
console.error("ConfirmSignUpCommand error", e);
|
|
579
|
+
return error_cognito(e);
|
|
580
|
+
})();
|
|
581
|
+
}
|
|
558
582
|
/**
|
|
559
583
|
* Starts a forgot-password flow by sending a reset code to the user.
|
|
560
584
|
*
|
|
@@ -562,16 +586,18 @@ const confirmSignup = ResultAsync.fromThrowable((a) => {
|
|
|
562
586
|
* @param a.clientId - Cognito app client ID.
|
|
563
587
|
* @param a.clientSecret - Cognito app client secret.
|
|
564
588
|
*/
|
|
565
|
-
|
|
566
|
-
return
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
589
|
+
function forgotPassword(a) {
|
|
590
|
+
return ResultAsync.fromThrowable(() => {
|
|
591
|
+
return getCognitoClient().send(new ForgotPasswordCommand({
|
|
592
|
+
ClientId: a.clientId,
|
|
593
|
+
Username: a.username,
|
|
594
|
+
SecretHash: computeSecretHash(a.username, a.clientId, a.clientSecret)
|
|
595
|
+
}));
|
|
596
|
+
}, (e) => {
|
|
597
|
+
console.error("ForgotPasswordCommand error", e);
|
|
598
|
+
return error_cognito(e);
|
|
599
|
+
})();
|
|
600
|
+
}
|
|
575
601
|
/**
|
|
576
602
|
* Signs a user in with ADMIN_USER_PASSWORD_AUTH.
|
|
577
603
|
*
|
|
@@ -581,21 +607,34 @@ const forgotPassword = ResultAsync.fromThrowable((a) => {
|
|
|
581
607
|
* @param a.clientSecret - Cognito app client secret.
|
|
582
608
|
* @param a.userPoolId - Cognito user pool ID.
|
|
583
609
|
*/
|
|
584
|
-
|
|
585
|
-
return
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
610
|
+
function login(a) {
|
|
611
|
+
return ResultAsync.fromThrowable(() => {
|
|
612
|
+
return getCognitoClient().send(new AdminInitiateAuthCommand({
|
|
613
|
+
AuthFlow: "ADMIN_USER_PASSWORD_AUTH",
|
|
614
|
+
ClientId: a.clientId,
|
|
615
|
+
UserPoolId: a.userPoolId,
|
|
616
|
+
AuthParameters: {
|
|
617
|
+
USERNAME: a.username,
|
|
618
|
+
PASSWORD: a.password,
|
|
619
|
+
SECRET_HASH: computeSecretHash(a.username, a.clientId, a.clientSecret)
|
|
620
|
+
}
|
|
621
|
+
}));
|
|
622
|
+
}, (e) => {
|
|
623
|
+
console.error("AdminInitiateAuthCommand error", e);
|
|
624
|
+
return error_cognito(e);
|
|
625
|
+
})();
|
|
626
|
+
}
|
|
627
|
+
/**
|
|
628
|
+
* Sends a GetTokensFromRefreshTokenCommand
|
|
629
|
+
*/
|
|
630
|
+
function refreshTokens(a) {
|
|
631
|
+
return ResultAsync.fromThrowable(() => {
|
|
632
|
+
return getCognitoClient().send(new GetTokensFromRefreshTokenCommand(a));
|
|
633
|
+
}, (e) => {
|
|
634
|
+
console.error("refreshTokens: GetTokensFromRefreshTokenCommand error", e);
|
|
635
|
+
return error_cognito(e);
|
|
636
|
+
})();
|
|
637
|
+
}
|
|
599
638
|
/**
|
|
600
639
|
* Exchanges a refresh token for new tokens.
|
|
601
640
|
*
|
|
@@ -605,31 +644,35 @@ const login = ResultAsync.fromThrowable((a) => {
|
|
|
605
644
|
* @param a.clientSecret - Cognito app client secret.
|
|
606
645
|
* @param a.userPoolId - Cognito user pool ID.
|
|
607
646
|
*/
|
|
608
|
-
|
|
609
|
-
return
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
647
|
+
function refreshTokensAuth(a) {
|
|
648
|
+
return ResultAsync.fromThrowable(() => {
|
|
649
|
+
return getCognitoClient().send(new AdminInitiateAuthCommand({
|
|
650
|
+
AuthFlow: "REFRESH_TOKEN_AUTH",
|
|
651
|
+
ClientId: a.clientId,
|
|
652
|
+
UserPoolId: a.userPoolId,
|
|
653
|
+
AuthParameters: {
|
|
654
|
+
REFRESH_TOKEN: a.refreshToken,
|
|
655
|
+
SECRET_HASH: computeSecretHash(a.username, a.clientId, a.clientSecret)
|
|
656
|
+
}
|
|
657
|
+
}));
|
|
658
|
+
}, (e) => {
|
|
659
|
+
console.error("refreshTokens: AdminInitiateAuthCommand error", e);
|
|
660
|
+
return error_cognito(e);
|
|
661
|
+
})();
|
|
662
|
+
}
|
|
622
663
|
/**
|
|
623
664
|
* Globally signs out a user by invalidating all refresh tokens.
|
|
624
665
|
*
|
|
625
666
|
* @param accessToken - Access token for the authenticated user.
|
|
626
667
|
*/
|
|
627
|
-
|
|
628
|
-
return
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
668
|
+
function logout(accessToken) {
|
|
669
|
+
return ResultAsync.fromThrowable(() => {
|
|
670
|
+
return getCognitoClient().send(new GlobalSignOutCommand({ AccessToken: accessToken }));
|
|
671
|
+
}, (e) => {
|
|
672
|
+
console.error("GlobalSignOutCommand error", e);
|
|
673
|
+
return error_cognito(e);
|
|
674
|
+
})();
|
|
675
|
+
}
|
|
633
676
|
/**
|
|
634
677
|
* Completes a NEW_PASSWORD_REQUIRED challenge for users who must set a new password.
|
|
635
678
|
*
|
|
@@ -639,21 +682,23 @@ const logout = ResultAsync.fromThrowable((accessToken) => {
|
|
|
639
682
|
* @param a.clientId - Cognito app client ID.
|
|
640
683
|
* @param a.clientSecret - Cognito app client secret.
|
|
641
684
|
*/
|
|
642
|
-
|
|
643
|
-
return
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
}
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
685
|
+
function resetPassword(a) {
|
|
686
|
+
return ResultAsync.fromThrowable(() => {
|
|
687
|
+
return getCognitoClient().send(new RespondToAuthChallengeCommand({
|
|
688
|
+
ChallengeName: "NEW_PASSWORD_REQUIRED",
|
|
689
|
+
ClientId: a.clientId,
|
|
690
|
+
Session: a.session,
|
|
691
|
+
ChallengeResponses: {
|
|
692
|
+
SECRET_HASH: computeSecretHash(a.username, a.clientId, a.clientSecret),
|
|
693
|
+
NEW_PASSWORD: a.newPassword,
|
|
694
|
+
USERNAME: a.username
|
|
695
|
+
}
|
|
696
|
+
}));
|
|
697
|
+
}, (e) => {
|
|
698
|
+
console.error("RespondToAuthChallengeCommand error", e);
|
|
699
|
+
return error_cognito(e);
|
|
700
|
+
})();
|
|
701
|
+
}
|
|
657
702
|
/**
|
|
658
703
|
* Registers a new user with Cognito and optional custom attributes.
|
|
659
704
|
*
|
|
@@ -663,28 +708,30 @@ const resetPassword = ResultAsync.fromThrowable((a) => {
|
|
|
663
708
|
* @param a.clientSecret - Cognito app client secret.
|
|
664
709
|
* @param a.<attribute> - Any additional user attributes to set.
|
|
665
710
|
*/
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
}
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
711
|
+
function signUp(a) {
|
|
712
|
+
return ResultAsync.fromThrowable(() => {
|
|
713
|
+
const cognitoClient = getCognitoClient();
|
|
714
|
+
const secretHash = computeSecretHash(a.username, a.clientId, a.clientSecret);
|
|
715
|
+
return cognitoClient.send(new SignUpCommand({
|
|
716
|
+
ClientId: a.clientId,
|
|
717
|
+
Username: a.username,
|
|
718
|
+
Password: a.password,
|
|
719
|
+
SecretHash: secretHash,
|
|
720
|
+
UserAttributes: Object.entries(a).filter(([key]) => ![
|
|
721
|
+
"username",
|
|
722
|
+
"password",
|
|
723
|
+
"clientId",
|
|
724
|
+
"clientSecret"
|
|
725
|
+
].includes(key)).map(([key, value]) => ({
|
|
726
|
+
Name: key,
|
|
727
|
+
Value: value
|
|
728
|
+
}))
|
|
729
|
+
}));
|
|
730
|
+
}, (e) => {
|
|
731
|
+
console.error("SignUpCommand error", e);
|
|
732
|
+
return error_cognito(e);
|
|
733
|
+
})();
|
|
734
|
+
}
|
|
688
735
|
/**
|
|
689
736
|
* Exchanges an OAuth2 authorization code for Cognito tokens using the token endpoint.
|
|
690
737
|
* See https://docs.aws.amazon.com/cognito/latest/developerguide/token-endpoint.html for request/response fields and grant details.
|
|
@@ -696,30 +743,32 @@ const signUp = ResultAsync.fromThrowable((a) => {
|
|
|
696
743
|
* @param a.cognitoDomain - Cognito domain URL (e.g., your-domain.auth.region.amazoncognito.com).
|
|
697
744
|
* @returns Parsed token payload containing `access_token`, `id_token`, `refresh_token`, token type, and expiry.
|
|
698
745
|
*/
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
746
|
+
function verifyOAuthToken(a) {
|
|
747
|
+
return ResultAsync.fromThrowable(async () => {
|
|
748
|
+
const basicAuth = Buffer.from(`${a.clientId}:${a.clientSecret}`).toString("base64");
|
|
749
|
+
const params = new URLSearchParams();
|
|
750
|
+
params.append("grant_type", "authorization_code");
|
|
751
|
+
params.append("code", a.code);
|
|
752
|
+
params.append("redirect_uri", a.redirectUri);
|
|
753
|
+
console.log("verifyOAuthToken: params", params.toString());
|
|
754
|
+
const tokenRes = await fetch(`https://${a.cognitoDomain}/oauth2/token`, {
|
|
755
|
+
method: "POST",
|
|
756
|
+
headers: {
|
|
757
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
758
|
+
Authorization: `Basic ${basicAuth}`
|
|
759
|
+
},
|
|
760
|
+
body: params.toString()
|
|
761
|
+
});
|
|
762
|
+
if (!tokenRes.ok) {
|
|
763
|
+
console.error("verifyOAuthToken: token exchange failed", await tokenRes.text());
|
|
764
|
+
throw Object.assign(/* @__PURE__ */ new Error("OAuth token exchange failed"), { name: "NotAuthorizedException" });
|
|
765
|
+
}
|
|
766
|
+
return await tokenRes.json();
|
|
767
|
+
}, (e) => {
|
|
768
|
+
console.error("verifyOAuthToken:error", e);
|
|
769
|
+
return error_cognito(e);
|
|
770
|
+
})();
|
|
771
|
+
}
|
|
723
772
|
/**
|
|
724
773
|
* Exchanges an OAuth2 refresh token for Cognito tokens using the oauth token endpoint.
|
|
725
774
|
* See https://docs.aws.amazon.com/cognito/latest/developerguide/token-endpoint.html for request/response fields and grant details.
|
|
@@ -730,29 +779,31 @@ const verifyOAuthToken = ResultAsync.fromThrowable(async (a) => {
|
|
|
730
779
|
* @param a.cognitoDomain - Cognito domain URL (e.g., your-domain.auth.region.amazoncognito.com).
|
|
731
780
|
* @returns Parsed token payload containing `access_token`, `id_token`, `refresh_token`, token type, and expiry.
|
|
732
781
|
*/
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
782
|
+
function refreshOAuthToken(a) {
|
|
783
|
+
return ResultAsync.fromThrowable(async () => {
|
|
784
|
+
const basicAuth = Buffer.from(`${a.clientId}:${a.clientSecret}`).toString("base64");
|
|
785
|
+
const params = new URLSearchParams();
|
|
786
|
+
params.append("grant_type", "refresh_token");
|
|
787
|
+
params.append("refresh_token", a.refreshToken);
|
|
788
|
+
console.log("refreshOAuthToken: params", params.toString());
|
|
789
|
+
const tokenRes = await fetch(`https://${a.cognitoDomain}/oauth2/token`, {
|
|
790
|
+
method: "POST",
|
|
791
|
+
headers: {
|
|
792
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
793
|
+
Authorization: `Basic ${basicAuth}`
|
|
794
|
+
},
|
|
795
|
+
body: params.toString()
|
|
796
|
+
});
|
|
797
|
+
if (!tokenRes.ok) {
|
|
798
|
+
console.error("refreshOAuthToken: token exchange failed", await tokenRes.text());
|
|
799
|
+
throw Object.assign(/* @__PURE__ */ new Error("OAuth token refresh failed"), { name: "NotAuthorizedException" });
|
|
800
|
+
}
|
|
801
|
+
return await tokenRes.json();
|
|
802
|
+
}, (e) => {
|
|
803
|
+
console.error("refreshOAuthToken:error", e);
|
|
804
|
+
return error_cognito(e);
|
|
805
|
+
})();
|
|
806
|
+
}
|
|
756
807
|
//#endregion
|
|
757
808
|
//#region src/s3/client.ts
|
|
758
809
|
/**
|
|
@@ -942,10 +993,12 @@ function getHttpStatusCode$1(error) {
|
|
|
942
993
|
}
|
|
943
994
|
//#endregion
|
|
944
995
|
//#region src/s3/signedUrl.ts
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
996
|
+
function getSignedUrl(...args) {
|
|
997
|
+
return ResultAsync.fromThrowable(getSignedUrl$1, (e) => {
|
|
998
|
+
console.error("getSignedUrl: Failed to get signed url", e);
|
|
999
|
+
return error_s3(e);
|
|
1000
|
+
})(...args);
|
|
1001
|
+
}
|
|
949
1002
|
//#endregion
|
|
950
1003
|
//#region src/s3/object.ts
|
|
951
1004
|
/**
|
|
@@ -955,23 +1008,25 @@ const getSignedUrl = ResultAsync.fromThrowable(getSignedUrl$1, (e) => {
|
|
|
955
1008
|
* @param {string} key - The key of the object to retrieve.
|
|
956
1009
|
* @returns {Promise<Buffer>} A promise that resolves to the object data as a Buffer.
|
|
957
1010
|
*/
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
}
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
1011
|
+
function getObject(bucketName, key) {
|
|
1012
|
+
return ResultAsync.fromThrowable(async () => {
|
|
1013
|
+
const s3 = getS3();
|
|
1014
|
+
const cmd = new GetObjectCommand({
|
|
1015
|
+
Bucket: bucketName,
|
|
1016
|
+
Key: key
|
|
1017
|
+
});
|
|
1018
|
+
const stream = (await s3.send(cmd)).Body;
|
|
1019
|
+
return new Promise((resolve, reject) => {
|
|
1020
|
+
const chunks = [];
|
|
1021
|
+
stream.on("data", (chunk) => chunks.push(chunk));
|
|
1022
|
+
stream.on("end", () => resolve(Buffer.concat(chunks)));
|
|
1023
|
+
stream.on("error", reject);
|
|
1024
|
+
});
|
|
1025
|
+
}, (e) => {
|
|
1026
|
+
console.error(`getObjectt: Error getting object from S3: ${e}`);
|
|
1027
|
+
return error_s3(e);
|
|
1028
|
+
})();
|
|
1029
|
+
}
|
|
975
1030
|
/**
|
|
976
1031
|
* Convenience function to get an object from S3 and return it as a string.
|
|
977
1032
|
*/
|
|
@@ -985,22 +1040,24 @@ function getObjectString(bucketName, key) {
|
|
|
985
1040
|
* @param {string} key - The key of the object to retrieve.
|
|
986
1041
|
* @returns {Promise<Buffer>} A promise that resolves to a boolean.
|
|
987
1042
|
*/
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
}
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1043
|
+
function objectExists(bucketName, key) {
|
|
1044
|
+
return ResultAsync.fromThrowable(async () => {
|
|
1045
|
+
const s3 = getS3();
|
|
1046
|
+
try {
|
|
1047
|
+
const cmd = new HeadObjectCommand({
|
|
1048
|
+
Bucket: bucketName,
|
|
1049
|
+
Key: key
|
|
1050
|
+
});
|
|
1051
|
+
return (await s3.send(cmd)).$metadata.httpStatusCode === 200;
|
|
1052
|
+
} catch (e) {
|
|
1053
|
+
if (is_s3_notFound(e)) return false;
|
|
1054
|
+
throw e;
|
|
1055
|
+
}
|
|
1056
|
+
}, (e) => {
|
|
1057
|
+
console.error(`objectExists: Error getting object head from S3: ${e}`);
|
|
1058
|
+
return error_s3(e);
|
|
1059
|
+
})();
|
|
1060
|
+
}
|
|
1004
1061
|
//#endregion
|
|
1005
1062
|
//#region src/dynamo/errors.ts
|
|
1006
1063
|
const defaultErrors$1 = {
|
|
@@ -1598,6 +1655,6 @@ async function readPackageJson(filePath) {
|
|
|
1598
1655
|
return JSON.parse(await readFile(filePath, { encoding: "utf-8" }));
|
|
1599
1656
|
}
|
|
1600
1657
|
//#endregion
|
|
1601
|
-
export { HTTPMethods, changePassword, computeSecretHash, confirmForgotPassword, confirmSignup, createApiRequest, deepDiff, error_cognito, error_dynamo, error_lambda_badRequest, error_lambda_conflict, error_lambda_forbidden, error_lambda_fromCognito, error_lambda_fromDynamo, error_lambda_fromS3, error_lambda_fromSes, error_lambda_internal, error_lambda_notFound, error_lambda_unauthorized, error_s3, error_ses, exists, extractAttributes, extractToc, forgotPassword, getByPath, getCognitoClient, getCookies, getErrorName, getObject, getObjectString, getS3, getSchemaByPath, getSignedUrl, getUserDetails, getUserGroups, isRecord, isSchema, is_s3_notFound, login, logout, objectExists, parseRaw, pruneToShape, readPackageJson, refreshOAuthToken, refreshTokens, resetPassword, response_error, response_ok, response_valibotError, setByPath, signUp, unwrap, verifyOAuthToken, wrapHandler, writeIfDifferent };
|
|
1658
|
+
export { HTTPMethods, changePassword, computeSecretHash, confirmForgotPassword, confirmSignup, createApiRequest, deepDiff, error_cognito, error_dynamo, error_lambda_badRequest, error_lambda_conflict, error_lambda_forbidden, error_lambda_fromCognito, error_lambda_fromDynamo, error_lambda_fromS3, error_lambda_fromSes, error_lambda_internal, error_lambda_notFound, error_lambda_unauthorized, error_s3, error_ses, exists, extractAttributes, extractToc, forgotPassword, getByPath, getCognitoClient, getCookies, getErrorName, getObject, getObjectString, getS3, getSchemaByPath, getSignedUrl, getUserDetails, getUserGroups, isRecord, isSchema, is_s3_notFound, login, logout, objectExists, parseRaw, pruneToShape, readPackageJson, refreshOAuthToken, refreshTokens, refreshTokensAuth, resetPassword, response_error, response_ok, response_valibotError, setByPath, signUp, unwrap, verifyOAuthToken, wrapHandler, writeIfDifferent };
|
|
1602
1659
|
|
|
1603
1660
|
//# sourceMappingURL=worker.mjs.map
|