mirror of
https://github.com/streetwriters/notesnook-sync-server.git
synced 2026-05-15 12:17:58 +02:00
inbox: improve http response status codes (#99)
* inbox: improve http response status codes * inbox api: respond with 401 unauthorized for invalid inbox api key * notesnook api (get public encryption key): respond with 404 if not found * Update Notesnook.Inbox.API/src/index.ts --------- Co-authored-by: Abdullah Atta <abdullahatta@streetwriters.co>
This commit is contained in:
@@ -133,7 +133,7 @@ namespace Notesnook.API.Controllers
|
||||
var userSetting = await userSettingsRepository.FindOneAsync(u => u.UserId == userId);
|
||||
if (string.IsNullOrWhiteSpace(userSetting?.InboxKeys?.Public))
|
||||
{
|
||||
return BadRequest(new { error = "Inbox public key is not configured." });
|
||||
return NotFound(new { error = "Inbox public key is not configured." });
|
||||
}
|
||||
return Ok(new { key = userSetting.InboxKeys.Public });
|
||||
}
|
||||
|
||||
@@ -56,7 +56,9 @@ async function encrypt(
|
||||
};
|
||||
}
|
||||
|
||||
async function getInboxPublicEncryptionKey(apiKey: string) {
|
||||
async function getInboxPublicEncryptionKey(
|
||||
apiKey: string,
|
||||
): Promise<{ status: "unauthorized" } | { status: "ok"; key: string | null }> {
|
||||
const response = await fetch(
|
||||
`${NOTESNOOK_API_SERVER_URL}/inbox/public-encryption-key`,
|
||||
{
|
||||
@@ -65,6 +67,9 @@ async function getInboxPublicEncryptionKey(apiKey: string) {
|
||||
},
|
||||
},
|
||||
);
|
||||
if (response.status === 401) {
|
||||
return { status: "unauthorized" };
|
||||
}
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
`failed to fetch inbox public encryption key: ${await response.text()}`,
|
||||
@@ -72,7 +77,7 @@ async function getInboxPublicEncryptionKey(apiKey: string) {
|
||||
}
|
||||
|
||||
const data = (await response.json()) as unknown as any;
|
||||
return (data?.key as string) || null;
|
||||
return { status: "ok", key: (data?.key as string) || null };
|
||||
}
|
||||
|
||||
async function postEncryptedInboxItem(
|
||||
@@ -110,10 +115,14 @@ app.post("/", async (req, res) => {
|
||||
return res.status(401).json({ error: "unauthorized" });
|
||||
}
|
||||
|
||||
const inboxPublicKey = await getInboxPublicEncryptionKey(apiKey);
|
||||
if (!inboxPublicKey) {
|
||||
return res.status(403).json({ error: "inbox public key not found" });
|
||||
const encryptionKeyResult = await getInboxPublicEncryptionKey(apiKey);
|
||||
if (encryptionKeyResult.status === "unauthorized") {
|
||||
return res.status(401).json({ error: "unauthorized" });
|
||||
}
|
||||
if (!encryptionKeyResult.key) {
|
||||
return res.status(404).json({ error: "inbox public key not found" });
|
||||
}
|
||||
const inboxPublicKey = encryptionKeyResult.key;
|
||||
console.log("[info] fetched inbox public key");
|
||||
|
||||
const validationResult = RawInboxItemSchema.safeParse(req.body);
|
||||
|
||||
Reference in New Issue
Block a user