Skip to content

Commit

Permalink
fix: totp changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Oct 5, 2023
1 parent f76d69b commit 5c8a0eb
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 52 deletions.
24 changes: 2 additions & 22 deletions lib/build/recipe/totp/api/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,21 @@ var __importDefault =
};
Object.defineProperty(exports, "__esModule", { value: true });
const recipe_1 = __importDefault(require("../recipe"));
const error_1 = __importDefault(require("../../session/error"));
// import { MfaClaim, completeFactorInSession } from '../../mfa';
function getAPIImplementation() {
return {
createDevicePOST: async function ({ session, options, deviceName, userContext }) {
// TODO: validate claims to check if createDevice can be allowed
let userIdentifierInfo = undefined;
const emailOrPhoneInfo = await recipe_1.default
.getInstanceOrThrowError()
.getUserIdentifierInfoForUserId(session.getUserId(), userContext);
if (emailOrPhoneInfo.status === "OK") {
userIdentifierInfo = emailOrPhoneInfo.info;
} else if (emailOrPhoneInfo.status === "UNKNOWN_USER_ID_ERROR") {
throw new error_1.default({
type: error_1.default.UNAUTHORISED,
message: "Unknown User ID provided",
});
}
const listDevicesResponse = await options.recipeImplementation.listDevices({
userId: session.getUserId(),
userContext,
});
const devices = listDevicesResponse.devices;
const verifiedDeviceCount = devices.filter((device) => device.verified).length;
if (verifiedDeviceCount > 0) {
// TODO: We need to assert that all factors have been completed
// before actually creating the device.
// await session.assertClaims(MfaClaim.validators.hasCompletedAllFactors(), userContext);
}
if (deviceName === undefined) {
// We need to set the device name:
deviceName = `TOTP Device ${devices.length + 1}`; // Assuming no one creates a device in the same format
}
const args = { deviceName, userId: session.getUserId(), userIdentifierInfo, userContext };
let response = await options.recipeImplementation.createDevice(args);
return Object.assign(Object.assign({}, response), { deviceName });
return Object.assign({}, response);
},
verifyCodePOST: async function ({ session, options, totp, userContext }) {
const args = { userId: session.getUserId(), totp, tenantId: session.getTenantId(), userContext };
Expand Down
3 changes: 2 additions & 1 deletion lib/build/recipe/totp/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default class Wrapper {
static Error: typeof SuperTokensError;
static createDevice(input: {
userId: string;
deviceName: string;
deviceName?: string;
skew?: number;
period?: number;
userContext?: any;
Expand All @@ -17,6 +17,7 @@ export default class Wrapper {
secret: string;
userIdentifier?: string | undefined;
qrCodeString: string;
deviceName: string;
}
| {
status: "DEVICE_ALREADY_EXISTS_ERROR";
Expand Down
1 change: 1 addition & 0 deletions lib/build/recipe/totp/recipeImplementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function getRecipeInterface(querier, config) {
issuerName,
userIdentifier,
secret: response.secret,
deviceName: response.deviceName,
qrCodeString: encodeURI(
`otpauth://totp/${issuerName}${userIdentifier !== undefined ? ":" + userIdentifier : ""}` +
`?secret=${response.secret}&issuer=${issuerName}`
Expand Down
3 changes: 2 additions & 1 deletion lib/build/recipe/totp/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export declare type TypeNormalisedInput = {
export declare type RecipeInterface = {
createDevice: (input: {
userId: string;
deviceName: string;
deviceName?: string;
skew?: number;
period?: number;
userIdentifierInfo?: string;
Expand All @@ -44,6 +44,7 @@ export declare type RecipeInterface = {
secret: string;
userIdentifier?: string;
qrCodeString: string;
deviceName: string;
}
| {
status: "DEVICE_ALREADY_EXISTS_ERROR";
Expand Down
29 changes: 4 additions & 25 deletions lib/ts/recipe/totp/api/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,26 @@

import { APIInterface } from "../types";
import TotpRecipe from "../recipe";
import SessionError from "../../session/error";
// import { MfaClaim, completeFactorInSession } from '../../mfa';

export default function getAPIImplementation(): APIInterface {
return {
createDevicePOST: async function ({ session, options, deviceName, userContext }) {
// TODO: validate claims to check if createDevice can be allowed

let userIdentifierInfo: string | undefined = undefined;

const emailOrPhoneInfo = await TotpRecipe.getInstanceOrThrowError().getUserIdentifierInfoForUserId(
session.getUserId(),
userContext
);
if (emailOrPhoneInfo.status === "OK") {
userIdentifierInfo = emailOrPhoneInfo.info;
} else if (emailOrPhoneInfo.status === "UNKNOWN_USER_ID_ERROR") {
throw new SessionError({
type: SessionError.UNAUTHORISED,
message: "Unknown User ID provided",
});
}

const listDevicesResponse = await options.recipeImplementation.listDevices({
userId: session.getUserId(),
userContext,
});
const devices = listDevicesResponse.devices;
const verifiedDeviceCount = devices.filter((device) => device.verified).length;

if (verifiedDeviceCount > 0) {
// TODO: We need to assert that all factors have been completed
// before actually creating the device.
// await session.assertClaims(MfaClaim.validators.hasCompletedAllFactors(), userContext);
}

if (deviceName === undefined) {
// We need to set the device name:
deviceName = `TOTP Device ${devices.length + 1}`; // Assuming no one creates a device in the same format
}

const args = { deviceName, userId: session.getUserId(), userIdentifierInfo, userContext };
let response = await options.recipeImplementation.createDevice(args);
return { ...response, deviceName };
return { ...response };
},

verifyCodePOST: async function ({ session, options, totp, userContext }) {
Expand Down
2 changes: 1 addition & 1 deletion lib/ts/recipe/totp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default class Wrapper {

static createDevice(input: {
userId: string;
deviceName: string;
deviceName?: string;
skew?: number;
period?: number;
userContext?: any;
Expand Down
1 change: 1 addition & 0 deletions lib/ts/recipe/totp/recipeImplementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default function getRecipeInterface(querier: Querier, config: TypeNormali
issuerName,
userIdentifier,
secret: response.secret,
deviceName: response.deviceName,
qrCodeString: encodeURI(
`otpauth://totp/${issuerName}${userIdentifier !== undefined ? ":" + userIdentifier : ""}` +
`?secret=${response.secret}&issuer=${issuerName}`
Expand Down
11 changes: 9 additions & 2 deletions lib/ts/recipe/totp/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,20 @@ export type TypeNormalisedInput = {
export type RecipeInterface = {
createDevice: (input: {
userId: string;
deviceName: string;
deviceName?: string;
skew?: number;
period?: number;
userIdentifierInfo?: string;
userContext: any;
}) => Promise<
| { status: "OK"; issuerName: string; secret: string; userIdentifier?: string; qrCodeString: string }
| {
status: "OK";
issuerName: string;
secret: string;
userIdentifier?: string;
qrCodeString: string;
deviceName: string;
}
| { status: "DEVICE_ALREADY_EXISTS_ERROR" }
>;
verifyCode: (input: {
Expand Down
132 changes: 132 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"next": "11.1.3",
"next-test-api-route-handler": "^3.1.8",
"nock": "11.7.0",
"otplib": "^12.0.1",
"prettier": "2.0.5",
"pretty-quick": "^3.1.1",
"react": "^17.0.2",
Expand Down
Loading

0 comments on commit 5c8a0eb

Please sign in to comment.