Allow OIDC users to import database without password

This commit is contained in:
Nikola Novoselec
2025-10-21 17:02:29 +02:00
parent 40ac75de81
commit 773e22fa3d

View File

@@ -918,7 +918,20 @@ app.post(
const userId = (req as any).userId; const userId = (req as any).userId;
const { password } = req.body; const { password } = req.body;
const mainDb = getDb();
const userRecords = await mainDb
.select()
.from(users)
.where(eq(users.id, userId));
if (!userRecords || userRecords.length === 0) {
return res.status(404).json({ error: "User not found" });
}
const isOidcUser = !!userRecords[0].is_oidc;
if (!isOidcUser) {
if (!password) { if (!password) {
return res.status(400).json({ return res.status(400).json({
error: "Password required for import", error: "Password required for import",
@@ -930,6 +943,14 @@ app.post(
if (!unlocked) { if (!unlocked) {
return res.status(401).json({ error: "Invalid password" }); return res.status(401).json({ error: "Invalid password" });
} }
} else if (!DataCrypto.getUserDataKey(userId)) {
const oidcUnlocked = await authManager.authenticateOIDCUser(userId);
if (!oidcUnlocked) {
return res.status(403).json({
error: "Failed to unlock user data with SSO credentials",
});
}
}
apiLogger.info("Importing SQLite data", { apiLogger.info("Importing SQLite data", {
operation: "sqlite_import_api", operation: "sqlite_import_api",
@@ -939,7 +960,13 @@ app.post(
mimetype: req.file.mimetype, mimetype: req.file.mimetype,
}); });
const userDataKey = DataCrypto.getUserDataKey(userId); let userDataKey = DataCrypto.getUserDataKey(userId);
if (!userDataKey && isOidcUser) {
const oidcUnlocked = await authManager.authenticateOIDCUser(userId);
if (oidcUnlocked) {
userDataKey = DataCrypto.getUserDataKey(userId);
}
}
if (!userDataKey) { if (!userDataKey) {
throw new Error("User data not unlocked"); throw new Error("User data not unlocked");
} }
@@ -993,7 +1020,6 @@ app.post(
}; };
try { try {
const mainDb = getDb();
try { try {
const importedHosts = importDb const importedHosts = importDb