Skip to content

Commit

Permalink
Expose mfa secret code
Browse files Browse the repository at this point in the history
  • Loading branch information
danielholmes committed May 22, 2024
1 parent c877fe1 commit 89645a2
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 74 deletions.
152 changes: 85 additions & 67 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dhau/react-aws-cognito",
"version": "0.0.21",
"version": "0.0.22",
"type": "module",
"sideEffects": false,
"description": "Context and hooks to manage an AWS Cognito authenticated user in React.",
Expand Down
16 changes: 12 additions & 4 deletions src/model/get-mfa-code-url.ts → src/model/get-mfa-code-info.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { CognitoUser } from "amazon-cognito-identity-js";

async function getMfaCodeUrl(
type MfaCodeInfo = {
readonly secretCode: string;
readonly qrCodeUri: string;
};

async function getMfaCodeInfo(
issuer: string,
user: CognitoUser,
emailAddress: string,
) {
return new Promise<string>((resolve, reject) => {
return new Promise<MfaCodeInfo>((resolve, reject) => {
user.associateSoftwareToken({
associateSecretCode: (secretCode: string) => {
const encodedIssuer = encodeURIComponent(issuer);
Expand All @@ -15,11 +20,14 @@ async function getMfaCodeUrl(
const qrCodeUri = `otpauth://totp/${encodedIssuer}:${encodeURIComponent(
emailAddress,
)}?secret=${encodeURIComponent(secretCode)}&issuer=${encodedIssuer}`;
resolve(qrCodeUri);
resolve({
secretCode,
qrCodeUri,
});
},
onFailure: reject,
});
});
}

export default getMfaCodeUrl;
export default getMfaCodeInfo;
4 changes: 2 additions & 2 deletions src/signed-in-auth-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import resendEmailAddressVerification from "./model/resend-email-address-verific
import verifyEmailAddress from "./model/verify-email-address.ts";
import enableMfa from "./model/enable-mfa.ts";
import disableMfa from "./model/disable-mfa.ts";
import getMfaCodeUrl from "./model/get-mfa-code-url.ts";
import getMfaCodeInfo from "./model/get-mfa-code-info.ts";
import changePassword from "./model/change-password.ts";
import { UserParser } from "./model/get-current-user.ts";

Expand Down Expand Up @@ -60,7 +60,7 @@ function createSignedInAuthState<TUser>({
user,
),
disableMfa: async () => disableMfa(setInternalAuthState, parseUser, user),
getMfaCodeUrl: partial(getMfaCodeUrl, mfaIssuer, user),
getMfaCodeInfo: partial(getMfaCodeInfo, mfaIssuer, user),
refreshUser,
verifyEmailAddress: partial(
verifyEmailAddress,
Expand Down

0 comments on commit 89645a2

Please sign in to comment.