@sudowealth/schwab-api
Version:
TypeScript client for Charles Schwab API with OAuth support, market data, trading functionality, and complete type safety
116 lines (115 loc) • 3.14 kB
TypeScript
import { type TokenData } from '../types';
/**
* Options for cookie-based token storage
*/
export interface CookieTokenStoreOptions {
/**
* Secret key for signing cookies
*/
encryptionKey: string;
/**
* Cookie name for storing tokens
*/
cookieName?: string;
/**
* Cookie options
*/
cookieOptions?: {
httpOnly?: boolean;
secure?: boolean;
sameSite?: 'strict' | 'lax' | 'none';
maxAge?: number;
path?: string;
domain?: string;
};
/**
* Whether to validate tokens on load
*/
validateOnLoad?: boolean;
}
/**
* Cookie-based token storage adapter
* Provides secure token persistence using signed cookies
*/
export declare class CookieTokenStore {
private options;
constructor(options: CookieTokenStoreOptions);
/**
* Create a signed cookie value
*
* @param data Data to sign and encode
* @returns Signed cookie value in format "signature.base64(data)"
*/
createSignedCookie(data: any): Promise<string>;
/**
* Verify and decode a signed cookie value
*
* @param cookieValue Signed cookie value to verify
* @returns Decoded data or null if verification fails
*/
verifyAndDecodeCookie<T>(cookieValue: string): Promise<T | null>;
/**
* Save tokens to a cookie
*
* @param tokens Token data to save
* @returns Cookie header value
*/
save(tokens: TokenData): Promise<string>;
/**
* Load tokens from a cookie
*
* @param cookieHeader Cookie header value
* @returns Token data or null if not found/invalid
*/
load(cookieHeader: string | null): Promise<TokenData | null>;
/**
* Clear the token cookie
*
* @returns Cookie header value to clear the cookie
*/
clear(): string;
/**
* Extract a specific cookie value from a cookie header
*
* @param cookieHeader Full cookie header string
* @param cookieName Name of cookie to extract
* @returns Cookie value or undefined
*/
private extractCookieValue;
/**
* Validate token data
*
* @param tokens Token data to validate
* @returns True if valid, false otherwise
*/
private validateTokens;
}
/**
* Helper function to create a cookie token store with Request/Response helpers
*/
export declare function createCookieTokenStore(options: CookieTokenStoreOptions): {
/**
* Save tokens and get the Set-Cookie header
*/
save(tokens: TokenData): Promise<string>;
/**
* Load tokens from a Request object
*/
loadFromRequest(request: Request): Promise<TokenData | null>;
/**
* Load tokens from cookie header string
*/
load(cookieHeader: string | null): Promise<TokenData | null>;
/**
* Create a Response with token cookie set
*/
createResponseWithTokens(response: Response, tokens: TokenData): Promise<Response>;
/**
* Clear tokens by setting an expired cookie
*/
clear(): string;
/**
* The underlying store instance
*/
store: CookieTokenStore;
};