// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. import { APIResource } from '../../core/resource'; import { APIPromise } from '../../core/api-promise'; import { buildHeaders } from '../../internal/headers'; import { RequestOptions } from '../../internal/request-options'; export class Mobile extends APIResource { /** * Handles the OAuth callback from StackAuth and redirects to mobile app. * * **Flow:** * 1. Receives authorization code and state from StackAuth * 2. Validates state parameter for CSRF protection * 3. Exchanges authorization code for access token * 4. Stores tokens temporarily in Redis with exchange code * 5. Returns HTML page that redirects to mobile app with exchange code * * **Security:** * - State parameter validation prevents CSRF attacks * - Exchange codes expire in 60 seconds * - One-time use codes prevent replay attacks * * **Note:** This endpoint does not require API key as it's called by StackAuth. */ callback( params: MobileCallbackParams | null | undefined = {}, options?: RequestOptions, ): APIPromise { const { 'Accept-Language': acceptLanguage, 'X-Error-Format': xErrorFormat, ...query } = params ?? {}; return this._client.get('/auth/mobile/callback', { query, ...options, headers: buildHeaders([ { Accept: 'text/html', ...(acceptLanguage != null ? { 'Accept-Language': acceptLanguage } : undefined), ...(xErrorFormat != null ? { 'X-Error-Format': xErrorFormat } : undefined), }, options?.headers, ]), }); } /** * Exchanges the exchange code for final authentication tokens. * * **Security Requirements:** * - Requires valid mobile API key in X-API-Key header * - Rate limited to prevent abuse * - Exchange codes are one-time use and expire in 60 seconds * * **Flow:** * 1. Validates API key and exchange code * 2. Retrieves stored authentication data from Redis * 3. Retrieves teams from StackAuth * 4. Returns complete authentication response and teams * * **Response includes:** * - StackAuth access and refresh tokens * - Teams information */ exchange(params: MobileExchangeParams, options?: RequestOptions): APIPromise { const { 'Accept-Language': acceptLanguage, 'X-Error-Format': xErrorFormat, ...body } = params; return this._client.post('/auth/mobile/exchange', { body, ...options, headers: buildHeaders([ { ...(acceptLanguage != null ? { 'Accept-Language': acceptLanguage } : undefined), ...(xErrorFormat != null ? { 'X-Error-Format': xErrorFormat } : undefined), }, options?.headers, ]), }); } /** * Initiates the mobile OAuth authentication flow by redirecting to StackAuth. * * **Security Requirements:** * - Requires valid mobile API key in X-API-Key header * - Rate limited to prevent abuse * * **Flow:** * 1. Validates API key and parameters * 2. Generates secure state parameter with CSRF protection * 3. Constructs StackAuth OAuth URL * 4. Redirects user to StackAuth for authentication * * **After OAuth:** * User will be redirected to `/auth/mobile/callback` which handles token exchange * and redirects back to the mobile app with an exchange code. */ login(params: MobileLoginParams, options?: RequestOptions): APIPromise { const { 'Accept-Language': acceptLanguage, 'X-Error-Format': xErrorFormat, ...query } = params; return this._client.get('/auth/mobile/login', { query, ...options, headers: buildHeaders([ { ...(acceptLanguage != null ? { 'Accept-Language': acceptLanguage } : undefined), ...(xErrorFormat != null ? { 'X-Error-Format': xErrorFormat } : undefined), }, options?.headers, ]), }); } } export type MobileCallbackResponse = string; /** * Mobile authentication response model. */ export interface MobileExchangeResponse { /** * StackAuth access token for API calls */ access_token: string; /** * Token expiration time in seconds */ expires_in: number; /** * StackAuth refresh token for token renewal */ refresh_token: string; /** * List of teams the user is a member of */ teams?: Array; /** * Token type (always Bearer for StackAuth) */ token_type?: string; } export namespace MobileExchangeResponse { /** * Team model. */ export interface Team { id: string; display_name: string; profile_image_url?: string | null; } } export type MobileLoginResponse = { [key: string]: string }; export interface MobileCallbackParams { /** * Query param: OAuth authorization code from StackAuth */ code?: string | null; /** * Query param: OAuth error if authentication failed */ error?: string | null; /** * Query param: CSRF state parameter */ state?: string | null; /** * Header param */ 'Accept-Language'?: string; /** * Header param: Set to `structured` to receive errors in the structured envelope. * Omit it to keep the legacy error body. */ 'X-Error-Format'?: string; } export interface MobileExchangeParams { /** * Body param: Exchange code received from OAuth callback */ exchange_code: string; /** * Body param: Original app-held PKCE verifier. Required when the login supplied a * code_challenge (the exchange is rejected without a matching verifier); ignored * for deprecated legacy logins without PKCE, which the server rejects when PKCE * enforcement is enabled. */ code_verifier?: string | null; /** * Header param */ 'Accept-Language'?: string; /** * Header param: Set to `structured` to receive errors in the structured envelope. * Omit it to keep the legacy error body. */ 'X-Error-Format'?: string; } export interface MobileLoginParams { /** * Query param: OAuth provider (google, apple, discord, etc.) */ provider: string; /** * Query param: Mobile app deep link URI for final redirect */ redirect_uri: string; /** * Query param: S256 challenge of the app-held verifier. Optional for backward * compatibility; omitting it (legacy flow without PKCE) is deprecated and rejected * when the server enforces PKCE. When supplied, the exchange requires the matching * code_verifier. */ code_challenge?: string | null; /** * Query param: Only S256 is accepted when code_challenge is supplied */ code_challenge_method?: string; /** * Header param */ 'Accept-Language'?: string; /** * Header param: Set to `structured` to receive errors in the structured envelope. * Omit it to keep the legacy error body. */ 'X-Error-Format'?: string; } export declare namespace Mobile { export { type MobileCallbackResponse as MobileCallbackResponse, type MobileExchangeResponse as MobileExchangeResponse, type MobileLoginResponse as MobileLoginResponse, type MobileCallbackParams as MobileCallbackParams, type MobileExchangeParams as MobileExchangeParams, type MobileLoginParams as MobileLoginParams, }; }