From 1b8cfa004d11871e65bd18ce94c7fac68dbd29d8 Mon Sep 17 00:00:00 2001 From: Guilherme Allen Date: Thu, 30 Jan 2025 16:46:14 +0000 Subject: [PATCH] Use bitte api key on calls that require authorization --- src/commands/delete.ts | 2 +- src/commands/deploy.ts | 5 ++--- src/commands/verify.ts | 1 - src/services/authentication.ts | 26 ++++++++-------------- src/services/plugin.ts | 40 ++++++++++++---------------------- src/services/tunnel.ts | 13 ++++------- 6 files changed, 30 insertions(+), 57 deletions(-) diff --git a/src/commands/delete.ts b/src/commands/delete.ts index 9f2191a..dba714f 100644 --- a/src/commands/delete.ts +++ b/src/commands/delete.ts @@ -32,7 +32,7 @@ export const deleteCommand = new Command() } const pluginService = new PluginService(); const authentication = - await pluginService.auth.getAuthentication(accountId); + await pluginService.auth.getAuthentication(); if (!authentication) { console.error("Authentication failed. Unable to delete the plugin."); return; diff --git a/src/commands/deploy.ts b/src/commands/deploy.ts index db64c04..8de1a06 100644 --- a/src/commands/deploy.ts +++ b/src/commands/deploy.ts @@ -34,12 +34,11 @@ export const deployCommand = new Command() } const pluginService = new PluginService(); try { - const updateRes = await pluginService.update(id, accountId); + const updateRes = await pluginService.update(id); if (!updateRes) { console.log("Attempting to register plugin..."); await pluginService.register({ - pluginId: id, - accountId, + pluginId: id }); } } catch (error) { diff --git a/src/commands/verify.ts b/src/commands/verify.ts index f979b34..93ce37f 100644 --- a/src/commands/verify.ts +++ b/src/commands/verify.ts @@ -65,7 +65,6 @@ export const verifyCommand = new Command() await new PluginService().verify({ pluginId, - accountId, email: options.email, repo: options.repo, version: options.version, diff --git a/src/services/authentication.ts b/src/services/authentication.ts index a642754..bc0befd 100644 --- a/src/services/authentication.ts +++ b/src/services/authentication.ts @@ -20,28 +20,20 @@ dotenv.config({ path: ".env.local", override: true }); export class AuthenticationService { private readonly bitteUrls: BitteUrls; + private readonly apiKey: string; constructor(bitteUrls: BitteUrls) { this.bitteUrls = bitteUrls; + this.apiKey = (() => { + if(!process?.env?.BITTE_API_KEY) { + throw new Error("Missing api key. Please define BITTE_API_KEY in your environment variables."); + } + return process.env.BITTE_API_KEY; + })(); } - async getAuthentication(accountId?: string): Promise { - const bitteKeyString = process.env.BITTE_KEY; - if (!bitteKeyString) return null; - - const parsedKey = JSON.parse(bitteKeyString) as KeySignMessageParams; - if ( - (accountId && - (await verifyMessage({ - params: parsedKey, - accountIdToVerify: accountId, - }))) || - !accountId - ) { - return bitteKeyString; - } - - return null; + async getAuthentication(): Promise { + return this.apiKey; } async authenticateOrCreateKey(): Promise { diff --git a/src/services/plugin.ts b/src/services/plugin.ts index 552ec23..8c89275 100644 --- a/src/services/plugin.ts +++ b/src/services/plugin.ts @@ -12,21 +12,14 @@ export class PluginService { async register({ pluginId, - accountId, }: { pluginId: string; - accountId?: string; }): Promise { - let message = await this.auth.getAuthentication(accountId); - if (!message || !accountId) { - const signedMessage = await this.auth.getSignedMessage(); - message = JSON.stringify(signedMessage); - } - + let apiKey = await this.auth.getAuthentication(); try { const response = await fetch(`${this.bitteUrls.BASE_URL}/${pluginId}`, { method: "POST", - headers: { "bitte-api-key": message }, + headers: { "authorization": apiKey }, }); if (response.ok) { @@ -34,13 +27,10 @@ export class PluginService { console.log("Plugin registered successfully"); return pluginId; } else { - const errorData = await response.json(); const errorMessage = `Failed to register plugin (ID: ${pluginId}). HTTP Status: ${response.status} - ${response.statusText}.`; console.error(errorMessage); - console.error(`Server response: ${JSON.stringify(errorData)}`); - if (errorData.debugUrl) { - console.log(`Debug URL: ${errorData.debugUrl}`); - } + const errorData = await response.text(); + console.error(`Server response: \n${errorData}`); return null; } } catch (error) { @@ -49,17 +39,17 @@ export class PluginService { } } - async update(pluginId: string, accountId?: string): Promise { - const message = await this.auth.getAuthentication(accountId); + async update(pluginId: string): Promise { + const apiKey = await this.auth.getAuthentication(); - if (!message) { + if (!apiKey) { console.warn(`No API key found for plugin ${pluginId}.`); return null; } const response = await fetch(`${this.bitteUrls.BASE_URL}/${pluginId}`, { method: "PUT", - headers: { "bitte-api-key": message }, + headers: { "authorization": apiKey }, }); if (!response.ok) { @@ -72,16 +62,16 @@ export class PluginService { } async delete(pluginId: string): Promise { - const message = await this.auth.getAuthentication(); + const apiKey = await this.auth.getAuthentication(); - if (!message) { + if (!apiKey) { console.error("No API key found. Unable to delete plugin."); return; } const response = await fetch(`${this.bitteUrls.BASE_URL}/${pluginId}`, { method: "DELETE", - headers: { "bitte-api-key": message }, + headers: { "authorization": apiKey }, }); if (response.ok) { @@ -96,7 +86,6 @@ export class PluginService { email, repo, version, - accountId, categories, chains, }: { @@ -104,12 +93,11 @@ export class PluginService { email: string; repo: string; version?: string; - accountId?: string; categories?: string[]; chains?: number[]; }): Promise { - const message = await this.auth.getAuthentication(accountId); - if (!message) { + const apiKey = await this.auth.getAuthentication(); + if (!apiKey) { console.error("No API key found. Unable to request plugin verification."); return; } @@ -117,7 +105,7 @@ export class PluginService { try { const res = await fetch(`${this.bitteUrls.BASE_URL}/verify/${pluginId}`, { method: "POST", - headers: { "bitte-api-key": message }, + headers: { "authorization": apiKey }, body: JSON.stringify({ repo: repo, email: email, diff --git a/src/services/tunnel.ts b/src/services/tunnel.ts index 6f1d039..81bd8a9 100644 --- a/src/services/tunnel.ts +++ b/src/services/tunnel.ts @@ -183,8 +183,7 @@ export class TunnelService { } const result = await this.pluginService.register({ - pluginId: this.pluginId, - accountId, + pluginId: this.pluginId }); if (!result) { @@ -228,17 +227,13 @@ export class TunnelService { `Change detected in ${relativePath}. Attempting to update or register the plugin...`, ); - const { accountId } = await validateAndParseOpenApiSpec( - getSpecUrl(this.tunnelUrl), - ); const authentication = - await this.pluginService.auth.getAuthentication(accountId); + await this.pluginService.auth.getAuthentication(); const result = authentication - ? await this.pluginService.update(this.pluginId, accountId) + ? await this.pluginService.update(this.pluginId) : await this.pluginService.register({ - pluginId: this.pluginId, - accountId, + pluginId: this.pluginId }); if (result && !authentication) {