@flusys/nestjs-auth 6.0.2 → 6.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/controllers/authentication.controller.spec.js +219 -0
- package/cjs/controllers/branch.controller.spec.js +134 -0
- package/cjs/controllers/company-selection.controller.spec.js +84 -0
- package/cjs/controllers/company.controller.spec.js +131 -0
- package/cjs/controllers/email-verification.controller.spec.js +142 -0
- package/cjs/controllers/user-permission.controller.spec.js +138 -0
- package/cjs/controllers/user.controller.spec.js +204 -0
- package/cjs/docs/auth-swagger.config.spec.js +130 -0
- package/cjs/entities/index.spec.js +73 -0
- package/cjs/helpers/auth-response.mapper.js +46 -0
- package/cjs/helpers/auth-response.mapper.spec.js +139 -0
- package/cjs/helpers/index.js +19 -0
- package/cjs/helpers/token.helper.js +58 -0
- package/cjs/helpers/token.helper.spec.js +86 -0
- package/cjs/index.js +1 -0
- package/cjs/interceptors/clear-token.interceptor.spec.js +107 -0
- package/cjs/interceptors/set-token.interceptor.spec.js +165 -0
- package/cjs/modules/auth.module.spec.js +243 -0
- package/cjs/services/auth-config.service.spec.js +97 -0
- package/cjs/services/auth-datasource.provider.spec.js +161 -0
- package/cjs/services/auth-email.service.spec.js +404 -0
- package/cjs/services/authentication.service.js +37 -109
- package/cjs/services/authentication.service.spec.js +904 -0
- package/cjs/services/branch.service.spec.js +154 -0
- package/cjs/services/company-selection-session.service.spec.js +90 -0
- package/cjs/services/company.service.spec.js +113 -0
- package/cjs/services/user-permission.service.spec.js +202 -0
- package/cjs/services/user.service.spec.js +704 -0
- package/cjs/strategies/jwt.strategy.js +1 -1
- package/cjs/strategies/jwt.strategy.spec.js +166 -0
- package/cjs/validators/password.validator.spec.js +161 -0
- package/entities/pending-registration.entity.d.ts +2 -7
- package/fesm/controllers/authentication.controller.spec.js +215 -0
- package/fesm/controllers/branch.controller.spec.js +130 -0
- package/fesm/controllers/company-selection.controller.spec.js +80 -0
- package/fesm/controllers/company.controller.spec.js +127 -0
- package/fesm/controllers/email-verification.controller.spec.js +138 -0
- package/fesm/controllers/user-permission.controller.spec.js +134 -0
- package/fesm/controllers/user.controller.spec.js +200 -0
- package/fesm/docs/auth-swagger.config.spec.js +126 -0
- package/fesm/entities/index.spec.js +69 -0
- package/fesm/helpers/auth-response.mapper.js +25 -0
- package/fesm/helpers/auth-response.mapper.spec.js +135 -0
- package/fesm/helpers/index.js +2 -0
- package/fesm/helpers/token.helper.js +40 -0
- package/fesm/helpers/token.helper.spec.js +82 -0
- package/fesm/index.js +1 -0
- package/fesm/interceptors/clear-token.interceptor.spec.js +103 -0
- package/fesm/interceptors/set-token.interceptor.spec.js +161 -0
- package/fesm/interfaces/authentication.interface.js +1 -1
- package/fesm/modules/auth.module.spec.js +239 -0
- package/fesm/services/auth-config.service.spec.js +93 -0
- package/fesm/services/auth-datasource.provider.spec.js +157 -0
- package/fesm/services/auth-email.service.spec.js +359 -0
- package/fesm/services/authentication.service.js +37 -109
- package/fesm/services/authentication.service.spec.js +859 -0
- package/fesm/services/branch.service.spec.js +150 -0
- package/fesm/services/company-selection-session.service.spec.js +86 -0
- package/fesm/services/company.service.spec.js +109 -0
- package/fesm/services/user-permission.service.spec.js +198 -0
- package/fesm/services/user.service.spec.js +659 -0
- package/fesm/strategies/jwt.strategy.js +1 -1
- package/fesm/strategies/jwt.strategy.spec.js +162 -0
- package/fesm/validators/password.validator.spec.js +157 -0
- package/helpers/auth-response.mapper.d.ts +22 -0
- package/helpers/index.d.ts +2 -0
- package/helpers/token.helper.d.ts +8 -0
- package/index.d.ts +1 -0
- package/interfaces/authentication.interface.d.ts +7 -0
- package/package.json +8 -3
- package/services/authentication.service.d.ts +1 -8
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
get toBranchInfo () {
|
|
13
|
+
return toBranchInfo;
|
|
14
|
+
},
|
|
15
|
+
get toCompanyInfo () {
|
|
16
|
+
return toCompanyInfo;
|
|
17
|
+
},
|
|
18
|
+
get toUserInfo () {
|
|
19
|
+
return toUserInfo;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
function toUserInfo(user) {
|
|
23
|
+
return {
|
|
24
|
+
id: user.id,
|
|
25
|
+
email: user.email,
|
|
26
|
+
name: user.name ?? undefined,
|
|
27
|
+
phone: user.phone ?? null,
|
|
28
|
+
profilePictureId: user.profilePictureId ?? null
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function toCompanyInfo(company) {
|
|
32
|
+
return {
|
|
33
|
+
id: company.id,
|
|
34
|
+
name: company.name,
|
|
35
|
+
slug: company.slug,
|
|
36
|
+
logoId: company.logoId ?? null
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function toBranchInfo(branch) {
|
|
40
|
+
return {
|
|
41
|
+
id: branch.id,
|
|
42
|
+
name: branch.name,
|
|
43
|
+
slug: branch.slug,
|
|
44
|
+
logoId: branch.logoId ?? null
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
const _authresponsemapper = require("./auth-response.mapper");
|
|
6
|
+
function buildUser(overrides = {}) {
|
|
7
|
+
return {
|
|
8
|
+
id: 'user-uuid-1',
|
|
9
|
+
email: 'user@example.com',
|
|
10
|
+
name: 'Test User',
|
|
11
|
+
phone: null,
|
|
12
|
+
profilePictureId: null,
|
|
13
|
+
isActive: true,
|
|
14
|
+
emailVerified: false,
|
|
15
|
+
phoneVerified: false,
|
|
16
|
+
lastLoginAt: null,
|
|
17
|
+
additionalFields: null,
|
|
18
|
+
createdAt: new Date(),
|
|
19
|
+
updatedAt: new Date(),
|
|
20
|
+
deletedAt: null,
|
|
21
|
+
createdById: null,
|
|
22
|
+
updatedById: null,
|
|
23
|
+
deletedById: null,
|
|
24
|
+
password: null,
|
|
25
|
+
...overrides
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function buildCompany(overrides = {}) {
|
|
29
|
+
return {
|
|
30
|
+
id: 'company-uuid-1',
|
|
31
|
+
name: 'Acme Corp',
|
|
32
|
+
slug: 'acme-corp',
|
|
33
|
+
logoId: null,
|
|
34
|
+
readOnly: false,
|
|
35
|
+
address: null,
|
|
36
|
+
phone: null,
|
|
37
|
+
email: null,
|
|
38
|
+
website: null,
|
|
39
|
+
serial: null,
|
|
40
|
+
isActive: true,
|
|
41
|
+
settings: null,
|
|
42
|
+
additionalFields: null,
|
|
43
|
+
branches: [],
|
|
44
|
+
...overrides
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function buildBranch(overrides = {}) {
|
|
48
|
+
return {
|
|
49
|
+
id: 'branch-uuid-1',
|
|
50
|
+
name: 'Main Branch',
|
|
51
|
+
slug: 'main-branch',
|
|
52
|
+
logoId: null,
|
|
53
|
+
readOnly: false,
|
|
54
|
+
address: null,
|
|
55
|
+
phone: null,
|
|
56
|
+
email: null,
|
|
57
|
+
serial: null,
|
|
58
|
+
isActive: true,
|
|
59
|
+
additionalFields: null,
|
|
60
|
+
parentId: null,
|
|
61
|
+
parent: null,
|
|
62
|
+
children: [],
|
|
63
|
+
companyId: 'company-uuid-1',
|
|
64
|
+
company: buildCompany(),
|
|
65
|
+
...overrides
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
describe('auth-response.mapper', ()=>{
|
|
69
|
+
describe('toUserInfo', ()=>{
|
|
70
|
+
it('maps all user fields', ()=>{
|
|
71
|
+
const user = buildUser({
|
|
72
|
+
name: 'John Doe',
|
|
73
|
+
phone: '+123',
|
|
74
|
+
profilePictureId: 'pic-1'
|
|
75
|
+
});
|
|
76
|
+
const result = (0, _authresponsemapper.toUserInfo)(user);
|
|
77
|
+
expect(result).toEqual({
|
|
78
|
+
id: 'user-uuid-1',
|
|
79
|
+
email: 'user@example.com',
|
|
80
|
+
name: 'John Doe',
|
|
81
|
+
phone: '+123',
|
|
82
|
+
profilePictureId: 'pic-1'
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
it('defaults name to undefined and nullable fields to null when absent', ()=>{
|
|
86
|
+
const user = buildUser({
|
|
87
|
+
name: null,
|
|
88
|
+
phone: null,
|
|
89
|
+
profilePictureId: null
|
|
90
|
+
});
|
|
91
|
+
const result = (0, _authresponsemapper.toUserInfo)(user);
|
|
92
|
+
expect(result.name).toBeUndefined();
|
|
93
|
+
expect(result.phone).toBeNull();
|
|
94
|
+
expect(result.profilePictureId).toBeNull();
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
describe('toCompanyInfo', ()=>{
|
|
98
|
+
it('maps all company fields', ()=>{
|
|
99
|
+
const company = buildCompany({
|
|
100
|
+
logoId: 'logo-1'
|
|
101
|
+
});
|
|
102
|
+
const result = (0, _authresponsemapper.toCompanyInfo)(company);
|
|
103
|
+
expect(result).toEqual({
|
|
104
|
+
id: 'company-uuid-1',
|
|
105
|
+
name: 'Acme Corp',
|
|
106
|
+
slug: 'acme-corp',
|
|
107
|
+
logoId: 'logo-1'
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
it('defaults logoId to null when absent', ()=>{
|
|
111
|
+
const company = buildCompany({
|
|
112
|
+
logoId: null
|
|
113
|
+
});
|
|
114
|
+
const result = (0, _authresponsemapper.toCompanyInfo)(company);
|
|
115
|
+
expect(result.logoId).toBeNull();
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
describe('toBranchInfo', ()=>{
|
|
119
|
+
it('maps all branch fields', ()=>{
|
|
120
|
+
const branch = buildBranch({
|
|
121
|
+
logoId: 'logo-2'
|
|
122
|
+
});
|
|
123
|
+
const result = (0, _authresponsemapper.toBranchInfo)(branch);
|
|
124
|
+
expect(result).toEqual({
|
|
125
|
+
id: 'branch-uuid-1',
|
|
126
|
+
name: 'Main Branch',
|
|
127
|
+
slug: 'main-branch',
|
|
128
|
+
logoId: 'logo-2'
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
it('defaults logoId to null when absent', ()=>{
|
|
132
|
+
const branch = buildBranch({
|
|
133
|
+
logoId: null
|
|
134
|
+
});
|
|
135
|
+
const result = (0, _authresponsemapper.toBranchInfo)(branch);
|
|
136
|
+
expect(result.logoId).toBeNull();
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
_export_star(require("./auth-response.mapper"), exports);
|
|
6
|
+
_export_star(require("./token.helper"), exports);
|
|
7
|
+
function _export_star(from, to) {
|
|
8
|
+
Object.keys(from).forEach(function(k) {
|
|
9
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
|
|
10
|
+
Object.defineProperty(to, k, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function() {
|
|
13
|
+
return from[k];
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
return from;
|
|
19
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
get generateTokens () {
|
|
13
|
+
return generateTokens;
|
|
14
|
+
},
|
|
15
|
+
get parseExpiryToMs () {
|
|
16
|
+
return parseExpiryToMs;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
function generateTokens(jwtService, authConfig, payload) {
|
|
20
|
+
const accessToken = jwtService.sign({
|
|
21
|
+
...payload,
|
|
22
|
+
type: 'access'
|
|
23
|
+
}, {
|
|
24
|
+
secret: authConfig.getJwtSecret(),
|
|
25
|
+
expiresIn: authConfig.getJwtExpiration(),
|
|
26
|
+
algorithm: 'HS256'
|
|
27
|
+
});
|
|
28
|
+
const refreshToken = jwtService.sign({
|
|
29
|
+
...payload,
|
|
30
|
+
type: 'refresh'
|
|
31
|
+
}, {
|
|
32
|
+
secret: authConfig.getRefreshTokenSecret(),
|
|
33
|
+
expiresIn: authConfig.getRefreshTokenExpiration(),
|
|
34
|
+
algorithm: 'HS256'
|
|
35
|
+
});
|
|
36
|
+
return {
|
|
37
|
+
accessToken,
|
|
38
|
+
refreshToken
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function parseExpiryToMs(expiry) {
|
|
42
|
+
const match = expiry.match(/^(\d+)([smhd])$/);
|
|
43
|
+
if (!match) return 7 * 24 * 60 * 60 * 1000;
|
|
44
|
+
const value = parseInt(match[1], 10);
|
|
45
|
+
const unit = match[2];
|
|
46
|
+
switch(unit){
|
|
47
|
+
case 's':
|
|
48
|
+
return value * 1000;
|
|
49
|
+
case 'm':
|
|
50
|
+
return value * 60 * 1000;
|
|
51
|
+
case 'h':
|
|
52
|
+
return value * 60 * 60 * 1000;
|
|
53
|
+
case 'd':
|
|
54
|
+
return value * 24 * 60 * 60 * 1000;
|
|
55
|
+
default:
|
|
56
|
+
return 7 * 24 * 60 * 60 * 1000;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
const _tokenhelper = require("./token.helper");
|
|
6
|
+
describe('token.helper', ()=>{
|
|
7
|
+
describe('generateTokens', ()=>{
|
|
8
|
+
let mockJwtService;
|
|
9
|
+
let mockAuthConfig;
|
|
10
|
+
beforeEach(()=>{
|
|
11
|
+
mockJwtService = {
|
|
12
|
+
sign: jest.fn()
|
|
13
|
+
};
|
|
14
|
+
mockAuthConfig = {
|
|
15
|
+
getJwtSecret: jest.fn().mockReturnValue('jwt-secret'),
|
|
16
|
+
getJwtExpiration: jest.fn().mockReturnValue('1h'),
|
|
17
|
+
getRefreshTokenSecret: jest.fn().mockReturnValue('refresh-secret'),
|
|
18
|
+
getRefreshTokenExpiration: jest.fn().mockReturnValue('7d')
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
it('signs an access token and a refresh token with distinct secrets', ()=>{
|
|
22
|
+
mockJwtService.sign.mockReturnValueOnce('access-token-value').mockReturnValueOnce('refresh-token-value');
|
|
23
|
+
const result = (0, _tokenhelper.generateTokens)(mockJwtService, mockAuthConfig, {
|
|
24
|
+
id: 'user-1',
|
|
25
|
+
email: 'user@example.com',
|
|
26
|
+
type: 'access'
|
|
27
|
+
});
|
|
28
|
+
expect(result).toEqual({
|
|
29
|
+
accessToken: 'access-token-value',
|
|
30
|
+
refreshToken: 'refresh-token-value'
|
|
31
|
+
});
|
|
32
|
+
expect(mockJwtService.sign).toHaveBeenNthCalledWith(1, expect.objectContaining({
|
|
33
|
+
id: 'user-1',
|
|
34
|
+
email: 'user@example.com',
|
|
35
|
+
type: 'access'
|
|
36
|
+
}), expect.objectContaining({
|
|
37
|
+
secret: 'jwt-secret',
|
|
38
|
+
expiresIn: '1h',
|
|
39
|
+
algorithm: 'HS256'
|
|
40
|
+
}));
|
|
41
|
+
expect(mockJwtService.sign).toHaveBeenNthCalledWith(2, expect.objectContaining({
|
|
42
|
+
id: 'user-1',
|
|
43
|
+
email: 'user@example.com',
|
|
44
|
+
type: 'refresh'
|
|
45
|
+
}), expect.objectContaining({
|
|
46
|
+
secret: 'refresh-secret',
|
|
47
|
+
expiresIn: '7d',
|
|
48
|
+
algorithm: 'HS256'
|
|
49
|
+
}));
|
|
50
|
+
});
|
|
51
|
+
it('includes companyId and branchId in the signed payload when provided', ()=>{
|
|
52
|
+
mockJwtService.sign.mockReturnValue('token');
|
|
53
|
+
(0, _tokenhelper.generateTokens)(mockJwtService, mockAuthConfig, {
|
|
54
|
+
id: 'user-1',
|
|
55
|
+
email: 'user@example.com',
|
|
56
|
+
type: 'access',
|
|
57
|
+
companyId: 'company-1',
|
|
58
|
+
branchId: 'branch-1'
|
|
59
|
+
});
|
|
60
|
+
expect(mockJwtService.sign).toHaveBeenCalledWith(expect.objectContaining({
|
|
61
|
+
companyId: 'company-1',
|
|
62
|
+
branchId: 'branch-1'
|
|
63
|
+
}), expect.anything());
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
describe('parseExpiryToMs', ()=>{
|
|
67
|
+
it('parses seconds', ()=>{
|
|
68
|
+
expect((0, _tokenhelper.parseExpiryToMs)('30s')).toBe(30 * 1000);
|
|
69
|
+
});
|
|
70
|
+
it('parses minutes', ()=>{
|
|
71
|
+
expect((0, _tokenhelper.parseExpiryToMs)('15m')).toBe(15 * 60 * 1000);
|
|
72
|
+
});
|
|
73
|
+
it('parses hours', ()=>{
|
|
74
|
+
expect((0, _tokenhelper.parseExpiryToMs)('2h')).toBe(2 * 60 * 60 * 1000);
|
|
75
|
+
});
|
|
76
|
+
it('parses days', ()=>{
|
|
77
|
+
expect((0, _tokenhelper.parseExpiryToMs)('7d')).toBe(7 * 24 * 60 * 60 * 1000);
|
|
78
|
+
});
|
|
79
|
+
it('falls back to 7 days for an unparsable value', ()=>{
|
|
80
|
+
expect((0, _tokenhelper.parseExpiryToMs)('not-a-duration')).toBe(7 * 24 * 60 * 60 * 1000);
|
|
81
|
+
});
|
|
82
|
+
it('falls back to 7 days for an unknown unit', ()=>{
|
|
83
|
+
expect((0, _tokenhelper.parseExpiryToMs)('5y')).toBe(7 * 24 * 60 * 60 * 1000);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
});
|
package/cjs/index.js
CHANGED
|
@@ -8,6 +8,7 @@ _export_star(require("./docs"), exports);
|
|
|
8
8
|
_export_star(require("./dtos"), exports);
|
|
9
9
|
_export_star(require("./entities"), exports);
|
|
10
10
|
_export_star(require("./enums"), exports);
|
|
11
|
+
_export_star(require("./helpers"), exports);
|
|
11
12
|
_export_star(require("./interceptors"), exports);
|
|
12
13
|
_export_star(require("./interfaces"), exports);
|
|
13
14
|
_export_star(require("./modules"), exports);
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
const _rxjs = require("rxjs");
|
|
6
|
+
const _cleartokeninterceptor = require("./clear-token.interceptor");
|
|
7
|
+
const _config = require("../config");
|
|
8
|
+
function buildContext(req, res) {
|
|
9
|
+
return {
|
|
10
|
+
switchToHttp: ()=>({
|
|
11
|
+
getRequest: ()=>req,
|
|
12
|
+
getResponse: ()=>res
|
|
13
|
+
})
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function buildCallHandler(value) {
|
|
17
|
+
return {
|
|
18
|
+
handle: ()=>(0, _rxjs.of)(value)
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
describe('ClearToken', ()=>{
|
|
22
|
+
let mockAuthConfig;
|
|
23
|
+
beforeEach(()=>{
|
|
24
|
+
mockAuthConfig = {
|
|
25
|
+
getRefreshTokenCookieName: jest.fn().mockReturnValue('custom_refresh_token')
|
|
26
|
+
};
|
|
27
|
+
});
|
|
28
|
+
it('clears the cookie for a browser request', (done)=>{
|
|
29
|
+
const interceptor = new _cleartokeninterceptor.ClearToken(mockAuthConfig);
|
|
30
|
+
const clearCookie = jest.fn();
|
|
31
|
+
const req = {
|
|
32
|
+
headers: {
|
|
33
|
+
'user-agent': 'Mozilla/5.0 Chrome/1.0'
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
const res = {
|
|
37
|
+
clearCookie
|
|
38
|
+
};
|
|
39
|
+
interceptor.intercept(buildContext(req, res), buildCallHandler({
|
|
40
|
+
success: true
|
|
41
|
+
})).subscribe((result)=>{
|
|
42
|
+
expect(clearCookie).toHaveBeenCalledWith('custom_refresh_token', expect.objectContaining({
|
|
43
|
+
httpOnly: true,
|
|
44
|
+
path: '/'
|
|
45
|
+
}));
|
|
46
|
+
expect(result).toEqual({
|
|
47
|
+
success: true
|
|
48
|
+
});
|
|
49
|
+
done();
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
it('does not clear the cookie for a non-browser (API) request', (done)=>{
|
|
53
|
+
const interceptor = new _cleartokeninterceptor.ClearToken(mockAuthConfig);
|
|
54
|
+
const clearCookie = jest.fn();
|
|
55
|
+
const req = {
|
|
56
|
+
headers: {
|
|
57
|
+
'user-agent': 'PostmanRuntime/7.0'
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const res = {
|
|
61
|
+
clearCookie
|
|
62
|
+
};
|
|
63
|
+
interceptor.intercept(buildContext(req, res), buildCallHandler({
|
|
64
|
+
success: true
|
|
65
|
+
})).subscribe(()=>{
|
|
66
|
+
expect(clearCookie).not.toHaveBeenCalled();
|
|
67
|
+
done();
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
it('falls back to the default cookie name when authConfig is unavailable', (done)=>{
|
|
71
|
+
const interceptor = new _cleartokeninterceptor.ClearToken(undefined);
|
|
72
|
+
const clearCookie = jest.fn();
|
|
73
|
+
const req = {
|
|
74
|
+
headers: {
|
|
75
|
+
'user-agent': 'Mozilla/5.0 Chrome/1.0'
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const res = {
|
|
79
|
+
clearCookie
|
|
80
|
+
};
|
|
81
|
+
interceptor.intercept(buildContext(req, res), buildCallHandler({
|
|
82
|
+
success: true
|
|
83
|
+
})).subscribe(()=>{
|
|
84
|
+
expect(clearCookie).toHaveBeenCalledWith(_config.DEFAULT_COOKIE_NAME, expect.anything());
|
|
85
|
+
done();
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
it('passes through the emitted value unchanged', (done)=>{
|
|
89
|
+
const interceptor = new _cleartokeninterceptor.ClearToken(mockAuthConfig);
|
|
90
|
+
const req = {
|
|
91
|
+
headers: {
|
|
92
|
+
'user-agent': 'PostmanRuntime/7.0'
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
const res = {
|
|
96
|
+
clearCookie: jest.fn()
|
|
97
|
+
};
|
|
98
|
+
const payload = {
|
|
99
|
+
success: true,
|
|
100
|
+
message: 'Logged out successfully'
|
|
101
|
+
};
|
|
102
|
+
interceptor.intercept(buildContext(req, res), buildCallHandler(payload)).subscribe((result)=>{
|
|
103
|
+
expect(result).toBe(payload);
|
|
104
|
+
done();
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
});
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
const _common = require("@nestjs/common");
|
|
6
|
+
const _rxjs = require("rxjs");
|
|
7
|
+
const _settokeninterceptor = require("./set-token.interceptor");
|
|
8
|
+
const _config = require("../config");
|
|
9
|
+
function buildContext(req, res) {
|
|
10
|
+
return {
|
|
11
|
+
switchToHttp: ()=>({
|
|
12
|
+
getRequest: ()=>req,
|
|
13
|
+
getResponse: ()=>res
|
|
14
|
+
})
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function buildCallHandler(value) {
|
|
18
|
+
return {
|
|
19
|
+
handle: ()=>(0, _rxjs.of)(value)
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
const browserReq = {
|
|
23
|
+
headers: {
|
|
24
|
+
'user-agent': 'Mozilla/5.0 Chrome/1.0'
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
const apiReq = {
|
|
28
|
+
headers: {
|
|
29
|
+
'user-agent': 'PostmanRuntime/7.0'
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
describe('SetToken', ()=>{
|
|
33
|
+
let mockAuthConfig;
|
|
34
|
+
beforeEach(()=>{
|
|
35
|
+
mockAuthConfig = {
|
|
36
|
+
getRefreshTokenCookieName: jest.fn().mockReturnValue('custom_refresh_token'),
|
|
37
|
+
getRefreshTokenExpiration: jest.fn().mockReturnValue('7d')
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
it('sets a refresh token cookie and strips refreshToken from data for browser requests', (done)=>{
|
|
41
|
+
const interceptor = new _settokeninterceptor.SetToken(mockAuthConfig);
|
|
42
|
+
const cookie = jest.fn();
|
|
43
|
+
const res = {
|
|
44
|
+
cookie
|
|
45
|
+
};
|
|
46
|
+
const payload = {
|
|
47
|
+
success: true,
|
|
48
|
+
data: {
|
|
49
|
+
accessToken: 'access-1',
|
|
50
|
+
refreshToken: 'refresh-1',
|
|
51
|
+
user: {
|
|
52
|
+
id: 'u1'
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
interceptor.intercept(buildContext(browserReq, res), buildCallHandler(payload)).subscribe((result)=>{
|
|
57
|
+
expect(cookie).toHaveBeenCalledWith('custom_refresh_token', 'refresh-1', expect.objectContaining({
|
|
58
|
+
httpOnly: true,
|
|
59
|
+
path: '/'
|
|
60
|
+
}));
|
|
61
|
+
expect(result).toEqual({
|
|
62
|
+
success: true,
|
|
63
|
+
data: {
|
|
64
|
+
accessToken: 'access-1',
|
|
65
|
+
user: {
|
|
66
|
+
id: 'u1'
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
expect(result.data.refreshToken).toBeUndefined();
|
|
71
|
+
done();
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
it('does not set a cookie or modify data for non-browser (API) requests', (done)=>{
|
|
75
|
+
const interceptor = new _settokeninterceptor.SetToken(mockAuthConfig);
|
|
76
|
+
const cookie = jest.fn();
|
|
77
|
+
const res = {
|
|
78
|
+
cookie
|
|
79
|
+
};
|
|
80
|
+
const payload = {
|
|
81
|
+
success: true,
|
|
82
|
+
data: {
|
|
83
|
+
accessToken: 'access-1',
|
|
84
|
+
refreshToken: 'refresh-1'
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
interceptor.intercept(buildContext(apiReq, res), buildCallHandler(payload)).subscribe((result)=>{
|
|
88
|
+
expect(cookie).not.toHaveBeenCalled();
|
|
89
|
+
expect(result).toBe(payload);
|
|
90
|
+
done();
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
it('passes through non-object values unchanged', (done)=>{
|
|
94
|
+
const interceptor = new _settokeninterceptor.SetToken(mockAuthConfig);
|
|
95
|
+
const res = {
|
|
96
|
+
cookie: jest.fn()
|
|
97
|
+
};
|
|
98
|
+
interceptor.intercept(buildContext(browserReq, res), buildCallHandler('plain-string')).subscribe((result)=>{
|
|
99
|
+
expect(result).toBe('plain-string');
|
|
100
|
+
done();
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
it('passes through null values unchanged', (done)=>{
|
|
104
|
+
const interceptor = new _settokeninterceptor.SetToken(mockAuthConfig);
|
|
105
|
+
const res = {
|
|
106
|
+
cookie: jest.fn()
|
|
107
|
+
};
|
|
108
|
+
interceptor.intercept(buildContext(browserReq, res), buildCallHandler(null)).subscribe((result)=>{
|
|
109
|
+
expect(result).toBeNull();
|
|
110
|
+
done();
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
it('passes through values without a refreshToken in data unchanged', (done)=>{
|
|
114
|
+
const interceptor = new _settokeninterceptor.SetToken(mockAuthConfig);
|
|
115
|
+
const cookie = jest.fn();
|
|
116
|
+
const res = {
|
|
117
|
+
cookie
|
|
118
|
+
};
|
|
119
|
+
const payload = {
|
|
120
|
+
success: true,
|
|
121
|
+
data: {
|
|
122
|
+
accessToken: 'access-1'
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
interceptor.intercept(buildContext(browserReq, res), buildCallHandler(payload)).subscribe((result)=>{
|
|
126
|
+
expect(cookie).not.toHaveBeenCalled();
|
|
127
|
+
expect(result).toBe(payload);
|
|
128
|
+
done();
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
it('falls back to the default cookie name when authConfig is unavailable', (done)=>{
|
|
132
|
+
const interceptor = new _settokeninterceptor.SetToken(undefined);
|
|
133
|
+
const cookie = jest.fn();
|
|
134
|
+
const res = {
|
|
135
|
+
cookie
|
|
136
|
+
};
|
|
137
|
+
const payload = {
|
|
138
|
+
data: {
|
|
139
|
+
refreshToken: 'refresh-1'
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
interceptor.intercept(buildContext(browserReq, res), buildCallHandler(payload)).subscribe(()=>{
|
|
143
|
+
expect(cookie).toHaveBeenCalledWith(_config.DEFAULT_COOKIE_NAME, 'refresh-1', expect.anything());
|
|
144
|
+
done();
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
it('throws PayloadTooLargeException when the refresh token exceeds the max cookie size', (done)=>{
|
|
148
|
+
const interceptor = new _settokeninterceptor.SetToken(mockAuthConfig);
|
|
149
|
+
const res = {
|
|
150
|
+
cookie: jest.fn()
|
|
151
|
+
};
|
|
152
|
+
const oversizedToken = 'a'.repeat(4000);
|
|
153
|
+
const payload = {
|
|
154
|
+
data: {
|
|
155
|
+
refreshToken: oversizedToken
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
interceptor.intercept(buildContext(browserReq, res), buildCallHandler(payload)).subscribe({
|
|
159
|
+
error: (err)=>{
|
|
160
|
+
expect(err).toBeInstanceOf(_common.PayloadTooLargeException);
|
|
161
|
+
done();
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
});
|