@flusys/nestjs-auth 7.0.2 → 8.0.0-beta.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/README.md +68 -1
- package/config/auth.constants.d.ts +2 -0
- package/config/event-actions.d.ts +2 -0
- package/config/index.d.ts +3 -3
- package/config/message-keys.d.ts +23 -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 -7
- package/controllers/social-auth-config.controller.d.ts +22 -0
- package/controllers/social-auth.controller.d.ts +11 -0
- package/controllers/user-permission.controller.d.ts +5 -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 -5
- package/dtos/social-auth.dto.d.ts +42 -0
- package/entities/company-branch.entity.d.ts +1 -1
- package/entities/company.entity.d.ts +1 -1
- package/entities/index.d.ts +15 -13
- package/entities/pending-registration.entity.d.ts +1 -1
- package/entities/social-auth-config.entity.d.ts +9 -0
- package/entities/user-company-permission.entity.d.ts +4 -4
- package/enums/index.d.ts +2 -1
- package/enums/social-provider-type.enum.d.ts +6 -0
- package/fesm/118.js +1230 -0
- package/fesm/120.js +2197 -0
- package/fesm/24.js +4389 -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 +244 -3
- package/fesm/controllers/index.js +27 -7
- package/fesm/docs/index.js +479 -1
- package/fesm/dtos/index.js +123 -5
- package/fesm/entities/index.js +68 -19
- package/fesm/enums/index.js +53 -1
- package/fesm/helpers/index.js +115 -2
- package/fesm/index.js +1437 -12
- package/fesm/interceptors/index.js +508 -2
- package/fesm/interfaces/index.js +75 -7
- package/fesm/modules/index.js +546 -1
- package/fesm/runtime.js +134 -0
- package/fesm/services/index.js +67 -9
- 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 -12
- 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 -7
- package/interfaces/social-auth.interface.d.ts +43 -0
- 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 +10 -0
- package/providers/google.social-provider.d.ts +10 -0
- package/providers/index.d.ts +1 -0
- package/providers/linkedin.social-provider.d.ts +10 -0
- package/providers/microsoft.social-provider.d.ts +10 -0
- package/providers/oauth-http.util.d.ts +10 -0
- package/providers/social-provider.registry.d.ts +3 -0
- package/services/auth-config.service.d.ts +4 -3
- package/services/auth-datasource.provider.d.ts +4 -3
- package/services/auth-email.service.d.ts +3 -3
- package/services/authentication.service.d.ts +15 -11
- package/services/branch.service.d.ts +7 -7
- package/services/company.service.d.ts +6 -6
- package/services/index.d.ts +11 -9
- package/services/social-auth-config.service.d.ts +21 -0
- package/services/social-auth.service.d.ts +24 -0
- package/services/user-permission.service.d.ts +8 -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 -41
- package/cjs/config/event-actions.js +0 -45
- package/cjs/config/index.js +0 -20
- package/cjs/config/message-keys.js +0 -108
- 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 -24
- 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 -453
- 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 -22
- 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 -65
- package/cjs/entities/index.spec.js +0 -73
- package/cjs/entities/pending-registration.entity.js +0 -135
- 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 -18
- 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 -29
- 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 -24
- package/cjs/interfaces/user-enricher.interface.js +0 -4
- package/cjs/interfaces/user.interface.js +0 -4
- package/cjs/modules/auth.module.js +0 -202
- package/cjs/modules/auth.module.spec.js +0 -243
- package/cjs/modules/index.js +0 -18
- package/cjs/services/auth-config.service.js +0 -110
- package/cjs/services/auth-config.service.spec.js +0 -97
- package/cjs/services/auth-datasource.provider.js +0 -134
- package/cjs/services/auth-datasource.provider.spec.js +0 -161
- 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 -1173
- package/cjs/services/authentication.service.spec.js +0 -962
- 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 -26
- 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 -11
- package/fesm/config/event-actions.js +0 -24
- package/fesm/config/message-keys.js +0 -78
- 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/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 -446
- 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/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 -69
- package/fesm/entities/pending-registration.entity.js +0 -125
- package/fesm/entities/user-company-permission.entity.js +0 -136
- package/fesm/entities/user-token.entity.js +0 -104
- 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/user-enricher.interface.js +0 -4
- package/fesm/interfaces/user.interface.js +0 -1
- package/fesm/modules/auth.module.js +0 -192
- package/fesm/modules/auth.module.spec.js +0 -239
- package/fesm/services/auth-config.service.js +0 -100
- package/fesm/services/auth-config.service.spec.js +0 -93
- package/fesm/services/auth-datasource.provider.js +0 -83
- package/fesm/services/auth-datasource.provider.spec.js +0 -157
- 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 -1122
- package/fesm/services/authentication.service.spec.js +0 -917
- 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/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,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
|
-
});
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Auth Email Provider Interface
|
|
3
|
-
*
|
|
4
|
-
* This interface defines the contract for email sending in the auth module.
|
|
5
|
-
* It allows auth to work independently of the email module.
|
|
6
|
-
*
|
|
7
|
-
* To use email features in auth:
|
|
8
|
-
* 1. Provide AUTH_EMAIL_PROVIDER in your app module
|
|
9
|
-
* 2. Implement IAuthEmailProvider using EmailSendService from nestjs-email
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```typescript
|
|
13
|
-
* // In app.module.ts or a provider file
|
|
14
|
-
* {
|
|
15
|
-
* provide: AUTH_EMAIL_PROVIDER,
|
|
16
|
-
* useFactory: (emailSendService: EmailSendService) => ({
|
|
17
|
-
* sendPasswordResetEmail: async (email, token, resetUrl) => {
|
|
18
|
-
* await emailSendService.sendTemplateEmail({
|
|
19
|
-
* templateSlug: 'password-reset',
|
|
20
|
-
* to: email,
|
|
21
|
-
* variables: { resetUrl, token },
|
|
22
|
-
* });
|
|
23
|
-
* },
|
|
24
|
-
* sendVerificationEmail: async (email, token, verifyUrl) => {
|
|
25
|
-
* await emailSendService.sendTemplateEmail({
|
|
26
|
-
* templateSlug: 'email-verification',
|
|
27
|
-
* to: email,
|
|
28
|
-
* variables: { verifyUrl, token },
|
|
29
|
-
* });
|
|
30
|
-
* },
|
|
31
|
-
* }),
|
|
32
|
-
* inject: [EmailSendService],
|
|
33
|
-
* }
|
|
34
|
-
* ```
|
|
35
|
-
*/ /**
|
|
36
|
-
* Injection token for auth email provider
|
|
37
|
-
* Use this to provide the email implementation
|
|
38
|
-
*/ export const AUTH_EMAIL_PROVIDER = 'AUTH_EMAIL_PROVIDER';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
/** Company/branch selection or creation data supplied during registration */ export { };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { };
|