mirror of
https://github.com/bretzel-app/crumbs.git
synced 2026-10-12 20:06:57 +02:00
Add Google, GitHub, and generic OIDC provider modules using arctic v3. Providers are auto-enabled when env vars are set. Google/GitHub use PKCE, OIDC uses OpenID Connect discovery. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
51 lines
1.7 KiB
TypeScript
51 lines
1.7 KiB
TypeScript
import * as arctic from 'arctic';
|
|
|
|
export type OAuthProviderInfo = {
|
|
id: string;
|
|
name: string;
|
|
icon?: string;
|
|
};
|
|
|
|
let googleClient: arctic.Google | null = null;
|
|
let githubClient: arctic.GitHub | null = null;
|
|
|
|
function getBaseUrl(): string {
|
|
return process.env.ORIGIN || process.env.AUTH_REDIRECT_BASE || 'http://localhost:5173';
|
|
}
|
|
|
|
export function getGoogleClient(): arctic.Google | null {
|
|
if (googleClient) return googleClient;
|
|
const clientId = process.env.AUTH_GOOGLE_CLIENT_ID;
|
|
const clientSecret = process.env.AUTH_GOOGLE_CLIENT_SECRET;
|
|
if (!clientId || !clientSecret) return null;
|
|
googleClient = new arctic.Google(clientId, clientSecret, `${getBaseUrl()}/api/auth/oauth/google/callback`);
|
|
return googleClient;
|
|
}
|
|
|
|
export function getGithubClient(): arctic.GitHub | null {
|
|
if (githubClient) return githubClient;
|
|
const clientId = process.env.AUTH_GITHUB_CLIENT_ID;
|
|
const clientSecret = process.env.AUTH_GITHUB_CLIENT_SECRET;
|
|
if (!clientId || !clientSecret) return null;
|
|
githubClient = new arctic.GitHub(clientId, clientSecret, `${getBaseUrl()}/api/auth/oauth/github/callback`);
|
|
return githubClient;
|
|
}
|
|
|
|
export function getEnabledProviders(): OAuthProviderInfo[] {
|
|
const providers: OAuthProviderInfo[] = [];
|
|
if (getGoogleClient()) providers.push({ id: 'google', name: 'Google' });
|
|
if (getGithubClient()) providers.push({ id: 'github', name: 'GitHub' });
|
|
|
|
const oidcIssuer = process.env.AUTH_OIDC_ISSUER;
|
|
const oidcClientId = process.env.AUTH_OIDC_CLIENT_ID;
|
|
const oidcClientSecret = process.env.AUTH_OIDC_CLIENT_SECRET;
|
|
if (oidcIssuer && oidcClientId && oidcClientSecret) {
|
|
providers.push({
|
|
id: 'oidc',
|
|
name: process.env.AUTH_OIDC_DISPLAY_NAME || 'SSO'
|
|
});
|
|
}
|
|
|
|
return providers;
|
|
}
|