ente/packages/accounts/api/srp.ts

141 lines
3.6 KiB
TypeScript
Raw Normal View History

2023-11-02 05:26:51 +00:00
import HTTPService from '@ente/shared/network/HTTPService';
import { getEndpoint } from '@ente/shared/network/api';
import {
CompleteSRPSetupRequest,
CompleteSRPSetupResponse,
2023-11-02 07:19:01 +00:00
CreateSRPSessionResponse,
2023-11-02 05:26:51 +00:00
GetSRPAttributesResponse,
SRPAttributes,
2023-11-02 07:19:01 +00:00
SRPVerificationResponse,
2023-11-02 05:26:51 +00:00
SetupSRPRequest,
SetupSRPResponse,
2023-11-02 12:06:34 +00:00
UpdateSRPAndKeysRequest,
UpdateSRPAndKeysResponse,
} from '@ente/accounts/types/srp';
2023-11-02 07:19:01 +00:00
import { ApiError, CustomError } from '@ente/shared/error';
import { HttpStatusCode } from 'axios';
2023-11-02 15:22:31 +00:00
import { logError } from '@ente/shared/sentry';
2023-11-02 05:26:51 +00:00
const ENDPOINT = getEndpoint();
export const getSRPAttributes = async (
email: string
): Promise<SRPAttributes | null> => {
try {
const resp = await HTTPService.get(`${ENDPOINT}/users/srp/attributes`, {
email,
});
return (resp.data as GetSRPAttributesResponse).attributes;
} catch (e) {
2023-11-02 15:22:31 +00:00
logError(e, 'failed to get SRP attributes');
2023-11-02 05:26:51 +00:00
return null;
}
};
export const startSRPSetup = async (
2023-11-10 09:38:10 +00:00
token: string,
2023-11-02 05:26:51 +00:00
setupSRPRequest: SetupSRPRequest
): Promise<SetupSRPResponse> => {
2023-11-02 15:22:31 +00:00
try {
const resp = await HTTPService.post(
`${ENDPOINT}/users/srp/setup`,
setupSRPRequest,
undefined,
{
'X-Auth-Token': token,
}
);
2023-11-02 05:26:51 +00:00
2023-11-02 15:22:31 +00:00
return resp.data as SetupSRPResponse;
} catch (e) {
logError(e, 'failed to post SRP attributes');
throw e;
}
2023-11-02 05:26:51 +00:00
};
export const completeSRPSetup = async (
2023-11-10 09:38:10 +00:00
token: string,
2023-11-02 05:26:51 +00:00
completeSRPSetupRequest: CompleteSRPSetupRequest
) => {
2023-11-02 15:22:31 +00:00
try {
const resp = await HTTPService.post(
`${ENDPOINT}/users/srp/complete`,
completeSRPSetupRequest,
undefined,
{
'X-Auth-Token': token,
}
);
return resp.data as CompleteSRPSetupResponse;
} catch (e) {
logError(e, 'failed to complete SRP setup');
throw e;
}
2023-11-02 05:26:51 +00:00
};
2023-11-02 07:19:01 +00:00
export const createSRPSession = async (srpUserID: string, srpA: string) => {
2023-11-02 15:22:31 +00:00
try {
const resp = await HTTPService.post(
`${ENDPOINT}/users/srp/create-session`,
{
srpUserID,
srpA,
}
);
return resp.data as CreateSRPSessionResponse;
} catch (e) {
logError(e, 'createSRPSession failed');
throw e;
}
2023-11-02 07:19:01 +00:00
};
export const verifySRPSession = async (
sessionID: string,
srpUserID: string,
srpM1: string
) => {
try {
const resp = await HTTPService.post(
`${ENDPOINT}/users/srp/verify-session`,
{
sessionID,
srpUserID,
srpM1,
},
undefined
);
return resp.data as SRPVerificationResponse;
} catch (e) {
2023-11-02 15:22:31 +00:00
logError(e, 'verifySRPSession failed');
2023-11-02 07:19:01 +00:00
if (
e instanceof ApiError &&
2023-11-17 14:43:35 +00:00
e.httpStatusCode === HttpStatusCode.Unauthorized
2023-11-02 07:19:01 +00:00
) {
throw Error(CustomError.INCORRECT_PASSWORD);
} else {
throw e;
}
}
};
2023-11-02 12:06:34 +00:00
export const updateSRPAndKeys = async (
token: string,
updateSRPAndKeyRequest: UpdateSRPAndKeysRequest
): Promise<UpdateSRPAndKeysResponse> => {
2023-11-02 15:22:31 +00:00
try {
const resp = await HTTPService.post(
`${ENDPOINT}/users/srp/update`,
updateSRPAndKeyRequest,
null,
{
'X-Auth-Token': token,
}
);
return resp.data as UpdateSRPAndKeysResponse;
} catch (e) {
logError(e, 'updateSRPAndKeys failed');
throw e;
}
2023-11-02 12:06:34 +00:00
};