fix(secu): don't store komga pwd but authstring
This commit is contained in:
@@ -1,17 +1,16 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { TestService } from "@/lib/services/test.service";
|
import { TestService } from "@/lib/services/test.service";
|
||||||
import { AuthConfig } from "@/types/auth";
|
import { ConfigDBService } from "@/lib/services/config-db.service";
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
export async function POST() {
|
||||||
try {
|
try {
|
||||||
const { serverUrl, username, password } = await request.json();
|
const config = await ConfigDBService.getConfig();
|
||||||
|
|
||||||
const config: AuthConfig = {
|
const { libraries } = await TestService.testConnection({
|
||||||
serverUrl,
|
serverUrl: config.url,
|
||||||
credentials: { username, password },
|
authHeader: config.authHeader,
|
||||||
};
|
});
|
||||||
|
|
||||||
const { libraries } = await TestService.testConnection(config);
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
message: "Connexion réussie",
|
message: "Connexion réussie",
|
||||||
librariesCount: libraries.length,
|
librariesCount: libraries.length,
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ export default async function SettingsPage() {
|
|||||||
config = {
|
config = {
|
||||||
url: mongoConfig.url,
|
url: mongoConfig.url,
|
||||||
username: mongoConfig.username,
|
username: mongoConfig.username,
|
||||||
password: mongoConfig.password,
|
|
||||||
userId: mongoConfig.userId,
|
userId: mongoConfig.userId,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import { CacheModeSwitch } from "@/components/settings/CacheModeSwitch";
|
|||||||
interface KomgaConfig {
|
interface KomgaConfig {
|
||||||
url: string;
|
url: string;
|
||||||
username: string;
|
username: string;
|
||||||
password: string;
|
|
||||||
userId: string;
|
userId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ const configSchema = new mongoose.Schema(
|
|||||||
type: String,
|
type: String,
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
password: {
|
authHeader: {
|
||||||
type: String,
|
type: String,
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -21,10 +21,7 @@ export abstract class BaseApiService {
|
|||||||
const config = await ConfigDBService.getConfig();
|
const config = await ConfigDBService.getConfig();
|
||||||
return {
|
return {
|
||||||
serverUrl: config.url,
|
serverUrl: config.url,
|
||||||
credentials: {
|
authHeader: config.authHeader,
|
||||||
username: config.username,
|
|
||||||
password: config.password,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Erreur lors de la récupération de la configuration:", error);
|
console.error("Erreur lors de la récupération de la configuration:", error);
|
||||||
@@ -33,16 +30,12 @@ export abstract class BaseApiService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected static getAuthHeaders(config: AuthConfig): Headers {
|
protected static getAuthHeaders(config: AuthConfig): Headers {
|
||||||
if (!config.credentials?.username || !config.credentials?.password) {
|
if (!config.authHeader) {
|
||||||
throw new Error("Credentials Komga manquants");
|
throw new Error("Credentials Komga manquants");
|
||||||
}
|
}
|
||||||
|
|
||||||
const auth = Buffer.from(
|
|
||||||
`${config.credentials.username}:${config.credentials.password}`
|
|
||||||
).toString("base64");
|
|
||||||
|
|
||||||
return new Headers({
|
return new Headers({
|
||||||
Authorization: `Basic ${auth}`,
|
Authorization: `Basic ${config.authHeader}`,
|
||||||
Accept: "application/json",
|
Accept: "application/json",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ interface KomgaConfigData {
|
|||||||
url: string;
|
url: string;
|
||||||
username: string;
|
username: string;
|
||||||
password: string;
|
password: string;
|
||||||
|
authHeader: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface TTLConfigData {
|
interface TTLConfigData {
|
||||||
@@ -37,13 +38,16 @@ export class ConfigDBService {
|
|||||||
const user = this.getCurrentUser();
|
const user = this.getCurrentUser();
|
||||||
await connectDB();
|
await connectDB();
|
||||||
|
|
||||||
|
const authHeader = Buffer.from(`${data.username}:${data.password}`).toString("base64");
|
||||||
|
|
||||||
const config = await KomgaConfig.findOneAndUpdate(
|
const config = await KomgaConfig.findOneAndUpdate(
|
||||||
{ userId: user.id },
|
{ userId: user.id },
|
||||||
{
|
{
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
url: data.url,
|
url: data.url,
|
||||||
username: data.username,
|
username: data.username,
|
||||||
password: data.password,
|
// password: data.password,
|
||||||
|
authHeader,
|
||||||
},
|
},
|
||||||
{ upsert: true, new: true }
|
{ upsert: true, new: true }
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -2,10 +2,7 @@ import { KomgaUser } from "./komga";
|
|||||||
|
|
||||||
export interface AuthConfig {
|
export interface AuthConfig {
|
||||||
serverUrl: string;
|
serverUrl: string;
|
||||||
credentials: {
|
authHeader: string;
|
||||||
username: string;
|
|
||||||
password: string;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AuthState {
|
export interface AuthState {
|
||||||
|
|||||||
Reference in New Issue
Block a user