Skip to content

Commit

Permalink
Merge pull request #1119 from tonwhales/fix/holders-unauth-401
Browse files Browse the repository at this point in the history
hotfix: holders reset token on unauth.
  • Loading branch information
vzhovnitsky authored Oct 8, 2024
2 parents e632a20 + 3c6dd06 commit a0c7f2b
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 46 deletions.
2 changes: 1 addition & 1 deletion VERSION_CODE
Original file line number Diff line number Diff line change
@@ -1 +1 @@
214
215
24 changes: 17 additions & 7 deletions app/engine/hooks/holders/useCardTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CardNotification, fetchCardsTransactions } from "../../api/holders/fetc
import { Queries } from "../../queries";
import { deleteHoldersToken, useHoldersAccountStatus } from "./useHoldersAccountStatus";
import { HoldersUserState } from "../../api/holders/fetchUserState";
import axios from "axios";

export function useCardTransactions(address: string, id: string) {
let status = useHoldersAccountStatus(address).data;
Expand All @@ -24,15 +25,24 @@ export function useCardTransactions(address: string, id: string) {
},
queryFn: async (ctx) => {
if (!!status && status.state !== HoldersUserState.NeedEnrollment) {
const cardRes = await fetchCardsTransactions(status.token, id, 40, ctx.pageParam?.lastCursor, 'desc');
try {
const cardRes = await fetchCardsTransactions(status.token, id, 40, ctx.pageParam?.lastCursor, 'desc');

if (!cardRes) {
deleteHoldersToken(address);
throw new Error('Unauthorized');
}
if (!cardRes) {
deleteHoldersToken(address);
throw new Error('Unauthorized');
}

if (!!cardRes) {
return cardRes;
if (!!cardRes) {
return cardRes;
}
} catch (error) {
if (axios.isAxiosError(error) && error.response?.status === 401) {
deleteHoldersToken(address);
throw new Error('Unauthorized');
} else {
throw error;
}
}
}
return null;
Expand Down
24 changes: 17 additions & 7 deletions app/engine/hooks/holders/useHoldersAccountStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { storage } from "../../../storage/storage";
import { HoldersUserState, userStateCodec, fetchUserState } from "../../api/holders/fetchUserState";
import { z } from 'zod';
import { removeProvisioningCredentials } from "../../holders/updateProvisioningCredentials";
import axios from "axios";

const holdersAccountStatus = z.union([
z.object({ state: z.literal(HoldersUserState.NeedEnrollment) }),
Expand All @@ -30,7 +31,7 @@ function migrateHoldersToken(addressString: string) {

export function deleteHoldersToken(address: string) {
// clean up provisioning credentials cache for this address
removeProvisioningCredentials(address);
removeProvisioningCredentials(address);
storage.delete(`holders-jwt-${address}`);
}

Expand Down Expand Up @@ -66,14 +67,23 @@ export function useHoldersAccountStatus(address: string | Address) {
return { state: HoldersUserState.NeedEnrollment } as HoldersAccountStatus; // This looks amazingly stupid
}

const fetched = await fetchUserState(token, isTestnet);
try {
const fetched = await fetchUserState(token, isTestnet);

if (!fetched) { // unauthorized
deleteHoldersToken(addr);
return { state: HoldersUserState.NeedEnrollment } as HoldersAccountStatus;
}
if (!fetched) { // unauthorized
deleteHoldersToken(addr);
return { state: HoldersUserState.NeedEnrollment } as HoldersAccountStatus;
}

return { ...fetched, token } as HoldersAccountStatus;
return { ...fetched, token } as HoldersAccountStatus;
} catch (error) {
if (axios.isAxiosError(error) && error.response?.status === 401) {
deleteHoldersToken(addressString);
throw new Error('Unauthorized');
} else {
throw error;
}
}
},
refetchOnWindowFocus: true,
refetchOnMount: true,
Expand Down
66 changes: 38 additions & 28 deletions app/engine/hooks/holders/useHoldersAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import { GeneralHoldersAccount, PrePaidHoldersCard, fetchAccountsList, fetchAcco
import { deleteHoldersToken, useHoldersAccountStatus } from "./useHoldersAccountStatus";
import { HoldersUserState } from "../../api/holders/fetchUserState";
import { updateProvisioningCredentials } from "../../holders/updateProvisioningCredentials";
import axios from "axios";

export type HoldersAccounts = {
accounts: GeneralHoldersAccount[],
type: 'public' | 'private',
accounts: GeneralHoldersAccount[],
type: 'public' | 'private',
prepaidCards?: PrePaidHoldersCard[]
}

Expand Down Expand Up @@ -38,38 +39,47 @@ export function useHoldersAccounts(address: string | Address) {
refetchInterval: 35000,
staleTime: 35000,
queryFn: async () => {
let accounts;
let prepaidCards: PrePaidHoldersCard[] | undefined;
let type = 'public';

if (token) {
const res = await fetchAccountsList(token, isTestnet);
try {
let accounts;
let prepaidCards: PrePaidHoldersCard[] | undefined;
let type = 'public';

if (!res) {
deleteHoldersToken(addressString);
throw new Error('Unauthorized');
}
if (token) {
const res = await fetchAccountsList(token, isTestnet);

type = 'private';
accounts = res?.accounts;
prepaidCards = res?.prepaidCards;
if (!res) {
deleteHoldersToken(addressString);
throw new Error('Unauthorized');
}

// fetch apple pay credentials and update provisioning credentials cache
await updateProvisioningCredentials(addressString, isTestnet);
} else {
accounts = await fetchAccountsPublic(addressString, isTestnet);
type = 'public';
}
type = 'private';
accounts = res?.accounts;
prepaidCards = res?.prepaidCards;

// fetch apple pay credentials and update provisioning credentials cache
await updateProvisioningCredentials(addressString, isTestnet);
} else {
accounts = await fetchAccountsPublic(addressString, isTestnet);
type = 'public';
}

const filtered = accounts?.filter((a) => a.network === (isTestnet ? 'ton-testnet' : 'ton-mainnet'));
const filtered = accounts?.filter((a) => a.network === (isTestnet ? 'ton-testnet' : 'ton-mainnet'));

const sorted = filtered?.sort((a, b) => {
if (a.cards.length > b.cards.length) return -1;
if (a.cards.length < b.cards.length) return 1;
return 0;
});
const sorted = filtered?.sort((a, b) => {
if (a.cards.length > b.cards.length) return -1;
if (a.cards.length < b.cards.length) return 1;
return 0;
});

return { accounts: sorted, type, prepaidCards } as HoldersAccounts;
return { accounts: sorted, type, prepaidCards } as HoldersAccounts;
} catch (error) {
if (axios.isAxiosError(error) && error.response?.status === 401) {
deleteHoldersToken(addressString);
throw new Error('Unauthorized');
} else {
throw error;
}
}
}
});

Expand Down
4 changes: 2 additions & 2 deletions ios/wallet/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>2.3.20</string>
<string>2.3.21</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleURLTypes</key>
Expand All @@ -41,7 +41,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>214</string>
<string>215</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSApplicationQueriesSchemes</key>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wallet",
"version": "2.3.20",
"version": "2.3.21",
"scripts": {
"start": "expo start --dev-client",
"android": "expo run:android",
Expand Down

0 comments on commit a0c7f2b

Please sign in to comment.