2020-09-12 21:53:41 +00:00
|
|
|
import HTTPService from './HTTPService';
|
2021-04-02 04:03:00 +00:00
|
|
|
import { KeyAttributes } from 'types';
|
2020-09-19 21:20:10 +00:00
|
|
|
import { getEndpoint } from 'utils/common/apiUtil';
|
2021-03-12 04:50:58 +00:00
|
|
|
import { clearKeys } from 'utils/storage/sessionStorage';
|
|
|
|
import router from 'next/router';
|
|
|
|
import { clearData } from 'utils/storage/localStorage';
|
2021-03-16 13:32:43 +00:00
|
|
|
import localForage from 'utils/storage/localForage';
|
2021-03-29 06:22:15 +00:00
|
|
|
import { getToken } from 'utils/common/key';
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-12 21:53:41 +00:00
|
|
|
export const getOtt = (email: string) => {
|
2021-02-09 09:33:54 +00:00
|
|
|
return HTTPService.get(`${ENDPOINT}/users/ott`, {
|
|
|
|
email: 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`,
|
|
|
|
{ email },
|
|
|
|
{
|
|
|
|
'X-Auth-Token': token,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return resp.data['publicKey'];
|
|
|
|
};
|
2020-09-12 21:53:41 +00:00
|
|
|
|
|
|
|
export const verifyOtt = (email: string, ott: string) => {
|
2020-09-14 09:32:01 +00:00
|
|
|
return HTTPService.get(`${ENDPOINT}/users/credentials`, { email, ott });
|
2021-02-09 09:33:54 +00:00
|
|
|
};
|
2020-09-12 21:53:41 +00:00
|
|
|
|
2021-05-23 14:38:00 +00:00
|
|
|
export const putAttributes = (token: string, keyAttributes: KeyAttributes) => {
|
2021-02-09 09:33:54 +00:00
|
|
|
return HTTPService.put(
|
|
|
|
`${ENDPOINT}/users/attributes`,
|
2021-05-23 14:38:00 +00:00
|
|
|
{ keyAttributes: keyAttributes },
|
2021-02-09 09:33:54 +00:00
|
|
|
null,
|
|
|
|
{
|
|
|
|
'X-Auth-Token': token,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
2021-03-12 04:50:58 +00:00
|
|
|
|
2021-04-05 05:39:25 +00:00
|
|
|
export const setKeys = (token: string, updatedKey: UpdatedKey) => {
|
|
|
|
return HTTPService.put(`${ENDPOINT}/users/keys`, updatedKey, null, {
|
|
|
|
'X-Auth-Token': token,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-04-05 09:03:12 +00:00
|
|
|
export const SetRecoveryKey = (token: string, recoveryKey: RecoveryKey) => {
|
|
|
|
return HTTPService.put(
|
|
|
|
`${ENDPOINT}/users/recovery-key`,
|
|
|
|
recoveryKey,
|
|
|
|
null,
|
|
|
|
{
|
|
|
|
'X-Auth-Token': token,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-03-12 04:50:58 +00:00
|
|
|
export const logoutUser = async () => {
|
|
|
|
clearKeys();
|
|
|
|
clearData();
|
|
|
|
const cache = 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;
|
|
|
|
}
|
|
|
|
};
|