@remcostoeten/fync
Version:
Unified TypeScript library for 9 popular APIs with consistent functional architecture
125 lines • 4.3 kB
TypeScript
import type { GitHubOAuthConfig, GitHubOAuthState, GitHubAuthorizationParams, GitHubTokenRequest, GitHubTokenResponse, GitHubUserInfo, GitHubEmailInfo, GitHubOAuthError, GitHubRefreshTokenRequest, GitHubRevokeTokenRequest, GitHubScope } from "./types/github-oauth";
/**
* GitHub OAuth2 Authentication Client
*
* Provides a chainable API for GitHub OAuth2 authentication flow.
* Supports both standard OAuth2 and PKCE (Proof Key for Code Exchange) flows.
*
* @example
* ```typescript
* const githubAuth = GitHubOAuth({
* clientId: 'your-client-id',
* clientSecret: 'your-client-secret',
* redirectUri: 'http://localhost:3000/auth/github/callback'
* });
*
* // Generate authorization URL
* const authUrl = githubAuth.getAuthorizationUrl({
* scope: ['user:email', 'repo'],
* state: 'random-state-string'
* });
*
* // Exchange code for token
* const tokens = await githubAuth.exchangeCodeForToken(code, state);
*
* // Get user information
* const user = await githubAuth.withToken(tokens.access_token).getUser();
* ```
*/
export declare class GitHubOAuth {
private config;
private token?;
constructor(config: GitHubOAuthConfig);
/**
* Set the access token for authenticated requests
*/
withToken(token: string): GitHubOAuth;
/**
* 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.allowSignup Whether to allow new user signups
* @param options.login Suggested username for login
* @param options.pkce Whether to use PKCE flow (returns code_verifier if true)
*/
getAuthorizationUrl(options?: {
scope?: GitHubScope[];
state?: string;
allowSignup?: boolean;
login?: string;
pkce?: boolean;
}): {
url: string;
codeVerifier?: string;
};
/**
* Exchange authorization code for access token
*
* @param code Authorization code received from GitHub
* @param state State parameter (should match the one sent in authorization request)
* @param codeVerifier Code verifier for PKCE flow
*/
exchangeCodeForToken(code: string, state?: string, codeVerifier?: string): Promise<GitHubTokenResponse>;
/**
* Refresh an existing access token (if refresh tokens are supported)
*/
refreshToken(refreshToken: string): Promise<GitHubTokenResponse>;
/**
* Revoke an access token
*/
revokeToken(accessToken?: string): Promise<void>;
/**
* Get authenticated user information
*/
getUser(): Promise<GitHubUserInfo>;
/**
* Get authenticated user's email addresses
*/
getUserEmails(): Promise<GitHubEmailInfo[]>;
/**
* Get authenticated user's primary email
*/
getPrimaryEmail(): Promise<string | null>;
/**
* 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 emails
*/
getCompleteProfile(): Promise<{
user: GitHubUserInfo;
emails: GitHubEmailInfo[];
primaryEmail: string | null;
scopes: string[];
}>;
}
/**
* Factory function to create a GitHub OAuth instance
*
* @param config OAuth configuration
* @returns GitHubOAuth instance
*/
export declare function createGitHubOAuth(config: GitHubOAuthConfig): GitHubOAuth;
/**
* Functional factory for GitHub OAuth - preferred pattern
*
* @param config OAuth configuration
* @returns GitHubOAuth instance
*/
export declare function gitHubOAuth(config: GitHubOAuthConfig): GitHubOAuth;
export type { GitHubOAuthConfig, GitHubOAuthState, GitHubAuthorizationParams, GitHubTokenRequest, GitHubTokenResponse, GitHubUserInfo, GitHubEmailInfo, GitHubOAuthError, GitHubRefreshTokenRequest, GitHubRevokeTokenRequest, GitHubScope, };
//# sourceMappingURL=oauth.d.ts.map