@dataroadinc/setup-auth
Version:
CLI tool and programmatic API for automated OAuth setup across cloud platforms
158 lines (157 loc) • 6.61 kB
JavaScript
import { buildRedirectUriList } from "../utils/redirect-urls.js";
import { GcpOAuthWebClientManager } from "../providers/gcp/oauth/client.js";
import { SetupAuthError } from "../utils/error.js";
export class SetupAuthAPI {
constructor() { }
static getInstance() {
if (!SetupAuthAPI.instance) {
SetupAuthAPI.instance = new SetupAuthAPI();
}
return SetupAuthAPI.instance;
}
async registerCallbackUrls(config) {
try {
this.validateCallbackUrlConfig(config);
const redirectUris = await this.buildRedirectUris(config);
switch (config.provider) {
case "gcp":
return await this.registerGcpCallbackUrls(config, redirectUris);
case "github":
return await this.registerGitHubCallbackUrls(config, redirectUris);
case "azure":
return await this.registerAzureCallbackUrls(config, redirectUris);
case "linkedin":
return await this.registerLinkedInCallbackUrls(config, redirectUris);
default:
throw new SetupAuthError(`Unsupported OAuth provider: ${config.provider}`);
}
}
catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : String(error),
};
}
}
async updateCallbackUrls(config) {
try {
this.validateCallbackUrlConfig(config);
const redirectUris = await this.buildRedirectUris(config);
switch (config.provider) {
case "gcp":
return await this.updateGcpCallbackUrls(config, redirectUris);
case "github":
return await this.updateGitHubCallbackUrls(config, redirectUris);
case "azure":
return await this.updateAzureCallbackUrls(config, redirectUris);
case "linkedin":
return await this.updateLinkedInCallbackUrls(config, redirectUris);
default:
throw new SetupAuthError(`Unsupported OAuth provider: ${config.provider}`);
}
}
catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : String(error),
};
}
}
validateCallbackUrlConfig(config) {
if (!config.provider) {
throw new SetupAuthError("OAuth provider is required");
}
if (!config.platform) {
throw new SetupAuthError("Platform is required");
}
switch (config.provider) {
case "gcp":
if (!config.projectConfig?.gcpProjectId) {
throw new SetupAuthError("GCP project ID is required for GCP provider");
}
break;
case "github":
if (!config.projectConfig?.githubAppName) {
throw new SetupAuthError("GitHub app name is required for GitHub provider");
}
break;
case "azure":
if (!config.projectConfig?.azureTenantId) {
throw new SetupAuthError("Azure tenant ID is required for Azure provider");
}
break;
}
}
async buildRedirectUris(config) {
const options = {
platform: config.platform,
oauthProvider: config.provider,
deploymentUrl: config.deploymentUrl,
callbackPath: config.callbackPath,
redirectOptions: {
gcpOauthProjectId: config.projectConfig?.gcpProjectId || "",
clientId: "",
additionalUrls: config.additionalUrls,
wildcardPatterns: config.wildcardPatterns,
},
};
return buildRedirectUriList(options);
}
async registerGcpCallbackUrls(config, redirectUris) {
if (!config.projectConfig?.gcpProjectId) {
throw new SetupAuthError("GCP project ID is required");
}
const oauthClient = new GcpOAuthWebClientManager(config.projectConfig.gcpProjectId);
const displayName = `${config.platform.charAt(0).toUpperCase() + config.platform.slice(1)} OAuth Client`;
const { clientId, clientSecret } = await oauthClient.createClient(displayName, redirectUris, []);
return {
success: true,
registeredUrls: redirectUris,
clientId,
providerDetails: {
clientSecret,
projectId: config.projectConfig.gcpProjectId,
},
};
}
async updateGcpCallbackUrls(config, redirectUris) {
if (!config.projectConfig?.gcpProjectId) {
throw new SetupAuthError("GCP project ID is required");
}
const clientId = process.env.GCP_OAUTH_CLIENT_ID?.replace(/\.apps\.googleusercontent\.com$/, "");
if (!clientId) {
throw new SetupAuthError("GCP OAuth client ID not found. Please run setup first.");
}
const oauthClient = new GcpOAuthWebClientManager(config.projectConfig.gcpProjectId);
await oauthClient.updateRedirectUris(clientId, redirectUris);
return {
success: true,
redirectUris,
};
}
async registerGitHubCallbackUrls(_config, _redirectUris) {
throw new SetupAuthError("GitHub OAuth app creation not yet implemented");
}
async updateGitHubCallbackUrls(_config, _redirectUris) {
throw new SetupAuthError("GitHub OAuth app update not yet implemented");
}
async registerAzureCallbackUrls(_config, _redirectUris) {
throw new SetupAuthError("Azure AD app registration not yet implemented");
}
async updateAzureCallbackUrls(_config, _redirectUris) {
throw new SetupAuthError("Azure AD app update not yet implemented");
}
async registerLinkedInCallbackUrls(_config, _redirectUris) {
throw new SetupAuthError("LinkedIn OAuth app creation not yet implemented");
}
async updateLinkedInCallbackUrls(_config, _redirectUris) {
throw new SetupAuthError("LinkedIn OAuth app update not yet implemented");
}
}
SetupAuthAPI.instance = null;
export async function registerCallbackUrls(config) {
return SetupAuthAPI.getInstance().registerCallbackUrls(config);
}
export async function updateCallbackUrls(config) {
return SetupAuthAPI.getInstance().updateCallbackUrls(config);
}