strip password

This commit is contained in:
Alex Holliday committed 2026-08-20 13:52:14 -07:00
1 parent 1b2d2887af
commit 4443a53a23
6 files changed
+64 -35

No files matched your search

+40 -16
View File
@@ -1,15 +1,15 @@
import { Proxy } from "@/domain/proxies/proxy.type.js";
import { Proxy, ProxyResponse } from "@/domain/proxies/proxy.type.js";
import { IMonitorsRepository } from "@/domain/monitors/monitor.repository.interface.js";
import { IProxiesRepository } from "@/domain/proxies/proxy.repository.interface.js";
import { AppError } from "@/utils/AppError.js";
import { ISettingsService } from "@/domain/app-settings/app-settings.service.js";
export interface IProxiesService {
createProxy(proxy: Partial<Proxy>, teamId: string): Promise<Proxy>;
getProxy(proxyId: string, teamId: string): Promise<Proxy>;
getProxiesByTeamId(teamId: string): Promise<Proxy[]>;
updateProxy(proxyId: string, teamId: string, patch: Partial<Proxy>): Promise<Proxy>;
deleteProxy(proxyId: string, teamId: string): Promise<Proxy>;
createProxy(proxy: Partial<Proxy>, teamId: string): Promise<ProxyResponse>;
getProxy(proxyId: string, teamId: string): Promise<ProxyResponse>;
getProxiesByTeamId(teamId: string): Promise<ProxyResponse[]>;
updateProxy(proxyId: string, teamId: string, patch: Partial<Proxy> & { clearPassword?: boolean }): Promise<ProxyResponse>;
deleteProxy(proxyId: string, teamId: string): Promise<ProxyResponse>;
}
const SERVICE_NAME = "ProxiesService";
@@ -20,23 +20,46 @@ export class ProxiesService implements IProxiesService {
private settingsService: ISettingsService
) {}
createProxy = async (proxyData: Partial<Proxy>, teamId: string): Promise<Proxy> => {
private toResponse = ({ password, ...rest }: Proxy): ProxyResponse => ({ ...rest, hasPassword: Boolean(password) });
createProxy = async (proxyData: Partial<Proxy>, teamId: string): Promise<ProxyResponse> => {
proxyData.teamId = teamId;
return await this.proxiesRepository.create(proxyData);
if (!proxyData.password) {
delete proxyData.password;
}
if (!proxyData.username) {
delete proxyData.username;
}
const raw = await this.proxiesRepository.create(proxyData);
return this.toResponse(raw);
};
getProxy = async (proxyId: string, teamId: string): Promise<Proxy> => {
return await this.proxiesRepository.findById(proxyId, teamId);
getProxy = async (proxyId: string, teamId: string): Promise<ProxyResponse> => {
const raw = await this.proxiesRepository.findById(proxyId, teamId);
return this.toResponse(raw);
};
getProxiesByTeamId = async (teamId: string): Promise<Proxy[]> => {
return await this.proxiesRepository.findByTeamId(teamId);
getProxiesByTeamId = async (teamId: string): Promise<ProxyResponse[]> => {
const raw = await this.proxiesRepository.findByTeamId(teamId);
const clean = raw.map((r) => {
return this.toResponse(r);
});
return clean;
};
updateProxy = async (proxyId: string, teamId: string, patch: Partial<Proxy>): Promise<Proxy> => {
return await this.proxiesRepository.updateById(proxyId, teamId, patch);
updateProxy = async (proxyId: string, teamId: string, patch: Partial<Proxy> & { clearPassword?: boolean }): Promise<ProxyResponse> => {
// Empty/absent password keeps the stored one; clearPassword removes it
const { clearPassword, ...proxyPatch } = patch;
if (!proxyPatch.password) {
delete proxyPatch.password;
}
if (!proxyPatch.username) {
delete proxyPatch.username;
}
const raw = await this.proxiesRepository.updateById(proxyId, teamId, proxyPatch, { unsetPassword: clearPassword === true });
return this.toResponse(raw);
};
deleteProxy = async (proxyId: string, teamId: string): Promise<Proxy> => {
deleteProxy = async (proxyId: string, teamId: string): Promise<ProxyResponse> => {
// Make sure proxy is not in use
const monitorsUsingProxy = await this.monitorsRepository.findMonitorCountByProxyId(proxyId);
if (monitorsUsingProxy > 0) {
@@ -56,6 +79,7 @@ export class ProxiesService implements IProxiesService {
status: 409,
});
}
return await this.proxiesRepository.deleteById(proxyId, teamId);
const raw = await this.proxiesRepository.deleteById(proxyId, teamId);
return this.toResponse(raw);
};
}