Skip to content
Merged
Show file tree
Hide file tree
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
31 changes: 12 additions & 19 deletions src/collaboration/SecondaryTokenAuthenticator.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,24 @@ export class SecondaryTokenAuthenticator {
};
}

const rawTravelDocId = this.extractTravelDocId(req);
if (!rawTravelDocId) {
const rawTravelDocId = payload?.travelItineraryId;
if (typeof rawTravelDocId !== "string" && typeof rawTravelDocId !== "number") {
return {
ok: false,
statusCode: 400,
message: "Missing travelItineraryId query parameter"
statusCode: 401,
message: "Secondary token must include travelItineraryId"
};
}

const userId = String(rawUserId);
const travelDocId = rawTravelDocId;
const travelDocId = String(rawTravelDocId).trim();
if (!travelDocId) {
return {
ok: false,
statusCode: 401,
message: "Secondary token must include travelItineraryId"
};
}
Comment on lines +43 to +60

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

travelItineraryId에 대한 유효성 검사 로직이 두 부분으로 나뉘어 있어 중복이 발생하고 있습니다. 이 두 검사를 하나로 통합하여 코드를 더 간결하고 유지보수하기 쉽게 만들 수 있습니다.

    const rawTravelDocId = payload?.travelItineraryId;
    if (
      (typeof rawTravelDocId !== "string" && typeof rawTravelDocId !== "number") ||
      String(rawTravelDocId).trim() === ""
    ) {
      return {
        ok: false,
        statusCode: 401,
        message: "Secondary token must include travelItineraryId"
      };
    }

    const userId = String(rawUserId);
    const travelDocId = String(rawTravelDocId).trim();


return {
ok: true,
Expand Down Expand Up @@ -83,20 +90,6 @@ export class SecondaryTokenAuthenticator {
return { ok: true, token };
}

extractTravelDocId(req) {
const travelItineraryId = this.getRequestUrl(req).searchParams.get("travelItineraryId");
if (!travelItineraryId) {
return null;
}

const normalizedTravelItineraryId = travelItineraryId.trim();
if (!normalizedTravelItineraryId) {
return null;
}

return normalizedTravelItineraryId;
}

isSecondaryTokenUsed(token) {
this.pruneUsedSecondaryTokens();
return this.usedSecondaryTokens.has(token);
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ travelDocWebSocketServer.start();
server.listen(port, () => {
console.log(`HTTP server listening on http://localhost:${port}`);
console.log(
`WebSocket server listening on ws://localhost:${port}?secondaryToken=Bearer%20{jwt}&travelItineraryId={travelItineraryId}`
`WebSocket server listening on ws://localhost:${port}?secondaryToken=Bearer%20{jwt}`
);
console.log(`Y-LevelDB path: ${yLeveldbPath}`);
});
Loading