feat(infisical-pg): made api key backward compat

This commit is contained in:
Akhil Mohan
2024-01-27 12:40:37 +05:30
parent 4d184003a8
commit deab700716
5 changed files with 35 additions and 37 deletions
@@ -7,7 +7,7 @@ export async function up(knex: Knex): Promise<void> {
const isTablePresent = await knex.schema.hasTable(TableName.ApiKey); const isTablePresent = await knex.schema.hasTable(TableName.ApiKey);
if (!isTablePresent) { if (!isTablePresent) {
await knex.schema.createTable(TableName.ApiKey, (t) => { await knex.schema.createTable(TableName.ApiKey, (t) => {
t.uuid("id", { primaryKey: true }).defaultTo(knex.fn.uuid()); t.string("id", 36).primary().defaultTo(knex.fn.uuid());
t.string("name").notNullable(); t.string("name").notNullable();
t.datetime("lastUsed"); t.datetime("lastUsed");
t.datetime("expiresAt"); t.datetime("expiresAt");
+1 -1
View File
@@ -8,7 +8,7 @@ import { z } from "zod";
import { TImmutableDBKeys } from "./models"; import { TImmutableDBKeys } from "./models";
export const ApiKeysSchema = z.object({ export const ApiKeysSchema = z.object({
id: z.string().uuid(), id: z.string(),
name: z.string(), name: z.string(),
lastUsed: z.date().nullable().optional(), lastUsed: z.date().nullable().optional(),
expiresAt: z.date().nullable().optional(), expiresAt: z.date().nullable().optional(),
+1 -3
View File
@@ -536,15 +536,13 @@ const main = async () => {
postgresTableName: TableName.ApiKey, postgresTableName: TableName.ApiKey,
returnKeys: ["id"], returnKeys: ["id"],
preProcessing: async (doc) => { preProcessing: async (doc) => {
const id = uuidV4();
const userId = await userKv.get(doc.user.toString()).catch(() => null); const userId = await userKv.get(doc.user.toString()).catch(() => null);
if (!userId) return; if (!userId) return;
// expired tokens can be removed // expired tokens can be removed
if (new Date(doc.expiresAt) < new Date()) return; if (new Date(doc.expiresAt) < new Date()) return;
return { return {
id, id: doc._id.toString(),
userId, userId,
name: doc.name, name: doc.name,
lastUsed: doc.lastUsed, lastUsed: doc.lastUsed,
+31 -31
View File
@@ -1,39 +1,39 @@
import { Schema, Types, model } from "mongoose"; import { Schema, Types, model } from "mongoose";
export interface IAPIKeyData { export interface IAPIKeyData {
name: string; _id: Types.ObjectId;
user: Types.ObjectId; name: string;
lastUsed: Date; user: Types.ObjectId;
expiresAt: Date; lastUsed: Date;
secretHash: string; expiresAt: Date;
secretHash: string;
} }
const apiKeyDataSchema = new Schema<IAPIKeyData>( const apiKeyDataSchema = new Schema<IAPIKeyData>(
{ {
name: { name: {
type: String, type: String,
required: true, required: true,
}, },
user: { user: {
type: Schema.Types.ObjectId, type: Schema.Types.ObjectId,
ref: "User", ref: "User",
required: true, required: true,
}, },
lastUsed: { lastUsed: {
type: Date, type: Date,
}, },
expiresAt: { expiresAt: {
type: Date, type: Date,
}, },
secretHash: { secretHash: {
type: String, type: String,
required: true, required: true,
},
}, },
}, {
{ timestamps: true,
timestamps: true, },
}
); );
export const APIKeyData = model<IAPIKeyData>("APIKeyData", apiKeyDataSchema); export const APIKeyData = model<IAPIKeyData>("APIKeyData", apiKeyDataSchema);
+1 -1
View File
@@ -8,7 +8,7 @@ import { z } from "zod";
import { TImmutableDBKeys } from "./models"; import { TImmutableDBKeys } from "./models";
export const ApiKeysSchema = z.object({ export const ApiKeysSchema = z.object({
id: z.string().uuid(), id: z.string(),
name: z.string(), name: z.string(),
lastUsed: z.date().nullable().optional(), lastUsed: z.date().nullable().optional(),
expiresAt: z.date().nullable().optional(), expiresAt: z.date().nullable().optional(),