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;
12 Payload?: ChallengePayload;
15 SSOResponseToken: string;
23 PersistentCookies: Number(persistent),
25 ignoreHandler: [HTTP_ERROR_CODES.TOO_MANY_REQUESTS],
28 export const auth2FA = (data: { TwoFactorCode: string } | { FIDO2: AuthenticationCredentialsPayload }) => ({
30 url: 'core/v4/auth/2fa',
34 export const authJwt = (data: { Token: String; ClientSecret?: String }) => ({
36 url: 'core/v4/auth/jwt',
40 export const revoke = (params?: { Child?: 1 }) => ({
46 interface RefreshArgs {
51 export const setRefreshCookies = (data?: RefreshArgs) => {
54 // WARNING: The refresh cookie is set with `path=/api/auth/refresh;`
63 ResponseType: 'token',
64 GrantType: 'refresh_token',
65 RefreshToken: data.RefreshToken,
66 RedirectURI: data.RedirectURI || 'https://protonmail.com',
71 interface CookiesArgs {
79 export const setCookies = ({
83 RedirectURI = 'https://protonmail.com',
87 url: 'core/v4/auth/cookies',
90 ResponseType: 'token',
91 GrantType: 'refresh_token',
94 Persistent: Number(Persistent),
99 export const getLocalKey = () => ({
101 url: 'auth/v4/sessions/local/key',
103 export const setLocalKey = (Key: string) => ({
105 url: 'auth/v4/sessions/local/key',
110 export const pushForkSession = (data: {
112 ChildClientID: APP_CLIENT_IDS;
118 url: 'auth/v4/sessions/forks',
122 export const pullForkSession = (selector: string) => ({
124 url: `auth/v4/sessions/forks/${selector}`,
127 export const getLocalSessions = (params?: { Email: string }) => ({
129 url: `auth/v4/sessions/local`,
133 export const getInfo = (Username?: string, intent: 'Proton' | 'Auto' | 'SSO' = 'Proton', isTesting?: boolean) => ({
135 url: 'core/v4/auth/info',
137 ...(Username ? { Username } : undefined),
139 ...(isTesting ? { IsTesting: isTesting } : undefined),
143 export const getModulus = () => ({
145 url: 'core/v4/auth/modulus',
148 export const createSession = (data?: { ClientSecret?: string; Payload?: ChallengePayload }) => ({
150 url: 'auth/v4/sessions',
154 export const querySessions = () => ({
156 url: 'auth/v4/sessions',
159 export const revokeOtherSessions = () => ({
161 url: 'auth/v4/sessions',
164 export const revokeSession = (UID: string | number) => ({
166 url: `auth/v4/sessions/${UID}`,
169 export const queryScopes = () => ({
171 url: 'core/v4/auth/scopes',
174 export const getMnemonicAuthInfo = (Username?: string) => ({
176 url: 'auth/v4/mnemonic/info',
177 data: Username ? { Username } : undefined,
180 export const authMnemonic = (Username: string, persistent: boolean) => ({
182 url: 'auth/v4/mnemonic',
183 data: { Username, PersistentCookies: Number(persistent) },
184 ignoreHandler: [HTTP_ERROR_CODES.TOO_MANY_REQUESTS],
187 export const payload = (data: ChallengePayload) => ({
189 url: `auth/v4/sessions/payload`,
195 export const reauthMnemonic = (data: { Username: string; PersistentCookies: boolean }) => ({
197 url: 'auth/v4/mnemonic/reauth',
200 PersistentCookies: Number(data.PersistentCookies),