Files
api/integra-mcp/store.mjs

35 lines
1.6 KiB
JavaScript

import { createHash } from 'node:crypto';
export const hash = value => createHash('sha256').update(value).digest('hex');
// Shared by all Coolify replicas. Only digests of bearer tokens, codes and browser
// transaction secrets are stored. No user passwords or IntegraRepara JWTs.
export class PgStore {
constructor(pool) { this.pool = pool; }
async init() {
await this.pool.query(`CREATE TABLE IF NOT EXISTS integra_mcp_oauth (
kind TEXT NOT NULL, key TEXT NOT NULL, data JSONB NOT NULL,
expires_at TIMESTAMPTZ NOT NULL, PRIMARY KEY (kind, key)
)`);
await this.pool.query('CREATE INDEX IF NOT EXISTS integra_mcp_oauth_expiry ON integra_mcp_oauth(expires_at)');
}
async set(kind, key, data, seconds) {
await this.pool.query(`INSERT INTO integra_mcp_oauth(kind,key,data,expires_at)
VALUES ($1,$2,$3,now()+$4*interval '1 second')
ON CONFLICT(kind,key) DO UPDATE SET data=EXCLUDED.data,expires_at=EXCLUDED.expires_at`,
[kind, key, JSON.stringify(data), seconds]);
}
async get(kind, key) {
const q = await this.pool.query('SELECT data FROM integra_mcp_oauth WHERE kind=$1 AND key=$2 AND expires_at>now()', [kind,key]);
return q.rows[0]?.data;
}
async take(kind, key) {
const q = await this.pool.query('DELETE FROM integra_mcp_oauth WHERE kind=$1 AND key=$2 AND expires_at>now() RETURNING data', [kind,key]);
return q.rows[0]?.data;
}
async delete(kind, key) {
await this.pool.query('DELETE FROM integra_mcp_oauth WHERE kind=$1 AND key=$2', [kind,key]);
}
async cleanup() { await this.pool.query('DELETE FROM integra_mcp_oauth WHERE expires_at<=now()'); }
}