Add missing status codes and rename enum members to PascalCase
Change-Id: Iab2893d75ac7cb797364a7a2e33a7140cc9d97db
diff --git a/server/src/app.ts b/server/src/app.ts
index 2d4fd7d..85e6b10 100644
--- a/server/src/app.ts
+++ b/server/src/app.ts
@@ -16,10 +16,10 @@
* <https://www.gnu.org/licenses/>.
*/
import express, { json, NextFunction, Request, Response } from 'express';
+import { HttpStatusCode } from 'jami-web-common';
import log from 'loglevel';
import { Service } from 'typedi';
-import { StatusCode } from './constants.js';
import { accountRouter } from './routers/account-router.js';
import { authRouter } from './routers/auth-router.js';
@@ -36,13 +36,13 @@
// Setup 404 error handling
app.use((_req, res) => {
- res.sendStatus(StatusCode.NOT_FOUND);
+ res.sendStatus(HttpStatusCode.NotFound);
});
// Setup internal error handling
app.use((err: Error, _req: Request, res: Response, _next: NextFunction) => {
log.error(err);
- res.status(StatusCode.INTERNAL_SERVER_ERROR).send(err.message);
+ res.status(HttpStatusCode.InternalServerError).send(err.message);
});
return app;
diff --git a/server/src/constants.ts b/server/src/constants.ts
deleted file mode 100644
index d73a292..0000000
--- a/server/src/constants.ts
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Copyright (C) 2022 Savoir-faire Linux Inc.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation; either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public
- * License along with this program. If not, see
- * <https://www.gnu.org/licenses/>.
- */
-export enum StatusCode {
- OK = 200,
- CREATED = 201,
- ACCEPTED = 202,
- NO_CONTENT = 204,
- BAD_REQUEST = 400,
- UNAUTHORIZED = 401,
- FORBIDDEN = 403,
- NOT_FOUND = 404,
- NOT_ACCEPTABLE = 406,
- CONFLICT = 409,
- GONE = 410,
- IM_A_TEAPOT = 418,
- TOO_MANY_REQUESTS = 429,
- INTERNAL_SERVER_ERROR = 500,
- NOT_IMPLEMENTED = 501,
-}
diff --git a/server/src/middleware/auth.ts b/server/src/middleware/auth.ts
index fad4165..c6992d3 100644
--- a/server/src/middleware/auth.ts
+++ b/server/src/middleware/auth.ts
@@ -16,10 +16,10 @@
* <https://www.gnu.org/licenses/>.
*/
import { NextFunction, Request, Response } from 'express';
+import { HttpStatusCode } from 'jami-web-common';
import { jwtVerify } from 'jose';
import { Container } from 'typedi';
-import { StatusCode } from '../constants.js';
import { Vault } from '../vault.js';
export async function authenticateToken(req: Request, res: Response, next: NextFunction) {
@@ -27,13 +27,13 @@
const authorizationHeader = req.headers.authorization;
if (!authorizationHeader) {
- res.status(StatusCode.UNAUTHORIZED).send('Missing Authorization header');
+ res.status(HttpStatusCode.Unauthorized).send('Missing Authorization header');
return;
}
const token = authorizationHeader.split(' ')[1];
if (token === undefined) {
- res.status(StatusCode.BAD_REQUEST).send('Missing JSON web token');
+ res.status(HttpStatusCode.BadRequest).send('Missing JSON web token');
return;
}
@@ -45,6 +45,6 @@
res.locals.accountId = payload.id as string;
next();
} catch (err) {
- res.sendStatus(StatusCode.UNAUTHORIZED);
+ res.sendStatus(HttpStatusCode.Unauthorized);
}
}
diff --git a/server/src/routers/auth-router.ts b/server/src/routers/auth-router.ts
index 08796a7..14559fe 100644
--- a/server/src/routers/auth-router.ts
+++ b/server/src/routers/auth-router.ts
@@ -19,11 +19,11 @@
import { Router } from 'express';
import asyncHandler from 'express-async-handler';
import { ParamsDictionary, Request } from 'express-serve-static-core';
+import { HttpStatusCode } from 'jami-web-common';
import { SignJWT } from 'jose';
import log from 'loglevel';
import { Container } from 'typedi';
-import { StatusCode } from '../constants.js';
import { Creds } from '../creds.js';
import { Jamid } from '../jamid/jamid.js';
import { Vault } from '../vault.js';
@@ -44,7 +44,7 @@
asyncHandler(async (req: Request<ParamsDictionary, any, Credentials>, res, _next) => {
const { username, password } = req.body;
if (!username || !password) {
- res.status(StatusCode.BAD_REQUEST).send('Missing username or password');
+ res.status(HttpStatusCode.BadRequest).send('Missing username or password');
return;
}
@@ -63,9 +63,9 @@
if (state !== 0) {
jamid.removeAccount(accountId);
if (state === 2) {
- res.status(StatusCode.BAD_REQUEST).send('Invalid username or password');
+ res.status(HttpStatusCode.BadRequest).send('Invalid username or password');
} else if (state === 3) {
- res.status(StatusCode.CONFLICT).send('Username already exists');
+ res.status(HttpStatusCode.Conflict).send('Username already exists');
} else {
throw new Error(`Unhandled state ${state}`);
}
@@ -75,7 +75,7 @@
creds.set(username, hashedPassword);
await creds.save();
- res.sendStatus(StatusCode.CREATED);
+ res.sendStatus(HttpStatusCode.Created);
})
);
@@ -84,7 +84,7 @@
asyncHandler(async (req: Request<ParamsDictionary, any, Credentials>, res, _next) => {
const { username, password } = req.body;
if (!username || !password) {
- res.status(StatusCode.BAD_REQUEST).send('Missing username or password');
+ res.status(HttpStatusCode.BadRequest).send('Missing username or password');
return;
}
@@ -93,14 +93,14 @@
// 2. found but not on this instance (but I'm not sure about this)
const accountId = jamid.getAccountIdFromUsername(username);
if (accountId === undefined) {
- res.status(StatusCode.NOT_FOUND).send('Username not found');
+ res.status(HttpStatusCode.NotFound).send('Username not found');
return;
}
// TODO: load the password from Jami
const hashedPassword = creds.get(username);
if (!hashedPassword) {
- res.status(StatusCode.NOT_FOUND).send('Password not found');
+ res.status(HttpStatusCode.NotFound).send('Password not found');
return;
}
@@ -108,7 +108,7 @@
const isPasswordVerified = await argon2.verify(hashedPassword, password);
if (!isPasswordVerified) {
- res.sendStatus(StatusCode.UNAUTHORIZED);
+ res.sendStatus(HttpStatusCode.Unauthorized);
return;
}