Skip to content

Commit

Permalink
Merge pull request #92 from DigiChanges/fix/LC/refresh-token
Browse files Browse the repository at this point in the history
feat: set secure and sameSite cookies from env variables
  • Loading branch information
Murzbul authored Mar 2, 2022
2 parents 732bcfc + dc14da3 commit c243ac4
Show file tree
Hide file tree
Showing 9 changed files with 10,895 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
NODE_ENV=development
NODE_PATH=/home/node/app

SET_COOKIE_SECURE=false
SET_COOKIE_SAME_SITE=none

# Node.js server configuration
SERVER_PORT=8089

Expand Down
3 changes: 3 additions & 0 deletions .env.prod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
NODE_ENV=production
NODE_PATH=/home/node/app

SET_COOKIE_SECURE=true
SET_COOKIE_SAME_SITE=none

# Node.js server configuration
SERVER_PORT=8089

Expand Down
2 changes: 2 additions & 0 deletions config/development.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"env": "development",
"nodePath": "/home/node/app",
"serverPort": 8089,
"setCookieSecure": false,
"setCookieSameSite": "none",
"auth": {
"authorization": false
},
Expand Down
2 changes: 2 additions & 0 deletions config/production.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"env": "production",
"nodePath": "/home/node/app",
"serverPort": 80,
"setCookieSecure": true,
"setCookieSameSite": "none",
"auth": {
"authorization": true
},
Expand Down
10 changes: 6 additions & 4 deletions src/Auth/Presentation/Handlers/Express/AuthHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ class AuthHandler
expires: moment.unix(payload.getExpires()).toDate(),
maxAge: payload.getExpires(),
path: '/api/auth/refresh-token',
secure: MainConfig.getInstance().getConfig().env === 'production',
httpOnly: true
secure: MainConfig.getInstance().getConfig().setCookieSecure,
httpOnly: true,
sameSite: MainConfig.getInstance().getConfig().setCookieSameSite
});

void await this.responder.send(payload, req, res, StatusCode.HTTP_CREATED, new AuthTransformer());
Expand Down Expand Up @@ -110,8 +111,9 @@ class AuthHandler
expires: moment.unix(payload.getExpires()).toDate(),
maxAge: payload.getExpires(),
path: '/api/auth/refresh-token',
secure: MainConfig.getInstance().getConfig().env === 'production',
httpOnly: true
secure: MainConfig.getInstance().getConfig().setCookieSecure,
httpOnly: true,
sameSite: MainConfig.getInstance().getConfig().setCookieSameSite
});

void await this.responder.send(payload, req, res, StatusCode.HTTP_CREATED, new AuthTransformer());
Expand Down
9 changes: 7 additions & 2 deletions src/Auth/Presentation/Handlers/Koa/AuthHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import RegisterRequest from '../../Requests/RegisterRequest';
import UpdateMeRequest from '../../Requests/UpdateMeRequest';
import VerifyYourAccountRequest from '../../Requests/VerifyYourAccountRequest';
import RefreshTokenMiddleware from '../../Middlewares/Koa/RefreshTokenMiddleware';
import MainConfig from '../../../../Config/mainConfig';

const routerOpts: Router.IRouterOptions = {
prefix: '/api/auth'
Expand Down Expand Up @@ -55,7 +56,9 @@ AuthHandler.post('/login', async(ctx: Koa.ParameterizedContext & any) =>
expires: moment.unix(payload.getExpires()).toDate(),
maxAge: payload.getExpires(),
path: '/api/auth/refresh-token',
httpOnly: true
secure: MainConfig.getInstance().getConfig().setCookieSecure,
httpOnly: true,
sameSite: MainConfig.getInstance().getConfig().setCookieSameSite
});

void await responder.send(payload, ctx, StatusCode.HTTP_CREATED, new AuthTransformer());
Expand Down Expand Up @@ -92,7 +95,9 @@ AuthHandler.post('/refresh-token', RefreshTokenMiddleware, async(ctx: Koa.Parame
expires: moment.unix(payload.getExpires()).toDate(),
maxAge: payload.getExpires(),
path: '/api/auth/refresh-token',
httpOnly: true
secure: MainConfig.getInstance().getConfig().setCookieSecure,
httpOnly: true,
sameSite: MainConfig.getInstance().getConfig().setCookieSameSite
});

void await responder.send(payload, ctx, StatusCode.HTTP_OK, new AuthTransformer());
Expand Down
2 changes: 2 additions & 0 deletions src/Config/mainConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ type ApiWhiteType = {
type ConfigType = {
env: string;
nodePath: string;
setCookieSecure: boolean;
setCookieSameSite: boolean | 'none' | 'lax' | 'strict';
serverPort: number;
auth: {
authorization: boolean;
Expand Down
2 changes: 2 additions & 0 deletions src/Config/validateEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export function validateEnv()
JWT_EXPIRES: num(),
JWT_ISS: str(),
JWT_AUD: str(),
SET_COOKIE_SECURE: bool(),
SET_COOKIE_SAME_SITE: str(),

SMTP_HOST: str(),
SMTP_PORT: num(),
Expand Down
Loading

0 comments on commit c243ac4

Please sign in to comment.