Skip to content

Commit

Permalink
Use bitte api key on calls that require authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
GAllen98 committed Jan 30, 2025
1 parent e4e4a2d commit 1b8cfa0
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/commands/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion src/commands/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export const verifyCommand = new Command()

await new PluginService().verify({
pluginId,
accountId,
email: options.email,
repo: options.repo,
version: options.version,
Expand Down
26 changes: 9 additions & 17 deletions src/services/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | null> {
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<string> {
return this.apiKey;
}

async authenticateOrCreateKey(): Promise<string | null> {
Expand Down
40 changes: 14 additions & 26 deletions src/services/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,25 @@ export class PluginService {

async register({
pluginId,
accountId,
}: {
pluginId: string;
accountId?: string;
}): Promise<string | null> {
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) {
await response.json();
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) {
Expand All @@ -49,17 +39,17 @@ export class PluginService {
}
}

async update(pluginId: string, accountId?: string): Promise<string | null> {
const message = await this.auth.getAuthentication(accountId);
async update(pluginId: string): Promise<string | null> {
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) {
Expand All @@ -72,16 +62,16 @@ export class PluginService {
}

async delete(pluginId: string): Promise<void> {
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) {
Expand All @@ -96,28 +86,26 @@ export class PluginService {
email,
repo,
version,
accountId,
categories,
chains,
}: {
pluginId: string;
email: string;
repo: string;
version?: string;
accountId?: string;
categories?: string[];
chains?: number[];
}): Promise<void> {
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;
}

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,
Expand Down
13 changes: 4 additions & 9 deletions src/services/tunnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ export class TunnelService {
}

const result = await this.pluginService.register({
pluginId: this.pluginId,
accountId,
pluginId: this.pluginId
});

if (!result) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1b8cfa0

Please sign in to comment.