Skip to content

Commit

Permalink
fix: compare app in whitelist check (sismo-core#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrupele authored Jul 3, 2023
1 parent 8f6b7be commit 62eb3a4
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
18 changes: 17 additions & 1 deletion src/app/api/zk-telegram-bot/verify/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,23 @@ describe("POST /api/zk-telegram-bot/verify", () => {
expect(data.message).toMatch(/Failed to verify ZK-Proof/);
});

it("Should return approved when the user is not in the whitelist yet", async () => {
it("Should return approved when the userId is not in the whitelist", async () => {
const response = await POST(
new MockedRequest({
spaceSlug: "spaceSlug",
appSlug: "appSlug",
response: mockResponse,
}) as any
);
const data = await response.json();
expect(data.status).toEqual("approved");
});

it("Should return approved when the userId is whitelisted but for another app", async () => {
await memoryUserStore.add({
userId: "6232426394",
appSlug: "alreadyApprovedAppSlug",
});
const response = await POST(
new MockedRequest({
spaceSlug: "spaceSlug",
Expand Down
14 changes: 11 additions & 3 deletions src/app/api/zk-telegram-bot/verify/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function POST(req: Request) {
const userStore = getUserStore();

const telegramId = result.getUserId(AuthType.TELEGRAM);
if (await isAlreadyApproved(userStore, telegramId)) {
if (await isAlreadyApproved(userStore, app.slug, telegramId)) {
if (env.isDev) {
console.info(`User ${telegramId} is already approved`);
}
Expand Down Expand Up @@ -74,8 +74,16 @@ const verifyResponse = async (
return await sismoConnect.verify(response, verifyParams);
};

const isAlreadyApproved = async (store: UserStore, telegramId: string): Promise<boolean> => {
const users = await store.getUsers({ userId: telegramId });
const isAlreadyApproved = async (
store: UserStore,
appSlug: string,
telegramId: string
): Promise<boolean> => {
const userQuery = {
appSlug: appSlug,
userId: telegramId
};
const users = await store.getUsers(userQuery);
return users.length > 0;
};

Expand Down
5 changes: 4 additions & 1 deletion src/libs/user-store/memory-user-store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ export class MemoryUserStore extends UserStore {

public async getUsers(queryUser?: Partial<User>): Promise<User[]> {
return this._users.filter((user) => {
return queryUser?.userId === user.userId;
if (queryUser?.appSlug && queryUser.appSlug !== user.appSlug)
return false;
if (queryUser?.userId && queryUser.userId !== user.userId) return false;
return true;
});
}

Expand Down
48 changes: 48 additions & 0 deletions src/libs/user-store/memory-user-store/memory-user-store.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { MemoryUserStore } from ".";
import { UserStore } from "../store";

let userStore: UserStore;

const user1 = {
appSlug: "appSlug1",
userId: "userId1",
};
const user2 = {
appSlug: "appSlug2",
userId: "userId2",
};
const user3 = {
appSlug: "appSlug1",
userId: "userId3"
};

describe("MemoryUserStore", () => {
beforeEach(async () => {
userStore = new MemoryUserStore();

await userStore.add(user1);
await userStore.add(user2);
await userStore.add(user3);
});

it("Should return all users when query is missing", async () => {
const users = await userStore.getUsers();
expect(users.length).toEqual(3);
expect(users[0]).toEqual(user1);
expect(users[1]).toEqual(user2);
expect(users[2]).toEqual(user3);
});

it("Should return all users matching partial query", async () => {
const users = await userStore.getUsers({ appSlug: "appSlug1"});
expect(users.length).toEqual(2);
expect(users[0]).toEqual(user1);
expect(users[1]).toEqual(user3);
});

it("Should return users matching full query", async () => {
const users = await userStore.getUsers({ appSlug: "appSlug1", userId: "userId1"});
expect(users.length).toEqual(1);
expect(users[0]).toEqual(user1);
});
});

0 comments on commit 62eb3a4

Please sign in to comment.