Created the AccessKey.ts model

This commit is contained in:
Mathias Wagner 2023-01-22 18:02:46 +01:00
parent bd6e5f4fd6
commit e8cf7f4be7
Signed by: Mathias
GPG Key ID: B8DC354B0A1F5B44

44
src/models/AccessKey.ts Normal file
View File

@ -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<IAccessKey>({
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<IAccessKey>("access_keys", AccessKeySchema);