UNPKG

@remcostoeten/fync

Version:

Unified TypeScript library for 9 popular APIs with consistent functional architecture

141 lines 5.07 kB
import type { GoogleOAuthConfig, GoogleOAuthState, GoogleAuthorizationParams, GoogleTokenRequest, GoogleTokenResponse, GoogleRefreshTokenRequest, GoogleRevokeTokenRequest, GoogleUserInfo, GoogleOAuthError, GoogleTokenInfo, GoogleScope } from "./types/google-oauth"; /** * Google OAuth2 Authentication Client * * Provides a chainable API for Google OAuth2 authentication flow. * Supports both standard OAuth2 and PKCE (Proof Key for Code Exchange) flows. * Works with all Google APIs including Calendar, Drive, Gmail, etc. * * @example * ```typescript * const googleAuth = GoogleOAuth({ * clientId: 'your-client-id.googleusercontent.com', * clientSecret: 'your-client-secret', * redirectUri: 'http://localhost:3000/auth/google/callback' * }); * * // Generate authorization URL * const authUrl = googleAuth.getAuthorizationUrl({ * scope: ['email', 'profile', 'https://www.googleapis.com/auth/calendar'], * state: 'random-state-string', * accessType: 'offline' // for refresh tokens * }); * * // Exchange code for token * const tokens = await googleAuth.exchangeCodeForToken(code); * * // Get user information * const user = await googleAuth.withToken(tokens.access_token).getUser(); * ``` */ export declare class GoogleOAuth { private config; private token?; constructor(config: GoogleOAuthConfig); /** * Set the access token for authenticated requests */ withToken(token: string): GoogleOAuth; /** * Generate PKCE code verifier and challenge */ generatePKCE(): { codeVerifier: string; codeChallenge: string; }; /** * Generate the OAuth2 authorization URL * * @param options Configuration options for the authorization request * @param options.scope Array of scopes to request * @param options.state Random state parameter for CSRF protection * @param options.accessType 'offline' for refresh tokens, 'online' for access tokens only * @param options.includeGrantedScopes Include previously granted scopes * @param options.loginHint Email hint for the login screen * @param options.prompt Control the OAuth flow prompts * @param options.pkce Whether to use PKCE flow (returns code_verifier if true) */ getAuthorizationUrl(options?: { scope?: GoogleScope[]; state?: string; accessType?: 'online' | 'offline'; includeGrantedScopes?: boolean; loginHint?: string; prompt?: 'none' | 'consent' | 'select_account'; pkce?: boolean; }): { url: string; codeVerifier?: string; }; /** * Exchange authorization code for access token * * @param code Authorization code received from Google * @param codeVerifier Code verifier for PKCE flow */ exchangeCodeForToken(code: string, codeVerifier?: string): Promise<GoogleTokenResponse>; /** * Refresh an existing access token using a refresh token */ refreshToken(refreshToken: string): Promise<GoogleTokenResponse>; /** * Revoke an access token or refresh token */ revokeToken(token?: string): Promise<void>; /** * Get authenticated user information from Google's userinfo endpoint */ getUser(): Promise<GoogleUserInfo>; /** * Get information about the current access token */ getTokenInfo(): Promise<GoogleTokenInfo>; /** * Check if the current token is valid */ validateToken(): Promise<boolean>; /** * Get the scopes associated with the current token */ getTokenScopes(): Promise<string[]>; /** * Create a complete user profile with user info and token details */ getCompleteProfile(): Promise<{ user: GoogleUserInfo; tokenInfo: GoogleTokenInfo; scopes: string[]; }>; /** * Check if the token has a specific scope */ hasScope(scope: string): Promise<boolean>; /** * Check if the token has all the required scopes */ hasScopes(requiredScopes: string[]): Promise<boolean>; /** * Get the remaining time until token expiration (in seconds) */ getTokenExpirationTime(): Promise<number>; /** * Check if the token is expired or will expire within the given seconds */ isTokenExpired(bufferSeconds?: number): Promise<boolean>; } /** * Factory function to create a Google OAuth instance * * @param config OAuth configuration * @returns GoogleOAuth instance */ export declare function createGoogleOAuth(config: GoogleOAuthConfig): GoogleOAuth; /** * Functional factory for Google OAuth - preferred pattern * * @param config OAuth configuration * @returns GoogleOAuth instance */ export declare function googleOAuth(config: GoogleOAuthConfig): GoogleOAuth; export type { GoogleOAuthConfig, GoogleOAuthState, GoogleAuthorizationParams, GoogleTokenRequest, GoogleTokenResponse, GoogleRefreshTokenRequest, GoogleRevokeTokenRequest, GoogleUserInfo, GoogleOAuthError, GoogleTokenInfo, GoogleScope, }; //# sourceMappingURL=oauth.d.ts.map