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';
|
2020-09-12 21:53:41 +00:00
|
|
|
|
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;
|
|
|
|
}
|
2020-09-19 21:20:10 +00:00
|
|
|
const ENDPOINT = getEndpoint();
|
2020-09-14 09:32:01 +00:00
|
|
|
|
2021-04-26 07:44:34 +00:00
|
|
|
export interface User {
|
2021-02-16 11:45:06 +00:00
|
|
|
id: number;
|
|
|
|
name: string;
|
|
|
|
email: string;
|
|
|
|
}
|
2021-06-22 06:21:08 +00:00
|
|
|
export interface EmailVerificationResponse {
|
|
|
|
id: number;
|
|
|
|
keyAttributes?: KeyAttributes;
|
|
|
|
encryptedToken?: string;
|
|
|
|
token?: string;
|
2021-06-23 12:24:55 +00:00
|
|
|
twoFactorSessionID: string
|
2021-06-22 06:21:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface TwoFactorVerificationResponse {
|
2021-06-14 14:54:10 +00:00
|
|
|
id: number;
|
|
|
|
keyAttributes: KeyAttributes;
|
|
|
|
encryptedToken?: string;
|
|
|
|
token?: string;
|
|
|
|
}
|
2021-06-22 06:21:08 +00:00
|
|
|
|
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-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-02-09 09:33:54 +00:00
|
|
|
};
|
2020-09-12 21:53:41 +00:00
|
|
|
|
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 () => {
|
|
|
|
clearKeys();
|
|
|
|
clearData();
|
2021-05-29 06:27:52 +00:00
|
|
|
await caches.delete('thumbs');
|
2021-03-16 06:50:24 +00:00
|
|
|
await clearFiles();
|
2021-03-12 04:50:58 +00:00
|
|
|
router.push('/');
|
|
|
|
};
|
2021-03-16 06:49:34 +00:00
|
|
|
|
2021-03-16 06:50:24 +00:00
|
|
|
export const clearFiles = async () => {
|
2021-03-16 06:49:34 +00:00
|
|
|
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 () => {
|
2021-06-21 15:22:42 +00:00
|
|
|
const resp = await HTTPService.post(`${ENDPOINT}/users/two-factor/setup`, null, null, {
|
|
|
|
'X-Auth-Token': getToken(),
|
|
|
|
});
|
|
|
|
return resp.data as TwoFactorSecret;
|
|
|
|
};
|
|
|
|
|
2021-06-23 12:24:55 +00:00
|
|
|
export const enableTwoFactor = async (otp: string, recoveryEncryptedTwoFactorSecret: B64EncryptionResult) => {
|
2021-06-22 06:21:08 +00:00
|
|
|
await HTTPService.post(`${ENDPOINT}/users/two-factor/enable`, {
|
2021-06-21 15:22:42 +00:00
|
|
|
otp,
|
2021-06-23 12:24:55 +00:00
|
|
|
recoveryEncryptedTwoFactorSecret: recoveryEncryptedTwoFactorSecret.encryptedData,
|
|
|
|
recoveryTwoFactorSecretDecryptionNonce: recoveryEncryptedTwoFactorSecret.nonce,
|
2021-06-21 15:22:42 +00:00
|
|
|
}, null, {
|
2021-06-17 10:45:52 +00:00
|
|
|
'X-Auth-Token': getToken(),
|
|
|
|
});
|
2021-06-22 06:21:08 +00:00
|
|
|
};
|
|
|
|
|
2021-06-23 12:24:55 +00:00
|
|
|
export const verifyTwoFactor = async (otp: string, sessionID: string) => {
|
2021-06-22 06:21:08 +00:00
|
|
|
const resp = await HTTPService.post(`${ENDPOINT}/users/two-factor/verify`, {
|
2021-06-23 12:24:55 +00:00
|
|
|
otp, sessionID,
|
2021-06-22 06:21:08 +00:00
|
|
|
}, 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;
|
|
|
|
};
|