UNPKG

@poppinss/oauth-client

Version:

A framework agnostic package to implement "Login with" flow using OAuth compliant authorization servers.

82 lines (81 loc) 2.69 kB
import { Oauth2AccessToken, Oauth2ClientConfig, ApiRequestContract, RedirectRequestContract } from '../../types.js'; import { HttpClient } from '../../http_client.js'; import { UrlBuilder } from '../../url_builder.js'; /** * Generic implementation of OAuth2. */ export declare class Oauth2Client<Token extends Oauth2AccessToken> { options: Oauth2ClientConfig; constructor(options: Oauth2ClientConfig); /** * Define the authorize url. Can be overridden by config */ protected authorizeUrl: string; /** * Define the access token url. Can be overridden by config */ protected accessTokenUrl: string; /** * Processing the API client response. The child class can overwrite it * for more control */ protected processClientResponse(client: HttpClient, response: any): any; /** * Configure the redirect request. Invoked before * the user callback. * * The client defaults can be removed using the `clearParam` method */ protected configureRedirectRequest(_: RedirectRequestContract): void; /** * Configure the access token request. Invoked before * the user callback. * * The client defaults can be removed using the `clearParam` or * `clearOauth1Param` methods */ protected configureAccessTokenRequest(_: ApiRequestContract): void; /** * Returns the instance of the HTTP client for internal use */ protected httpClient(url: string): HttpClient; /** * Returns the instance of the URL builder */ protected urlBuilder(url: string): UrlBuilder; /** * Returns the redirect url for redirecting the user. Pre-defines * the following params * * - redirect_uri * - client_id */ getRedirectUrl(callback?: (request: RedirectRequestContract) => void): string | Promise<string>; /** * Generates a random token to be stored as a state and to be sent along * for later verification */ getState(): string; /** * Verifies the redirect input with the state input */ verifyState(state: string, inputValue?: string): void; /** * Get the access token from the authorization code. One must define * the authorization code using the callback input. * * ```ts * client.getAccessToken((request) => { * request.field('code', authorizationCode) * }) * ``` * * Pre-defines the following form fields * * - grant_type = 'authorization_code' * - redirect_uri * - client_id * - client_secret */ getAccessToken(callback?: (request: ApiRequestContract) => void): Promise<Token>; }