mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-07 04:27:29 +00:00
feat(infisical-pg): made api key backward compat
This commit is contained in:
@@ -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");
|
||||||
|
|||||||
@@ -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(),
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -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(),
|
||||||
|
|||||||
Reference in New Issue
Block a user