Skip to content

Commit

Permalink
Merge pull request #10 from Apillon/main
Browse files Browse the repository at this point in the history
Support for multiple admin wallets (comma separated)
  • Loading branch information
vinkoS993 authored Feb 15, 2024
2 parents 7cf0d15 + 7bb5e44 commit 00df59f
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 9 deletions.
5 changes: 3 additions & 2 deletions backend/src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface IEnv {
SMTP_EMAIL_FROM: string;
SMTP_EMAIL_FROM_HELLO: string;
APP_URL_BASE: string;
ADMIN_WALLET: string;
ADMIN_WALLET: string[];

MYSQL_HOST_TEST: string;
MYSQL_PORT_TEST: number;
Expand Down Expand Up @@ -81,7 +81,8 @@ export const env = {
/**
* Admin
*/
ADMIN_WALLET: process.env["ADMIN_WALLET"].toLowerCase() || "",
ADMIN_WALLET:
process.env["ADMIN_WALLET"]?.toLocaleLowerCase().split(/[,;]/) || [],

/**
* Mysql URL.
Expand Down
1 change: 1 addition & 0 deletions backend/src/config/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,5 @@ export enum RouteErrorCode {
SIGNATURE_NOT_PRESENT = 400005,
REQUEST_TOKEN_NOT_PRESENT = 400006,
AIRDROP_ERROR = 400007,
INVALID_ADMIN = 400008,
}
2 changes: 1 addition & 1 deletion backend/src/lib/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function readAdminAuthToken(token: string) {
const { wallet } = jwt.verify(token, env.APP_SECRET, {
subject,
}) as any;
if (wallet && wallet.toLowerCase() === env.ADMIN_WALLET) {
if (wallet && env.ADMIN_WALLET.includes(wallet.toLowerCase())) {
return {
wallet,
subject,
Expand Down
8 changes: 6 additions & 2 deletions backend/src/routes/admin-login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@ export async function resolve(req: Request, res: Response): Promise<void> {

const identity = new Identity(null);

if (!context.env.ADMIN_WALLET.includes(body.address?.toLowerCase())) {
throw new ResourceError(RouteErrorCode.INVALID_ADMIN, context);
}

const { isValid } = await identity.validateEvmWalletSignature({
walletAddress: context.env.ADMIN_WALLET,
walletAddress: body.address,
signature: body.signature,
signatureValidityMinutes: 10,
message: `test\n${body.timestamp}`,
timestamp: body.timestamp,
});

if (isValid) {
const jwt = generateAdminAuthToken(context.env.ADMIN_WALLET);
const jwt = generateAdminAuthToken(body.address);
return res.respond(200, { jwt });
} else {
throw new ResourceError(RouteErrorCode.USER_DOES_NOT_EXIST, context);
Expand Down
2 changes: 1 addition & 1 deletion backend/src/tests/routes/admin-login.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("admin login", () => {
beforeAll(async () => {
adminWallet = Wallet.createRandom();
stage = await createContextAndStartServer({
ADMIN_WALLET: adminWallet.address,
ADMIN_WALLET: [adminWallet.address.toLowerCase()],
});
await setupTestDatabase();
});
Expand Down
2 changes: 1 addition & 1 deletion backend/src/tests/routes/create-user-admin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let token;

describe("create user", () => {
beforeAll(async () => {
token = generateAdminAuthToken(env.ADMIN_WALLET);
token = generateAdminAuthToken(env.ADMIN_WALLET[0]);
stage = await createContextAndStartServer();
await setupTestDatabase();
});
Expand Down
2 changes: 1 addition & 1 deletion backend/src/tests/routes/get-statistics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let token;
describe("get statistics", () => {
beforeAll(async () => {
stage = await createContextAndStartServer();
token = generateAdminAuthToken(env.ADMIN_WALLET);
token = generateAdminAuthToken(env.ADMIN_WALLET[0]);
await setupTestDatabase();
await new User({}, stage.context).fake().create();
await new User({}, stage.context)
Expand Down
2 changes: 1 addition & 1 deletion backend/src/tests/routes/get-user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let token;
describe("get user", () => {
beforeAll(async () => {
stage = await createContextAndStartServer();
token = generateAdminAuthToken(env.ADMIN_WALLET);
token = generateAdminAuthToken(env.ADMIN_WALLET[0]);
await setupTestDatabase();
await new User({}, stage.context).fake().create();
});
Expand Down

0 comments on commit 00df59f

Please sign in to comment.