ente/src/services/userService.ts

233 lines
6.1 KiB
TypeScript
Raw Normal View History

2021-05-30 16:56:48 +00:00
import { KeyAttributes } from 'types';
import { getEndpoint } from 'utils/common/apiUtil';
import { clearKeys } from 'utils/storage/sessionStorage';
2021-03-12 04:50:58 +00:00
import router from 'next/router';
2021-05-30 16:56:48 +00:00
import { clearData } from 'utils/storage/localStorage';
2021-03-16 13:32:43 +00:00
import localForage from 'utils/storage/localForage';
2021-05-30 16:56:48 +00:00
import { getToken } from 'utils/common/key';
2021-05-29 06:27:52 +00:00
import HTTPService from './HTTPService';
2021-06-23 12:24:55 +00:00
import { B64EncryptionResult } from './uploadService';
2021-07-06 07:09:48 +00:00
import { logError } from 'utils/sentry';
2021-08-05 04:19:04 +00:00
import { Subscription } from './billingService';
2021-04-05 05:39:25 +00:00
export interface UpdatedKey {
kekSalt: string;
encryptedKey: string;
keyDecryptionNonce: string;
memLimit: number;
opsLimit: number;
}
2021-04-05 09:03:12 +00:00
export interface RecoveryKey {
masterKeyEncryptedWithRecoveryKey: string;
masterKeyDecryptionNonce: string;
recoveryKeyEncryptedWithMasterKey: string;
recoveryKeyDecryptionNonce: string;
}
const ENDPOINT = getEndpoint();
2020-09-14 09:32:01 +00:00
export interface User {
2021-02-16 11:45:06 +00:00
id: number;
name: string;
email: string;
}
export interface EmailVerificationResponse {
id: number;
keyAttributes?: KeyAttributes;
encryptedToken?: string;
token?: string;
2021-06-23 12:24:55 +00:00
twoFactorSessionID: string
}
export interface TwoFactorVerificationResponse {
2021-06-14 14:54:10 +00:00
id: number;
keyAttributes: KeyAttributes;
encryptedToken?: string;
token?: string;
}
2021-06-17 12:41:40 +00:00
export interface TwoFactorSecret {
secretCode: string
2021-06-17 10:45:52 +00:00
qrCode: string
}
2021-02-16 11:45:06 +00:00
2021-06-23 12:24:55 +00:00
export interface TwoFactorRecoveryResponse {
encryptedSecret: string
secretDecryptionNonce: string
}
2021-08-05 04:19:04 +00:00
export interface UserDetails{
email:string;
usage:string;
fileCount:number;
sharedCollectionCount:number;
subscription:Subscription;
}
2021-05-29 06:27:52 +00:00
export const getOtt = (email: string) => HTTPService.get(`${ENDPOINT}/users/ott`, {
email,
client: 'web',
});
2021-04-28 08:00:15 +00:00
export const getPublicKey = async (email: string) => {
const token = getToken();
const resp = await HTTPService.get(
`${ENDPOINT}/users/public-key`,
2021-05-30 16:56:48 +00:00
{ email },
2021-04-28 08:00:15 +00:00
{
'X-Auth-Token': token,
2021-05-29 06:27:52 +00:00
},
2021-04-28 08:00:15 +00:00
);
2021-05-29 06:27:52 +00:00
return resp.data.publicKey;
};
2021-06-14 14:54:10 +00:00
export const verifyOtt = (email: string, ott: string) => HTTPService.post(`${ENDPOINT}/users/verify-email`, { email, ott });
2021-03-12 04:50:58 +00:00
2021-05-29 06:27:52 +00:00
export const putAttributes = (token: string, keyAttributes: KeyAttributes) => HTTPService.put(
`${ENDPOINT}/users/attributes`,
2021-05-30 16:56:48 +00:00
{ keyAttributes },
2021-05-29 06:27:52 +00:00
null,
{
2021-04-05 05:39:25 +00:00
'X-Auth-Token': token,
2021-05-29 06:27:52 +00:00
},
);
2021-04-05 05:39:25 +00:00
2021-05-29 06:27:52 +00:00
export const setKeys = (token: string, updatedKey: UpdatedKey) => HTTPService.put(`${ENDPOINT}/users/keys`, updatedKey, null, {
'X-Auth-Token': token,
});
export const setRecoveryKey = (token: string, recoveryKey: RecoveryKey) => HTTPService.put(
`${ENDPOINT}/users/recovery-key`,
recoveryKey,
null,
{
'X-Auth-Token': token,
},
);
2021-04-05 09:03:12 +00:00
2021-03-12 04:50:58 +00:00
export const logoutUser = async () => {
2021-07-06 04:21:59 +00:00
// ignore server logout result as logoutUser can be triggered before sign up or on token expiry
await _logout();
2021-03-12 04:50:58 +00:00
clearKeys();
clearData();
2021-05-29 06:27:52 +00:00
await caches.delete('thumbs');
await clearFiles();
2021-03-12 04:50:58 +00:00
router.push('/');
};
export const clearFiles = async () => {
await localForage.clear();
};
2021-03-29 06:22:15 +00:00
export const isTokenValid = async () => {
try {
2021-04-01 13:52:30 +00:00
await HTTPService.get(`${ENDPOINT}/users/session-validity`, null, {
2021-03-29 06:22:15 +00:00
'X-Auth-Token': getToken(),
});
return true;
} catch (e) {
return false;
}
};
2021-06-17 10:45:52 +00:00
export const setupTwoFactor = async () => {
const resp = await HTTPService.post(`${ENDPOINT}/users/two-factor/setup`, null, null, {
'X-Auth-Token': getToken(),
});
return resp.data as TwoFactorSecret;
};
2021-06-29 11:45:40 +00:00
export const enableTwoFactor = async (code: string, recoveryEncryptedTwoFactorSecret: B64EncryptionResult) => {
await HTTPService.post(`${ENDPOINT}/users/two-factor/enable`, {
2021-06-29 11:45:40 +00:00
code,
2021-06-24 04:40:10 +00:00
encryptedTwoFactorSecret: recoveryEncryptedTwoFactorSecret.encryptedData,
twoFactorSecretDecryptionNonce: recoveryEncryptedTwoFactorSecret.nonce,
}, null, {
2021-06-17 10:45:52 +00:00
'X-Auth-Token': getToken(),
});
};
2021-06-29 11:45:40 +00:00
export const verifyTwoFactor = async (code: string, sessionID: string) => {
const resp = await HTTPService.post(`${ENDPOINT}/users/two-factor/verify`, {
2021-06-29 11:45:40 +00:00
code, sessionID,
}, null);
return resp.data as TwoFactorVerificationResponse;
2021-06-17 10:45:52 +00:00
};
2021-06-23 12:24:55 +00:00
export const recoverTwoFactor = async (sessionID: string) => {
const resp = await HTTPService.get(`${ENDPOINT}/users/two-factor/recover`, {
sessionID,
});
return resp.data as TwoFactorRecoveryResponse;
};
export const removeTwoFactor = async (sessionID: string, secret: string) => {
const resp = await HTTPService.post(`${ENDPOINT}/users/two-factor/remove`, {
sessionID, secret,
});
return resp.data as TwoFactorVerificationResponse;
};
2021-06-24 08:32:49 +00:00
export const disableTwoFactor = async () => {
await HTTPService.post(`${ENDPOINT}/users/two-factor/disable`, null, null, {
'X-Auth-Token': getToken(),
});
};
2021-06-29 11:49:34 +00:00
export const getTwoFactorStatus = async () => {
const resp = await HTTPService.get(`${ENDPOINT}/users/two-factor/status`, null, {
'X-Auth-Token': getToken(),
});
return resp.data['status'];
};
2021-07-06 04:21:59 +00:00
export const _logout = async () => {
if (!getToken()) return true;
try {
await HTTPService.post(`${ENDPOINT}/users/logout`, null, null, {
'X-Auth-Token': getToken(),
});
return true;
} catch (e) {
2021-07-06 07:09:48 +00:00
logError(e, '/users/logout failed');
2021-07-06 04:21:59 +00:00
return false;
}
};
2021-08-04 08:42:55 +00:00
export const getOTTForEmailChange=async (email:string)=>{
if (!getToken()) {
return null;
}
await HTTPService.get(`${ENDPOINT}/users/ott`, {
email,
client: 'web',
purpose: 'change',
2021-08-04 09:35:24 +00:00
});
};
export const changeEmail=async (email:string, ott:string)=>{
if (!getToken()) {
return null;
}
await HTTPService.post(`${ENDPOINT}/users/change-email`, {
email,
ott,
}, null, {
2021-08-04 08:42:55 +00:00
'X-Auth-Token': getToken(),
});
};
2021-08-05 04:19:04 +00:00
export const getUserDetails = async ():Promise<UserDetails> => {
const token = getToken();
const resp = await HTTPService.get(
`${ENDPOINT}/users/details`,
null,
{
'X-Auth-Token': token,
},
);
return resp.data['details'];
};