forked from mirror/crumbs-original
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>
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import * as arctic from 'arctic';
|
|
import { getGoogleClient } from './providers.js';
|
|
|
|
export function createGoogleAuthUrl(): { url: URL; state: string; codeVerifier: string } | null {
|
|
const google = getGoogleClient();
|
|
if (!google) return null;
|
|
|
|
const state = arctic.generateState();
|
|
const codeVerifier = arctic.generateCodeVerifier();
|
|
const scopes = ['openid', 'profile', 'email'];
|
|
const url = google.createAuthorizationURL(state, codeVerifier, scopes);
|
|
|
|
return { url, state, codeVerifier };
|
|
}
|
|
|
|
export async function validateGoogleCallback(
|
|
code: string,
|
|
codeVerifier: string
|
|
): Promise<{ email: string; name: string; providerId: string } | null> {
|
|
const google = getGoogleClient();
|
|
if (!google) return null;
|
|
|
|
const tokens = await google.validateAuthorizationCode(code, codeVerifier);
|
|
const idToken = tokens.idToken();
|
|
const claims = arctic.decodeIdToken(idToken) as {
|
|
sub: string;
|
|
email?: string;
|
|
name?: string;
|
|
};
|
|
|
|
if (!claims.email) return null;
|
|
|
|
return {
|
|
email: claims.email,
|
|
name: claims.name || claims.email.split('@')[0],
|
|
providerId: claims.sub
|
|
};
|
|
}
|