aboutsummaryrefslogtreecommitdiff
path: root/src/server/database.ts
diff options
context:
space:
mode:
authorMelissa Zhang <mzhang19096@gmail.com>2020-07-08 08:57:21 -0700
committerMelissa Zhang <mzhang19096@gmail.com>2020-07-08 08:57:21 -0700
commit876437db1f7347787badbdd48ac751dba111a752 (patch)
tree84c026159c6a7196d1eb82997f7f91ee3f0d512d /src/server/database.ts
parent901610007e7b33b1c3db3c93aa6e96dacd414256 (diff)
store hypothesis API tokens to database
Diffstat (limited to 'src/server/database.ts')
-rw-r--r--src/server/database.ts39
1 files changed, 38 insertions, 1 deletions
diff --git a/src/server/database.ts b/src/server/database.ts
index 2372cbcf2..7fbab357b 100644
--- a/src/server/database.ts
+++ b/src/server/database.ts
@@ -304,7 +304,8 @@ export namespace Database {
*/
export enum AuxiliaryCollections {
GooglePhotosUploadHistory = "uploadedFromGooglePhotos",
- GoogleAccess = "googleAuthentication"
+ GoogleAccess = "googleAuthentication",
+ HypothesisAccess = "hypothesisAuthentication"
}
/**
@@ -405,6 +406,42 @@ export namespace Database {
}
+ export namespace HypothesisAccessToken {
+ /**
+ * Format stored in database.
+ */
+ interface StoredCredentials {
+ userId: string;
+ hypothesisApiKey: string;
+ _id?: string;
+ }
+
+ /**
+ * Writes the @param enrichedCredentials to the database, associated
+ * with @param userId for later retrieval and updating.
+ */
+ export const Write = async (userId: string, hypothesisApiKey: string) => {
+ return Instance.insert({ userId, hypothesisApiKey }, AuxiliaryCollections.HypothesisAccess);
+ };
+
+ /**
+ * Retrieves the credentials associaed with @param userId
+ * and optionally removes their database id according to @param removeId.
+ */
+ export const Fetch = async (userId: string, removeId = true): Promise<Opt<StoredCredentials>> => {
+ return SanitizedSingletonQuery<StoredCredentials>({ userId }, AuxiliaryCollections.HypothesisAccess, removeId);
+ };
+
+ /**
+ * Revokes the credentials associated with @param userId.
+ */
+ export const Revoke = async (userId: string) => {
+ const entry = await Fetch(userId, false);
+ if (entry) {
+ Instance.delete({ _id: entry._id }, AuxiliaryCollections.HypothesisAccess);
+ }
+ };
+ }
}
}