Skip to content

Commit

Permalink
Rebase main
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmachin committed Jan 16, 2024
1 parent 67a2668 commit 875eddf
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 32 deletions.
2 changes: 0 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,3 @@ OTP_EXPIRATION_MINUTES=15
ENABLE_RATE_LIMIT='true'
COOKIE_SECRET="secret"
COOKIE_EXPIRATION_SECONDS=86400 # 24 hours
ENABLE_COOKIE="true"
ENABLE_JWT="true"
2 changes: 0 additions & 2 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ env:
ENABLE_RATE_LIMIT: 'true'
COOKIE_SECRET: 'secret'
COOKIE_EXPIRATION_SECONDS: '3600'
ENABLE_COOKIE: 'true'
ENABLE_JWT: 'true'

jobs:
build:
Expand Down
2 changes: 0 additions & 2 deletions .woodpecker/.backend-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ x-common: &common
- ENABLE_RATE_LIMIT=true
- COOKIE_SECRET=secret
- COOKIE_EXPIRATION_SECONDS=3600
- ENABLE_COOKIE=true
- ENABLE_JWT=true

pipeline:
setup:
Expand Down
4 changes: 3 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@types/bcryptjs": "^2.4.2",
"@types/body-parser": "^1.19.2",
"@types/compression": "^1.7.2",
"@types/cookie-parser": "^1.4.6",
"@types/cors": "^2.8.12",
"@types/cross-spawn": "^6.0.6",
"@types/express": "^4.17.13",
Expand Down Expand Up @@ -63,7 +64,6 @@
},
"dependencies": {
"@prisma/client": "^5.5.2",
"@types/cookie-parser": "^1.4.6",
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.2",
"bullmq": "^4.13.2",
Expand Down
4 changes: 0 additions & 4 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ const envVarsSchema = z
(val) => !Number.isNaN(val),
'COOKIE EXPIRATION SECONDS must be a number',
),
ENABLE_COOKIE: z.string(),
ENABLE_JWT: z.string(),
})
.passthrough();

Expand All @@ -75,8 +73,6 @@ export const isTest = envVars.NODE_ENV === 'test';
export const isProduction = envVars.NODE_ENV === 'production';
export const hasToApplyRateLimit =
envVars.ENABLE_RATE_LIMIT.toLocaleLowerCase() === 'true';
export const cookieEnabled = envVars.ENABLE_COOKIE === 'true';
export const JWTEnabled = envVars.ENABLE_JWT === 'true';

export const config: Config = {
env: envVars.NODE_ENV,
Expand Down
17 changes: 6 additions & 11 deletions src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
AuthenticatedRequest,
LoginParams,
} from 'types';
import { cookieEnabled, JWTEnabled } from 'config/config';
import { COOKIE_NAME, cookieConfig } from 'utils/auth';

@Route('v1/auth')
Expand All @@ -20,13 +19,12 @@ export class AuthControllerV1 extends Controller {
@Request() req: AuthenticatedRequest,
): Promise<ReturnAuth | null> {
const { sessionId, ...authReturn } = await AuthService.register(user);

const { res } = req;
if (cookieEnabled) {
res?.cookie(COOKIE_NAME, sessionId, cookieConfig);
}
res?.cookie(COOKIE_NAME, sessionId, cookieConfig);

this.setStatus(httpStatus.CREATED);
if (JWTEnabled) return authReturn;
return null;
return authReturn;
}

@Post('/login')
Expand All @@ -36,12 +34,9 @@ export class AuthControllerV1 extends Controller {
): Promise<ReturnAuth | null> {
const { sessionId, ...authReturn } = await AuthService.login(loginParams);
const { res } = req;
if (cookieEnabled) {
res?.cookie(COOKIE_NAME, sessionId, cookieConfig);
}
res?.cookie(COOKIE_NAME, sessionId, cookieConfig);
this.setStatus(httpStatus.OK);
if (JWTEnabled) return authReturn;
return null;
return authReturn;
}

@Post('/logout')
Expand Down
3 changes: 1 addition & 2 deletions src/controllers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
PasswordResetCodeRequest,
ResetPassword,
} from 'types';
import { cookieEnabled } from 'config/config';
import { COOKIE_NAME } from 'utils/auth';

@Route('v1/users')
Expand Down Expand Up @@ -74,7 +73,7 @@ export class UsersControllerV1 extends Controller {
): Promise<void> {
const { user, res } = req;
await UserService.destroy(id);
if (cookieEnabled && user.id === id) res?.clearCookie(COOKIE_NAME);
if (user.id === id) res?.clearCookie(COOKIE_NAME);
this.setStatus(httpStatus.NO_CONTENT);
}

Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request } from 'express';
import jwt from 'jsonwebtoken';
import { config, JWTEnabled } from 'config/config';
import { config } from 'config/config';
import { ApiError } from 'utils/apiError';
import { errors } from 'config/errors';
import { verifyCookie } from 'utils/auth';
Expand All @@ -15,7 +15,7 @@ export function expressAuthentication(
const token = request.headers.authorization!;

return new Promise((resolve, reject) => {
if (!token || !JWTEnabled) {
if (!token) {
reject(new ApiError(errors.UNAUTHENTICATED));
}
jwt.verify(token, config.accessTokenSecret, (err: any, decoded: any) => {
Expand Down
14 changes: 9 additions & 5 deletions src/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@ import { CookieOptions } from 'express';

import { errors } from 'config/errors';
import prisma from 'root/prisma/client';
import { cookieEnabled, config, isProduction } from 'config/config';
import { config, isProduction } from 'config/config';
import { ApiError } from './apiError';

export const COOKIE_NAME = 'token';

const SECONDS_TO_MILLISECONDS = 1000;

export const cookieConfig = {
type SignedCookie = {
token: string;
};

export const cookieConfig: CookieOptions = {
signed: true,
httpOnly: true,
maxAge: config.cookieExpirationSeconds * SECONDS_TO_MILLISECONDS,
secure: isProduction,
} as CookieOptions;
};

export const verifyCookie = async (signedCookies: any) => {
if (!cookieEnabled || !signedCookies || !signedCookies.token) {
export const verifyCookie = async (signedCookies: SignedCookie | null) => {
if (!signedCookies || !signedCookies.token) {
throw new ApiError(errors.UNAUTHENTICATED);
}

Expand Down

0 comments on commit 875eddf

Please sign in to comment.