Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ refactor: add user server api key method in the server mode #4870

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/database/server/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DeepPartial } from 'utility-types';
import { serverDB } from '@/database/server/core/db';
import { KeyVaultsGateKeeper } from '@/server/modules/KeyVaultsEncrypt';
import { UserGuide, UserPreference } from '@/types/user';
import { UserSettings } from '@/types/user/settings';
import { UserKeyVaults, UserSettings } from '@/types/user/settings';
import { merge } from '@/utils/merge';

import { NewUser, UserItem, userSettings, users } from '../schemas/lobechat';
Expand Down Expand Up @@ -105,6 +105,38 @@ export class UserModel {
};
};

static getUserApiKeys = async (id: string) => {
const result = await serverDB
.select({
settingsKeyVaults: userSettings.keyVaults,
})
.from(userSettings)
.where(eq(userSettings.id, id));

if (!result || !result[0]) {
throw new UserNotFoundError();
}

const state = result[0];

// Decrypt keyVaults
let decryptKeyVaults = {};
if (state.settingsKeyVaults) {
const gateKeeper = await KeyVaultsGateKeeper.initWithEnvKey();
const { wasAuthentic, plaintext } = await gateKeeper.decrypt(state.settingsKeyVaults);

if (wasAuthentic) {
try {
decryptKeyVaults = JSON.parse(plaintext);
} catch (e) {
console.error(`Failed to parse keyVaults ,userId: ${id}. Error:`, e);
}
}
}

return decryptKeyVaults as UserKeyVaults;
};

async updateUser(id: string, value: Partial<UserItem>) {
return serverDB
.update(users)
Expand Down
Loading