@sentclose/sentc-light
Version:
User and group management
217 lines (216 loc) • 13.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.User = exports.getUser = void 0;
const sentc_wasm_light_1 = require("sentc_wasm_light");
const Sentc_1 = require("./Sentc");
const sentc_common_1 = require("@sentclose/sentc-common");
const Group_1 = require("./Group");
async function getUser(deviceIdentifier, user_data) {
//Only fetch the older keys when needed, this is not like a group where all keys must be available
const store_user_data = user_data;
if (Sentc_1.Sentc.options.refresh.endpoint !== 2 /* REFRESH_ENDPOINT.api */) {
//if the refresh token should not be stored on the client -> invalidates the stored refresh token
//but just return the refresh token with the rest of the user data
store_user_data.refresh_token = "";
}
const user = new User(Sentc_1.Sentc.options.base_url, Sentc_1.Sentc.options.app_token, user_data, deviceIdentifier);
//save user data in indexeddb
const storage = await Sentc_1.Sentc.getStore();
await Promise.all([
storage.set("user_data" /* USER_KEY_STORAGE_NAMES.userData */ + "_id_" + deviceIdentifier, store_user_data),
storage.set("actual_user" /* USER_KEY_STORAGE_NAMES.actualUser */, deviceIdentifier)
]);
return user;
}
exports.getUser = getUser;
async function setUserStorageData(user_data, deviceIdentifier) {
const storage = await Sentc_1.Sentc.getStore();
const store_user_data = user_data;
if (Sentc_1.Sentc.options.refresh.endpoint !== 2 /* REFRESH_ENDPOINT.api */) {
//if the refresh token should not be stored on the client -> invalidates the stored refresh token
//but just return the refresh token with the rest of the user data
store_user_data.refresh_token = "";
}
return storage.set("user_data" /* USER_KEY_STORAGE_NAMES.userData */ + "_id_" + deviceIdentifier, store_user_data);
}
class User {
constructor(base_url, app_token, user_data, userIdentifier, group_invites = []) {
this.base_url = base_url;
this.app_token = app_token;
this.user_data = user_data;
this.userIdentifier = userIdentifier;
this.group_invites = group_invites;
}
enabledMfa() {
return this.user_data.mfa;
}
async getJwt() {
const jwt_data = (0, sentc_wasm_light_1.decode_jwt)(this.user_data.jwt);
const exp = jwt_data.get_exp();
if (exp <= Date.now() / 1000 + 30) {
//refresh even when the jwt is valid for 30 sec
//update the user data to safe the updated values, we don't need the class here
this.user_data.jwt = await Sentc_1.Sentc.refreshJwt(this.user_data.jwt, this.user_data.refresh_token);
//save the user data with the new jwt
await setUserStorageData(this.user_data, this.userIdentifier);
}
return this.user_data.jwt;
}
async updateUser(newIdentifier) {
const jwt = await this.getJwt();
const url = this.base_url + "/api/v1/user";
const body = (0, sentc_wasm_light_1.user_prepare_user_identifier_update)(newIdentifier);
const res = await (0, sentc_common_1.make_req)("PUT" /* HttpMethod.PUT */, url, this.app_token, body, jwt);
return (0, sentc_common_1.handle_general_server_response)(res);
}
async logOut() {
const storage = await Sentc_1.Sentc.getStore();
return storage.delete("user_data" /* USER_KEY_STORAGE_NAMES.userData */ + "_id_" + this.userIdentifier);
}
async deleteUser(password, mfa_token, mfa_recovery) {
if (this.user_data.mfa && !mfa_token) {
throw (0, sentc_common_1.create_error)("client_10000", "The user enabled mfa. To delete the user, the user must also enter the mfa token");
}
const fresh_jwt = await this.getFreshJwt(this.userIdentifier, password, mfa_token, mfa_recovery);
await (0, sentc_wasm_light_1.delete_user)(this.base_url, this.app_token, fresh_jwt);
return this.logOut();
}
async deleteDevice(password, device_id, mfa_token, mfa_recovery) {
if (this.user_data.mfa && !mfa_token) {
throw (0, sentc_common_1.create_error)("client_10000", "The user enabled mfa. To delete the user, the user must also enter the mfa token");
}
const fresh_jwt = await this.getFreshJwt(this.userIdentifier, password, mfa_token, mfa_recovery);
await (0, sentc_wasm_light_1.delete_device)(this.base_url, this.app_token, fresh_jwt, device_id);
if (device_id === this.user_data.device_id) {
//only log the device out if it is the actual used device
return this.logOut();
}
}
changePassword(oldPassword, newPassword, mfa_token, mfa_recovery) {
if (this.user_data.mfa && !mfa_token) {
throw (0, sentc_common_1.create_error)("client_10000", "The user enabled mfa. To change the password, the user must also enter the mfa token");
}
return (0, sentc_wasm_light_1.change_password)(this.base_url, this.app_token, this.userIdentifier, oldPassword, newPassword, mfa_token, mfa_recovery);
}
//__________________________________________________________________________________________________________________
async registerDevice(server_output) {
const jwt = await this.getJwt();
return (0, sentc_wasm_light_1.register_device)(this.base_url, this.app_token, jwt, server_output);
}
async getDevices(last_fetched_item = null) {
var _a, _b;
const jwt = await this.getJwt();
const last_fetched_time = (_a = last_fetched_item === null || last_fetched_item === void 0 ? void 0 : last_fetched_item.time.toString()) !== null && _a !== void 0 ? _a : "0";
const last_id = (_b = last_fetched_item === null || last_fetched_item === void 0 ? void 0 : last_fetched_item.device_id) !== null && _b !== void 0 ? _b : "none";
const url = this.base_url + "/api/v1/user/device/" + last_fetched_time + "/" + last_id;
const res = await (0, sentc_common_1.make_req)("GET" /* HttpMethod.GET */, url, this.app_token, undefined, jwt);
const out = (0, sentc_common_1.handle_server_response)(res);
return out;
}
//__________________________________________________________________________________________________________________
async getGroups(last_fetched_item = null) {
var _a, _b;
const jwt = await this.getJwt();
const last_fetched_time = (_a = last_fetched_item === null || last_fetched_item === void 0 ? void 0 : last_fetched_item.time.toString()) !== null && _a !== void 0 ? _a : "0";
const last_id = (_b = last_fetched_item === null || last_fetched_item === void 0 ? void 0 : last_fetched_item.group_id) !== null && _b !== void 0 ? _b : "none";
const url = this.base_url + "/api/v1/group/all/" + last_fetched_time + "/" + last_id;
const res = await (0, sentc_common_1.make_req)("GET" /* HttpMethod.GET */, url, this.app_token, undefined, jwt);
const out = (0, sentc_common_1.handle_server_response)(res);
return out;
}
async getGroupInvites(last_fetched_item = null) {
var _a, _b;
const jwt = await this.getJwt();
const last_fetched_time = (_a = last_fetched_item === null || last_fetched_item === void 0 ? void 0 : last_fetched_item.time.toString()) !== null && _a !== void 0 ? _a : "0";
const last_id = (_b = last_fetched_item === null || last_fetched_item === void 0 ? void 0 : last_fetched_item.group_id) !== null && _b !== void 0 ? _b : "none";
const url = this.base_url + "/api/v1/group/invite/" + last_fetched_time + "/" + last_id;
const res = await (0, sentc_common_1.make_req)("GET" /* HttpMethod.GET */, url, this.app_token, undefined, jwt);
const out = (0, sentc_common_1.handle_server_response)(res);
return out;
}
async acceptGroupInvite(group_id) {
const jwt = await this.getJwt();
const url = this.base_url + "/api/v1/group/" + group_id + "/invite";
const res = await (0, sentc_common_1.make_req)("PATCH" /* HttpMethod.PATCH */, url, this.app_token, undefined, jwt);
return (0, sentc_common_1.handle_general_server_response)(res);
}
async rejectGroupInvite(group_id) {
const jwt = await this.getJwt();
const url = this.base_url + "/api/v1/group/" + group_id + "/invite";
const res = await (0, sentc_common_1.make_req)("DELETE" /* HttpMethod.DELETE */, url, this.app_token, undefined, jwt);
return (0, sentc_common_1.handle_general_server_response)(res);
}
//join req
async groupJoinRequest(group_id) {
const jwt = await this.getJwt();
const url = this.base_url + "/api/v1/group/" + group_id + "/join_req";
const res = await (0, sentc_common_1.make_req)("PATCH" /* HttpMethod.PATCH */, url, this.app_token, undefined, jwt);
return (0, sentc_common_1.handle_general_server_response)(res);
}
async sentJoinReq(last_fetched_item = null) {
var _a, _b;
const jwt = await this.getJwt();
const last_fetched_time = (_a = last_fetched_item === null || last_fetched_item === void 0 ? void 0 : last_fetched_item.time.toString()) !== null && _a !== void 0 ? _a : "0";
const last_id = (_b = last_fetched_item === null || last_fetched_item === void 0 ? void 0 : last_fetched_item.group_id) !== null && _b !== void 0 ? _b : "none";
const url = this.base_url + "/api/v1/group/joins/" + last_fetched_time + "/" + last_id;
const res = await (0, sentc_common_1.make_req)("GET" /* HttpMethod.GET */, url, this.app_token, undefined, jwt);
const out = (0, sentc_common_1.handle_server_response)(res);
return out;
}
async deleteJoinReq(id) {
const jwt = await this.getJwt();
const url = this.base_url + "/api/v1/group/joins/" + id;
const res = await (0, sentc_common_1.make_req)("DELETE" /* HttpMethod.DELETE */, url, this.app_token, undefined, jwt);
return (0, sentc_common_1.handle_general_server_response)(res);
}
//__________________________________________________________________________________________________________________
async createGroup() {
const jwt = await this.getJwt();
return (0, sentc_wasm_light_1.group_create_group)(this.base_url, this.app_token, jwt);
}
getGroup(group_id, group_as_member) {
return (0, Group_1.getGroup)(group_id, this.base_url, this.app_token, this, false, group_as_member);
}
//__________________________________________________________________________________________________________________
//Otp
getFreshJwt(username, password, mfa_token, mfa_recovery) {
return (0, sentc_wasm_light_1.get_fresh_jwt)(this.base_url, this.app_token, username, password, mfa_token, mfa_recovery);
}
async registerRawOtp(password, mfa_token, mfa_recovery) {
const fresh_jwt = await this.getFreshJwt(this.userIdentifier, password, mfa_token, mfa_recovery);
const url = this.base_url + "/api/v1/user/register_otp";
const res = await (0, sentc_common_1.make_req)("PATCH" /* HttpMethod.PATCH */, url, this.app_token, undefined, fresh_jwt);
this.user_data.mfa = true;
await setUserStorageData(this.user_data, this.userIdentifier);
return (0, sentc_common_1.handle_server_response)(res);
}
async registerOtp(issuer, audience, password, mfa_token, mfa_recovery) {
const out = await this.registerRawOtp(password, mfa_token, mfa_recovery);
return [`otpauth://totp/${issuer}:${audience}?secret=${out.secret}&algorithm=SHA256&issuer=${issuer}`, out.recover];
}
async getOtpRecoverKeys(password, mfa_token, mfa_recovery) {
const fresh_jwt = await this.getFreshJwt(this.userIdentifier, password, mfa_token, mfa_recovery);
const url = this.base_url + "/api/v1/user/otp_recovery_keys";
const res = await (0, sentc_common_1.make_req)("GET" /* HttpMethod.GET */, url, this.app_token, undefined, fresh_jwt);
return (0, sentc_common_1.handle_server_response)(res).keys;
}
async resetRawOtp(password, mfa_token, mfa_recovery) {
const fresh_jwt = await this.getFreshJwt(this.userIdentifier, password, mfa_token, mfa_recovery);
const url = this.base_url + "/api/v1/user/reset_otp";
const res = await (0, sentc_common_1.make_req)("PATCH" /* HttpMethod.PATCH */, url, this.app_token, undefined, fresh_jwt);
return (0, sentc_common_1.handle_server_response)(res);
}
async resetOtp(issuer, audience, password, mfa_token, mfa_recovery) {
const out = await this.resetRawOtp(password, mfa_token, mfa_recovery);
return [`otpauth://totp/${issuer}:${audience}?secret=${out.secret}&algorithm=SHA256&issuer=${issuer}`, out.recover];
}
async disableOtp(password, mfa_token, mfa_recovery) {
const fresh_jwt = await this.getFreshJwt(this.userIdentifier, password, mfa_token, mfa_recovery);
const url = this.base_url + "/api/v1/user/disable_otp";
const res = await (0, sentc_common_1.make_req)("PATCH" /* HttpMethod.PATCH */, url, this.app_token, undefined, fresh_jwt);
(0, sentc_common_1.handle_general_server_response)(res);
this.user_data.mfa = false;
return setUserStorageData(this.user_data, this.userIdentifier);
}
}
exports.User = User;