ts-ag 1.2.3 → 1.2.4
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/index.d.mts +3 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +255 -240
- package/dist/index.mjs.map +1 -1
- package/dist/worker.d.mts +3 -2
- package/dist/worker.d.mts.map +1 -1
- package/dist/worker.mjs +255 -240
- package/dist/worker.mjs.map +1 -1
- package/package.json +3 -1
package/dist/index.mjs
CHANGED
|
@@ -255,19 +255,20 @@ function response_ok(body, headers, cookies) {
|
|
|
255
255
|
}
|
|
256
256
|
//#endregion
|
|
257
257
|
//#region src/lambda/server/authentication.ts
|
|
258
|
+
const getCookiesResult = /* @__PURE__ */ Result.fromThrowable((event) => {
|
|
259
|
+
if (!("headers" in event) || !event.headers) throw new Error("No headers in event");
|
|
260
|
+
const cookieString = Array.isArray(event.cookies) && event.cookies.length > 0 ? event.cookies.join("; ") : event.headers.Cookie || event.headers.cookie;
|
|
261
|
+
if (!cookieString) throw new Error("No cookies found in event");
|
|
262
|
+
return parse$2(cookieString);
|
|
263
|
+
}, (e) => {
|
|
264
|
+
if (e instanceof Error) return error_lambda_unauthorized(e.message);
|
|
265
|
+
return error_lambda_unauthorized("Invalid Cookies");
|
|
266
|
+
});
|
|
258
267
|
/**
|
|
259
268
|
* Wraps cookies parse along with the api gateway event with neverthrow
|
|
260
269
|
*/
|
|
261
270
|
function getCookies(event) {
|
|
262
|
-
return
|
|
263
|
-
if (!("headers" in event) || !event.headers) throw new Error("No headers in event");
|
|
264
|
-
const cookieString = Array.isArray(event.cookies) && event.cookies.length > 0 ? event.cookies.join("; ") : event.headers.Cookie || event.headers.cookie;
|
|
265
|
-
if (!cookieString) throw new Error("No cookies found in event");
|
|
266
|
-
return parse$2(cookieString);
|
|
267
|
-
}, (e) => {
|
|
268
|
-
if (e instanceof Error) return error_lambda_unauthorized(e.message);
|
|
269
|
-
return error_lambda_unauthorized("Invalid Cookies");
|
|
270
|
-
})();
|
|
271
|
+
return getCookiesResult(event);
|
|
271
272
|
}
|
|
272
273
|
//#endregion
|
|
273
274
|
//#region src/cognito/client.ts
|
|
@@ -460,24 +461,35 @@ function isRecord$2(value) {
|
|
|
460
461
|
}
|
|
461
462
|
//#endregion
|
|
462
463
|
//#region src/cognito/user.ts
|
|
464
|
+
const getUserDetailsResult = /* @__PURE__ */ ResultAsync.fromThrowable(async (a) => {
|
|
465
|
+
console.log("getUserDetails: Getting details for user: ", a.username);
|
|
466
|
+
const res = await getCognitoClient().send(new AdminGetUserCommand({
|
|
467
|
+
UserPoolId: a.userPoolId,
|
|
468
|
+
Username: a.username
|
|
469
|
+
}));
|
|
470
|
+
return {
|
|
471
|
+
...res,
|
|
472
|
+
UserAttributes: extractAttributes(res.UserAttributes)
|
|
473
|
+
};
|
|
474
|
+
}, (e) => {
|
|
475
|
+
console.error("getUserDetails:error:", e);
|
|
476
|
+
return error_cognito(e);
|
|
477
|
+
});
|
|
478
|
+
const getUserGroupsResult = /* @__PURE__ */ ResultAsync.fromThrowable(async (a) => {
|
|
479
|
+
console.log("getUserGroups: Getting groups for user: ", a.username);
|
|
480
|
+
return getCognitoClient().send(new AdminListGroupsForUserCommand({
|
|
481
|
+
UserPoolId: a.userPoolId,
|
|
482
|
+
Username: a.username
|
|
483
|
+
}));
|
|
484
|
+
}, (e) => {
|
|
485
|
+
console.error("getUserGroups:error:", e);
|
|
486
|
+
return error_cognito(e);
|
|
487
|
+
});
|
|
463
488
|
/**
|
|
464
489
|
* Performs an AdminGetUserCommand and extracts the user attributes into an object
|
|
465
490
|
*/
|
|
466
491
|
function getUserDetails(a) {
|
|
467
|
-
return
|
|
468
|
-
console.log("getUserDetails: Getting details for user: ", a.username);
|
|
469
|
-
const res = await getCognitoClient().send(new AdminGetUserCommand({
|
|
470
|
-
UserPoolId: a.userPoolId,
|
|
471
|
-
Username: a.username
|
|
472
|
-
}));
|
|
473
|
-
return {
|
|
474
|
-
...res,
|
|
475
|
-
UserAttributes: extractAttributes(res.UserAttributes)
|
|
476
|
-
};
|
|
477
|
-
}, (e) => {
|
|
478
|
-
console.error("getUserDetails:error:", e);
|
|
479
|
-
return error_cognito(e);
|
|
480
|
-
})();
|
|
492
|
+
return getUserDetailsResult(a);
|
|
481
493
|
}
|
|
482
494
|
/**
|
|
483
495
|
* @returns An object of attributes with their names as keys and values as values.
|
|
@@ -493,19 +505,178 @@ function extractAttributes(attrs) {
|
|
|
493
505
|
* Performs an AdminGetUserCommand and extracts the user attributes into an object
|
|
494
506
|
*/
|
|
495
507
|
function getUserGroups(a) {
|
|
496
|
-
return
|
|
497
|
-
console.log("getUserGroups: Getting groups for user: ", a.username);
|
|
498
|
-
return await getCognitoClient().send(new AdminListGroupsForUserCommand({
|
|
499
|
-
UserPoolId: a.userPoolId,
|
|
500
|
-
Username: a.username
|
|
501
|
-
}));
|
|
502
|
-
}, (e) => {
|
|
503
|
-
console.error("getUserGroups:error:", e);
|
|
504
|
-
return error_cognito(e);
|
|
505
|
-
})();
|
|
508
|
+
return getUserGroupsResult(a);
|
|
506
509
|
}
|
|
507
510
|
//#endregion
|
|
508
511
|
//#region src/cognito/password.ts
|
|
512
|
+
const changePasswordResult = /* @__PURE__ */ ResultAsync.fromThrowable(async (accessToken, oldPassword, newPassword) => {
|
|
513
|
+
return getCognitoClient().send(new ChangePasswordCommand({
|
|
514
|
+
AccessToken: accessToken,
|
|
515
|
+
PreviousPassword: oldPassword,
|
|
516
|
+
ProposedPassword: newPassword
|
|
517
|
+
}));
|
|
518
|
+
}, (e) => {
|
|
519
|
+
console.error("ChangePasswordCommand error", e);
|
|
520
|
+
return error_cognito(e);
|
|
521
|
+
});
|
|
522
|
+
const confirmForgotPasswordResult = /* @__PURE__ */ ResultAsync.fromThrowable((a) => {
|
|
523
|
+
return getCognitoClient().send(new ConfirmForgotPasswordCommand({
|
|
524
|
+
ClientId: a.clientId,
|
|
525
|
+
Username: a.username,
|
|
526
|
+
ConfirmationCode: a.confirmationCode,
|
|
527
|
+
Password: a.newPassword,
|
|
528
|
+
SecretHash: computeSecretHash(a.username, a.clientId, a.clientSecret)
|
|
529
|
+
}));
|
|
530
|
+
}, (e) => {
|
|
531
|
+
console.error("ConfirmForgotPasswordCommand error", e);
|
|
532
|
+
return error_cognito(e);
|
|
533
|
+
});
|
|
534
|
+
const confirmSignupResult = /* @__PURE__ */ ResultAsync.fromThrowable((a) => {
|
|
535
|
+
return getCognitoClient().send(new ConfirmSignUpCommand({
|
|
536
|
+
ClientId: a.clientId,
|
|
537
|
+
Username: a.username,
|
|
538
|
+
ConfirmationCode: a.confirmationCode,
|
|
539
|
+
SecretHash: computeSecretHash(a.username, a.clientId, a.clientSecret)
|
|
540
|
+
}));
|
|
541
|
+
}, (e) => {
|
|
542
|
+
console.error("ConfirmSignUpCommand error", e);
|
|
543
|
+
return error_cognito(e);
|
|
544
|
+
});
|
|
545
|
+
const forgotPasswordResult = /* @__PURE__ */ ResultAsync.fromThrowable((a) => {
|
|
546
|
+
return getCognitoClient().send(new ForgotPasswordCommand({
|
|
547
|
+
ClientId: a.clientId,
|
|
548
|
+
Username: a.username,
|
|
549
|
+
SecretHash: computeSecretHash(a.username, a.clientId, a.clientSecret)
|
|
550
|
+
}));
|
|
551
|
+
}, (e) => {
|
|
552
|
+
console.error("ForgotPasswordCommand error", e);
|
|
553
|
+
return error_cognito(e);
|
|
554
|
+
});
|
|
555
|
+
const loginResult = /* @__PURE__ */ ResultAsync.fromThrowable((a) => {
|
|
556
|
+
return getCognitoClient().send(new AdminInitiateAuthCommand({
|
|
557
|
+
AuthFlow: "ADMIN_USER_PASSWORD_AUTH",
|
|
558
|
+
ClientId: a.clientId,
|
|
559
|
+
UserPoolId: a.userPoolId,
|
|
560
|
+
AuthParameters: {
|
|
561
|
+
USERNAME: a.username,
|
|
562
|
+
PASSWORD: a.password,
|
|
563
|
+
SECRET_HASH: computeSecretHash(a.username, a.clientId, a.clientSecret)
|
|
564
|
+
}
|
|
565
|
+
}));
|
|
566
|
+
}, (e) => {
|
|
567
|
+
console.error("AdminInitiateAuthCommand error", e);
|
|
568
|
+
return error_cognito(e);
|
|
569
|
+
});
|
|
570
|
+
const refreshTokensResult = /* @__PURE__ */ ResultAsync.fromThrowable((a) => {
|
|
571
|
+
return getCognitoClient().send(new GetTokensFromRefreshTokenCommand(a));
|
|
572
|
+
}, (e) => {
|
|
573
|
+
console.error("refreshTokens: GetTokensFromRefreshTokenCommand error", e);
|
|
574
|
+
return error_cognito(e);
|
|
575
|
+
});
|
|
576
|
+
const refreshTokensAuthResult = /* @__PURE__ */ ResultAsync.fromThrowable((a) => {
|
|
577
|
+
return getCognitoClient().send(new AdminInitiateAuthCommand({
|
|
578
|
+
AuthFlow: "REFRESH_TOKEN_AUTH",
|
|
579
|
+
ClientId: a.clientId,
|
|
580
|
+
UserPoolId: a.userPoolId,
|
|
581
|
+
AuthParameters: {
|
|
582
|
+
REFRESH_TOKEN: a.refreshToken,
|
|
583
|
+
SECRET_HASH: computeSecretHash(a.username, a.clientId, a.clientSecret)
|
|
584
|
+
}
|
|
585
|
+
}));
|
|
586
|
+
}, (e) => {
|
|
587
|
+
console.error("refreshTokens: AdminInitiateAuthCommand error", e);
|
|
588
|
+
return error_cognito(e);
|
|
589
|
+
});
|
|
590
|
+
const logoutResult = /* @__PURE__ */ ResultAsync.fromThrowable((accessToken) => {
|
|
591
|
+
return getCognitoClient().send(new GlobalSignOutCommand({ AccessToken: accessToken }));
|
|
592
|
+
}, (e) => {
|
|
593
|
+
console.error("GlobalSignOutCommand error", e);
|
|
594
|
+
return error_cognito(e);
|
|
595
|
+
});
|
|
596
|
+
const resetPasswordResult = /* @__PURE__ */ ResultAsync.fromThrowable((a) => {
|
|
597
|
+
return getCognitoClient().send(new RespondToAuthChallengeCommand({
|
|
598
|
+
ChallengeName: "NEW_PASSWORD_REQUIRED",
|
|
599
|
+
ClientId: a.clientId,
|
|
600
|
+
Session: a.session,
|
|
601
|
+
ChallengeResponses: {
|
|
602
|
+
SECRET_HASH: computeSecretHash(a.username, a.clientId, a.clientSecret),
|
|
603
|
+
NEW_PASSWORD: a.newPassword,
|
|
604
|
+
USERNAME: a.username
|
|
605
|
+
}
|
|
606
|
+
}));
|
|
607
|
+
}, (e) => {
|
|
608
|
+
console.error("RespondToAuthChallengeCommand error", e);
|
|
609
|
+
return error_cognito(e);
|
|
610
|
+
});
|
|
611
|
+
const signUpResult = /* @__PURE__ */ ResultAsync.fromThrowable((a) => {
|
|
612
|
+
const cognitoClient = getCognitoClient();
|
|
613
|
+
const secretHash = computeSecretHash(a.username, a.clientId, a.clientSecret);
|
|
614
|
+
return cognitoClient.send(new SignUpCommand({
|
|
615
|
+
ClientId: a.clientId,
|
|
616
|
+
Username: a.username,
|
|
617
|
+
Password: a.password,
|
|
618
|
+
SecretHash: secretHash,
|
|
619
|
+
UserAttributes: Object.entries(a).filter(([key]) => ![
|
|
620
|
+
"username",
|
|
621
|
+
"password",
|
|
622
|
+
"clientId",
|
|
623
|
+
"clientSecret"
|
|
624
|
+
].includes(key)).map(([key, value]) => ({
|
|
625
|
+
Name: key,
|
|
626
|
+
Value: value
|
|
627
|
+
}))
|
|
628
|
+
}));
|
|
629
|
+
}, (e) => {
|
|
630
|
+
console.error("SignUpCommand error", e);
|
|
631
|
+
return error_cognito(e);
|
|
632
|
+
});
|
|
633
|
+
const verifyOAuthTokenResult = /* @__PURE__ */ ResultAsync.fromThrowable(async (a) => {
|
|
634
|
+
const basicAuth = Buffer.from(`${a.clientId}:${a.clientSecret}`).toString("base64");
|
|
635
|
+
const params = new URLSearchParams();
|
|
636
|
+
params.append("grant_type", "authorization_code");
|
|
637
|
+
params.append("code", a.code);
|
|
638
|
+
params.append("redirect_uri", a.redirectUri);
|
|
639
|
+
console.log("verifyOAuthToken: params", params.toString());
|
|
640
|
+
const tokenRes = await fetch(`https://${a.cognitoDomain}/oauth2/token`, {
|
|
641
|
+
method: "POST",
|
|
642
|
+
headers: {
|
|
643
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
644
|
+
Authorization: `Basic ${basicAuth}`
|
|
645
|
+
},
|
|
646
|
+
body: params.toString()
|
|
647
|
+
});
|
|
648
|
+
if (!tokenRes.ok) {
|
|
649
|
+
console.error("verifyOAuthToken: token exchange failed", await tokenRes.text());
|
|
650
|
+
throw Object.assign(/* @__PURE__ */ new Error("OAuth token exchange failed"), { name: "NotAuthorizedException" });
|
|
651
|
+
}
|
|
652
|
+
return await tokenRes.json();
|
|
653
|
+
}, (e) => {
|
|
654
|
+
console.error("verifyOAuthToken:error", e);
|
|
655
|
+
return error_cognito(e);
|
|
656
|
+
});
|
|
657
|
+
const refreshOAuthTokenResult = /* @__PURE__ */ ResultAsync.fromThrowable(async (a) => {
|
|
658
|
+
const basicAuth = Buffer.from(`${a.clientId}:${a.clientSecret}`).toString("base64");
|
|
659
|
+
const params = new URLSearchParams();
|
|
660
|
+
params.append("grant_type", "refresh_token");
|
|
661
|
+
params.append("refresh_token", a.refreshToken);
|
|
662
|
+
console.log("refreshOAuthToken: params", params.toString());
|
|
663
|
+
const tokenRes = await fetch(`https://${a.cognitoDomain}/oauth2/token`, {
|
|
664
|
+
method: "POST",
|
|
665
|
+
headers: {
|
|
666
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
667
|
+
Authorization: `Basic ${basicAuth}`
|
|
668
|
+
},
|
|
669
|
+
body: params.toString()
|
|
670
|
+
});
|
|
671
|
+
if (!tokenRes.ok) {
|
|
672
|
+
console.error("refreshOAuthToken: token exchange failed", await tokenRes.text());
|
|
673
|
+
throw Object.assign(/* @__PURE__ */ new Error("OAuth token refresh failed"), { name: "NotAuthorizedException" });
|
|
674
|
+
}
|
|
675
|
+
return await tokenRes.json();
|
|
676
|
+
}, (e) => {
|
|
677
|
+
console.error("refreshOAuthToken:error", e);
|
|
678
|
+
return error_cognito(e);
|
|
679
|
+
});
|
|
509
680
|
/**
|
|
510
681
|
* Computes Cognito secret hash used for client-side authentication flows.
|
|
511
682
|
*
|
|
@@ -525,16 +696,7 @@ function computeSecretHash(username, clientId, clientSecret) {
|
|
|
525
696
|
* @param newPassword - New password to set.
|
|
526
697
|
*/
|
|
527
698
|
function changePassword(accessToken, oldPassword, newPassword) {
|
|
528
|
-
return
|
|
529
|
-
return getCognitoClient().send(new ChangePasswordCommand({
|
|
530
|
-
AccessToken: accessToken,
|
|
531
|
-
PreviousPassword: oldPassword,
|
|
532
|
-
ProposedPassword: newPassword
|
|
533
|
-
}));
|
|
534
|
-
}, (e) => {
|
|
535
|
-
console.error("ChangePasswordCommand error", e);
|
|
536
|
-
return error_cognito(e);
|
|
537
|
-
})();
|
|
699
|
+
return changePasswordResult(accessToken, oldPassword, newPassword);
|
|
538
700
|
}
|
|
539
701
|
/**
|
|
540
702
|
* Completes a forgot-password flow by submitting the confirmation code and new password.
|
|
@@ -546,18 +708,7 @@ function changePassword(accessToken, oldPassword, newPassword) {
|
|
|
546
708
|
* @param a.clientSecret - Cognito app client secret.
|
|
547
709
|
*/
|
|
548
710
|
function confirmForgotPassword(a) {
|
|
549
|
-
return
|
|
550
|
-
return getCognitoClient().send(new ConfirmForgotPasswordCommand({
|
|
551
|
-
ClientId: a.clientId,
|
|
552
|
-
Username: a.username,
|
|
553
|
-
ConfirmationCode: a.confirmationCode,
|
|
554
|
-
Password: a.newPassword,
|
|
555
|
-
SecretHash: computeSecretHash(a.username, a.clientId, a.clientSecret)
|
|
556
|
-
}));
|
|
557
|
-
}, (e) => {
|
|
558
|
-
console.error("ConfirmForgotPasswordCommand error", e);
|
|
559
|
-
return error_cognito(e);
|
|
560
|
-
})();
|
|
711
|
+
return confirmForgotPasswordResult(a);
|
|
561
712
|
}
|
|
562
713
|
/**
|
|
563
714
|
* Confirms a user's signup using the confirmation code sent by Cognito.
|
|
@@ -568,17 +719,7 @@ function confirmForgotPassword(a) {
|
|
|
568
719
|
* @param a.clientSecret - Cognito app client secret.
|
|
569
720
|
*/
|
|
570
721
|
function confirmSignup(a) {
|
|
571
|
-
return
|
|
572
|
-
return getCognitoClient().send(new ConfirmSignUpCommand({
|
|
573
|
-
ClientId: a.clientId,
|
|
574
|
-
Username: a.username,
|
|
575
|
-
ConfirmationCode: a.confirmationCode,
|
|
576
|
-
SecretHash: computeSecretHash(a.username, a.clientId, a.clientSecret)
|
|
577
|
-
}));
|
|
578
|
-
}, (e) => {
|
|
579
|
-
console.error("ConfirmSignUpCommand error", e);
|
|
580
|
-
return error_cognito(e);
|
|
581
|
-
})();
|
|
722
|
+
return confirmSignupResult(a);
|
|
582
723
|
}
|
|
583
724
|
/**
|
|
584
725
|
* Starts a forgot-password flow by sending a reset code to the user.
|
|
@@ -588,16 +729,7 @@ function confirmSignup(a) {
|
|
|
588
729
|
* @param a.clientSecret - Cognito app client secret.
|
|
589
730
|
*/
|
|
590
731
|
function forgotPassword(a) {
|
|
591
|
-
return
|
|
592
|
-
return getCognitoClient().send(new ForgotPasswordCommand({
|
|
593
|
-
ClientId: a.clientId,
|
|
594
|
-
Username: a.username,
|
|
595
|
-
SecretHash: computeSecretHash(a.username, a.clientId, a.clientSecret)
|
|
596
|
-
}));
|
|
597
|
-
}, (e) => {
|
|
598
|
-
console.error("ForgotPasswordCommand error", e);
|
|
599
|
-
return error_cognito(e);
|
|
600
|
-
})();
|
|
732
|
+
return forgotPasswordResult(a);
|
|
601
733
|
}
|
|
602
734
|
/**
|
|
603
735
|
* Signs a user in with ADMIN_USER_PASSWORD_AUTH.
|
|
@@ -609,32 +741,13 @@ function forgotPassword(a) {
|
|
|
609
741
|
* @param a.userPoolId - Cognito user pool ID.
|
|
610
742
|
*/
|
|
611
743
|
function login(a) {
|
|
612
|
-
return
|
|
613
|
-
return getCognitoClient().send(new AdminInitiateAuthCommand({
|
|
614
|
-
AuthFlow: "ADMIN_USER_PASSWORD_AUTH",
|
|
615
|
-
ClientId: a.clientId,
|
|
616
|
-
UserPoolId: a.userPoolId,
|
|
617
|
-
AuthParameters: {
|
|
618
|
-
USERNAME: a.username,
|
|
619
|
-
PASSWORD: a.password,
|
|
620
|
-
SECRET_HASH: computeSecretHash(a.username, a.clientId, a.clientSecret)
|
|
621
|
-
}
|
|
622
|
-
}));
|
|
623
|
-
}, (e) => {
|
|
624
|
-
console.error("AdminInitiateAuthCommand error", e);
|
|
625
|
-
return error_cognito(e);
|
|
626
|
-
})();
|
|
744
|
+
return loginResult(a);
|
|
627
745
|
}
|
|
628
746
|
/**
|
|
629
747
|
* Sends a GetTokensFromRefreshTokenCommand
|
|
630
748
|
*/
|
|
631
749
|
function refreshTokens(a) {
|
|
632
|
-
return
|
|
633
|
-
return getCognitoClient().send(new GetTokensFromRefreshTokenCommand(a));
|
|
634
|
-
}, (e) => {
|
|
635
|
-
console.error("refreshTokens: GetTokensFromRefreshTokenCommand error", e);
|
|
636
|
-
return error_cognito(e);
|
|
637
|
-
})();
|
|
750
|
+
return refreshTokensResult(a);
|
|
638
751
|
}
|
|
639
752
|
/**
|
|
640
753
|
* Exchanges a refresh token for new tokens.
|
|
@@ -646,20 +759,7 @@ function refreshTokens(a) {
|
|
|
646
759
|
* @param a.userPoolId - Cognito user pool ID.
|
|
647
760
|
*/
|
|
648
761
|
function refreshTokensAuth(a) {
|
|
649
|
-
return
|
|
650
|
-
return getCognitoClient().send(new AdminInitiateAuthCommand({
|
|
651
|
-
AuthFlow: "REFRESH_TOKEN_AUTH",
|
|
652
|
-
ClientId: a.clientId,
|
|
653
|
-
UserPoolId: a.userPoolId,
|
|
654
|
-
AuthParameters: {
|
|
655
|
-
REFRESH_TOKEN: a.refreshToken,
|
|
656
|
-
SECRET_HASH: computeSecretHash(a.username, a.clientId, a.clientSecret)
|
|
657
|
-
}
|
|
658
|
-
}));
|
|
659
|
-
}, (e) => {
|
|
660
|
-
console.error("refreshTokens: AdminInitiateAuthCommand error", e);
|
|
661
|
-
return error_cognito(e);
|
|
662
|
-
})();
|
|
762
|
+
return refreshTokensAuthResult(a);
|
|
663
763
|
}
|
|
664
764
|
/**
|
|
665
765
|
* Globally signs out a user by invalidating all refresh tokens.
|
|
@@ -667,12 +767,7 @@ function refreshTokensAuth(a) {
|
|
|
667
767
|
* @param accessToken - Access token for the authenticated user.
|
|
668
768
|
*/
|
|
669
769
|
function logout(accessToken) {
|
|
670
|
-
return
|
|
671
|
-
return getCognitoClient().send(new GlobalSignOutCommand({ AccessToken: accessToken }));
|
|
672
|
-
}, (e) => {
|
|
673
|
-
console.error("GlobalSignOutCommand error", e);
|
|
674
|
-
return error_cognito(e);
|
|
675
|
-
})();
|
|
770
|
+
return logoutResult(accessToken);
|
|
676
771
|
}
|
|
677
772
|
/**
|
|
678
773
|
* Completes a NEW_PASSWORD_REQUIRED challenge for users who must set a new password.
|
|
@@ -684,21 +779,7 @@ function logout(accessToken) {
|
|
|
684
779
|
* @param a.clientSecret - Cognito app client secret.
|
|
685
780
|
*/
|
|
686
781
|
function resetPassword(a) {
|
|
687
|
-
return
|
|
688
|
-
return getCognitoClient().send(new RespondToAuthChallengeCommand({
|
|
689
|
-
ChallengeName: "NEW_PASSWORD_REQUIRED",
|
|
690
|
-
ClientId: a.clientId,
|
|
691
|
-
Session: a.session,
|
|
692
|
-
ChallengeResponses: {
|
|
693
|
-
SECRET_HASH: computeSecretHash(a.username, a.clientId, a.clientSecret),
|
|
694
|
-
NEW_PASSWORD: a.newPassword,
|
|
695
|
-
USERNAME: a.username
|
|
696
|
-
}
|
|
697
|
-
}));
|
|
698
|
-
}, (e) => {
|
|
699
|
-
console.error("RespondToAuthChallengeCommand error", e);
|
|
700
|
-
return error_cognito(e);
|
|
701
|
-
})();
|
|
782
|
+
return resetPasswordResult(a);
|
|
702
783
|
}
|
|
703
784
|
/**
|
|
704
785
|
* Registers a new user with Cognito and optional custom attributes.
|
|
@@ -710,28 +791,7 @@ function resetPassword(a) {
|
|
|
710
791
|
* @param a.<attribute> - Any additional user attributes to set.
|
|
711
792
|
*/
|
|
712
793
|
function signUp(a) {
|
|
713
|
-
return
|
|
714
|
-
const cognitoClient = getCognitoClient();
|
|
715
|
-
const secretHash = computeSecretHash(a.username, a.clientId, a.clientSecret);
|
|
716
|
-
return cognitoClient.send(new SignUpCommand({
|
|
717
|
-
ClientId: a.clientId,
|
|
718
|
-
Username: a.username,
|
|
719
|
-
Password: a.password,
|
|
720
|
-
SecretHash: secretHash,
|
|
721
|
-
UserAttributes: Object.entries(a).filter(([key]) => ![
|
|
722
|
-
"username",
|
|
723
|
-
"password",
|
|
724
|
-
"clientId",
|
|
725
|
-
"clientSecret"
|
|
726
|
-
].includes(key)).map(([key, value]) => ({
|
|
727
|
-
Name: key,
|
|
728
|
-
Value: value
|
|
729
|
-
}))
|
|
730
|
-
}));
|
|
731
|
-
}, (e) => {
|
|
732
|
-
console.error("SignUpCommand error", e);
|
|
733
|
-
return error_cognito(e);
|
|
734
|
-
})();
|
|
794
|
+
return signUpResult(a);
|
|
735
795
|
}
|
|
736
796
|
/**
|
|
737
797
|
* Exchanges an OAuth2 authorization code for Cognito tokens using the token endpoint.
|
|
@@ -745,30 +805,7 @@ function signUp(a) {
|
|
|
745
805
|
* @returns Parsed token payload containing `access_token`, `id_token`, `refresh_token`, token type, and expiry.
|
|
746
806
|
*/
|
|
747
807
|
function verifyOAuthToken(a) {
|
|
748
|
-
return
|
|
749
|
-
const basicAuth = Buffer.from(`${a.clientId}:${a.clientSecret}`).toString("base64");
|
|
750
|
-
const params = new URLSearchParams();
|
|
751
|
-
params.append("grant_type", "authorization_code");
|
|
752
|
-
params.append("code", a.code);
|
|
753
|
-
params.append("redirect_uri", a.redirectUri);
|
|
754
|
-
console.log("verifyOAuthToken: params", params.toString());
|
|
755
|
-
const tokenRes = await fetch(`https://${a.cognitoDomain}/oauth2/token`, {
|
|
756
|
-
method: "POST",
|
|
757
|
-
headers: {
|
|
758
|
-
"Content-Type": "application/x-www-form-urlencoded",
|
|
759
|
-
Authorization: `Basic ${basicAuth}`
|
|
760
|
-
},
|
|
761
|
-
body: params.toString()
|
|
762
|
-
});
|
|
763
|
-
if (!tokenRes.ok) {
|
|
764
|
-
console.error("verifyOAuthToken: token exchange failed", await tokenRes.text());
|
|
765
|
-
throw Object.assign(/* @__PURE__ */ new Error("OAuth token exchange failed"), { name: "NotAuthorizedException" });
|
|
766
|
-
}
|
|
767
|
-
return await tokenRes.json();
|
|
768
|
-
}, (e) => {
|
|
769
|
-
console.error("verifyOAuthToken:error", e);
|
|
770
|
-
return error_cognito(e);
|
|
771
|
-
})();
|
|
808
|
+
return verifyOAuthTokenResult(a);
|
|
772
809
|
}
|
|
773
810
|
/**
|
|
774
811
|
* Exchanges an OAuth2 refresh token for Cognito tokens using the oauth token endpoint.
|
|
@@ -781,29 +818,7 @@ function verifyOAuthToken(a) {
|
|
|
781
818
|
* @returns Parsed token payload containing `access_token`, `id_token`, `refresh_token`, token type, and expiry.
|
|
782
819
|
*/
|
|
783
820
|
function refreshOAuthToken(a) {
|
|
784
|
-
return
|
|
785
|
-
const basicAuth = Buffer.from(`${a.clientId}:${a.clientSecret}`).toString("base64");
|
|
786
|
-
const params = new URLSearchParams();
|
|
787
|
-
params.append("grant_type", "refresh_token");
|
|
788
|
-
params.append("refresh_token", a.refreshToken);
|
|
789
|
-
console.log("refreshOAuthToken: params", params.toString());
|
|
790
|
-
const tokenRes = await fetch(`https://${a.cognitoDomain}/oauth2/token`, {
|
|
791
|
-
method: "POST",
|
|
792
|
-
headers: {
|
|
793
|
-
"Content-Type": "application/x-www-form-urlencoded",
|
|
794
|
-
Authorization: `Basic ${basicAuth}`
|
|
795
|
-
},
|
|
796
|
-
body: params.toString()
|
|
797
|
-
});
|
|
798
|
-
if (!tokenRes.ok) {
|
|
799
|
-
console.error("refreshOAuthToken: token exchange failed", await tokenRes.text());
|
|
800
|
-
throw Object.assign(/* @__PURE__ */ new Error("OAuth token refresh failed"), { name: "NotAuthorizedException" });
|
|
801
|
-
}
|
|
802
|
-
return await tokenRes.json();
|
|
803
|
-
}, (e) => {
|
|
804
|
-
console.error("refreshOAuthToken:error", e);
|
|
805
|
-
return error_cognito(e);
|
|
806
|
-
})();
|
|
821
|
+
return refreshOAuthTokenResult(a);
|
|
807
822
|
}
|
|
808
823
|
//#endregion
|
|
809
824
|
//#region src/s3/client.ts
|
|
@@ -994,14 +1009,45 @@ function getHttpStatusCode$1(error) {
|
|
|
994
1009
|
}
|
|
995
1010
|
//#endregion
|
|
996
1011
|
//#region src/s3/signedUrl.ts
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
})(...args);
|
|
1002
|
-
}
|
|
1012
|
+
const getSignedUrl = /* @__PURE__ */ ResultAsync.fromThrowable(getSignedUrl$1, (e) => {
|
|
1013
|
+
console.error("getSignedUrl: Failed to get signed url", e);
|
|
1014
|
+
return error_s3(e);
|
|
1015
|
+
});
|
|
1003
1016
|
//#endregion
|
|
1004
1017
|
//#region src/s3/object.ts
|
|
1018
|
+
const getObjectResult = /* @__PURE__ */ ResultAsync.fromThrowable(async (bucketName, key) => {
|
|
1019
|
+
const s3 = getS3();
|
|
1020
|
+
const cmd = new GetObjectCommand({
|
|
1021
|
+
Bucket: bucketName,
|
|
1022
|
+
Key: key
|
|
1023
|
+
});
|
|
1024
|
+
const stream = (await s3.send(cmd)).Body;
|
|
1025
|
+
return new Promise((resolve, reject) => {
|
|
1026
|
+
const chunks = [];
|
|
1027
|
+
stream.on("data", (chunk) => chunks.push(chunk));
|
|
1028
|
+
stream.on("end", () => resolve(Buffer.concat(chunks)));
|
|
1029
|
+
stream.on("error", reject);
|
|
1030
|
+
});
|
|
1031
|
+
}, (e) => {
|
|
1032
|
+
console.error(`getObjectt: Error getting object from S3: ${e}`);
|
|
1033
|
+
return error_s3(e);
|
|
1034
|
+
});
|
|
1035
|
+
const objectExistsResult = /* @__PURE__ */ ResultAsync.fromThrowable(async (bucketName, key) => {
|
|
1036
|
+
const s3 = getS3();
|
|
1037
|
+
try {
|
|
1038
|
+
const cmd = new HeadObjectCommand({
|
|
1039
|
+
Bucket: bucketName,
|
|
1040
|
+
Key: key
|
|
1041
|
+
});
|
|
1042
|
+
return (await s3.send(cmd)).$metadata.httpStatusCode === 200;
|
|
1043
|
+
} catch (e) {
|
|
1044
|
+
if (is_s3_notFound(e)) return false;
|
|
1045
|
+
throw e;
|
|
1046
|
+
}
|
|
1047
|
+
}, (e) => {
|
|
1048
|
+
console.error(`objectExists: Error getting object head from S3: ${e}`);
|
|
1049
|
+
return error_s3(e);
|
|
1050
|
+
});
|
|
1005
1051
|
/**
|
|
1006
1052
|
* Retrieves an object from an S3 bucket.
|
|
1007
1053
|
*
|
|
@@ -1010,23 +1056,7 @@ function getSignedUrl(...args) {
|
|
|
1010
1056
|
* @returns {Promise<Buffer>} A promise that resolves to the object data as a Buffer.
|
|
1011
1057
|
*/
|
|
1012
1058
|
function getObject(bucketName, key) {
|
|
1013
|
-
return
|
|
1014
|
-
const s3 = getS3();
|
|
1015
|
-
const cmd = new GetObjectCommand({
|
|
1016
|
-
Bucket: bucketName,
|
|
1017
|
-
Key: key
|
|
1018
|
-
});
|
|
1019
|
-
const stream = (await s3.send(cmd)).Body;
|
|
1020
|
-
return new Promise((resolve, reject) => {
|
|
1021
|
-
const chunks = [];
|
|
1022
|
-
stream.on("data", (chunk) => chunks.push(chunk));
|
|
1023
|
-
stream.on("end", () => resolve(Buffer.concat(chunks)));
|
|
1024
|
-
stream.on("error", reject);
|
|
1025
|
-
});
|
|
1026
|
-
}, (e) => {
|
|
1027
|
-
console.error(`getObjectt: Error getting object from S3: ${e}`);
|
|
1028
|
-
return error_s3(e);
|
|
1029
|
-
})();
|
|
1059
|
+
return getObjectResult(bucketName, key);
|
|
1030
1060
|
}
|
|
1031
1061
|
/**
|
|
1032
1062
|
* Convenience function to get an object from S3 and return it as a string.
|
|
@@ -1042,22 +1072,7 @@ function getObjectString(bucketName, key) {
|
|
|
1042
1072
|
* @returns {Promise<Buffer>} A promise that resolves to a boolean.
|
|
1043
1073
|
*/
|
|
1044
1074
|
function objectExists(bucketName, key) {
|
|
1045
|
-
return
|
|
1046
|
-
const s3 = getS3();
|
|
1047
|
-
try {
|
|
1048
|
-
const cmd = new HeadObjectCommand({
|
|
1049
|
-
Bucket: bucketName,
|
|
1050
|
-
Key: key
|
|
1051
|
-
});
|
|
1052
|
-
return (await s3.send(cmd)).$metadata.httpStatusCode === 200;
|
|
1053
|
-
} catch (e) {
|
|
1054
|
-
if (is_s3_notFound(e)) return false;
|
|
1055
|
-
throw e;
|
|
1056
|
-
}
|
|
1057
|
-
}, (e) => {
|
|
1058
|
-
console.error(`objectExists: Error getting object head from S3: ${e}`);
|
|
1059
|
-
return error_s3(e);
|
|
1060
|
-
})();
|
|
1075
|
+
return objectExistsResult(bucketName, key);
|
|
1061
1076
|
}
|
|
1062
1077
|
//#endregion
|
|
1063
1078
|
//#region src/dynamo/errors.ts
|