1 import type { ChallengePayload } from '../authentication/interface';
2 import type { APP_CLIENT_IDS } from '../constants';
3 import { HTTP_ERROR_CODES } from '../errors';
4 import type { AuthenticationCredentialsPayload } from '../webauthn/interface';
6 export const PASSWORD_WRONG_ERROR = 8002;
7 export const SCOPE_REAUTH_SSO = 9107;
13 Payload?: ChallengePayload;
16 SSOResponseToken: string;
24 PersistentCookies: Number(persistent),
26 ignoreHandler: [HTTP_ERROR_CODES.TOO_MANY_REQUESTS],
29 export const auth2FA = (data: { TwoFactorCode: string } | { FIDO2: AuthenticationCredentialsPayload }) => ({
31 url: 'core/v4/auth/2fa',
35 export const authJwt = (data: { Token: String; ClientSecret?: String }) => ({
37 url: 'core/v4/auth/jwt',
41 export const revoke = (params?: { Child?: 1 }) => ({
47 interface RefreshArgs {
52 export const setRefreshCookies = (data?: RefreshArgs) => {
55 // WARNING: The refresh cookie is set with `path=/api/auth/refresh;`
64 ResponseType: 'token',
65 GrantType: 'refresh_token',
66 RefreshToken: data.RefreshToken,
67 RedirectURI: data.RedirectURI || 'https://protonmail.com',
72 interface CookiesArgs {
80 export const setCookies = ({
84 RedirectURI = 'https://protonmail.com',
88 url: 'core/v4/auth/cookies',
91 ResponseType: 'token',
92 GrantType: 'refresh_token',
95 Persistent: Number(Persistent),
100 export const getLocalKey = () => ({
102 url: 'auth/v4/sessions/local/key',
104 export const setLocalKey = (Key: string) => ({
106 url: 'auth/v4/sessions/local/key',
111 export const pushForkSession = (data: {
113 ChildClientID: APP_CLIENT_IDS;
119 url: 'auth/v4/sessions/forks',
123 export const pullForkSession = (selector: string) => ({
125 url: `auth/v4/sessions/forks/${selector}`,
128 export const getLocalSessions = (params?: { Email: string }) => ({
130 url: `auth/v4/sessions/local`,
134 export const getInfo = ({
141 intent?: 'Proton' | 'Auto' | 'SSO';
143 reauthScope?: 'password' | 'locked';
146 url: 'core/v4/auth/info',
148 ...(username ? { Username: username } : undefined),
150 ...(isTesting ? { IsTesting: isTesting } : undefined),
151 ...(reauthScope ? { ReauthScope: reauthScope } : undefined),
155 export const getModulus = () => ({
157 url: 'core/v4/auth/modulus',
160 export const createSession = (data?: { ClientSecret?: string; Payload?: ChallengePayload }) => ({
162 url: 'auth/v4/sessions',
166 export const querySessions = () => ({
168 url: 'auth/v4/sessions',
171 export const revokeOtherSessions = () => ({
173 url: 'auth/v4/sessions',
176 export const revokeSession = (UID: string | number) => ({
178 url: `auth/v4/sessions/${UID}`,
181 export const queryScopes = () => ({
183 url: 'core/v4/auth/scopes',
186 export const getMnemonicAuthInfo = (Username?: string) => ({
188 url: 'auth/v4/mnemonic/info',
189 data: Username ? { Username } : undefined,
192 export const authMnemonic = (Username: string, persistent: boolean) => ({
194 url: 'auth/v4/mnemonic',
195 data: { Username, PersistentCookies: Number(persistent) },
196 ignoreHandler: [HTTP_ERROR_CODES.TOO_MANY_REQUESTS],
199 export const payload = (data: ChallengePayload) => ({
201 url: `auth/v4/sessions/payload`,
207 export const reauthMnemonic = (data: { Username: string; PersistentCookies: boolean }) => ({
209 url: 'auth/v4/mnemonic/reauth',
212 PersistentCookies: Number(data.PersistentCookies),