Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(platforms): idena error handling #2450

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions platforms/src/Idena/__tests__/provider.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable @typescript-eslint/unbound-method */
/* eslint-disable @typescript-eslint/require-await */

import { ProviderContext, RequestPayload } from "@gitcoin/passport-types";
import { RequestPayload } from "@gitcoin/passport-types";
import { IdenaContext } from "../procedures/idenaSignIn";
import { initCacheSession, loadCacheSession, PlatformSession } from "../../utils/platform-cache";
import { initCacheSession, loadCacheSession } from "../../utils/platform-cache";
import {
IdenaStateHumanProvider,
IdenaStateNewbieProvider,
Expand All @@ -12,20 +12,29 @@ import {

// ----- Libs
import axios from "axios";
import { ProviderExternalVerificationError, ProviderInternalVerificationError } from "../../types";

jest.mock("axios");

const mockedAxios = axios as jest.Mocked<typeof axios>;

const MOCK_ADDRESS = "0x5867b46bd12769e0b7522a5b64acd7c1eacb183a";
const BAD_ADDRESS = "0x1111116bd12769e0b7522a5b64acd7c1eacb183a";
const MOCK_SESSION_KEY = "sessionKey";

const ageResponse = {
data: { result: 7 },
status: 200,
};

const badIdentityResponse = {
data: {
error: {
message: "no data found",
},
},
status: 200,
};

const identityResponse = {
data: { result: { state: "Human" } },
status: 200,
Expand Down Expand Up @@ -67,6 +76,8 @@ beforeEach(async () => {

mockedAxios.get.mockImplementation(async (url, config) => {
switch (url) {
case `/api/identity/${BAD_ADDRESS}`:
return badIdentityResponse;
case `/api/identity/${MOCK_ADDRESS}/age`:
return ageResponse;
case `/api/identity/${MOCK_ADDRESS}`:
Expand Down Expand Up @@ -182,4 +193,23 @@ describe("Check valid cases for state providers", function () {
expiresInSeconds: 86401,
});
});
it("Should throw external provider error", async () => {
const mockSessionKey = "newSess";
await initCacheSession(mockSessionKey);
const session = await loadCacheSession<IdenaCache>(mockSessionKey);
await session.set("address", BAD_ADDRESS);
await session.set("signature", "signature");

const provider = new IdenaStateVerifiedProvider();
const payload = {
proofs: {
sessionKey: mockSessionKey,
},
};

await expect(provider.verify(payload as unknown as RequestPayload, {} as IdenaContext)).rejects.toThrow(
// eslint-disable-next-line quotes
'Error verifying Idena Status. It is likely that you do not qualify for this stamp. {"error":{"message":"no data found"},"address":"0x1111116bd12769e0b7522a5b64acd7c1eacb183a"}'
);
});
});
18 changes: 8 additions & 10 deletions platforms/src/Idena/procedures/idenaSignIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import crypto from "crypto";
import axios, { AxiosInstance, AxiosResponse } from "axios";
import { initCacheSession, loadCacheSession, clearCacheSession, PlatformSession } from "../../utils/platform-cache";
import { ProviderContext } from "@gitcoin/passport-types";
import { ProviderInternalVerificationError } from "../../types";
import { ProviderExternalVerificationError, ProviderInternalVerificationError } from "../../types";
import { handleProviderAxiosError } from "../../utils/handleProviderAxiosError";

type IdenaCache = {
Expand Down Expand Up @@ -116,6 +116,13 @@ export const requestIdentityState = async (
context: IdenaContext
): Promise<{ address: string; state: string; expirationDate: string }> => {
const data: IdentityResponse = await request(token, context, "/api/identity/_address_");
if (!data.address || !data.result || !data.result.state) {
throw new ProviderExternalVerificationError(
`Error verifying Idena Status. It is likely that you do not qualify for this stamp. ${String(
JSON.stringify(data)
)}`
);
}
const expirationDate = await requestValidationTime(token, context);
return { address: data.address, state: data.result.state, expirationDate };
};
Expand All @@ -129,15 +136,6 @@ export const requestIdentityAge = async (
return { address: data.address, age: +data.result, expirationDate };
};

export const requestIdentityStake = async (
token: string,
context: IdenaContext
): Promise<{ address: string; stake: number; expirationDate: string }> => {
const data: AddressResponse = await request(token, context, "/api/address/_address_");
const expirationDate = await requestValidationTime(token, context);
return { address: data.address, stake: +data.result.stake, expirationDate };
};

const apiClient = (): AxiosInstance => {
return axios.create({
baseURL: API_URL,
Expand Down
Loading