strip password
This commit is contained in:
1 parent
1b2d2887af
commit
4443a53a23
6 files changed
+64
-35
No files matched your search
@@ -1,12 +1,12 @@
|
||||
import { IProxiesController } from "@/api/controllers/proxyController.js";
|
||||
import { Router } from "express";
|
||||
|
||||
export const createProxyRoutes = (tagController: IProxiesController): Router => {
|
||||
export const createProxyRoutes = (proxyController: IProxiesController): Router => {
|
||||
const router = Router();
|
||||
router.post("/", tagController.createProxy);
|
||||
router.get("/team", tagController.getProxiesByTeamId);
|
||||
router.get("/:id", tagController.getProxyById);
|
||||
router.delete("/:id", tagController.deleteProxy);
|
||||
router.patch("/:id", tagController.editProxy);
|
||||
router.post("/", proxyController.createProxy);
|
||||
router.get("/team", proxyController.getProxiesByTeamId);
|
||||
router.get("/:id", proxyController.getProxyById);
|
||||
router.delete("/:id", proxyController.deleteProxy);
|
||||
router.patch("/:id", proxyController.editProxy);
|
||||
return router;
|
||||
};
|
||||
@@ -14,7 +14,10 @@ export const createProxyBodyValidation = z.object({
|
||||
password: z.string().optional(),
|
||||
});
|
||||
|
||||
export const editProxyBodyValidation = createProxyBodyValidation;
|
||||
// Empty/absent password keeps doesn't delete, only clearPassword: true does.
|
||||
export const editProxyBodyValidation = createProxyBodyValidation.extend({
|
||||
clearPassword: z.boolean().optional(),
|
||||
});
|
||||
|
||||
export const getProxyByIdParamValidation = z.object({
|
||||
id: z.string().min(1, "Proxy ID is required"),
|
||||
@@ -26,8 +29,6 @@ export const deleteProxyParamValidation = z.object({
|
||||
id: z.string().min(1, "Proxy ID is required"),
|
||||
});
|
||||
|
||||
// Canonical proxy shape returned by /proxies endpoints. Keep aligned with what
|
||||
// the controllers actually serialize (password is intentionally omitted; hasPassword tells the client one is stored).
|
||||
export const proxyResponseSchema = z
|
||||
.object({
|
||||
id: z.string(),
|
||||
|
||||
@@ -7,7 +7,7 @@ export interface IProxiesRepository {
|
||||
findById(proxyId: string, teamId: string): Promise<Proxy>;
|
||||
findByTeamId(teamId: string): Promise<Proxy[]>;
|
||||
// update
|
||||
updateById(proxyId: string, teamId: string, patch: Partial<Proxy>): Promise<Proxy>;
|
||||
updateById(proxyId: string, teamId: string, patch: Partial<Proxy>, options?: { unsetPassword?: boolean }): Promise<Proxy>;
|
||||
// delete
|
||||
deleteById(proxyId: string, teamId: string): Promise<Proxy>;
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import { Proxy } from "@/domain/proxies/proxy.type.js";
|
||||
import { ProxyDocument, ProxyModel } from "@/domain/proxies/proxy.model.js";
|
||||
import { AppError } from "@/utils/AppError.js";
|
||||
import { toStringId, toDateString } from "@/utils/mongoMappers.js";
|
||||
import { UpdateQuery } from "mongoose";
|
||||
|
||||
const SERVICE_NAME = "ProxiesRepository";
|
||||
|
||||
@@ -49,17 +50,18 @@ class MongoProxiesRepository implements IProxiesRepository {
|
||||
return proxies.map(this.toEntity);
|
||||
}
|
||||
|
||||
async updateById(proxyId: string, teamId: string, patch: Partial<Proxy>): Promise<Proxy> {
|
||||
async updateById(proxyId: string, teamId: string, patch: Partial<Proxy>, options?: { unsetPassword?: boolean }): Promise<Proxy> {
|
||||
try {
|
||||
const updatedProxy = await ProxyModel.findOneAndUpdate(
|
||||
{ _id: proxyId, teamId },
|
||||
{
|
||||
$set: {
|
||||
...patch,
|
||||
},
|
||||
const update: UpdateQuery<ProxyDocument> = {
|
||||
$set: {
|
||||
...patch,
|
||||
},
|
||||
{ new: true, runValidators: true }
|
||||
);
|
||||
};
|
||||
if (options?.unsetPassword) {
|
||||
delete update.$set?.password;
|
||||
update.$unset = { password: 1 };
|
||||
}
|
||||
const updatedProxy = await ProxyModel.findOneAndUpdate({ _id: proxyId, teamId }, update, { new: true, runValidators: true });
|
||||
if (!updatedProxy) {
|
||||
throw new AppError({ message: "Proxy not found or could not be updated", service: SERVICE_NAME, status: 404 });
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
}
|
||||
@@ -13,3 +13,5 @@ export interface Proxy {
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
export type ProxyResponse = Omit<Proxy, "password"> & { hasPassword: boolean };
|
||||
Reference in new issue
Block a user