import * as arctic from "arctic"; import { ArcticFetchError, OAuth2RequestError, OAuth2Tokens, decodeIdToken, generateCodeVerifier } from "arctic"; import Elysia from "elysia"; import { ElysiaCookie } from "elysia/cookies"; //#region src/utils.d.ts /** * List of arctic properties that aren't providers. * * @kravetsone Declined PR in arctic repo https://github.com/pilcrowonpaper/arctic/pull/159 */ declare const notProviders: ["generateCodeVerifier", "generateState", "decodeIdToken", "OAuth2RequestError", "ArcticFetchError", "OAuth2Tokens", "CodeChallengeMethod", "UnexpectedErrorResponseBodyError", "UnexpectedResponseError", "OAuth2Client"]; /** * Generate a random state with payload and provider info. * @param provider * @param payload */ declare function generateState(provider: Provider, payload?: unknown): string; /** * Parse state string into an object. * @param state */ declare function parseState(state: string): OAuth2State; //#endregion //#region src/types.d.ts type Arctic = typeof arctic; type Provider = Exclude; type RefreshableProvider = { [K in Provider]: ProviderInstance extends { refreshAccessToken: Function; } ? K : never; }[Provider]; type RevokableProvider = { [K in Provider]: ProviderInstance extends { revokeToken: Function; } ? K : never; }[Provider]; type ProviderOptions = { [K in Provider]?: ProviderParameters>; }; interface ElysiaOAuth2Options { cookie?: Omit; } type ProviderParameters = T extends [...infer R, redirectURI: string] ? [...R, redirectURI: RedirectURI] : T; type ProviderInstance = InstanceType; type ProviderURLOptions = [...(Parameters['createAuthorizationURL']> extends [state: string, ...infer R] ? R extends [codeVerifier: string, ...infer R] ? R : R : Parameters['createAuthorizationURL']>), payload?: unknown]; type ProviderAuthorizeOptions = Parameters['validateAuthorizationCode']> extends [code: string, ...infer R] ? R extends [codeVerifier: string, ...infer R] ? R : R : Parameters['validateAuthorizationCode']>; type RedirectURI = string | ((request: Request) => string); interface OAuth2State

{ csrf: string; provider: P; payload?: T; } interface OAuth2Response { /** * The tokens info returned from Arctic. */ tokens: arctic.OAuth2Tokens; /** * Provider that used for the authentication. */ provider: Provider; /** * Stored payload in the state. */ payload: V; /** * OpenID Connect information when it exists. */ openId?: OpenIDConnect; } interface OpenIDConnect { /** * **Issuer Identifier**: * Identifies the OpenID Provider that issued the ID Token. * * @example 'https://accounts.google.com' */ iss: string; /** * **Subject Identifier**: * A unique and never-reassigned identifier for the authenticated user * within the issuer. */ sub: string; /** * **Audience**: * Identifies the intended recipient(s) of the ID Token. * Usually the OAuth Client ID. */ aud: string | string[]; /** * **Expiration time**: * Unix timestamp (seconds) after which the ID Token must not be accepted. */ exp: number; /** * **Issued-at time**: * Unix timestamp (seconds) when the ID Token was issued. */ iat: number; [key: string]: unknown; } //#endregion //#region src/index.d.ts /** * Elysia Plugin for OAuth 2.0 Authorization Flow. * @param providers Declare providers options and creds. * @param config Optional configuration. * @returns */ declare function oauth2(providers: T, config?: ElysiaOAuth2Options): Elysia<"", { decorator: {}; store: {}; derive: {}; resolve: {}; }, { typebox: {}; error: { OAUTH2_REQUEST_ERROR: arctic.OAuth2RequestError; } & { ARCTIC_FETCH_ERROR: arctic.ArcticFetchError; }; }, { schema: {}; standaloneSchema: {}; macro: {}; macroFn: {}; parser: {}; response: {}; }, {}, { derive: { readonly oauth2: { /** * Create an authorization URL for end-user to redirect to. * @param provider The provider to create from. * @param payload Optional data to store in state for use later. * @returns */ createURL

(provider: P & Provider, ...options: ProviderURLOptions

): URL; /** * Create an authorization URL and immediately redirect the end-user to it. * @param provider The provider to authenticate from. * @param options Optional data to store in state for use later. * @returns */ redirect

(provider: P & Provider, ...options: ProviderURLOptions

): Response; /** * Validate and authorize the code for callback. * @param provider The provider to validate. * @returns */ authorize(provider?: P & Provider, ...options: ProviderAuthorizeOptions

): Promise>; /** * Generate new access token using a valid refresh token. * @param provider * @returns */ refresh

(provider: P, ...options: Parameters['refreshAccessToken']>): Promise; /** * Revoke token. * @param provider * @returns */ revoke

(provider: P, ...options: Parameters['revokeToken']>): Promise; }; }; resolve: {}; schema: {}; standaloneSchema: {}; response: import("elysia").ExtractErrorFromHandle<{ readonly oauth2: { /** * Create an authorization URL for end-user to redirect to. * @param provider The provider to create from. * @param payload Optional data to store in state for use later. * @returns */ createURL

(provider: P & Provider, ...options: ProviderURLOptions

): URL; /** * Create an authorization URL and immediately redirect the end-user to it. * @param provider The provider to authenticate from. * @param options Optional data to store in state for use later. * @returns */ redirect

(provider: P & Provider, ...options: ProviderURLOptions

): Response; /** * Validate and authorize the code for callback. * @param provider The provider to validate. * @returns */ authorize(provider?: P & Provider, ...options: ProviderAuthorizeOptions

): Promise>; /** * Generate new access token using a valid refresh token. * @param provider * @returns */ refresh

(provider: P, ...options: Parameters['refreshAccessToken']>): Promise; /** * Revoke token. * @param provider * @returns */ revoke

(provider: P, ...options: Parameters['revokeToken']>): Promise; }; }>; }, { derive: {}; resolve: {}; schema: {}; standaloneSchema: {}; response: {}; }>; //#endregion export { type Arctic, ArcticFetchError, type ElysiaOAuth2Options, OAuth2RequestError, type OAuth2Response, type OAuth2State, OAuth2Tokens, type OpenIDConnect, type Provider, type ProviderAuthorizeOptions, type ProviderInstance, type ProviderOptions, type ProviderParameters, type ProviderURLOptions, type RedirectURI, type RefreshableProvider, type RevokableProvider, decodeIdToken, oauth2 as default, oauth2, generateCodeVerifier, generateState, notProviders, parseState };