Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

feat: impl session validation

thecodrr4ee2d307c3b3
diff --git a/packages/core/api/index.js b/packages/core/api/index.js
index 1da222629..b6dd84ecc 100644
--- a/packages/core/api/index.js
+++ b/packages/core/api/index.js
@@ -10,6 +10,7 @@ import Lookup from "./lookup";
import Content from "../collections/content";
import Conflicts from "./sync/conflicts";
import EventManager from "../utils/event-manager";
+import Session from "./session";
class Database {
constructor(context) {
@@ -18,6 +19,14 @@ class Database {
}
async init() {
+ this.session = new Session();
+ if (!(await this.session.valid())) {
+ throw new Error(
+ "Your system clock is not setup correctly. Please adjust your date and time and then retry."
+ );
+ }
+
+ await this.context.write("t", Date.now());
this.user = new User(this);
this.syncer = new Sync(this);
this.vault = new Vault(this);
diff --git a/packages/core/api/session.js b/packages/core/api/session.js
new file mode 100644
index 000000000..0d6480a7d
--- /dev/null
+++ b/packages/core/api/session.js
@@ -0,0 +1,23 @@
+class Session {
+ /**
+ *
+ * @param {import("../database/storage").default} context
+ */
+ constructor(context) {
+ this._context = context;
+ }
+
+ get() {
+ return this._context.read("t");
+ }
+
+ set() {
+ return this._context.write("t", Date.now());
+ }
+
+ async valid() {
+ const t = await this.get();
+ return !t || t < Date.now();
+ }
+}
+export default Session;