deepsource-autofix-76c6eb20
Dan Gowans 2022-09-15 11:09:03 -04:00
parent 6fc5d90e6d
commit 00e89f44e8
13 changed files with 128 additions and 8 deletions

4
.gitignore vendored
View File

@ -1,14 +1,18 @@
.nyc_output/
bin/daemon/
coverage/
cypress/downloads/
cypress/screenshots/
cypress/videos/
data/sessions/
node_modules/
data/*.db
data/*.db-journal
data/apiKeys.json
data/config.d.ts
data/config.js

4
helpers/functions.api.d.ts vendored 100644
View File

@ -0,0 +1,4 @@
import * as recordTypes from "../types/recordTypes";
export declare const regenerateApiKey: (userName: string) => Promise<void>;
export declare const getApiKey: (userName: string) => Promise<string>;
export declare const getApiKeyFromSession: (session: recordTypes.PartialSession) => Promise<string>;

View File

@ -0,0 +1,43 @@
import fs from "node:fs/promises";
import crypto from "node:crypto";
import Debug from "debug";
const debug = Debug("lot-occupancy-system:functions.api");
const apiKeyPath = "data/apiKeys.json";
let apiKeys;
const loadApiKeys = async () => {
try {
const fileData = await fs.readFile(apiKeyPath, "utf8");
apiKeys = JSON.parse(fileData);
}
catch (error) {
debug(error);
apiKeys = {};
}
};
const saveApiKeys = async () => {
try {
await fs.writeFile(apiKeyPath, JSON.stringify(apiKeys), "utf8");
}
catch (error) {
debug(error);
}
};
const generateApiKey = (apiKeyPrefix) => {
return apiKeyPrefix + "-" + crypto.randomUUID() + "-" + Date.now();
};
export const regenerateApiKey = async (userName) => {
apiKeys[userName] = generateApiKey(userName);
await saveApiKeys();
};
export const getApiKey = async (userName) => {
if (!apiKeys) {
await loadApiKeys();
}
if (!apiKeys[userName]) {
await regenerateApiKey(userName);
}
return apiKeys[userName];
};
export const getApiKeyFromSession = async (session) => {
return await getApiKey(session.user.userName);
};

View File

@ -0,0 +1,56 @@
import fs from "node:fs/promises";
import crypto from "node:crypto";
import Debug from "debug";
import * as recordTypes from "../types/recordTypes";
const debug = Debug("lot-occupancy-system:functions.api");
const apiKeyPath = "data/apiKeys.json";
let apiKeys: { [userName: string]: string };
const loadApiKeys = async () => {
try {
const fileData = await fs.readFile(apiKeyPath, "utf8");
apiKeys = JSON.parse(fileData);
} catch (error) {
debug(error);
apiKeys = {};
}
};
const saveApiKeys = async () => {
try {
await fs.writeFile(apiKeyPath, JSON.stringify(apiKeys), "utf8");
} catch (error) {
debug(error);
}
};
const generateApiKey = (apiKeyPrefix: string) => {
return apiKeyPrefix + "-" + crypto.randomUUID() + "-" + Date.now();
};
export const regenerateApiKey = async (userName: string) => {
apiKeys[userName] = generateApiKey(userName);
await saveApiKeys();
};
export const getApiKey = async (userName: string) => {
if (!apiKeys) {
await loadApiKeys();
}
if (!apiKeys[userName]) {
await regenerateApiKey(userName);
}
return apiKeys[userName];
};
export const getApiKeyFromSession = async (
session: recordTypes.PartialSession
) => {
return await getApiKey(session.user.userName);
};

View File

@ -13,7 +13,8 @@ const session = {
userName: "init.cemetery",
userProperties: {
canUpdate: true,
isAdmin: true
isAdmin: true,
apiKey: ""
}
}
};

View File

@ -24,7 +24,8 @@ const session: PartialSession = {
userName: "init.cemetery",
userProperties: {
canUpdate: true,
isAdmin: true
isAdmin: true,
apiKey: ""
}
}
};

View File

@ -2,6 +2,7 @@ import { Router } from "express";
import * as configFunctions from "../helpers/functions.config.js";
import * as authenticationFunctions from "../helpers/functions.authentication.js";
import { useTestDatabases } from "../data/databasePaths.js";
import { getApiKey } from "../helpers/functions.api.js";
export const router = Router();
const safeRedirects = new Set([
"/admin/fees",
@ -75,11 +76,13 @@ router
.some((currentUserName) => {
return (userNameLowerCase === currentUserName.toLowerCase());
});
const apiKey = await getApiKey(userNameLowerCase);
userObject = {
userName: userNameLowerCase,
userProperties: {
canUpdate,
isAdmin
isAdmin,
apiKey
}
};
}

View File

@ -6,6 +6,8 @@ import * as authenticationFunctions from "../helpers/functions.authentication.js
import { useTestDatabases } from "../data/databasePaths.js";
import { getApiKey } from "../helpers/functions.api.js";
import type * as recordTypes from "../types/recordTypes";
export const router = Router();
@ -112,11 +114,14 @@ router
);
});
const apiKey = await getApiKey(userNameLowerCase);
userObject = {
userName: userNameLowerCase,
userProperties: {
canUpdate,
isAdmin
isAdmin,
apiKey
}
};
}

View File

@ -28,7 +28,8 @@ const user = {
userName: "import.unix",
userProperties: {
canUpdate: true,
isAdmin: false
isAdmin: false,
apiKey: ""
}
}
};

View File

@ -203,7 +203,8 @@ const user: recordTypes.PartialSession = {
userName: "import.unix",
userProperties: {
canUpdate: true,
isAdmin: false
isAdmin: false,
apiKey: ""
}
}
};

View File

@ -1,2 +1 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
export {};

View File

@ -246,6 +246,7 @@ export interface User {
export interface UserProperties {
canUpdate: boolean;
isAdmin: boolean;
apiKey: string;
}
declare module "express-session" {
interface Session {

View File

@ -337,6 +337,7 @@ export interface User {
export interface UserProperties {
canUpdate: boolean;
isAdmin: boolean;
apiKey: string;
}
declare module "express-session" {