Skip to content

Commit 9691df1

Browse files
feat(authentication): control auth cookies through separate constant
1 parent 53925da commit 9691df1

File tree

6 files changed

+20
-6
lines changed

6 files changed

+20
-6
lines changed

microservices/authentication/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ This microservice provides authentication mechanism for microservices.
3939
- `DB_PASSWORD` - Database password. Default: `example`
4040
- `DB_DATABASE` - Database db name. Default: `ms-authentication`
4141
- `IS_SECURE_COOKIE` - Set secure cookie for `returnType: cookies`. Default: `1`
42+
- `IS_HTTPONLY_COOKIE` - Set httpOnly cookie for `returnType: cookies`. Default: `1`
43+
- `COOKIE_SAME_SITE` - Set sameSite cookie for `returnType: cookies`. Default: `undefined`
4244

4345
### <a id="how-to-run"></a>HOW TO RUN:
4446
1. Run `Inverted Json` job server.

microservices/authentication/__tests__/services/methods/create-auth-token-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ describe('services/methods/create-auth-token', () => {
104104
action: 'add',
105105
name: 'jwt-access',
106106
value: token.access,
107-
options: { httpOnly: true, secure: true },
107+
options: { httpOnly: true, secure: true, sameSite: undefined },
108108
},
109109
],
110110
},

microservices/authentication/__tests__/services/methods/renew-auth-token-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('services/methods/renew-auth-token', () => {
4949
action: 'add',
5050
name: 'jwt-access',
5151
value: token.access,
52-
options: { httpOnly: true, secure: true },
52+
options: { httpOnly: true, secure: true, sameSite: undefined },
5353
},
5454
],
5555
},

microservices/authentication/src/constants/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const MS_ENABLE_REMOTE_MIDDLEWARE = Number(process.env.MS_ENABLE_REMOTE_MIDDLEWA
1515
const MS_JWT_PARAMS = JSON.parse(process.env.MS_JWT_PARAMS || '{}');
1616
const MS_JWT_SECRET_KEY = process.env.MS_JWT_SECRET_KEY || undefined;
1717
const IS_SECURE_COOKIE = Boolean(Number(process.env.IS_SECURE_COOKIE || 1));
18+
const IS_HTTPONLY_COOKIE = Boolean(Number(process.env.IS_HTTPONLY_COOKIE || 1));
19+
const COOKIE_SAME_SITE = (process.env.COOKIE_SAME_SITE || undefined) as undefined;
1820
const MS_REMOTE_CONFIG = Number(process.env.MS_REMOTE_CONFIG || 1);
1921

2022
const DB_FROM_CONFIG_MS = Number(process.env.DB_FROM_CONFIG_MS ?? 1);
@@ -46,4 +48,6 @@ export {
4648
IS_BUILD,
4749
SRC_FOLDER,
4850
IS_SECURE_COOKIE,
51+
COOKIE_SAME_SITE,
52+
IS_HTTPONLY_COOKIE,
4953
};

microservices/authentication/src/services/methods/create-auth-token.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { IsEnum, IsObject, IsString, Length } from 'class-validator';
44
import { JSONSchema } from 'class-validator-jsonschema';
55
import type { Repository } from 'typeorm';
66
import type { IJwtConfig } from '@config/jwt';
7-
import { IS_SECURE_COOKIE } from '@constants/index';
7+
import { COOKIE_SAME_SITE, IS_HTTPONLY_COOKIE, IS_SECURE_COOKIE } from '@constants/index';
88
import TokenType from '@constants/token-type';
99
import type Token from '@entities/token';
1010
import Jwt from '@services/tokens/jwt';
@@ -167,7 +167,11 @@ class CreateAuthToken {
167167
action: 'add',
168168
name: 'jwt-access',
169169
value: result['access'],
170-
options: { httpOnly: true, secure: IS_SECURE_COOKIE },
170+
options: {
171+
httpOnly: IS_HTTPONLY_COOKIE,
172+
secure: IS_SECURE_COOKIE,
173+
sameSite: COOKIE_SAME_SITE,
174+
},
171175
},
172176
],
173177
},

microservices/authentication/src/services/methods/renew-auth-token.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { IsEnum, IsObject, IsString, Length } from 'class-validator';
55
import { JSONSchema } from 'class-validator-jsonschema';
66
import type { Repository } from 'typeorm';
77
import type { IJwtConfig } from '@config/jwt';
8-
import { IS_SECURE_COOKIE } from '@constants/index';
8+
import { COOKIE_SAME_SITE, IS_HTTPONLY_COOKIE, IS_SECURE_COOKIE } from '@constants/index';
99
import type Token from '@entities/token';
1010
import { TokenCreateReturnType } from '@services/methods/create-auth-token';
1111
import { IdentifyAuthToken } from '@services/methods/identity-auth-token';
@@ -146,7 +146,11 @@ class RenewAuthToken {
146146
action: 'add',
147147
name: 'jwt-access',
148148
value: result['access'],
149-
options: { httpOnly: true, secure: IS_SECURE_COOKIE },
149+
options: {
150+
httpOnly: IS_HTTPONLY_COOKIE,
151+
secure: IS_SECURE_COOKIE,
152+
sameSite: COOKIE_SAME_SITE,
153+
},
150154
},
151155
],
152156
},

0 commit comments

Comments
 (0)