@flusys/nestjs-auth 7.1.0 → 8.0.0
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/README.md +1 -1
- package/config/index.d.ts +3 -3
- package/config/message-keys.d.ts +4 -0
- package/controllers/authentication.controller.d.ts +5 -5
- package/controllers/branch.controller.d.ts +2 -2
- package/controllers/company-selection.controller.d.ts +4 -4
- package/controllers/company.controller.d.ts +2 -2
- package/controllers/email-verification.controller.d.ts +6 -6
- package/controllers/index.d.ts +9 -9
- package/controllers/social-auth-config.controller.d.ts +2 -2
- package/controllers/social-auth.controller.d.ts +3 -3
- package/controllers/user-permission.controller.d.ts +7 -4
- package/controllers/user.controller.d.ts +7 -7
- package/docs/auth-swagger.config.d.ts +2 -2
- package/docs/index.d.ts +1 -1
- package/dtos/index.d.ts +6 -6
- package/dtos/social-auth.dto.d.ts +1 -1
- package/dtos/user-permission.dto.d.ts +30 -0
- package/entities/company-branch.entity.d.ts +1 -1
- package/entities/company.entity.d.ts +1 -1
- package/entities/index.d.ts +14 -14
- package/entities/pending-registration.entity.d.ts +1 -1
- package/entities/user-company-permission.entity.d.ts +4 -4
- package/enums/index.d.ts +2 -2
- package/fesm/118.js +1404 -0
- package/fesm/120.js +2265 -0
- package/fesm/24.js +4422 -0
- package/fesm/370.js +1163 -0
- package/fesm/446.js +877 -0
- package/fesm/chunks/117.js +69 -0
- package/fesm/config/index.js +246 -3
- package/fesm/controllers/index.js +27 -9
- package/fesm/docs/index.js +479 -1
- package/fesm/dtos/index.js +135 -6
- package/fesm/entities/index.js +68 -22
- package/fesm/enums/index.js +53 -2
- package/fesm/helpers/index.js +115 -2
- package/fesm/index.js +1455 -13
- package/fesm/interceptors/index.js +510 -2
- package/fesm/interfaces/index.js +75 -8
- package/fesm/modules/index.js +546 -1
- package/fesm/runtime.js +134 -0
- package/fesm/services/index.js +67 -11
- package/helpers/auth-response.mapper.d.ts +3 -3
- package/helpers/index.d.ts +2 -2
- package/helpers/token.helper.d.ts +2 -2
- package/index.d.ts +13 -13
- package/interceptors/clear-token.interceptor.d.ts +2 -2
- package/interceptors/index.d.ts +2 -2
- package/interceptors/set-token.interceptor.d.ts +2 -2
- package/interfaces/auth-module-options.interface.d.ts +3 -3
- package/interfaces/authentication.interface.d.ts +3 -3
- package/interfaces/company-branch.interface.d.ts +2 -2
- package/interfaces/company.interface.d.ts +1 -1
- package/interfaces/index.d.ts +8 -8
- package/interfaces/social-auth.interface.d.ts +2 -2
- package/interfaces/user-enricher.interface.d.ts +3 -3
- package/modules/auth.module.d.ts +4 -4
- package/modules/index.d.ts +1 -1
- package/package.json +32 -39
- package/providers/facebook.social-provider.d.ts +2 -2
- package/providers/google.social-provider.d.ts +2 -2
- package/providers/index.d.ts +1 -1
- package/providers/linkedin.social-provider.d.ts +2 -2
- package/providers/microsoft.social-provider.d.ts +2 -2
- package/providers/social-provider.registry.d.ts +2 -2
- package/services/auth-config.service.d.ts +3 -3
- package/services/auth-datasource.provider.d.ts +3 -3
- package/services/auth-email.service.d.ts +3 -3
- package/services/authentication.service.d.ts +12 -12
- package/services/branch.service.d.ts +7 -7
- package/services/company.service.d.ts +6 -6
- package/services/index.d.ts +11 -11
- package/services/social-auth-config.service.d.ts +6 -6
- package/services/social-auth.service.d.ts +5 -5
- package/services/user-permission.service.d.ts +10 -4
- package/services/user.service.d.ts +7 -7
- package/strategies/jwt.strategy.d.ts +4 -4
- package/validators/password.validator.d.ts +1 -1
- package/cjs/config/auth.constants.js +0 -49
- package/cjs/config/event-actions.js +0 -47
- package/cjs/config/index.js +0 -20
- package/cjs/config/message-keys.js +0 -135
- package/cjs/controllers/authentication.controller.js +0 -427
- package/cjs/controllers/authentication.controller.spec.js +0 -219
- package/cjs/controllers/branch.controller.js +0 -102
- package/cjs/controllers/branch.controller.spec.js +0 -134
- package/cjs/controllers/company-selection.controller.js +0 -198
- package/cjs/controllers/company-selection.controller.spec.js +0 -84
- package/cjs/controllers/company.controller.js +0 -102
- package/cjs/controllers/company.controller.spec.js +0 -131
- package/cjs/controllers/email-verification.controller.js +0 -224
- package/cjs/controllers/email-verification.controller.spec.js +0 -142
- package/cjs/controllers/index.js +0 -26
- package/cjs/controllers/social-auth-config.controller.js +0 -107
- package/cjs/controllers/social-auth.controller.js +0 -175
- package/cjs/controllers/social-auth.controller.spec.js +0 -79
- package/cjs/controllers/user-permission.controller.js +0 -223
- package/cjs/controllers/user-permission.controller.spec.js +0 -138
- package/cjs/controllers/user.controller.js +0 -438
- package/cjs/controllers/user.controller.spec.js +0 -204
- package/cjs/docs/auth-swagger.config.js +0 -461
- package/cjs/docs/auth-swagger.config.spec.js +0 -130
- package/cjs/docs/index.js +0 -18
- package/cjs/dtos/authentication.dto.js +0 -755
- package/cjs/dtos/company-branch.dto.js +0 -294
- package/cjs/dtos/company.dto.js +0 -298
- package/cjs/dtos/index.js +0 -23
- package/cjs/dtos/social-auth.dto.js +0 -318
- package/cjs/dtos/user-permission.dto.js +0 -358
- package/cjs/dtos/user.dto.js +0 -413
- package/cjs/entities/app-user.entity.js +0 -25
- package/cjs/entities/company-branch.entity.js +0 -163
- package/cjs/entities/company.entity.js +0 -146
- package/cjs/entities/index.js +0 -67
- package/cjs/entities/index.spec.js +0 -84
- package/cjs/entities/pending-registration.entity.js +0 -135
- package/cjs/entities/social-auth-config.entity.js +0 -102
- package/cjs/entities/user-company-permission.entity.js +0 -153
- package/cjs/entities/user-token.entity.js +0 -120
- package/cjs/enums/index.js +0 -19
- package/cjs/enums/social-provider-type.enum.js +0 -17
- package/cjs/enums/user-org-permission-type.enum.js +0 -15
- package/cjs/helpers/auth-response.mapper.js +0 -46
- package/cjs/helpers/auth-response.mapper.spec.js +0 -139
- package/cjs/helpers/index.js +0 -19
- package/cjs/helpers/token.helper.js +0 -58
- package/cjs/helpers/token.helper.spec.js +0 -86
- package/cjs/index.js +0 -30
- package/cjs/interceptors/clear-token.interceptor.js +0 -73
- package/cjs/interceptors/clear-token.interceptor.spec.js +0 -107
- package/cjs/interceptors/index.js +0 -19
- package/cjs/interceptors/set-token.interceptor.js +0 -107
- package/cjs/interceptors/set-token.interceptor.spec.js +0 -165
- package/cjs/interfaces/auth-email-provider.interface.js +0 -45
- package/cjs/interfaces/auth-module-options.interface.js +0 -4
- package/cjs/interfaces/authentication.interface.js +0 -4
- package/cjs/interfaces/company-branch.interface.js +0 -4
- package/cjs/interfaces/company.interface.js +0 -4
- package/cjs/interfaces/index.js +0 -25
- package/cjs/interfaces/social-auth.interface.js +0 -4
- package/cjs/interfaces/user-enricher.interface.js +0 -4
- package/cjs/interfaces/user.interface.js +0 -4
- package/cjs/modules/auth.module.js +0 -208
- package/cjs/modules/auth.module.spec.js +0 -258
- package/cjs/modules/index.js +0 -18
- package/cjs/providers/facebook.social-provider.js +0 -73
- package/cjs/providers/google.social-provider.js +0 -68
- package/cjs/providers/index.js +0 -18
- package/cjs/providers/linkedin.social-provider.js +0 -66
- package/cjs/providers/microsoft.social-provider.js +0 -78
- package/cjs/providers/oauth-http.util.js +0 -107
- package/cjs/providers/social-provider.registry.js +0 -24
- package/cjs/providers/social-provider.registry.spec.js +0 -135
- package/cjs/services/auth-config.service.js +0 -113
- package/cjs/services/auth-config.service.spec.js +0 -100
- package/cjs/services/auth-datasource.provider.js +0 -138
- package/cjs/services/auth-datasource.provider.spec.js +0 -191
- package/cjs/services/auth-email.service.js +0 -539
- package/cjs/services/auth-email.service.spec.js +0 -456
- package/cjs/services/authentication.service.js +0 -1203
- package/cjs/services/authentication.service.spec.js +0 -1020
- package/cjs/services/branch.service.js +0 -153
- package/cjs/services/branch.service.spec.js +0 -169
- package/cjs/services/company-selection-session.service.js +0 -121
- package/cjs/services/company-selection-session.service.spec.js +0 -90
- package/cjs/services/company.service.js +0 -140
- package/cjs/services/company.service.spec.js +0 -127
- package/cjs/services/index.js +0 -28
- package/cjs/services/social-auth-config.service.js +0 -103
- package/cjs/services/social-auth-config.service.spec.js +0 -71
- package/cjs/services/social-auth.service.js +0 -310
- package/cjs/services/social-auth.service.spec.js +0 -310
- package/cjs/services/user-permission.service.js +0 -236
- package/cjs/services/user-permission.service.spec.js +0 -254
- package/cjs/services/user.service.js +0 -671
- package/cjs/services/user.service.spec.js +0 -718
- package/cjs/strategies/jwt.strategy.js +0 -140
- package/cjs/strategies/jwt.strategy.spec.js +0 -166
- package/cjs/validators/password.validator.js +0 -45
- package/cjs/validators/password.validator.spec.js +0 -161
- package/fesm/config/auth.constants.js +0 -14
- package/fesm/config/event-actions.js +0 -26
- package/fesm/config/message-keys.js +0 -99
- package/fesm/controllers/authentication.controller.js +0 -417
- package/fesm/controllers/authentication.controller.spec.js +0 -215
- package/fesm/controllers/branch.controller.js +0 -92
- package/fesm/controllers/branch.controller.spec.js +0 -130
- package/fesm/controllers/company-selection.controller.js +0 -188
- package/fesm/controllers/company-selection.controller.spec.js +0 -80
- package/fesm/controllers/company.controller.js +0 -92
- package/fesm/controllers/company.controller.spec.js +0 -127
- package/fesm/controllers/email-verification.controller.js +0 -214
- package/fesm/controllers/email-verification.controller.spec.js +0 -138
- package/fesm/controllers/social-auth-config.controller.js +0 -97
- package/fesm/controllers/social-auth.controller.js +0 -165
- package/fesm/controllers/social-auth.controller.spec.js +0 -75
- package/fesm/controllers/user-permission.controller.js +0 -213
- package/fesm/controllers/user-permission.controller.spec.js +0 -134
- package/fesm/controllers/user.controller.js +0 -428
- package/fesm/controllers/user.controller.spec.js +0 -200
- package/fesm/docs/auth-swagger.config.js +0 -454
- package/fesm/docs/auth-swagger.config.spec.js +0 -126
- package/fesm/dtos/authentication.dto.js +0 -683
- package/fesm/dtos/company-branch.dto.js +0 -279
- package/fesm/dtos/company.dto.js +0 -283
- package/fesm/dtos/social-auth.dto.js +0 -282
- package/fesm/dtos/user-permission.dto.js +0 -339
- package/fesm/dtos/user.dto.js +0 -391
- package/fesm/entities/app-user.entity.js +0 -15
- package/fesm/entities/company-branch.entity.js +0 -153
- package/fesm/entities/company.entity.js +0 -136
- package/fesm/entities/index.spec.js +0 -80
- package/fesm/entities/pending-registration.entity.js +0 -125
- package/fesm/entities/social-auth-config.entity.js +0 -92
- package/fesm/entities/user-company-permission.entity.js +0 -136
- package/fesm/entities/user-token.entity.js +0 -104
- package/fesm/enums/social-provider-type.enum.js +0 -7
- package/fesm/enums/user-org-permission-type.enum.js +0 -5
- package/fesm/helpers/auth-response.mapper.js +0 -25
- package/fesm/helpers/auth-response.mapper.spec.js +0 -135
- package/fesm/helpers/token.helper.js +0 -40
- package/fesm/helpers/token.helper.spec.js +0 -82
- package/fesm/interceptors/clear-token.interceptor.js +0 -63
- package/fesm/interceptors/clear-token.interceptor.spec.js +0 -103
- package/fesm/interceptors/set-token.interceptor.js +0 -98
- package/fesm/interceptors/set-token.interceptor.spec.js +0 -161
- package/fesm/interfaces/auth-email-provider.interface.js +0 -38
- package/fesm/interfaces/auth-module-options.interface.js +0 -3
- package/fesm/interfaces/authentication.interface.js +0 -1
- package/fesm/interfaces/company-branch.interface.js +0 -1
- package/fesm/interfaces/company.interface.js +0 -1
- package/fesm/interfaces/social-auth.interface.js +0 -1
- package/fesm/interfaces/user-enricher.interface.js +0 -4
- package/fesm/interfaces/user.interface.js +0 -1
- package/fesm/modules/auth.module.js +0 -198
- package/fesm/modules/auth.module.spec.js +0 -254
- package/fesm/providers/facebook.social-provider.js +0 -63
- package/fesm/providers/google.social-provider.js +0 -58
- package/fesm/providers/index.js +0 -1
- package/fesm/providers/linkedin.social-provider.js +0 -56
- package/fesm/providers/microsoft.social-provider.js +0 -68
- package/fesm/providers/oauth-http.util.js +0 -77
- package/fesm/providers/social-provider.registry.js +0 -14
- package/fesm/providers/social-provider.registry.spec.js +0 -131
- package/fesm/services/auth-config.service.js +0 -103
- package/fesm/services/auth-config.service.spec.js +0 -96
- package/fesm/services/auth-datasource.provider.js +0 -87
- package/fesm/services/auth-datasource.provider.spec.js +0 -187
- package/fesm/services/auth-email.service.js +0 -488
- package/fesm/services/auth-email.service.spec.js +0 -411
- package/fesm/services/authentication.service.js +0 -1152
- package/fesm/services/authentication.service.spec.js +0 -975
- package/fesm/services/branch.service.js +0 -143
- package/fesm/services/branch.service.spec.js +0 -165
- package/fesm/services/company-selection-session.service.js +0 -111
- package/fesm/services/company-selection-session.service.spec.js +0 -86
- package/fesm/services/company.service.js +0 -130
- package/fesm/services/company.service.spec.js +0 -123
- package/fesm/services/social-auth-config.service.js +0 -93
- package/fesm/services/social-auth-config.service.spec.js +0 -67
- package/fesm/services/social-auth.service.js +0 -300
- package/fesm/services/social-auth.service.spec.js +0 -265
- package/fesm/services/user-permission.service.js +0 -226
- package/fesm/services/user-permission.service.spec.js +0 -250
- package/fesm/services/user.service.js +0 -620
- package/fesm/services/user.service.spec.js +0 -673
- package/fesm/strategies/jwt.strategy.js +0 -130
- package/fesm/strategies/jwt.strategy.spec.js +0 -162
- package/fesm/validators/password.validator.js +0 -48
- package/fesm/validators/password.validator.spec.js +0 -157
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import { toBranchInfo, toCompanyInfo, toUserInfo } from './auth-response.mapper';
|
|
2
|
-
function buildUser(overrides = {}) {
|
|
3
|
-
return {
|
|
4
|
-
id: 'user-uuid-1',
|
|
5
|
-
email: 'user@example.com',
|
|
6
|
-
name: 'Test User',
|
|
7
|
-
phone: null,
|
|
8
|
-
profilePictureId: null,
|
|
9
|
-
isActive: true,
|
|
10
|
-
emailVerified: false,
|
|
11
|
-
phoneVerified: false,
|
|
12
|
-
lastLoginAt: null,
|
|
13
|
-
additionalFields: null,
|
|
14
|
-
createdAt: new Date(),
|
|
15
|
-
updatedAt: new Date(),
|
|
16
|
-
deletedAt: null,
|
|
17
|
-
createdById: null,
|
|
18
|
-
updatedById: null,
|
|
19
|
-
deletedById: null,
|
|
20
|
-
password: null,
|
|
21
|
-
...overrides
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
function buildCompany(overrides = {}) {
|
|
25
|
-
return {
|
|
26
|
-
id: 'company-uuid-1',
|
|
27
|
-
name: 'Acme Corp',
|
|
28
|
-
slug: 'acme-corp',
|
|
29
|
-
logoId: null,
|
|
30
|
-
readOnly: false,
|
|
31
|
-
address: null,
|
|
32
|
-
phone: null,
|
|
33
|
-
email: null,
|
|
34
|
-
website: null,
|
|
35
|
-
serial: null,
|
|
36
|
-
isActive: true,
|
|
37
|
-
settings: null,
|
|
38
|
-
additionalFields: null,
|
|
39
|
-
branches: [],
|
|
40
|
-
...overrides
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
function buildBranch(overrides = {}) {
|
|
44
|
-
return {
|
|
45
|
-
id: 'branch-uuid-1',
|
|
46
|
-
name: 'Main Branch',
|
|
47
|
-
slug: 'main-branch',
|
|
48
|
-
logoId: null,
|
|
49
|
-
readOnly: false,
|
|
50
|
-
address: null,
|
|
51
|
-
phone: null,
|
|
52
|
-
email: null,
|
|
53
|
-
serial: null,
|
|
54
|
-
isActive: true,
|
|
55
|
-
additionalFields: null,
|
|
56
|
-
parentId: null,
|
|
57
|
-
parent: null,
|
|
58
|
-
children: [],
|
|
59
|
-
companyId: 'company-uuid-1',
|
|
60
|
-
company: buildCompany(),
|
|
61
|
-
...overrides
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
describe('auth-response.mapper', ()=>{
|
|
65
|
-
describe('toUserInfo', ()=>{
|
|
66
|
-
it('maps all user fields', ()=>{
|
|
67
|
-
const user = buildUser({
|
|
68
|
-
name: 'John Doe',
|
|
69
|
-
phone: '+123',
|
|
70
|
-
profilePictureId: 'pic-1'
|
|
71
|
-
});
|
|
72
|
-
const result = toUserInfo(user);
|
|
73
|
-
expect(result).toEqual({
|
|
74
|
-
id: 'user-uuid-1',
|
|
75
|
-
email: 'user@example.com',
|
|
76
|
-
name: 'John Doe',
|
|
77
|
-
phone: '+123',
|
|
78
|
-
profilePictureId: 'pic-1'
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
it('defaults name to undefined and nullable fields to null when absent', ()=>{
|
|
82
|
-
const user = buildUser({
|
|
83
|
-
name: null,
|
|
84
|
-
phone: null,
|
|
85
|
-
profilePictureId: null
|
|
86
|
-
});
|
|
87
|
-
const result = toUserInfo(user);
|
|
88
|
-
expect(result.name).toBeUndefined();
|
|
89
|
-
expect(result.phone).toBeNull();
|
|
90
|
-
expect(result.profilePictureId).toBeNull();
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
describe('toCompanyInfo', ()=>{
|
|
94
|
-
it('maps all company fields', ()=>{
|
|
95
|
-
const company = buildCompany({
|
|
96
|
-
logoId: 'logo-1'
|
|
97
|
-
});
|
|
98
|
-
const result = toCompanyInfo(company);
|
|
99
|
-
expect(result).toEqual({
|
|
100
|
-
id: 'company-uuid-1',
|
|
101
|
-
name: 'Acme Corp',
|
|
102
|
-
slug: 'acme-corp',
|
|
103
|
-
logoId: 'logo-1'
|
|
104
|
-
});
|
|
105
|
-
});
|
|
106
|
-
it('defaults logoId to null when absent', ()=>{
|
|
107
|
-
const company = buildCompany({
|
|
108
|
-
logoId: null
|
|
109
|
-
});
|
|
110
|
-
const result = toCompanyInfo(company);
|
|
111
|
-
expect(result.logoId).toBeNull();
|
|
112
|
-
});
|
|
113
|
-
});
|
|
114
|
-
describe('toBranchInfo', ()=>{
|
|
115
|
-
it('maps all branch fields', ()=>{
|
|
116
|
-
const branch = buildBranch({
|
|
117
|
-
logoId: 'logo-2'
|
|
118
|
-
});
|
|
119
|
-
const result = toBranchInfo(branch);
|
|
120
|
-
expect(result).toEqual({
|
|
121
|
-
id: 'branch-uuid-1',
|
|
122
|
-
name: 'Main Branch',
|
|
123
|
-
slug: 'main-branch',
|
|
124
|
-
logoId: 'logo-2'
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
|
-
it('defaults logoId to null when absent', ()=>{
|
|
128
|
-
const branch = buildBranch({
|
|
129
|
-
logoId: null
|
|
130
|
-
});
|
|
131
|
-
const result = toBranchInfo(branch);
|
|
132
|
-
expect(result.logoId).toBeNull();
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
|
-
});
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
export function generateTokens(jwtService, authConfig, payload) {
|
|
2
|
-
const accessToken = jwtService.sign({
|
|
3
|
-
...payload,
|
|
4
|
-
type: 'access'
|
|
5
|
-
}, {
|
|
6
|
-
secret: authConfig.getJwtSecret(),
|
|
7
|
-
expiresIn: authConfig.getJwtExpiration(),
|
|
8
|
-
algorithm: 'HS256'
|
|
9
|
-
});
|
|
10
|
-
const refreshToken = jwtService.sign({
|
|
11
|
-
...payload,
|
|
12
|
-
type: 'refresh'
|
|
13
|
-
}, {
|
|
14
|
-
secret: authConfig.getRefreshTokenSecret(),
|
|
15
|
-
expiresIn: authConfig.getRefreshTokenExpiration(),
|
|
16
|
-
algorithm: 'HS256'
|
|
17
|
-
});
|
|
18
|
-
return {
|
|
19
|
-
accessToken,
|
|
20
|
-
refreshToken
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
export function parseExpiryToMs(expiry) {
|
|
24
|
-
const match = expiry.match(/^(\d+)([smhd])$/);
|
|
25
|
-
if (!match) return 7 * 24 * 60 * 60 * 1000;
|
|
26
|
-
const value = parseInt(match[1], 10);
|
|
27
|
-
const unit = match[2];
|
|
28
|
-
switch(unit){
|
|
29
|
-
case 's':
|
|
30
|
-
return value * 1000;
|
|
31
|
-
case 'm':
|
|
32
|
-
return value * 60 * 1000;
|
|
33
|
-
case 'h':
|
|
34
|
-
return value * 60 * 60 * 1000;
|
|
35
|
-
case 'd':
|
|
36
|
-
return value * 24 * 60 * 60 * 1000;
|
|
37
|
-
default:
|
|
38
|
-
return 7 * 24 * 60 * 60 * 1000;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { generateTokens, parseExpiryToMs } from './token.helper';
|
|
2
|
-
describe('token.helper', ()=>{
|
|
3
|
-
describe('generateTokens', ()=>{
|
|
4
|
-
let mockJwtService;
|
|
5
|
-
let mockAuthConfig;
|
|
6
|
-
beforeEach(()=>{
|
|
7
|
-
mockJwtService = {
|
|
8
|
-
sign: jest.fn()
|
|
9
|
-
};
|
|
10
|
-
mockAuthConfig = {
|
|
11
|
-
getJwtSecret: jest.fn().mockReturnValue('jwt-secret'),
|
|
12
|
-
getJwtExpiration: jest.fn().mockReturnValue('1h'),
|
|
13
|
-
getRefreshTokenSecret: jest.fn().mockReturnValue('refresh-secret'),
|
|
14
|
-
getRefreshTokenExpiration: jest.fn().mockReturnValue('7d')
|
|
15
|
-
};
|
|
16
|
-
});
|
|
17
|
-
it('signs an access token and a refresh token with distinct secrets', ()=>{
|
|
18
|
-
mockJwtService.sign.mockReturnValueOnce('access-token-value').mockReturnValueOnce('refresh-token-value');
|
|
19
|
-
const result = generateTokens(mockJwtService, mockAuthConfig, {
|
|
20
|
-
id: 'user-1',
|
|
21
|
-
email: 'user@example.com',
|
|
22
|
-
type: 'access'
|
|
23
|
-
});
|
|
24
|
-
expect(result).toEqual({
|
|
25
|
-
accessToken: 'access-token-value',
|
|
26
|
-
refreshToken: 'refresh-token-value'
|
|
27
|
-
});
|
|
28
|
-
expect(mockJwtService.sign).toHaveBeenNthCalledWith(1, expect.objectContaining({
|
|
29
|
-
id: 'user-1',
|
|
30
|
-
email: 'user@example.com',
|
|
31
|
-
type: 'access'
|
|
32
|
-
}), expect.objectContaining({
|
|
33
|
-
secret: 'jwt-secret',
|
|
34
|
-
expiresIn: '1h',
|
|
35
|
-
algorithm: 'HS256'
|
|
36
|
-
}));
|
|
37
|
-
expect(mockJwtService.sign).toHaveBeenNthCalledWith(2, expect.objectContaining({
|
|
38
|
-
id: 'user-1',
|
|
39
|
-
email: 'user@example.com',
|
|
40
|
-
type: 'refresh'
|
|
41
|
-
}), expect.objectContaining({
|
|
42
|
-
secret: 'refresh-secret',
|
|
43
|
-
expiresIn: '7d',
|
|
44
|
-
algorithm: 'HS256'
|
|
45
|
-
}));
|
|
46
|
-
});
|
|
47
|
-
it('includes companyId and branchId in the signed payload when provided', ()=>{
|
|
48
|
-
mockJwtService.sign.mockReturnValue('token');
|
|
49
|
-
generateTokens(mockJwtService, mockAuthConfig, {
|
|
50
|
-
id: 'user-1',
|
|
51
|
-
email: 'user@example.com',
|
|
52
|
-
type: 'access',
|
|
53
|
-
companyId: 'company-1',
|
|
54
|
-
branchId: 'branch-1'
|
|
55
|
-
});
|
|
56
|
-
expect(mockJwtService.sign).toHaveBeenCalledWith(expect.objectContaining({
|
|
57
|
-
companyId: 'company-1',
|
|
58
|
-
branchId: 'branch-1'
|
|
59
|
-
}), expect.anything());
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
describe('parseExpiryToMs', ()=>{
|
|
63
|
-
it('parses seconds', ()=>{
|
|
64
|
-
expect(parseExpiryToMs('30s')).toBe(30 * 1000);
|
|
65
|
-
});
|
|
66
|
-
it('parses minutes', ()=>{
|
|
67
|
-
expect(parseExpiryToMs('15m')).toBe(15 * 60 * 1000);
|
|
68
|
-
});
|
|
69
|
-
it('parses hours', ()=>{
|
|
70
|
-
expect(parseExpiryToMs('2h')).toBe(2 * 60 * 60 * 1000);
|
|
71
|
-
});
|
|
72
|
-
it('parses days', ()=>{
|
|
73
|
-
expect(parseExpiryToMs('7d')).toBe(7 * 24 * 60 * 60 * 1000);
|
|
74
|
-
});
|
|
75
|
-
it('falls back to 7 days for an unparsable value', ()=>{
|
|
76
|
-
expect(parseExpiryToMs('not-a-duration')).toBe(7 * 24 * 60 * 60 * 1000);
|
|
77
|
-
});
|
|
78
|
-
it('falls back to 7 days for an unknown unit', ()=>{
|
|
79
|
-
expect(parseExpiryToMs('5y')).toBe(7 * 24 * 60 * 60 * 1000);
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
});
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
function _define_property(obj, key, value) {
|
|
2
|
-
if (key in obj) {
|
|
3
|
-
Object.defineProperty(obj, key, {
|
|
4
|
-
value: value,
|
|
5
|
-
enumerable: true,
|
|
6
|
-
configurable: true,
|
|
7
|
-
writable: true
|
|
8
|
-
});
|
|
9
|
-
} else {
|
|
10
|
-
obj[key] = value;
|
|
11
|
-
}
|
|
12
|
-
return obj;
|
|
13
|
-
}
|
|
14
|
-
function _ts_decorate(decorators, target, key, desc) {
|
|
15
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
16
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
17
|
-
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
18
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
19
|
-
}
|
|
20
|
-
function _ts_metadata(k, v) {
|
|
21
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
22
|
-
}
|
|
23
|
-
function _ts_param(paramIndex, decorator) {
|
|
24
|
-
return function(target, key) {
|
|
25
|
-
decorator(target, key, paramIndex);
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
import { buildCookieOptions, isBrowserRequest } from '@flusys/nestjs-shared/utils';
|
|
29
|
-
import { Injectable, Optional } from '@nestjs/common';
|
|
30
|
-
import { map } from 'rxjs';
|
|
31
|
-
import { DEFAULT_COOKIE_NAME } from '../config';
|
|
32
|
-
import { AuthConfigService } from '../services/auth-config.service';
|
|
33
|
-
export class ClearToken {
|
|
34
|
-
get cookieName() {
|
|
35
|
-
return this.authConfig?.getRefreshTokenCookieName() ?? DEFAULT_COOKIE_NAME;
|
|
36
|
-
}
|
|
37
|
-
intercept(context, next) {
|
|
38
|
-
const req = context.switchToHttp().getRequest();
|
|
39
|
-
const res = context.switchToHttp().getResponse();
|
|
40
|
-
return next.handle().pipe(map((value)=>{
|
|
41
|
-
if (isBrowserRequest(req)) {
|
|
42
|
-
res.clearCookie(this.cookieName, {
|
|
43
|
-
httpOnly: true,
|
|
44
|
-
path: '/',
|
|
45
|
-
...buildCookieOptions(req)
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
return value;
|
|
49
|
-
}));
|
|
50
|
-
}
|
|
51
|
-
constructor(authConfig){
|
|
52
|
-
_define_property(this, "authConfig", void 0);
|
|
53
|
-
this.authConfig = authConfig;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
ClearToken = _ts_decorate([
|
|
57
|
-
Injectable(),
|
|
58
|
-
_ts_param(0, Optional()),
|
|
59
|
-
_ts_metadata("design:type", Function),
|
|
60
|
-
_ts_metadata("design:paramtypes", [
|
|
61
|
-
typeof AuthConfigService === "undefined" ? Object : AuthConfigService
|
|
62
|
-
])
|
|
63
|
-
], ClearToken);
|
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { of } from 'rxjs';
|
|
2
|
-
import { ClearToken } from './clear-token.interceptor';
|
|
3
|
-
import { DEFAULT_COOKIE_NAME } from '../config';
|
|
4
|
-
function buildContext(req, res) {
|
|
5
|
-
return {
|
|
6
|
-
switchToHttp: ()=>({
|
|
7
|
-
getRequest: ()=>req,
|
|
8
|
-
getResponse: ()=>res
|
|
9
|
-
})
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
function buildCallHandler(value) {
|
|
13
|
-
return {
|
|
14
|
-
handle: ()=>of(value)
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
describe('ClearToken', ()=>{
|
|
18
|
-
let mockAuthConfig;
|
|
19
|
-
beforeEach(()=>{
|
|
20
|
-
mockAuthConfig = {
|
|
21
|
-
getRefreshTokenCookieName: jest.fn().mockReturnValue('custom_refresh_token')
|
|
22
|
-
};
|
|
23
|
-
});
|
|
24
|
-
it('clears the cookie for a browser request', (done)=>{
|
|
25
|
-
const interceptor = new ClearToken(mockAuthConfig);
|
|
26
|
-
const clearCookie = jest.fn();
|
|
27
|
-
const req = {
|
|
28
|
-
headers: {
|
|
29
|
-
'user-agent': 'Mozilla/5.0 Chrome/1.0'
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
const res = {
|
|
33
|
-
clearCookie
|
|
34
|
-
};
|
|
35
|
-
interceptor.intercept(buildContext(req, res), buildCallHandler({
|
|
36
|
-
success: true
|
|
37
|
-
})).subscribe((result)=>{
|
|
38
|
-
expect(clearCookie).toHaveBeenCalledWith('custom_refresh_token', expect.objectContaining({
|
|
39
|
-
httpOnly: true,
|
|
40
|
-
path: '/'
|
|
41
|
-
}));
|
|
42
|
-
expect(result).toEqual({
|
|
43
|
-
success: true
|
|
44
|
-
});
|
|
45
|
-
done();
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
it('does not clear the cookie for a non-browser (API) request', (done)=>{
|
|
49
|
-
const interceptor = new ClearToken(mockAuthConfig);
|
|
50
|
-
const clearCookie = jest.fn();
|
|
51
|
-
const req = {
|
|
52
|
-
headers: {
|
|
53
|
-
'user-agent': 'PostmanRuntime/7.0'
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
const res = {
|
|
57
|
-
clearCookie
|
|
58
|
-
};
|
|
59
|
-
interceptor.intercept(buildContext(req, res), buildCallHandler({
|
|
60
|
-
success: true
|
|
61
|
-
})).subscribe(()=>{
|
|
62
|
-
expect(clearCookie).not.toHaveBeenCalled();
|
|
63
|
-
done();
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
it('falls back to the default cookie name when authConfig is unavailable', (done)=>{
|
|
67
|
-
const interceptor = new ClearToken(undefined);
|
|
68
|
-
const clearCookie = jest.fn();
|
|
69
|
-
const req = {
|
|
70
|
-
headers: {
|
|
71
|
-
'user-agent': 'Mozilla/5.0 Chrome/1.0'
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
const res = {
|
|
75
|
-
clearCookie
|
|
76
|
-
};
|
|
77
|
-
interceptor.intercept(buildContext(req, res), buildCallHandler({
|
|
78
|
-
success: true
|
|
79
|
-
})).subscribe(()=>{
|
|
80
|
-
expect(clearCookie).toHaveBeenCalledWith(DEFAULT_COOKIE_NAME, expect.anything());
|
|
81
|
-
done();
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
it('passes through the emitted value unchanged', (done)=>{
|
|
85
|
-
const interceptor = new ClearToken(mockAuthConfig);
|
|
86
|
-
const req = {
|
|
87
|
-
headers: {
|
|
88
|
-
'user-agent': 'PostmanRuntime/7.0'
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
const res = {
|
|
92
|
-
clearCookie: jest.fn()
|
|
93
|
-
};
|
|
94
|
-
const payload = {
|
|
95
|
-
success: true,
|
|
96
|
-
message: 'Logged out successfully'
|
|
97
|
-
};
|
|
98
|
-
interceptor.intercept(buildContext(req, res), buildCallHandler(payload)).subscribe((result)=>{
|
|
99
|
-
expect(result).toBe(payload);
|
|
100
|
-
done();
|
|
101
|
-
});
|
|
102
|
-
});
|
|
103
|
-
});
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
function _define_property(obj, key, value) {
|
|
2
|
-
if (key in obj) {
|
|
3
|
-
Object.defineProperty(obj, key, {
|
|
4
|
-
value: value,
|
|
5
|
-
enumerable: true,
|
|
6
|
-
configurable: true,
|
|
7
|
-
writable: true
|
|
8
|
-
});
|
|
9
|
-
} else {
|
|
10
|
-
obj[key] = value;
|
|
11
|
-
}
|
|
12
|
-
return obj;
|
|
13
|
-
}
|
|
14
|
-
function _ts_decorate(decorators, target, key, desc) {
|
|
15
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
16
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
17
|
-
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
18
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
19
|
-
}
|
|
20
|
-
function _ts_metadata(k, v) {
|
|
21
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
22
|
-
}
|
|
23
|
-
function _ts_param(paramIndex, decorator) {
|
|
24
|
-
return function(target, key) {
|
|
25
|
-
decorator(target, key, paramIndex);
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
import { buildCookieOptions, isBrowserRequest, parseDurationToMs } from '@flusys/nestjs-shared/utils';
|
|
29
|
-
import { AUTH_MESSAGES } from '../config';
|
|
30
|
-
import { Inject, Injectable, Optional, PayloadTooLargeException } from '@nestjs/common';
|
|
31
|
-
import { Buffer } from 'buffer';
|
|
32
|
-
import { catchError, map, throwError } from 'rxjs';
|
|
33
|
-
import { DEFAULT_COOKIE_NAME, MAX_COOKIE_BYTES } from '../config';
|
|
34
|
-
import { AuthConfigService } from '../services/auth-config.service';
|
|
35
|
-
export class SetToken {
|
|
36
|
-
get cookieName() {
|
|
37
|
-
return this.authConfig?.getRefreshTokenCookieName() ?? DEFAULT_COOKIE_NAME;
|
|
38
|
-
}
|
|
39
|
-
get maxAgeMs() {
|
|
40
|
-
return parseDurationToMs(this.authConfig?.getRefreshTokenExpiration() ?? '7d');
|
|
41
|
-
}
|
|
42
|
-
intercept(context, next) {
|
|
43
|
-
const req = context.switchToHttp().getRequest();
|
|
44
|
-
const res = context.switchToHttp().getResponse();
|
|
45
|
-
return next.handle().pipe(map((value)=>{
|
|
46
|
-
if (typeof value !== 'object' || value === null) {
|
|
47
|
-
return value;
|
|
48
|
-
}
|
|
49
|
-
const response = value;
|
|
50
|
-
const data = typeof response['data'] === 'object' && response['data'] !== null ? response['data'] : null;
|
|
51
|
-
const tokenInData = data && typeof data['refreshToken'] === 'string';
|
|
52
|
-
if (!tokenInData) {
|
|
53
|
-
return value;
|
|
54
|
-
}
|
|
55
|
-
if (!isBrowserRequest(req)) {
|
|
56
|
-
return value;
|
|
57
|
-
}
|
|
58
|
-
this.setRefreshTokenCookie(req, res, data['refreshToken']);
|
|
59
|
-
const { refreshToken: _, ...dataWithoutToken } = data;
|
|
60
|
-
return {
|
|
61
|
-
...response,
|
|
62
|
-
data: dataWithoutToken
|
|
63
|
-
};
|
|
64
|
-
}), catchError((err)=>throwError(()=>err)));
|
|
65
|
-
}
|
|
66
|
-
setRefreshTokenCookie(req, res, tokenValue) {
|
|
67
|
-
const rawCookieString = `${this.cookieName}=${tokenValue}; HttpOnly`;
|
|
68
|
-
const estimatedBytes = Buffer.byteLength(rawCookieString, 'utf8');
|
|
69
|
-
if (estimatedBytes > MAX_COOKIE_BYTES) {
|
|
70
|
-
throw new PayloadTooLargeException({
|
|
71
|
-
message: `Refresh token too large (≈${estimatedBytes} bytes)`,
|
|
72
|
-
messageKey: AUTH_MESSAGES.TOKEN_TOO_LARGE,
|
|
73
|
-
messageVariables: {
|
|
74
|
-
size: estimatedBytes
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
res.cookie(this.cookieName, tokenValue, {
|
|
79
|
-
httpOnly: true,
|
|
80
|
-
path: '/',
|
|
81
|
-
maxAge: this.maxAgeMs,
|
|
82
|
-
...buildCookieOptions(req)
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
constructor(authConfig){
|
|
86
|
-
_define_property(this, "authConfig", void 0);
|
|
87
|
-
this.authConfig = authConfig;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
SetToken = _ts_decorate([
|
|
91
|
-
Injectable(),
|
|
92
|
-
_ts_param(0, Optional()),
|
|
93
|
-
_ts_param(0, Inject(AuthConfigService)),
|
|
94
|
-
_ts_metadata("design:type", Function),
|
|
95
|
-
_ts_metadata("design:paramtypes", [
|
|
96
|
-
typeof AuthConfigService === "undefined" ? Object : AuthConfigService
|
|
97
|
-
])
|
|
98
|
-
], SetToken);
|
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
import { PayloadTooLargeException } from '@nestjs/common';
|
|
2
|
-
import { of } from 'rxjs';
|
|
3
|
-
import { SetToken } from './set-token.interceptor';
|
|
4
|
-
import { DEFAULT_COOKIE_NAME } from '../config';
|
|
5
|
-
function buildContext(req, res) {
|
|
6
|
-
return {
|
|
7
|
-
switchToHttp: ()=>({
|
|
8
|
-
getRequest: ()=>req,
|
|
9
|
-
getResponse: ()=>res
|
|
10
|
-
})
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
function buildCallHandler(value) {
|
|
14
|
-
return {
|
|
15
|
-
handle: ()=>of(value)
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
const browserReq = {
|
|
19
|
-
headers: {
|
|
20
|
-
'user-agent': 'Mozilla/5.0 Chrome/1.0'
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
const apiReq = {
|
|
24
|
-
headers: {
|
|
25
|
-
'user-agent': 'PostmanRuntime/7.0'
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
describe('SetToken', ()=>{
|
|
29
|
-
let mockAuthConfig;
|
|
30
|
-
beforeEach(()=>{
|
|
31
|
-
mockAuthConfig = {
|
|
32
|
-
getRefreshTokenCookieName: jest.fn().mockReturnValue('custom_refresh_token'),
|
|
33
|
-
getRefreshTokenExpiration: jest.fn().mockReturnValue('7d')
|
|
34
|
-
};
|
|
35
|
-
});
|
|
36
|
-
it('sets a refresh token cookie and strips refreshToken from data for browser requests', (done)=>{
|
|
37
|
-
const interceptor = new SetToken(mockAuthConfig);
|
|
38
|
-
const cookie = jest.fn();
|
|
39
|
-
const res = {
|
|
40
|
-
cookie
|
|
41
|
-
};
|
|
42
|
-
const payload = {
|
|
43
|
-
success: true,
|
|
44
|
-
data: {
|
|
45
|
-
accessToken: 'access-1',
|
|
46
|
-
refreshToken: 'refresh-1',
|
|
47
|
-
user: {
|
|
48
|
-
id: 'u1'
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
interceptor.intercept(buildContext(browserReq, res), buildCallHandler(payload)).subscribe((result)=>{
|
|
53
|
-
expect(cookie).toHaveBeenCalledWith('custom_refresh_token', 'refresh-1', expect.objectContaining({
|
|
54
|
-
httpOnly: true,
|
|
55
|
-
path: '/'
|
|
56
|
-
}));
|
|
57
|
-
expect(result).toEqual({
|
|
58
|
-
success: true,
|
|
59
|
-
data: {
|
|
60
|
-
accessToken: 'access-1',
|
|
61
|
-
user: {
|
|
62
|
-
id: 'u1'
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
expect(result.data.refreshToken).toBeUndefined();
|
|
67
|
-
done();
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
it('does not set a cookie or modify data for non-browser (API) requests', (done)=>{
|
|
71
|
-
const interceptor = new SetToken(mockAuthConfig);
|
|
72
|
-
const cookie = jest.fn();
|
|
73
|
-
const res = {
|
|
74
|
-
cookie
|
|
75
|
-
};
|
|
76
|
-
const payload = {
|
|
77
|
-
success: true,
|
|
78
|
-
data: {
|
|
79
|
-
accessToken: 'access-1',
|
|
80
|
-
refreshToken: 'refresh-1'
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
interceptor.intercept(buildContext(apiReq, res), buildCallHandler(payload)).subscribe((result)=>{
|
|
84
|
-
expect(cookie).not.toHaveBeenCalled();
|
|
85
|
-
expect(result).toBe(payload);
|
|
86
|
-
done();
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
it('passes through non-object values unchanged', (done)=>{
|
|
90
|
-
const interceptor = new SetToken(mockAuthConfig);
|
|
91
|
-
const res = {
|
|
92
|
-
cookie: jest.fn()
|
|
93
|
-
};
|
|
94
|
-
interceptor.intercept(buildContext(browserReq, res), buildCallHandler('plain-string')).subscribe((result)=>{
|
|
95
|
-
expect(result).toBe('plain-string');
|
|
96
|
-
done();
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
it('passes through null values unchanged', (done)=>{
|
|
100
|
-
const interceptor = new SetToken(mockAuthConfig);
|
|
101
|
-
const res = {
|
|
102
|
-
cookie: jest.fn()
|
|
103
|
-
};
|
|
104
|
-
interceptor.intercept(buildContext(browserReq, res), buildCallHandler(null)).subscribe((result)=>{
|
|
105
|
-
expect(result).toBeNull();
|
|
106
|
-
done();
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
it('passes through values without a refreshToken in data unchanged', (done)=>{
|
|
110
|
-
const interceptor = new SetToken(mockAuthConfig);
|
|
111
|
-
const cookie = jest.fn();
|
|
112
|
-
const res = {
|
|
113
|
-
cookie
|
|
114
|
-
};
|
|
115
|
-
const payload = {
|
|
116
|
-
success: true,
|
|
117
|
-
data: {
|
|
118
|
-
accessToken: 'access-1'
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
interceptor.intercept(buildContext(browserReq, res), buildCallHandler(payload)).subscribe((result)=>{
|
|
122
|
-
expect(cookie).not.toHaveBeenCalled();
|
|
123
|
-
expect(result).toBe(payload);
|
|
124
|
-
done();
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
|
-
it('falls back to the default cookie name when authConfig is unavailable', (done)=>{
|
|
128
|
-
const interceptor = new SetToken(undefined);
|
|
129
|
-
const cookie = jest.fn();
|
|
130
|
-
const res = {
|
|
131
|
-
cookie
|
|
132
|
-
};
|
|
133
|
-
const payload = {
|
|
134
|
-
data: {
|
|
135
|
-
refreshToken: 'refresh-1'
|
|
136
|
-
}
|
|
137
|
-
};
|
|
138
|
-
interceptor.intercept(buildContext(browserReq, res), buildCallHandler(payload)).subscribe(()=>{
|
|
139
|
-
expect(cookie).toHaveBeenCalledWith(DEFAULT_COOKIE_NAME, 'refresh-1', expect.anything());
|
|
140
|
-
done();
|
|
141
|
-
});
|
|
142
|
-
});
|
|
143
|
-
it('throws PayloadTooLargeException when the refresh token exceeds the max cookie size', (done)=>{
|
|
144
|
-
const interceptor = new SetToken(mockAuthConfig);
|
|
145
|
-
const res = {
|
|
146
|
-
cookie: jest.fn()
|
|
147
|
-
};
|
|
148
|
-
const oversizedToken = 'a'.repeat(4000);
|
|
149
|
-
const payload = {
|
|
150
|
-
data: {
|
|
151
|
-
refreshToken: oversizedToken
|
|
152
|
-
}
|
|
153
|
-
};
|
|
154
|
-
interceptor.intercept(buildContext(browserReq, res), buildCallHandler(payload)).subscribe({
|
|
155
|
-
error: (err)=>{
|
|
156
|
-
expect(err).toBeInstanceOf(PayloadTooLargeException);
|
|
157
|
-
done();
|
|
158
|
-
}
|
|
159
|
-
});
|
|
160
|
-
});
|
|
161
|
-
});
|