From 8bab86bdafd8701cc68afe2e6439365d3c5f8d01 Mon Sep 17 00:00:00 2001 From: Ian Mustafa Date: Wed, 3 Dec 2025 16:00:04 +0700 Subject: [PATCH] Failed to test signature verification --- src/db/migrations/1764747189_brave_kree.sql | 12 +++--- src/middlewares/apiKeyAuth.ts | 4 +- src/modules/auth/commands/command.ts | 43 ++++++++++++--------- src/modules/auth/router.ts | 10 +++++ 4 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/db/migrations/1764747189_brave_kree.sql b/src/db/migrations/1764747189_brave_kree.sql index b95892c..87b7af2 100644 --- a/src/db/migrations/1764747189_brave_kree.sql +++ b/src/db/migrations/1764747189_brave_kree.sql @@ -18,13 +18,11 @@ BEGIN DO UPDATE SET last_value = weekly_id_counters.last_value + 1 RETURNING last_value INTO v_seq; - RETURN format( - '%s-%02s%02s%04s', - p_prefix, - v_yy % 100, - v_ww, - v_seq - ); + RETURN + p_prefix || '-' || + to_char((v_yy % 100), 'FM00') || + to_char(v_ww, 'FM00') || + to_char(v_seq, 'FM0000'); END; $$; --> statement-breakpoint diff --git a/src/middlewares/apiKeyAuth.ts b/src/middlewares/apiKeyAuth.ts index 579f85a..d18b288 100644 --- a/src/middlewares/apiKeyAuth.ts +++ b/src/middlewares/apiKeyAuth.ts @@ -6,7 +6,7 @@ import merchantNonces from '~/db/schema/merchant_nonces' import { verify } from '~/helpers/signature' import { UnauthenticatedError } from '~/helpers/errors' -export const apiKey = new Elysia().macro({ +export const apiKeyAuth = new Elysia().macro({ verifyKey: { headers: t.Object({ 'x-api-key': t.String(), @@ -14,7 +14,7 @@ export const apiKey = new Elysia().macro({ 'x-signature': t.String(), 'x-timestamp': t.Number(), }), - + body: t.Unknown(), async beforeHandle({ headers, body }) { const apiKey = headers['x-api-key'] const nonce = headers['x-nonce'] diff --git a/src/modules/auth/commands/command.ts b/src/modules/auth/commands/command.ts index 5b8c298..3fc0464 100644 --- a/src/modules/auth/commands/command.ts +++ b/src/modules/auth/commands/command.ts @@ -15,26 +15,31 @@ export async function createMerchant(body: CreateMerchantBody) { const apiKey = generateApiKey() const secretKey = generateSecretKey() - // Insert new merchant - const result = await db - .insert(table.merchants) - .values({ - merchantName: body.merchantName, - contactEmail: body.contactEmail, - contactPhone: body.contactPhone, - apiKey, - secretKey, - }) - .returning({ - merchantId: table.merchants.merchantId, - apiKey: table.merchants.apiKey, - secretKey: table.merchants.secretKey, - createdAt: table.merchants.createdAt, - }) + try { + // Insert new merchant + const result = await db + .insert(table.merchants) + .values({ + merchantName: body.merchantName, + contactEmail: body.contactEmail, + contactPhone: body.contactPhone, + apiKey, + secretKey, + }) + .returning({ + merchantId: table.merchants.merchantId, + apiKey: table.merchants.apiKey, + secretKey: table.merchants.secretKey, + createdAt: table.merchants.createdAt, + }) - if (result.length === 0) { + if (result.length === 0) { + throw new Error('Failed to create merchant') + } + + return result[0] + } catch (error) { + console.error('Error creating merchant:', error) throw new Error('Failed to create merchant') } - - return result[0] } diff --git a/src/modules/auth/router.ts b/src/modules/auth/router.ts index 888aa79..eae8730 100644 --- a/src/modules/auth/router.ts +++ b/src/modules/auth/router.ts @@ -3,6 +3,7 @@ import { createMerchantBody, createMerchantResponseSchema } from './schema' import CreateMerchantService from './commands/service' import { basicAuthMiddleware } from '~/middlewares/basicAuth' import { DataAlreadyExistsError } from '~/helpers/errors' +import { apiKeyAuth } from '~/middlewares/apiKeyAuth' export const router = new Elysia({ name: 'modules.auth', @@ -40,5 +41,14 @@ export const router = new Elysia({ security: [{ basicAuth: [] }], }, }) + .use(apiKeyAuth) + .get('/test-key', async ({ body }) => { + return body + }, { + verifyKey: true, + detail: { + security: [{ basicAuth: [] }], + }, + }) export default router