Skip to content

Commit d19b44d

Browse files
feat(authentication): control secure cookies through separate constant
disable eslint consistent type imports (problem with import classes)
1 parent ea18915 commit d19b44d

File tree

10 files changed

+28
-23
lines changed

10 files changed

+28
-23
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module.exports = {
2929
settings: {},
3030
rules: {
3131
'@typescript-eslint/no-unsafe-return': 'off',
32+
'@typescript-eslint/consistent-type-imports': 'off',
3233
'no-await-in-loop': 'off',
3334
'prettier/prettier': [
3435
'error',

example/middlewares.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
},
113113
"convertResult": {
114114
"payload.cookies": "$middleware.payload.cookies",
115+
"tokens.access": "$middleware.access",
115116
"tokens.refresh": "$middleware.refresh"
116117
}
117118
}
@@ -146,6 +147,7 @@
146147
},
147148
"convertResult": {
148149
"payload.cookies": "$middleware.payload.cookies",
150+
"tokens.access": "$middleware.access",
149151
"tokens.refresh": "$middleware.refresh"
150152
}
151153
}
@@ -180,6 +182,7 @@
180182
},
181183
"convertResult": {
182184
"payload.cookies": "$middleware.payload.cookies",
185+
"tokens.access": "$middleware.access",
183186
"tokens.refresh": "$middleware.refresh"
184187
}
185188
}

microservices/authentication/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ This microservice provides authentication mechanism for microservices.
3838
- `DB_USERNAME` - Database user name. Default: `postgres`
3939
- `DB_PASSWORD` - Database password. Default: `example`
4040
- `DB_DATABASE` - Database db name. Default: `ms-authentication`
41+
- `IS_SECURE_COOKIE` - Set secure cookie for `returnType: cookies`. Default: `1`
4142

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

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ import jsonwebtoken from 'jsonwebtoken';
55
import rewiremock from 'rewiremock';
66
import TokenType from '@constants/token-type';
77
import Token from '@entities/token';
8-
import {
9-
CreateAuthToken as OriginalCreateAuthToken,
10-
TokenCreateReturnType,
11-
} from '@services/methods/create-auth-token';
8+
import type { CreateAuthToken as OriginalCreateAuthToken } from '@services/methods/create-auth-token';
9+
import { TokenCreateReturnType } from '@services/methods/create-auth-token';
1210

1311
const { CreateAuthToken } = rewiremock.proxy<{ CreateAuthToken: typeof OriginalCreateAuthToken }>(
1412
() => require('@services/methods/create-auth-token'),
@@ -106,7 +104,7 @@ describe('services/methods/create-auth-token', () => {
106104
action: 'add',
107105
name: 'jwt-access',
108106
value: token.access,
109-
options: { httpOnly: true, secure: false },
107+
options: { httpOnly: true, secure: true },
110108
},
111109
],
112110
},

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { expect } from 'chai';
44
import rewiremock from 'rewiremock';
55
import Token from '@entities/token';
66
import { TokenCreateReturnType } from '@services/methods/create-auth-token';
7-
import { RenewAuthToken as OriginalRenewAuthToken } from '@services/methods/renew-auth-token';
7+
import type { RenewAuthToken as OriginalRenewAuthToken } from '@services/methods/renew-auth-token';
88
import Jwt from '@services/tokens/jwt';
99

1010
const { RenewAuthToken } = rewiremock.proxy<{ RenewAuthToken: typeof OriginalRenewAuthToken }>(
@@ -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: false },
52+
options: { httpOnly: true, secure: true },
5353
},
5454
],
5555
},

microservices/authentication/src/constants/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ 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 MS_REMOTE_CONFIG = Number(process.env.MS_REMOTE_CONFIG || 1);
18+
const IS_SECURE_COOKIE = Boolean(Number(process.env.IS_SECURE_COOKIE || 1));
1819

1920
const DB_FROM_CONFIG_MS = Number(process.env.DB_FROM_CONFIG_MS ?? 1);
2021
const DB_ENV = {
@@ -44,4 +45,5 @@ export {
4445
IS_TEST,
4546
IS_BUILD,
4647
SRC_FOLDER,
48+
IS_SECURE_COOKIE,
4749
};

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { IsTimestamp, IsUndefinable } from '@lomray/microservice-helpers';
22
import type { IMicroserviceResponseCookie } from '@lomray/microservice-nodejs-lib';
33
import { IsEnum, IsObject, IsString, Length } from 'class-validator';
44
import { JSONSchema } from 'class-validator-jsonschema';
5-
import { Repository } from 'typeorm';
5+
import type { Repository } from 'typeorm';
66
import type { IJwtConfig } from '@config/jwt';
7-
import { IS_PROD } from '@constants/index';
7+
import { IS_SECURE_COOKIE } from '@constants/index';
88
import TokenType from '@constants/token-type';
9-
import Token from '@entities/token';
9+
import type Token from '@entities/token';
1010
import Jwt from '@services/tokens/jwt';
1111
import Personal from '@services/tokens/personal';
1212

@@ -167,7 +167,7 @@ class CreateAuthToken {
167167
action: 'add',
168168
name: 'jwt-access',
169169
value: result['access'],
170-
options: { httpOnly: true, secure: IS_PROD },
170+
options: { httpOnly: true, secure: IS_SECURE_COOKIE },
171171
},
172172
],
173173
},

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import type { IMicroserviceResponseCookie } from '@lomray/microservice-nodejs-li
33
import { BaseException } from '@lomray/microservice-nodejs-lib';
44
import { IsEnum, IsObject, IsString, Length } from 'class-validator';
55
import { JSONSchema } from 'class-validator-jsonschema';
6-
import { Repository } from 'typeorm';
6+
import type { Repository } from 'typeorm';
77
import type { IJwtConfig } from '@config/jwt';
8-
import { IS_PROD } from '@constants/index';
9-
import Token from '@entities/token';
8+
import { IS_SECURE_COOKIE } from '@constants/index';
9+
import type Token from '@entities/token';
1010
import { TokenCreateReturnType } from '@services/methods/create-auth-token';
1111
import { IdentifyAuthToken } from '@services/methods/identity-auth-token';
1212
import Jwt from '@services/tokens/jwt';
@@ -146,7 +146,7 @@ class RenewAuthToken {
146146
action: 'add',
147147
name: 'jwt-access',
148148
value: result['access'],
149-
options: { httpOnly: true, secure: IS_PROD },
149+
options: { httpOnly: true, secure: IS_SECURE_COOKIE },
150150
},
151151
],
152152
},

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"@commitlint/cli": "^13.1.0",
4343
"@commitlint/config-conventional": "^13.1.0",
4444
"@istanbuljs/nyc-config-typescript": "^1.0.1",
45-
"@lomray/eslint-config": "^1.1.0",
45+
"@lomray/eslint-config": "^1.2.1",
4646
"@lomray/prettier-config": "^1.0.1",
4747
"@rollup/plugin-json": "^4.1.0",
4848
"@rollup/plugin-replace": "^4.0.0",

0 commit comments

Comments
 (0)