diff options
Diffstat (limited to 'src/server')
| -rw-r--r-- | src/server/ApiManagers/HypothesisManager.ts | 14 | ||||
| -rw-r--r-- | src/server/database.ts | 40 |
2 files changed, 44 insertions, 10 deletions
diff --git a/src/server/ApiManagers/HypothesisManager.ts b/src/server/ApiManagers/HypothesisManager.ts index 33badbc42..370d02a49 100644 --- a/src/server/ApiManagers/HypothesisManager.ts +++ b/src/server/ApiManagers/HypothesisManager.ts @@ -13,11 +13,8 @@ export default class HypothesisManager extends ApiManager { method: Method.GET, subscription: "/readHypothesisAccessToken", secureHandler: async ({ user, res }) => { - if (existsSync(serverPathToFile(Directory.hypothesis, user.id))) { - const read = readFileSync(serverPathToFile(Directory.hypothesis, user.id), "base64") || ""; - console.log("READ = " + read); - res.send(read); - } else res.send(""); + const credentials = await Database.Auxiliary.HypothesisAccessToken.Fetch(user.id); + res.send(credentials ? { username: credentials.hypothesisUsername, apiKey: credentials.hypothesisApiKey } : ""); } }); @@ -25,9 +22,8 @@ export default class HypothesisManager extends ApiManager { method: Method.POST, subscription: "/writeHypothesisAccessToken", secureHandler: async ({ user, req, res }) => { - const write = req.body.authenticationCode; - console.log("WRITE = " + write); - res.send(await writeFile(serverPathToFile(Directory.hypothesis, user.id), write, "base64", () => { })); + await Database.Auxiliary.HypothesisAccessToken.Write(user.id, req.body.authenticationCode, req.body.hypothesisUsername); + res.send(); } }); @@ -35,7 +31,7 @@ export default class HypothesisManager extends ApiManager { method: Method.GET, subscription: "/revokeHypothesisAccessToken", secureHandler: async ({ user, res }) => { - await Database.Auxiliary.GoogleAccessToken.Revoke("dash-hyp-" + user.id); + await Database.Auxiliary.HypothesisAccessToken.Revoke(user.id); res.send(); } }); diff --git a/src/server/database.ts b/src/server/database.ts index 2372cbcf2..456c1c254 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,43 @@ export namespace Database { } + export namespace HypothesisAccessToken { + /** + * Format stored in database. + */ + interface StoredCredentials { + userId: string; + hypothesisApiKey: string; + hypothesisUsername: string; + _id?: string; + } + + /** + * Writes the @param hypothesisApiKey to the database, associated + * with @param userId for later retrieval and updating. + */ + export const Write = async (userId: string, hypothesisApiKey: string, hypothesisUsername: string) => { + return Instance.insert({ userId, hypothesisApiKey, hypothesisUsername }, 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); + } + }; + } } } |
