Skip to content

Commit

Permalink
feat: add default value for cookie TTL (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmousse authored Jul 5, 2022
1 parent 4663734 commit cfac188
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ describe('Authentication Module with async options', () => {
options: {
httpOnly: true,
secure: false,
maxAge: 86400000,
},
queryParamName: 'authToken',
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
export const AUTHENTICATION_MODULE_OPTIONS = ' AUTHENTICATION_MODULE_OPTIONS';

export const AUTHENTICATION_DEFAULT_COOKIE_NAME = 'authCookie';

/**
* Default cookie TTL (24h)
*/
export const AUTHENTICATION_DEFAULT_COOKIE_TTL = 1000 * 60 * 60 * 24;
export const AUTHENTICATION_DEFAULT_AUTH_TOKEN = 'authToken';
export const AUTHENTICATION_DEFAULT_QUERY_PARAM_NAME = 'authToken';

export const DEFAULT_ID_FIELD = 'id';
export const DEFAULT_LOGIN_FIELD = 'email';
export const DEFAULT_PASSWORD_FIELD = 'password';
Expand Down
2 changes: 1 addition & 1 deletion libs/nestjs/authentication/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './authentication.contants';
export * from './authentication.constants';
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,14 @@ describe('Authentication Module', () => {
.set('Authorization', `bearer ${accessToken}`);

expect(response.status).toBe(200);
expect(response.headers['set-cookie']).toEqual([
'authCookie=; Path=/; HttpOnly',
]);

const cookie = response.headers['set-cookie'][0];

expect(cookie).toMatch(/authCookie=;/);
expect(cookie).toMatch(/Path=\/;/);
expect(cookie).toMatch(/Max-Age=86400;/);
expect(cookie).toMatch(/Expires=/);
expect(cookie).toMatch(/HttpOnly/);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { AuthenticationOptionsCookies } from './authentication-options-cookies.dto';

import { transformAndValidate } from '@tractr/common';

describe('AuthenticationOptionsCookies', () => {
it('should pass if config is valid', () => {
const config = {
cookieName: 'authCookie',
options: {
httpOnly: true,
maxAge: 86400000,
secure: false,
},
queryParamName: 'authToken',
};

const validatedDtoWithDefaults = transformAndValidate(
AuthenticationOptionsCookies,
)(config);

expect(validatedDtoWithDefaults).toEqual(config);
});

it('should throw an error if config is not valid', () => {
const config = {
cookieName: 123,
};

const validate = transformAndValidate(AuthenticationOptionsCookies);

expect(() => validate(config)).toThrowError();
});

it('should initialize default values properly', () => {
const config = {};

const dtoDefault = {
cookieName: 'authCookie',
options: {
httpOnly: true,
maxAge: 86400000,
secure: false,
},
queryParamName: 'authToken',
};

const validatedDtoWithDefaults = transformAndValidate(
AuthenticationOptionsCookies,
)(config);

expect(validatedDtoWithDefaults).toEqual(dtoDefault);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CookieOptions } from 'express';
import {
AUTHENTICATION_DEFAULT_AUTH_TOKEN,
AUTHENTICATION_DEFAULT_COOKIE_NAME,
AUTHENTICATION_DEFAULT_COOKIE_TTL,
} from '../constants';

import { isProduction } from '@tractr/nestjs-core';
Expand All @@ -17,6 +18,7 @@ export class AuthenticationOptionsCookies {

@ValidateNested()
options: CookieOptions = {
maxAge: AUTHENTICATION_DEFAULT_COOKIE_TTL,
httpOnly: true,
secure: isProduction(),
};
Expand Down

0 comments on commit cfac188

Please sign in to comment.