2023-08-24 04:15:07 +02:00
|
|
|
const { Telemetry } = require("./telemetry");
|
2023-09-28 23:00:03 +02:00
|
|
|
const prisma = require("../utils/prisma");
|
2023-08-24 04:15:07 +02:00
|
|
|
|
|
|
|
const ApiKey = {
|
|
|
|
tablename: "api_keys",
|
|
|
|
writable: [],
|
2023-09-28 23:00:03 +02:00
|
|
|
|
2023-08-24 04:15:07 +02:00
|
|
|
makeSecret: () => {
|
|
|
|
const uuidAPIKey = require("uuid-apikey");
|
|
|
|
return uuidAPIKey.create().apiKey;
|
|
|
|
},
|
|
|
|
|
|
|
|
create: async function (createdByUserId = null) {
|
2023-09-28 23:00:03 +02:00
|
|
|
try {
|
|
|
|
const apiKey = await prisma.api_keys.create({
|
|
|
|
data: {
|
|
|
|
secret: this.makeSecret(),
|
|
|
|
createdBy: createdByUserId,
|
|
|
|
},
|
2023-08-24 04:15:07 +02:00
|
|
|
});
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
await Telemetry.sendTelemetry("api_key_created");
|
|
|
|
return { apiKey, error: null };
|
|
|
|
} catch (error) {
|
|
|
|
console.error("FAILED TO CREATE API KEY.", error.message);
|
|
|
|
return { apiKey: null, error: error.message };
|
2023-08-24 04:15:07 +02:00
|
|
|
}
|
|
|
|
},
|
2023-09-28 23:00:03 +02:00
|
|
|
|
|
|
|
get: async function (clause = {}) {
|
|
|
|
try {
|
|
|
|
const apiKey = await prisma.api_keys.findFirst({ where: clause });
|
|
|
|
return apiKey;
|
|
|
|
} catch (error) {
|
|
|
|
console.error("FAILED TO GET API KEY.", error.message);
|
|
|
|
return null;
|
|
|
|
}
|
2023-08-24 04:15:07 +02:00
|
|
|
},
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
count: async function (clause = {}) {
|
|
|
|
try {
|
|
|
|
const count = await prisma.api_keys.count({ where: clause });
|
|
|
|
return count;
|
|
|
|
} catch (error) {
|
|
|
|
console.error("FAILED TO COUNT API KEYS.", error.message);
|
|
|
|
return 0;
|
|
|
|
}
|
2023-08-24 04:15:07 +02:00
|
|
|
},
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
delete: async function (clause = {}) {
|
|
|
|
try {
|
|
|
|
await prisma.api_keys.deleteMany({ where: clause });
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
console.error("FAILED TO DELETE API KEY.", error.message);
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-24 04:15:07 +02:00
|
|
|
},
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
where: async function (clause = {}, limit) {
|
|
|
|
try {
|
|
|
|
const apiKeys = await prisma.api_keys.findMany({
|
|
|
|
where: clause,
|
|
|
|
take: limit,
|
|
|
|
});
|
|
|
|
return apiKeys;
|
|
|
|
} catch (error) {
|
|
|
|
console.error("FAILED TO GET API KEYS.", error.message);
|
|
|
|
return [];
|
|
|
|
}
|
2023-08-24 04:15:07 +02:00
|
|
|
},
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
whereWithUser: async function (clause = {}, limit) {
|
|
|
|
try {
|
|
|
|
const { User } = require("./user");
|
|
|
|
const apiKeys = await this.where(clause, limit);
|
2023-08-24 04:15:07 +02:00
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
for (const apiKey of apiKeys) {
|
|
|
|
if (!apiKey.createdBy) continue;
|
|
|
|
const user = await User.get({ id: apiKey.createdBy });
|
|
|
|
if (!user) continue;
|
2023-08-24 04:15:07 +02:00
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
apiKey.createdBy = {
|
|
|
|
id: user.id,
|
|
|
|
username: user.username,
|
|
|
|
role: user.role,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiKeys;
|
|
|
|
} catch (error) {
|
|
|
|
console.error("FAILED TO GET API KEYS WITH USER.", error.message);
|
|
|
|
return [];
|
|
|
|
}
|
2023-08-24 04:15:07 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = { ApiKey };
|