From e8cf7f4be765532938e760a4f22d545ae8baa18c Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Sun, 22 Jan 2023 18:02:46 +0100 Subject: [PATCH] Created the AccessKey.ts model --- src/models/AccessKey.ts | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/models/AccessKey.ts diff --git a/src/models/AccessKey.ts b/src/models/AccessKey.ts new file mode 100644 index 0000000..401809d --- /dev/null +++ b/src/models/AccessKey.ts @@ -0,0 +1,44 @@ +import { model, ObjectId, Schema } from "mongoose"; +import { fieldEncryption } from "mongoose-field-encryption"; +import crypto from "crypto"; + +export enum IKeyRole { + VIEW, + MANAGE, + ADMIN +} + +export interface IAccessKey { + projectId: ObjectId, + name: string, + role: IKeyRole, + token: string +} + +const AccessKeySchema = new Schema({ + projectId: { + type: String, + required: true, + }, + name: { + type: String, + default: "Standardschlüssel", + }, + role: { + type: Number, + default: IKeyRole.VIEW, + enum: IKeyRole, + }, + token: { + type: String, + default: crypto.randomBytes(64).toString("hex"), + }, +}); + +AccessKeySchema.plugin(fieldEncryption, { + fields: ["projectId", "name", "role", "token"], + secret: process.env.ENC_KEY, + saltGenerator: () => process.env.SIG_KEY, +}); + +export const AccessKey = model("access_keys", AccessKeySchema); \ No newline at end of file