Skip to content

Commit

Permalink
Fix api consumption errors
Browse files Browse the repository at this point in the history
  • Loading branch information
amitsingh-007 committed Jan 31, 2024
1 parent 99cb058 commit 170a45d
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ const TwoFactorAuthenticate = () => {
if (!user) {
return;
}
const { isVerified } = await api.twoFactorAuth.authenticate.query({
uid: user.uid ?? '',
totp,
});
const { isVerified } = await api.twoFactorAuth.authenticate.query(totp);
if (isVerified) {
user.isTOTPVerified = true;
await chrome.storage.local.set({ [STORAGE_KEYS.userProfile]: user });
Expand Down
4 changes: 1 addition & 3 deletions apps/extension/src/HomePopup/utils/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ import { AuthProgress } from './authProgress';

const syncAuthenticationToStorage = async (userProfile: UserInfo) => {
AuthProgress.start('Checking 2FA status');
const { is2FAEnabled } = await api.twoFactorAuth.status.query(
userProfile.uid ?? ''
);
const { is2FAEnabled } = await api.twoFactorAuth.status.query();
userProfile.is2FAEnabled = is2FAEnabled;
userProfile.isTOTPVerified = false;
await chrome.storage.local.set({ [STORAGE_KEYS.userProfile]: userProfile });
Expand Down
10 changes: 2 additions & 8 deletions apps/extension/src/SettingsPanel/components/Setup2FA.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ const Setup2FA = memo(function Setup2FA({ isOpen, handleClose }: Props) {
const [showVerifyToken, setShowVerifyToken] = useState(false);

const init2FA = async () => {
const userProfile = await getUserProfile();
const { otpAuthUrl, secretKey } = await api.twoFactorAuth.setup.mutate(
userProfile.uid ?? ''
);
const { otpAuthUrl, secretKey } = await api.twoFactorAuth.setup.mutate();
setSecretKey(secretKey);
setOptAuthUrl(otpAuthUrl);
};
Expand All @@ -37,10 +34,7 @@ const Setup2FA = memo(function Setup2FA({ isOpen, handleClose }: Props) {

const handleTOTPVerify = async (totp: string) => {
const userProfile = await getUserProfile();
const { isVerified } = await api.twoFactorAuth.verify.query({
uid: userProfile.uid ?? '',
totp,
});
const { isVerified } = await api.twoFactorAuth.verify.query(totp);
if (isVerified) {
userProfile.is2FAEnabled = true;
await chrome.storage.local.set({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ const TwoFactorAuth = memo(function TwoFactorAuth() {

const handle2FARevoke = async () => {
const userProfile = await getUserProfile();
const { isRevoked } = await api.twoFactorAuth.revoke.mutate(
userProfile.uid ?? ''
);
const { isRevoked } = await api.twoFactorAuth.revoke.mutate();
if (!isRevoked) {
displayToast({ message: 'Something went wrong', severity: 'error' });
return;
Expand Down
5 changes: 1 addition & 4 deletions apps/web/src/pages/web-ext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ export default function Web() {
};

const onVerify = async (totp: string) => {
const { isVerified } = await api.twoFactorAuth.authenticate.query({
uid: user?.uid ?? '',
totp,
});
const { isVerified } = await api.twoFactorAuth.authenticate.query(totp);
if (isVerified) {
const twoFAData = await getFromLocalStorage<ITwoFactorAuth>(
STORAGE_KEYS.twoFactorAuth
Expand Down
7 changes: 3 additions & 4 deletions apps/web/src/ui/TwoFactorAuth/hooks/usePreload2FA.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import {
} from '@/ui/provider/utils';
import { api } from '@/utils/api';
import { STORAGE_KEYS } from '@bypass/shared';
import { User } from 'firebase/auth';
import { useCallback, useState } from 'react';
import { ITwoFactorAuth } from '../interface';

const sync2FAToStorage = async (user: User) => {
const sync2FAToStorage = async () => {
if (isExistsInLocalStorage(STORAGE_KEYS.twoFactorAuth)) {
return;
}
const { is2FAEnabled } = await api.twoFactorAuth.status.query(user.uid);
const { is2FAEnabled } = await api.twoFactorAuth.status.query();
const data: ITwoFactorAuth = {
is2FAEnabled,
isTOTPVerified: false,
Expand All @@ -31,7 +30,7 @@ const usePreload2FA = () => {
return;
}
setIsLoading(true);
await sync2FAToStorage(user);
await sync2FAToStorage();
setIsLoading(false);
}, [user]);

Expand Down
34 changes: 11 additions & 23 deletions packages/trpc/tests/routers/twoFactorAuth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ describe('Two Factor Auth Setup Flow', async () => {

it('should have expected response', async () => {
const caller = await getTrpcCaller();
const output = await caller.twoFactorAuth.status(user.uid);
const output = await caller.twoFactorAuth.status();
expect(output).toStrictEqual({ is2FAEnabled: false });
});

it('should create new totp for the first time', async () => {
const caller = await getTrpcCaller();
const output = await caller.twoFactorAuth.setup(user.uid);
const output = await caller.twoFactorAuth.setup();
expect(output).toHaveProperty('secretKey');
expect(output).toHaveProperty('otpAuthUrl');
secretKey = output.secretKey;
Expand All @@ -36,69 +36,57 @@ describe('Two Factor Auth Setup Flow', async () => {

it('should return already created totp if requested to setup again', async () => {
const caller = await getTrpcCaller();
const output = await caller.twoFactorAuth.setup(user.uid);
const output = await caller.twoFactorAuth.setup();
expect(output).toStrictEqual({ secretKey, otpAuthUrl });
});

it('should still show totp not setup after setup step', async () => {
const caller = await getTrpcCaller();
const output = await caller.twoFactorAuth.status(user.uid);
const output = await caller.twoFactorAuth.status();
expect(output).toStrictEqual({ is2FAEnabled: false });
});

it('should not verify if user enters wrong totp token', async () => {
const caller = await getTrpcCaller();
const output = await caller.twoFactorAuth.verify({
uid: user.uid,
totp: getRandomTotp(),
});
const output = await caller.twoFactorAuth.verify(getRandomTotp());
expect(output).toStrictEqual({ isVerified: false });
});

it('should verify if user enters correct totp token', async () => {
const caller = await getTrpcCaller();
const correctToken = authenticator.generate(secretKey ?? '');
const output = await caller.twoFactorAuth.verify({
uid: user.uid,
totp: correctToken,
});
const output = await caller.twoFactorAuth.verify(correctToken);
expect(output).toStrictEqual({ isVerified: true });
});

it('should show totp as setup after successfully verifying', async () => {
const caller = await getTrpcCaller();
const output = await caller.twoFactorAuth.status(user.uid);
const output = await caller.twoFactorAuth.status();
expect(output).toStrictEqual({ is2FAEnabled: true });
});

it('should not authenticate the user if wrong totp token is entered', async () => {
const caller = await getTrpcCaller();
const output = await caller.twoFactorAuth.authenticate({
uid: user.uid,
totp: getRandomTotp(),
});
const output = await caller.twoFactorAuth.authenticate(getRandomTotp());
expect(output).toStrictEqual({ isVerified: false });
});

it('should authenticate the user if correct totp token is entered', async () => {
const caller = await getTrpcCaller();
const token = authenticator.generate(secretKey ?? '');
const output = await caller.twoFactorAuth.authenticate({
uid: user.uid,
totp: token,
});
const output = await caller.twoFactorAuth.authenticate(token);
expect(output).toStrictEqual({ isVerified: true });
});

it('should revoke the totp status', async () => {
const caller = await getTrpcCaller();
const output = await caller.twoFactorAuth.revoke(user.uid);
const output = await caller.twoFactorAuth.revoke();
expect(output).toStrictEqual({ isRevoked: true });
});

it('should show totp not setup after being revoked', async () => {
const caller = await getTrpcCaller();
const output = await caller.twoFactorAuth.status(user.uid);
const output = await caller.twoFactorAuth.status();
expect(output).toStrictEqual({ is2FAEnabled: false });
});
});

0 comments on commit 170a45d

Please sign in to comment.