Skip to content

Commit

Permalink
chore: refactor SecretsManager to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
zunderscore committed Dec 24, 2024
1 parent 064ac4f commit 5263111
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 84 deletions.
4 changes: 2 additions & 2 deletions src/backend/auth/twitch-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios from "axios";
import authManager from "./auth-manager";
import accountAccess, { FirebotAccount } from "../common/account-access";
import logger from "../logwrapper";
import { secrets } from "../secrets-manager";
import { SecretsManager } from "../secrets-manager";
import { AuthProviderDefinition } from "./auth";
import { getExpiryDateOfAccessToken } from "@twurple/auth";

Expand All @@ -14,7 +14,7 @@ class TwitchAuthProviders {
readonly streamerAccountProviderId = "twitch:streamer-account";
readonly botAccountProviderId = "twitch:bot-account";

readonly twitchClientId = secrets.twitchClientId;
readonly twitchClientId = SecretsManager.secrets.twitchClientId;

readonly streamerAccountProvider: AuthProviderDefinition = {
id: this.streamerAccountProviderId,
Expand Down
6 changes: 3 additions & 3 deletions src/backend/integrations/builtin/streamlabs/streamlabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const io = require("socket.io-client");
const axios = require("axios").default;
const logger = require("../../../logwrapper");

const { secrets } = require("../../../secrets-manager");
const { SecretsManager } = require("../../../secrets-manager");

const slEventHandler = require("./events/streamlabs-event-handler");
const slEffectsLoader = require("./effects/streamlabs-effect-loader");
Expand All @@ -20,8 +20,8 @@ const integrationDefinition = {
name: "StreamLabs",
redirectUriHost: "localhost",
client: {
id: secrets.streamLabsClientId,
secret: secrets.streamLabsClientSecret
id: SecretsManager.secrets.streamLabsClientId,
secret: SecretsManager.secrets.streamLabsClientSecret
},
auth: {
type: "code",
Expand Down
6 changes: 3 additions & 3 deletions src/backend/integrations/builtin/tipeeestream/tipeeestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const EventEmitter = require("events");
const io = require("socket.io-client");
const axios = require("axios").default;
const logger = require("../../../logwrapper");
const { secrets } = require("../../../secrets-manager");
const { SecretsManager } = require("../../../secrets-manager");

const tsEventHandler = require("./events/tipeeestream-event-handler");

Expand All @@ -17,8 +17,8 @@ const integrationDefinition = {
id: "tipeeestream",
name: "TipeeeStream",
client: {
id: secrets.tipeeeStreamClientId,
secret: secrets.tipeeeStreamClientSecret
id: SecretsManager.secrets.tipeeeStreamClientId,
secret: SecretsManager.secrets.tipeeeStreamClientSecret
},
auth: {
type: "code",
Expand Down
74 changes: 0 additions & 74 deletions src/backend/secrets-manager.js

This file was deleted.

70 changes: 70 additions & 0 deletions src/backend/secrets-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { resolve } from 'path';
import { readFileSync } from 'fs';
import logger from "./logwrapper";
import argv from './common/argv-parser';

interface FirebotSecrets {
twitchClientId: string;
tipeeeStreamClientId: string;
tipeeeStreamClientSecret: string;
streamLabsClientId: string;
streamLabsClientSecret: string;
fontAwesome5KitId: string;
}

class SecretsManager {
secrets: FirebotSecrets;
expectedKeys: (keyof FirebotSecrets)[] = [
"twitchClientId",
"tipeeeStreamClientId",
"tipeeeStreamClientSecret",
"streamLabsClientId",
"streamLabsClientSecret",
"fontAwesome5KitId"
];

constructor() {
try {
this.secrets = require("../secrets.json");
} catch (error) {
//silently fail
}
}

testSecrets(): boolean {
logger.debug("...Starting secrets test");
let missingKeys = this.expectedKeys;
try {
let secrets: FirebotSecrets;
if (Object.hasOwn(argv, 'fbsecrets-config') && typeof argv['fbsecrets-config'] === 'string' && /^\.json/i.test(argv['fbsecrets-config'])) {
secrets = JSON.parse(readFileSync(resolve(__dirname, '../', argv['fbsecrets-config']), 'utf-8'));
} else {
secrets = require("../secrets.json") || {};
}

missingKeys = this.expectedKeys.filter(k => secrets[k] == null);

if (!missingKeys.length) {
// We have no missing keys
return true;
}
} catch (err) {
if (err && err.code === "MODULE_NOT_FOUND") {
logger.error("Unable to find secrets.json in the root directory. Please create one. Contact us in the CrowbarTools Discord if you have any questions.");
return false;
} else if (err && err.code === 'ENOENT') {
logger.error(`Unable to find user-specified secrets file '${resolve(__dirname, '../', argv['fb-secrets-json'])}'`);
return false;
}
logger.error(`Secrets file is invalid JSON data`);
return false;
}

logger.error(`secrets.json is missing the following key(s): ${missingKeys.join(", ")}`);
return false;
}
}

const secretsManager = new SecretsManager();

export { secretsManager as SecretsManager };
4 changes: 2 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { app } = require("electron");
const path = require('node:path');

const logger = require("./backend/logwrapper");
const secretsManager = require("./backend/secrets-manager");
const { SecretsManager } = require("./backend/secrets-manager");
const { handleSquirrelEvents } = require("./backend/app-management/squirrel-events");
const {
whenReady,
Expand All @@ -16,7 +16,7 @@ const {

logger.info("Starting Firebot...");

if (!secretsManager.testSecrets()) {
if (!SecretsManager.testSecrets()) {
logger.debug("...Testing for secrets failed");
app.quit();
return;
Expand Down

0 comments on commit 5263111

Please sign in to comment.