sourcebot/packages/crypto/src/tokenUtils.ts

32 lines
1,008 B
TypeScript
Raw Normal View History

import { Token } from "@sourcebot/schemas/v3/shared.type";
2025-11-01 23:45:09 +00:00
import { SecretManagerServiceClient } from "@google-cloud/secret-manager";
export const getTokenFromConfig = async (token: Token) => {
if ('env' in token) {
const envToken = process.env[token.env];
if (!envToken) {
throw new Error(`Environment variable ${token.env} not found.`);
}
return envToken;
2025-11-01 23:45:09 +00:00
} else if ('gcpSecretName' in token) {
try {
const client = new SecretManagerServiceClient();
const [ response ] = await client.accessSecretVersion({
name: token.gcpSecretName,
});
if (!response.payload?.data) {
throw new Error(`Secret ${token.gcpSecretName} not found.`);
}
return response.payload.data.toString();
} catch (error) {
console.log("HERE IN THE EXCEPTION HANDLER");
throw error;
}
} else {
throw new Error('Invalid token configuration');
}
};