1import { CreateURLOptions } from 'expo-linking';
2import { WebBrowserOpenOptions, WebBrowserWindowFeatures } from 'expo-web-browser';
3
4// @needsAudit
5export enum CodeChallengeMethod {
6  /**
7   * The default and recommended method for transforming the code verifier.
8   * - Convert the code verifier to ASCII.
9   * - Create a digest of the string using crypto method SHA256.
10   * - Convert the digest to Base64 and URL encode it.
11   */
12  S256 = 'S256',
13  /**
14   * This should not be used. When used, the code verifier will be sent to the server as-is.
15   */
16  Plain = 'plain',
17}
18
19// @needsAudit
20/**
21 * The client informs the authorization server of the desired grant type by using the response type.
22 *
23 * @see [Section 3.1.1](https://tools.ietf.org/html/rfc6749#section-3.1.1).
24 */
25export enum ResponseType {
26  /**
27   * For requesting an authorization code as described by [Section 4.1.1](https://tools.ietf.org/html/rfc6749#section-4.1.1).
28   */
29  Code = 'code',
30  /**
31   * For requesting an access token (implicit grant) as described by [Section 4.2.1](https://tools.ietf.org/html/rfc6749#section-4.2.1).
32   */
33  Token = 'token',
34  /**
35   * A custom registered type for getting an `id_token` from Google OAuth.
36   */
37  IdToken = 'id_token',
38}
39
40// @needsAudit
41/**
42 * Informs the server if the user should be prompted to login or consent again.
43 * This can be used to present a dialog for switching accounts after the user has already been logged in.
44 * You should use this in favor of clearing cookies (which is mostly not possible on iOS).
45 *
46 * @see [Section 3.1.2.1](https://openid.net/specs/openid-connect-core-1_0.html#AuthorizationRequest).
47 */
48export enum Prompt {
49  /**
50   * Server must not display any auth or consent UI. Can be used to check for existing auth or consent.
51   * An error is returned if a user isn't already authenticated or the client doesn't have pre-configured consent for the requested claims, or does not fulfill other conditions for processing the request.
52   * The error code will typically be `login_required`, `interaction_required`, or another code defined in [Section 3.1.2.6](https://openid.net/specs/openid-connect-core-1_0.html#AuthError).
53   */
54  None = 'none',
55  /**
56   * The server should prompt the user to reauthenticate.
57   * If it cannot reauthenticate the End-User, it must return an error, typically `login_required`.
58   */
59  Login = 'login',
60  /**
61   * Server should prompt the user for consent before returning information to the client.
62   * If it cannot obtain consent, it must return an error, typically `consent_required`.
63   */
64  Consent = 'consent',
65  /**
66   * Server should prompt the user to select an account. Can be used to switch accounts.
67   * If it can't obtain an account selection choice made by the user, it must return an error, typically `account_selection_required`.
68   */
69  SelectAccount = 'select_account',
70}
71
72// @needsAudit
73/**
74 * Options passed to the `promptAsync()` method of `AuthRequest`s.
75 * This can be used to configure how the web browser should look and behave.
76 */
77export type AuthRequestPromptOptions = Omit<WebBrowserOpenOptions, 'windowFeatures'> & {
78  /**
79   * URL to open when prompting the user. This usually should be defined internally and left `undefined` in most cases.
80   */
81  url?: string;
82  /**
83   * Should the authentication request use the Expo proxy service `auth.expo.io`.
84   * @default false
85   */
86  useProxy?: boolean;
87  /**
88   * URL options to be used when creating the redirect URL for the auth proxy.
89   */
90  proxyOptions?: Omit<CreateURLOptions, 'queryParams'> & { path?: string };
91  /**
92   * Features to use with `window.open()`.
93   * @platform web
94   */
95  windowFeatures?: WebBrowserWindowFeatures;
96};
97
98// @needsAudit
99/**
100 * Represents an OAuth authorization request as JSON.
101 */
102export interface AuthRequestConfig {
103  /**
104   * Specifies what is returned from the authorization server.
105   *
106   * [Section 3.1.1](https://tools.ietf.org/html/rfc6749#section-3.1.1)
107   *
108   * @default ResponseType.Code
109   */
110  responseType?: ResponseType | string;
111  /**
112   * A unique string representing the registration information provided by the client.
113   * The client identifier is not a secret; it is exposed to the resource owner and shouldn't be used
114   * alone for client authentication.
115   *
116   * The client identifier is unique to the authorization server.
117   *
118   * [Section 2.2](https://tools.ietf.org/html/rfc6749#section-2.2)
119   */
120  clientId: string;
121  /**
122   * After completing an interaction with a resource owner the
123   * server will redirect to this URI. Learn more about [linking in Expo](https://docs.expo.dev/versions/latest/workflow/linking/).
124   *
125   * [Section 3.1.2](https://tools.ietf.org/html/rfc6749#section-3.1.2)
126   */
127  redirectUri: string;
128  /**
129   * List of strings to request access to.
130   *
131   * [Section 3.3](https://tools.ietf.org/html/rfc6749#section-3.3)
132   */
133  scopes?: string[];
134  /**
135   * Client secret supplied by an auth provider.
136   * There is no secure way to store this on the client.
137   *
138   * [Section 2.3.1](https://tools.ietf.org/html/rfc6749#section-2.3.1)
139   */
140  clientSecret?: string;
141  /**
142   * Method used to generate the code challenge. You should never use `Plain` as it's not good enough for secure verification.
143   * @default CodeChallengeMethod.S256
144   */
145  codeChallengeMethod?: CodeChallengeMethod;
146  /**
147   * Derived from the code verifier by using the `CodeChallengeMethod`.
148   *
149   * [Section 4.2](https://tools.ietf.org/html/rfc7636#section-4.2)
150   */
151  codeChallenge?: string;
152  /**
153   * Informs the server if the user should be prompted to login or consent again.
154   * This can be used to present a dialog for switching accounts after the user has already been logged in.
155   *
156   * [Section 3.1.2.1](https://openid.net/specs/openid-connect-core-1_0.html#AuthorizationRequest)
157   */
158  prompt?: Prompt;
159  /**
160   * Used for protection against [Cross-Site Request Forgery](https://tools.ietf.org/html/rfc6749#section-10.12).
161   */
162  state?: string;
163  /**
164   * Extra query params that'll be added to the query string.
165   */
166  extraParams?: Record<string, string>;
167  /**
168   * Should use [Proof Key for Code Exchange](https://oauth.net/2/pkce/).
169   * @default true
170   */
171  usePKCE?: boolean;
172}
173