UNPKG

@narangcia-oss/cryptic-auth-client-plain-ts

Version:

A TypeScript client for interacting with a cryptic-auth host web server, crafted by Narangcia OSS.

104 lines (103 loc) 3.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OAuthCallbackHandler = void 0; const oauth_1 = require("./oauth"); const tokens_1 = require("./tokens"); /** * Core OAuth callback handler - framework agnostic * Handles the OAuth callback flow logic without any UI dependencies */ class OAuthCallbackHandler { constructor(authClient) { this.authClient = authClient; } /** * Checks if the current URL is an OAuth callback URL */ isOAuthCallback() { return (0, oauth_1.isOAuthCallback)(); } /** * Processes the OAuth callback from the current URL * Returns the result without any UI side effects */ async processCallback() { try { if (!this.isOAuthCallback()) { return { success: false, error: "Not an OAuth callback URL", }; } const { code, state, error: oauthError } = (0, oauth_1.extractOAuthParams)(); if (oauthError) { return { success: false, error: `OAuth error: ${oauthError}`, }; } if (!code) { return { success: false, error: "Authorization code not found", }; } if (!state) { return { success: false, error: "State parameter not found", }; } if (!(0, oauth_1.validateOAuthState)(state)) { return { success: false, error: "Invalid state parameter - possible CSRF attack", }; } // Clean up stored state (0, oauth_1.clearOAuthState)(); // Extract provider from pathname (you might want to make this more robust) const provider = this.extractProviderFromUrl(); // Handle OAuth callback const response = await this.authClient.oauthLoginCallback(provider, { code, state, }); // Extract tokens from response const tokens = (0, tokens_1.extractTokens)(response); return { success: true, tokens, }; } catch (err) { const errorMessage = err instanceof Error ? err.message : "OAuth authentication failed"; return { success: false, error: errorMessage, }; } } /** * Extracts the OAuth provider from the current URL * Override this method for custom provider detection logic */ extractProviderFromUrl() { const pathname = window.location.pathname; if (pathname.includes("github")) return "github"; if (pathname.includes("google")) return "google"; if (pathname.includes("microsoft")) return "microsoft"; // Default fallback - you might want to throw an error instead return "github"; } /** * Cleans the OAuth parameters from the current URL */ cleanUrl() { window.history.replaceState({}, document.title, window.location.origin); } } exports.OAuthCallbackHandler = OAuthCallbackHandler;