@flusys/nestjs-auth 7.0.1 → 7.1.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 +67 -0
- package/cjs/config/auth.constants.js +8 -0
- package/cjs/config/event-actions.js +4 -2
- package/cjs/config/message-keys.js +27 -0
- package/cjs/controllers/index.js +2 -0
- package/cjs/controllers/social-auth-config.controller.js +107 -0
- package/cjs/controllers/social-auth.controller.js +175 -0
- package/cjs/controllers/social-auth.controller.spec.js +79 -0
- package/cjs/docs/auth-swagger.config.js +9 -1
- package/cjs/dtos/index.js +1 -0
- package/cjs/dtos/social-auth.dto.js +318 -0
- package/cjs/entities/index.js +3 -1
- package/cjs/entities/index.spec.js +12 -1
- package/cjs/entities/social-auth-config.entity.js +102 -0
- package/cjs/enums/index.js +1 -0
- package/cjs/enums/social-provider-type.enum.js +17 -0
- package/cjs/index.js +1 -0
- package/cjs/interfaces/index.js +1 -0
- package/cjs/interfaces/social-auth.interface.js +4 -0
- package/cjs/modules/auth.module.js +6 -0
- package/cjs/modules/auth.module.spec.js +15 -0
- package/cjs/providers/facebook.social-provider.js +73 -0
- package/cjs/providers/google.social-provider.js +68 -0
- package/cjs/providers/index.js +18 -0
- package/cjs/providers/linkedin.social-provider.js +66 -0
- package/cjs/providers/microsoft.social-provider.js +78 -0
- package/cjs/providers/oauth-http.util.js +107 -0
- package/cjs/providers/social-provider.registry.js +24 -0
- package/cjs/providers/social-provider.registry.spec.js +135 -0
- package/cjs/services/auth-config.service.js +3 -0
- package/cjs/services/auth-config.service.spec.js +3 -0
- package/cjs/services/auth-datasource.provider.js +4 -0
- package/cjs/services/auth-datasource.provider.spec.js +30 -0
- package/cjs/services/authentication.service.js +38 -8
- package/cjs/services/authentication.service.spec.js +58 -0
- package/cjs/services/index.js +2 -0
- package/cjs/services/social-auth-config.service.js +103 -0
- package/cjs/services/social-auth-config.service.spec.js +71 -0
- package/cjs/services/social-auth.service.js +310 -0
- package/cjs/services/social-auth.service.spec.js +310 -0
- package/config/auth.constants.d.ts +2 -0
- package/config/event-actions.d.ts +2 -0
- package/config/message-keys.d.ts +21 -0
- package/controllers/index.d.ts +2 -0
- package/controllers/social-auth-config.controller.d.ts +22 -0
- package/controllers/social-auth.controller.d.ts +11 -0
- package/dtos/index.d.ts +1 -0
- package/dtos/social-auth.dto.d.ts +42 -0
- package/entities/index.d.ts +3 -1
- package/entities/social-auth-config.entity.d.ts +9 -0
- package/enums/index.d.ts +1 -0
- package/enums/social-provider-type.enum.d.ts +6 -0
- package/fesm/config/auth.constants.js +3 -0
- package/fesm/config/event-actions.js +4 -2
- package/fesm/config/message-keys.js +21 -0
- package/fesm/controllers/index.js +2 -0
- package/fesm/controllers/social-auth-config.controller.js +97 -0
- package/fesm/controllers/social-auth.controller.js +165 -0
- package/fesm/controllers/social-auth.controller.spec.js +75 -0
- package/fesm/docs/auth-swagger.config.js +9 -1
- package/fesm/dtos/index.js +1 -0
- package/fesm/dtos/social-auth.dto.js +282 -0
- package/fesm/entities/index.js +4 -1
- package/fesm/entities/index.spec.js +12 -1
- package/fesm/entities/social-auth-config.entity.js +92 -0
- package/fesm/enums/index.js +1 -0
- package/fesm/enums/social-provider-type.enum.js +7 -0
- package/fesm/index.js +1 -0
- package/fesm/interfaces/index.js +1 -0
- package/fesm/interfaces/social-auth.interface.js +1 -0
- package/fesm/modules/auth.module.js +8 -2
- package/fesm/modules/auth.module.spec.js +17 -2
- package/fesm/providers/facebook.social-provider.js +63 -0
- package/fesm/providers/google.social-provider.js +58 -0
- package/fesm/providers/index.js +1 -0
- package/fesm/providers/linkedin.social-provider.js +56 -0
- package/fesm/providers/microsoft.social-provider.js +68 -0
- package/fesm/providers/oauth-http.util.js +77 -0
- package/fesm/providers/social-provider.registry.js +14 -0
- package/fesm/providers/social-provider.registry.spec.js +131 -0
- package/fesm/services/auth-config.service.js +3 -0
- package/fesm/services/auth-config.service.spec.js +3 -0
- package/fesm/services/auth-datasource.provider.js +4 -0
- package/fesm/services/auth-datasource.provider.spec.js +30 -0
- package/fesm/services/authentication.service.js +38 -8
- package/fesm/services/authentication.service.spec.js +58 -0
- package/fesm/services/index.js +2 -0
- package/fesm/services/social-auth-config.service.js +93 -0
- package/fesm/services/social-auth-config.service.spec.js +67 -0
- package/fesm/services/social-auth.service.js +300 -0
- package/fesm/services/social-auth.service.spec.js +265 -0
- package/index.d.ts +1 -0
- package/interfaces/index.d.ts +1 -0
- package/interfaces/social-auth.interface.d.ts +43 -0
- package/package.json +3 -3
- 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 +1 -0
- package/services/auth-datasource.provider.d.ts +1 -0
- package/services/authentication.service.d.ts +4 -0
- package/services/index.d.ts +2 -0
- package/services/social-auth-config.service.d.ts +21 -0
- package/services/social-auth.service.d.ts +24 -0
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
const _repositorymock = require("@test-utils/mocks/repository.mock");
|
|
6
|
+
const _common = require("@nestjs/common");
|
|
7
|
+
const _appuserentity = require("../entities/app-user.entity");
|
|
8
|
+
const _socialauthconfigentity = require("../entities/social-auth-config.entity");
|
|
9
|
+
const _socialprovidertypeenum = require("../enums/social-provider-type.enum");
|
|
10
|
+
const _socialproviderregistry = /*#__PURE__*/ _interop_require_wildcard(require("../providers/social-provider.registry"));
|
|
11
|
+
const _socialauthservice = require("./social-auth.service");
|
|
12
|
+
function _getRequireWildcardCache(nodeInterop) {
|
|
13
|
+
if (typeof WeakMap !== "function") return null;
|
|
14
|
+
var cacheBabelInterop = new WeakMap();
|
|
15
|
+
var cacheNodeInterop = new WeakMap();
|
|
16
|
+
return (_getRequireWildcardCache = function(nodeInterop) {
|
|
17
|
+
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
18
|
+
})(nodeInterop);
|
|
19
|
+
}
|
|
20
|
+
function _interop_require_wildcard(obj, nodeInterop) {
|
|
21
|
+
if (!nodeInterop && obj && obj.__esModule) {
|
|
22
|
+
return obj;
|
|
23
|
+
}
|
|
24
|
+
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
25
|
+
return {
|
|
26
|
+
default: obj
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
var cache = _getRequireWildcardCache(nodeInterop);
|
|
30
|
+
if (cache && cache.has(obj)) {
|
|
31
|
+
return cache.get(obj);
|
|
32
|
+
}
|
|
33
|
+
var newObj = {
|
|
34
|
+
__proto__: null
|
|
35
|
+
};
|
|
36
|
+
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
37
|
+
for(var key in obj){
|
|
38
|
+
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
39
|
+
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
40
|
+
if (desc && (desc.get || desc.set)) {
|
|
41
|
+
Object.defineProperty(newObj, key, desc);
|
|
42
|
+
} else {
|
|
43
|
+
newObj[key] = obj[key];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
newObj.default = obj;
|
|
48
|
+
if (cache) {
|
|
49
|
+
cache.set(obj, newObj);
|
|
50
|
+
}
|
|
51
|
+
return newObj;
|
|
52
|
+
}
|
|
53
|
+
const REDIRECT_URI = 'http://localhost:2001/auth/social/callback';
|
|
54
|
+
function buildConfig(overrides = {}) {
|
|
55
|
+
return {
|
|
56
|
+
id: 'config-1',
|
|
57
|
+
provider: _socialprovidertypeenum.SocialProviderTypeEnum.GOOGLE,
|
|
58
|
+
isEnabled: true,
|
|
59
|
+
clientId: 'client-id',
|
|
60
|
+
clientSecret: 'client-secret',
|
|
61
|
+
scopes: null,
|
|
62
|
+
extraConfig: null,
|
|
63
|
+
createdAt: new Date(),
|
|
64
|
+
updatedAt: new Date(),
|
|
65
|
+
deletedAt: null,
|
|
66
|
+
createdById: null,
|
|
67
|
+
updatedById: null,
|
|
68
|
+
deletedById: null,
|
|
69
|
+
...overrides
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function buildProfile(overrides = {}) {
|
|
73
|
+
return {
|
|
74
|
+
providerUserId: 'google-user-1',
|
|
75
|
+
email: 'user@example.com',
|
|
76
|
+
emailVerified: true,
|
|
77
|
+
name: 'Test User',
|
|
78
|
+
avatarUrl: null,
|
|
79
|
+
...overrides
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function buildUser(overrides = {}) {
|
|
83
|
+
return {
|
|
84
|
+
id: 'user-1',
|
|
85
|
+
email: 'user@example.com',
|
|
86
|
+
isActive: true,
|
|
87
|
+
...overrides
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
describe('SocialAuthService', ()=>{
|
|
91
|
+
let configRepo;
|
|
92
|
+
let userRepo;
|
|
93
|
+
let cache;
|
|
94
|
+
let authService;
|
|
95
|
+
let fetchProfile;
|
|
96
|
+
let buildAuthorizationUrl;
|
|
97
|
+
let signUpEnabled;
|
|
98
|
+
let companyFeatureEnabled;
|
|
99
|
+
function buildService() {
|
|
100
|
+
const dataSourceProvider = {
|
|
101
|
+
getRepository: jest.fn().mockImplementation(async (entity)=>{
|
|
102
|
+
if (entity === _socialauthconfigentity.SocialAuthConfig) return configRepo;
|
|
103
|
+
if (entity === _appuserentity.AppUser) return userRepo;
|
|
104
|
+
throw new Error(`Unexpected entity: ${String(entity)}`);
|
|
105
|
+
}),
|
|
106
|
+
isSignUpEnabled: ()=>signUpEnabled,
|
|
107
|
+
isCompanyFeatureEnabled: ()=>companyFeatureEnabled
|
|
108
|
+
};
|
|
109
|
+
return new _socialauthservice.SocialAuthService(authService, dataSourceProvider, cache);
|
|
110
|
+
}
|
|
111
|
+
beforeEach(()=>{
|
|
112
|
+
signUpEnabled = true;
|
|
113
|
+
companyFeatureEnabled = false;
|
|
114
|
+
configRepo = (0, _repositorymock.createMockRepository)();
|
|
115
|
+
userRepo = (0, _repositorymock.createMockRepository)();
|
|
116
|
+
cache = {
|
|
117
|
+
get: jest.fn(),
|
|
118
|
+
set: jest.fn(),
|
|
119
|
+
del: jest.fn()
|
|
120
|
+
};
|
|
121
|
+
authService = {
|
|
122
|
+
issueLoginForUser: jest.fn().mockResolvedValue({
|
|
123
|
+
accessToken: 'access-token'
|
|
124
|
+
})
|
|
125
|
+
};
|
|
126
|
+
fetchProfile = jest.fn().mockResolvedValue(buildProfile());
|
|
127
|
+
buildAuthorizationUrl = jest.fn().mockReturnValue('https://provider.test/consent');
|
|
128
|
+
jest.spyOn(_socialproviderregistry, 'getSocialProvider').mockReturnValue({
|
|
129
|
+
provider: _socialprovidertypeenum.SocialProviderTypeEnum.GOOGLE,
|
|
130
|
+
defaultScopes: [
|
|
131
|
+
'openid'
|
|
132
|
+
],
|
|
133
|
+
buildAuthorizationUrl,
|
|
134
|
+
fetchProfile
|
|
135
|
+
});
|
|
136
|
+
userRepo.create.mockImplementation((data)=>({
|
|
137
|
+
...data
|
|
138
|
+
}));
|
|
139
|
+
userRepo.save.mockImplementation(async (entity)=>({
|
|
140
|
+
id: 'new-user',
|
|
141
|
+
...entity
|
|
142
|
+
}));
|
|
143
|
+
});
|
|
144
|
+
afterEach(()=>jest.restoreAllMocks());
|
|
145
|
+
describe('listEnabledProviders', ()=>{
|
|
146
|
+
it('drops enabled providers that have no credentials yet', async ()=>{
|
|
147
|
+
configRepo.find.mockResolvedValue([
|
|
148
|
+
buildConfig(),
|
|
149
|
+
buildConfig({
|
|
150
|
+
id: 'config-2',
|
|
151
|
+
provider: _socialprovidertypeenum.SocialProviderTypeEnum.FACEBOOK,
|
|
152
|
+
clientSecret: null
|
|
153
|
+
})
|
|
154
|
+
]);
|
|
155
|
+
const providers = await buildService().listEnabledProviders(REDIRECT_URI);
|
|
156
|
+
expect(providers).toEqual([
|
|
157
|
+
{
|
|
158
|
+
provider: _socialprovidertypeenum.SocialProviderTypeEnum.GOOGLE,
|
|
159
|
+
redirectUri: REDIRECT_URI
|
|
160
|
+
}
|
|
161
|
+
]);
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
describe('createAuthorization', ()=>{
|
|
165
|
+
it('caches the state and returns the provider consent URL', async ()=>{
|
|
166
|
+
configRepo.findOne.mockResolvedValue(buildConfig());
|
|
167
|
+
const result = await buildService().createAuthorization(_socialprovidertypeenum.SocialProviderTypeEnum.GOOGLE, REDIRECT_URI);
|
|
168
|
+
expect(result.authorizationUrl).toBe('https://provider.test/consent');
|
|
169
|
+
expect(cache.set).toHaveBeenCalledWith(`auth:social:state:${result.state}`, {
|
|
170
|
+
provider: _socialprovidertypeenum.SocialProviderTypeEnum.GOOGLE,
|
|
171
|
+
redirectUri: REDIRECT_URI
|
|
172
|
+
}, 10 * 60 * 1000);
|
|
173
|
+
});
|
|
174
|
+
it('rejects a provider that is configured but switched off', async ()=>{
|
|
175
|
+
configRepo.findOne.mockResolvedValue(buildConfig({
|
|
176
|
+
isEnabled: false
|
|
177
|
+
}));
|
|
178
|
+
await expect(buildService().createAuthorization(_socialprovidertypeenum.SocialProviderTypeEnum.GOOGLE, REDIRECT_URI)).rejects.toThrow(_common.BadRequestException);
|
|
179
|
+
});
|
|
180
|
+
it('rejects a provider that is switched on but has no credentials', async ()=>{
|
|
181
|
+
configRepo.findOne.mockResolvedValue(buildConfig({
|
|
182
|
+
clientId: null
|
|
183
|
+
}));
|
|
184
|
+
await expect(buildService().createAuthorization(_socialprovidertypeenum.SocialProviderTypeEnum.GOOGLE, REDIRECT_URI)).rejects.toThrow(_common.BadRequestException);
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
describe('completeLogin', ()=>{
|
|
188
|
+
beforeEach(()=>{
|
|
189
|
+
cache.get.mockResolvedValue({
|
|
190
|
+
provider: _socialprovidertypeenum.SocialProviderTypeEnum.GOOGLE,
|
|
191
|
+
redirectUri: REDIRECT_URI
|
|
192
|
+
});
|
|
193
|
+
configRepo.findOne.mockResolvedValue(buildConfig());
|
|
194
|
+
});
|
|
195
|
+
it('signs in the AppUser that already has this email, marking the email verified', async ()=>{
|
|
196
|
+
userRepo.findOne.mockResolvedValue(buildUser());
|
|
197
|
+
const result = await buildService().completeLogin('code', 'state', REDIRECT_URI);
|
|
198
|
+
// The provider already proved ownership of the email, so a previously-unverified
|
|
199
|
+
// account is marked verified rather than silently bypassing app-side verification.
|
|
200
|
+
expect(userRepo.save).toHaveBeenCalledWith(expect.objectContaining({
|
|
201
|
+
id: 'user-1',
|
|
202
|
+
emailVerified: true
|
|
203
|
+
}));
|
|
204
|
+
expect(authService.issueLoginForUser).toHaveBeenCalledWith(expect.objectContaining({
|
|
205
|
+
id: 'user-1',
|
|
206
|
+
emailVerified: true
|
|
207
|
+
}));
|
|
208
|
+
expect(result).toEqual({
|
|
209
|
+
accessToken: 'access-token'
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
it('refuses to sign in or re-register an email that belongs to a permanently deleted account', async ()=>{
|
|
213
|
+
userRepo.findOne.mockResolvedValue(buildUser({
|
|
214
|
+
deletedAt: new Date()
|
|
215
|
+
}));
|
|
216
|
+
await expect(buildService().completeLogin('code', 'state', REDIRECT_URI)).rejects.toThrow(_common.UnauthorizedException);
|
|
217
|
+
expect(userRepo.findOne).toHaveBeenCalledWith(expect.objectContaining({
|
|
218
|
+
withDeleted: true
|
|
219
|
+
}));
|
|
220
|
+
expect(userRepo.save).not.toHaveBeenCalled();
|
|
221
|
+
expect(userRepo.create).not.toHaveBeenCalled();
|
|
222
|
+
});
|
|
223
|
+
it('does not re-save an existing user whose email is already verified', async ()=>{
|
|
224
|
+
userRepo.findOne.mockResolvedValue(buildUser({
|
|
225
|
+
emailVerified: true
|
|
226
|
+
}));
|
|
227
|
+
await buildService().completeLogin('code', 'state', REDIRECT_URI);
|
|
228
|
+
expect(userRepo.save).not.toHaveBeenCalled();
|
|
229
|
+
});
|
|
230
|
+
it('adopts an existing password account that shares the email without creating a new one', async ()=>{
|
|
231
|
+
userRepo.findOne.mockResolvedValue(buildUser());
|
|
232
|
+
await buildService().completeLogin('code', 'state', REDIRECT_URI);
|
|
233
|
+
expect(userRepo.create).not.toHaveBeenCalled();
|
|
234
|
+
});
|
|
235
|
+
it('creates a new AppUser with no password on a first sign-in', async ()=>{
|
|
236
|
+
userRepo.findOne.mockResolvedValue(null);
|
|
237
|
+
await buildService().completeLogin('code', 'state', REDIRECT_URI);
|
|
238
|
+
expect(userRepo.save).toHaveBeenCalledWith(expect.objectContaining({
|
|
239
|
+
email: 'user@example.com',
|
|
240
|
+
password: null,
|
|
241
|
+
emailVerified: true
|
|
242
|
+
}));
|
|
243
|
+
});
|
|
244
|
+
it('refuses an unknown email when sign-up is disabled', async ()=>{
|
|
245
|
+
signUpEnabled = false;
|
|
246
|
+
userRepo.findOne.mockResolvedValue(null);
|
|
247
|
+
await expect(buildService().completeLogin('code', 'state', REDIRECT_URI)).rejects.toThrow(_common.UnauthorizedException);
|
|
248
|
+
expect(userRepo.save).not.toHaveBeenCalled();
|
|
249
|
+
});
|
|
250
|
+
it('signs an existing user in even when sign-up is disabled', async ()=>{
|
|
251
|
+
signUpEnabled = false;
|
|
252
|
+
userRepo.findOne.mockResolvedValue(buildUser());
|
|
253
|
+
await expect(buildService().completeLogin('code', 'state', REDIRECT_URI)).resolves.toEqual({
|
|
254
|
+
accessToken: 'access-token'
|
|
255
|
+
});
|
|
256
|
+
expect(userRepo.create).not.toHaveBeenCalled();
|
|
257
|
+
});
|
|
258
|
+
it('refuses to create an account that could not be given a company', async ()=>{
|
|
259
|
+
companyFeatureEnabled = true;
|
|
260
|
+
userRepo.findOne.mockResolvedValue(null);
|
|
261
|
+
await expect(buildService().completeLogin('code', 'state', REDIRECT_URI)).rejects.toThrow(_common.UnauthorizedException);
|
|
262
|
+
expect(userRepo.save).not.toHaveBeenCalled();
|
|
263
|
+
});
|
|
264
|
+
it('signs in a user who already has a company, company feature and all', async ()=>{
|
|
265
|
+
companyFeatureEnabled = true;
|
|
266
|
+
userRepo.findOne.mockResolvedValue(buildUser());
|
|
267
|
+
await expect(buildService().completeLogin('code', 'state', REDIRECT_URI)).resolves.toEqual({
|
|
268
|
+
accessToken: 'access-token'
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
it('refuses an email the provider has not verified', async ()=>{
|
|
272
|
+
fetchProfile.mockResolvedValue(buildProfile({
|
|
273
|
+
emailVerified: false
|
|
274
|
+
}));
|
|
275
|
+
userRepo.findOne.mockResolvedValue(buildUser());
|
|
276
|
+
await expect(buildService().completeLogin('code', 'state', REDIRECT_URI)).rejects.toThrow(_common.UnauthorizedException);
|
|
277
|
+
expect(userRepo.findOne).not.toHaveBeenCalled();
|
|
278
|
+
});
|
|
279
|
+
it('marks an auto-created account verified, since only verified profiles get that far', async ()=>{
|
|
280
|
+
userRepo.findOne.mockResolvedValue(null);
|
|
281
|
+
await buildService().completeLogin('code', 'state', REDIRECT_URI);
|
|
282
|
+
expect(userRepo.save).toHaveBeenCalledWith(expect.objectContaining({
|
|
283
|
+
emailVerified: true
|
|
284
|
+
}));
|
|
285
|
+
});
|
|
286
|
+
it('refuses a profile the provider gave no email for', async ()=>{
|
|
287
|
+
fetchProfile.mockResolvedValue(buildProfile({
|
|
288
|
+
email: null
|
|
289
|
+
}));
|
|
290
|
+
await expect(buildService().completeLogin('code', 'state', REDIRECT_URI)).rejects.toThrow(_common.UnauthorizedException);
|
|
291
|
+
});
|
|
292
|
+
it('refuses a profile with no account identifier', async ()=>{
|
|
293
|
+
fetchProfile.mockResolvedValue(buildProfile({
|
|
294
|
+
providerUserId: ''
|
|
295
|
+
}));
|
|
296
|
+
await expect(buildService().completeLogin('code', 'state', REDIRECT_URI)).rejects.toThrow(_common.UnauthorizedException);
|
|
297
|
+
});
|
|
298
|
+
it('refuses an unknown or expired state', async ()=>{
|
|
299
|
+
cache.get.mockResolvedValue(undefined);
|
|
300
|
+
await expect(buildService().completeLogin('code', 'state', REDIRECT_URI)).rejects.toThrow(_common.UnauthorizedException);
|
|
301
|
+
});
|
|
302
|
+
it('refuses a redirect URI the sign-in did not start with', async ()=>{
|
|
303
|
+
await expect(buildService().completeLogin('code', 'state', 'https://evil.test/callback')).rejects.toThrow(_common.UnauthorizedException);
|
|
304
|
+
});
|
|
305
|
+
it('burns the state even when the redirect URI does not match, so it cannot be retried', async ()=>{
|
|
306
|
+
await expect(buildService().completeLogin('code', 'state', 'https://evil.test/callback')).rejects.toThrow(_common.UnauthorizedException);
|
|
307
|
+
expect(cache.del).toHaveBeenCalledWith('auth:social:state:state');
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
});
|
|
@@ -5,3 +5,5 @@ export declare const DEFAULT_COOKIE_NAME = "fsn_refresh_token";
|
|
|
5
5
|
export declare const MAX_COOKIE_BYTES = 3500;
|
|
6
6
|
export declare const SESSION_CACHE_PREFIX = "auth:session";
|
|
7
7
|
export declare const TOKEN_REVOKED_PREFIX = "auth:revoked";
|
|
8
|
+
export declare const SOCIAL_STATE_PREFIX = "auth:social:state";
|
|
9
|
+
export declare const SOCIAL_STATE_TTL_MS: number;
|
|
@@ -10,6 +10,7 @@ export declare const AUTH_EVENT_ACTIONS: {
|
|
|
10
10
|
readonly EMAIL_VERIFIED: "email-verified";
|
|
11
11
|
readonly PERMISSION_GRANTED: "permission-granted";
|
|
12
12
|
readonly PERMISSION_REVOKED: "permission-revoked";
|
|
13
|
+
readonly SOCIAL_LOGGED_IN: "social-logged-in";
|
|
13
14
|
};
|
|
14
15
|
export declare const AUTH_EVENT_ENTITIES: {
|
|
15
16
|
readonly USER: "appuser";
|
|
@@ -17,5 +18,6 @@ export declare const AUTH_EVENT_ENTITIES: {
|
|
|
17
18
|
readonly COMPANY_BRANCH: "company_branch";
|
|
18
19
|
readonly SESSION: "session";
|
|
19
20
|
readonly USER_PERMISSION: "user_company_permission";
|
|
21
|
+
readonly SOCIAL_AUTH_CONFIG: "social_auth_config";
|
|
20
22
|
};
|
|
21
23
|
export declare const AUTH_EVENT_MODULE = "auth";
|
package/config/message-keys.d.ts
CHANGED
|
@@ -12,7 +12,9 @@ export declare const AUTH_MESSAGES: {
|
|
|
12
12
|
readonly SELECT_SUCCESS: "auth.select.success";
|
|
13
13
|
readonly SWITCH_COMPANY_SUCCESS: "auth.switch.company.success";
|
|
14
14
|
readonly REGISTER_EMAIL_EXISTS: "auth.register.email.exists";
|
|
15
|
+
readonly REGISTER_ACCOUNT_DELETED: "auth.register.account.deleted";
|
|
15
16
|
readonly REGISTER_COMPANY_ALREADY_ASSIGNED: "auth.register.company.already.assigned";
|
|
17
|
+
readonly REGISTER_SIGNUP_DISABLED: "auth.register.signup.disabled";
|
|
16
18
|
readonly PASSWORD_CHANGE_SUCCESS: "auth.password.change.success";
|
|
17
19
|
readonly PASSWORD_CHANGE_INVALID_CURRENT: "auth.password.change.invalid.current";
|
|
18
20
|
readonly PASSWORD_CHANGE_CURRENT_REQUIRED: "auth.password.change.current.required";
|
|
@@ -49,6 +51,25 @@ export declare const EMAIL_VERIFICATION_MESSAGES: {
|
|
|
49
51
|
readonly VERIFICATION_NOT_ENABLED: "email.verification.not.enabled";
|
|
50
52
|
readonly RESEND_SUCCESS: "email.verification.resend.success";
|
|
51
53
|
};
|
|
54
|
+
export declare const SOCIAL_AUTH_MESSAGES: {
|
|
55
|
+
readonly PROVIDERS_SUCCESS: "auth.social.providers.success";
|
|
56
|
+
readonly AUTHORIZE_SUCCESS: "auth.social.authorize.success";
|
|
57
|
+
readonly LOGIN_SUCCESS: "auth.social.login.success";
|
|
58
|
+
readonly PROVIDER_DISABLED: "auth.social.provider.disabled";
|
|
59
|
+
readonly PROVIDER_NOT_CONFIGURED: "auth.social.provider.not.configured";
|
|
60
|
+
readonly STATE_INVALID: "auth.social.state.invalid";
|
|
61
|
+
readonly REDIRECT_URI_MISMATCH: "auth.social.redirect.uri.mismatch";
|
|
62
|
+
readonly EXCHANGE_FAILED: "auth.social.exchange.failed";
|
|
63
|
+
readonly PROFILE_INCOMPLETE: "auth.social.profile.incomplete";
|
|
64
|
+
readonly ACCOUNT_NOT_LINKED: "auth.social.account.not.linked";
|
|
65
|
+
readonly COMPANY_REQUIRED: "auth.social.company.required";
|
|
66
|
+
readonly EMAIL_REQUIRED: "auth.social.email.required";
|
|
67
|
+
readonly EMAIL_NOT_VERIFIED: "auth.social.email.not.verified";
|
|
68
|
+
readonly ACCOUNT_DELETED: "auth.social.account.deleted";
|
|
69
|
+
};
|
|
70
|
+
export declare const SOCIAL_AUTH_CONFIG_MESSAGES: {
|
|
71
|
+
readonly PROVIDER_ALREADY_CONFIGURED: "social.config.provider.already.configured";
|
|
72
|
+
};
|
|
52
73
|
export declare const USER_MESSAGES: {
|
|
53
74
|
readonly NOT_FOUND: "user.not.found";
|
|
54
75
|
readonly PROFILE_UPDATE_SUCCESS: "user.profile.update.success";
|
package/controllers/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export * from './authentication.controller';
|
|
2
2
|
export * from './company-selection.controller';
|
|
3
3
|
export * from './email-verification.controller';
|
|
4
|
+
export * from './social-auth.controller';
|
|
5
|
+
export * from './social-auth-config.controller';
|
|
4
6
|
export * from './user.controller';
|
|
5
7
|
export * from './company.controller';
|
|
6
8
|
export * from './branch.controller';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { CreateSocialAuthConfigDto, SocialAuthConfigResponseDto, UpdateSocialAuthConfigDto } from '../dtos/social-auth.dto';
|
|
2
|
+
import { SocialAuthConfigService } from '../services/social-auth-config.service';
|
|
3
|
+
declare const SocialAuthConfigController_base: abstract new (service: SocialAuthConfigService) => {
|
|
4
|
+
readonly enabledEndpoints: import("@flusys/nestjs-shared").ApiEndpoint[] | "all";
|
|
5
|
+
service: SocialAuthConfigService;
|
|
6
|
+
isEnabled(endpoint: import("@flusys/nestjs-shared").ApiEndpoint): boolean;
|
|
7
|
+
insert(addDto: CreateSocialAuthConfigDto, user: import("@flusys/nestjs-shared").ILoggedUserInfo | null): Promise<import("@flusys/nestjs-shared").SingleResponseDto<SocialAuthConfigResponseDto>>;
|
|
8
|
+
insertMany(addDto: CreateSocialAuthConfigDto[], user: import("@flusys/nestjs-shared").ILoggedUserInfo | null): Promise<import("@flusys/nestjs-shared").BulkResponseDto<SocialAuthConfigResponseDto>>;
|
|
9
|
+
getById(id: string, body: import("@flusys/nestjs-shared").GetByIdBodyDto, user: import("@flusys/nestjs-shared").ILoggedUserInfo | null): Promise<import("@flusys/nestjs-shared").SingleResponseDto<SocialAuthConfigResponseDto>>;
|
|
10
|
+
getByIds(body: import("@flusys/nestjs-shared").GetByIdsDto, user: import("@flusys/nestjs-shared").ILoggedUserInfo | null): Promise<import("@flusys/nestjs-shared").ListResponseDto<SocialAuthConfigResponseDto>>;
|
|
11
|
+
update(updateDto: UpdateSocialAuthConfigDto, user: import("@flusys/nestjs-shared").ILoggedUserInfo | null): Promise<import("@flusys/nestjs-shared").SingleResponseDto<SocialAuthConfigResponseDto>>;
|
|
12
|
+
updateMany(updateDtos: UpdateSocialAuthConfigDto[], user: import("@flusys/nestjs-shared").ILoggedUserInfo | null): Promise<import("@flusys/nestjs-shared").BulkResponseDto<SocialAuthConfigResponseDto>>;
|
|
13
|
+
bulkUpsert(dtos: (CreateSocialAuthConfigDto | UpdateSocialAuthConfigDto)[], user: import("@flusys/nestjs-shared").ILoggedUserInfo | null): Promise<import("@flusys/nestjs-shared").BulkResponseDto<SocialAuthConfigResponseDto>>;
|
|
14
|
+
getByFilter(filter: Record<string, any>, user: import("@flusys/nestjs-shared").ILoggedUserInfo | null): Promise<import("@flusys/nestjs-shared").SingleResponseDto<SocialAuthConfigResponseDto>>;
|
|
15
|
+
getAll(filterAndPaginationDto: import("@flusys/nestjs-shared").FilterAndPaginationDto, user: import("@flusys/nestjs-shared").ILoggedUserInfo | null, search?: string): Promise<import("@flusys/nestjs-shared").ListResponseDto<SocialAuthConfigResponseDto>>;
|
|
16
|
+
delete(deleteDto: import("@flusys/nestjs-shared").DeleteDto, user: import("@flusys/nestjs-shared").ILoggedUserInfo | null): Promise<import("@flusys/nestjs-shared").MessageResponseDto>;
|
|
17
|
+
};
|
|
18
|
+
export declare class SocialAuthConfigController extends SocialAuthConfigController_base {
|
|
19
|
+
socialAuthConfigService: SocialAuthConfigService;
|
|
20
|
+
constructor(socialAuthConfigService: SocialAuthConfigService);
|
|
21
|
+
}
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { SingleResponseDto } from '@flusys/nestjs-shared/dtos';
|
|
2
|
+
import { CompanySelectionResponseDto, LoginResponseDto } from '../dtos/authentication.dto';
|
|
3
|
+
import { SocialAuthorizeDto, SocialAuthorizeResponseDto, SocialCallbackDto, SocialProviderInfoDto, SocialProvidersDto } from '../dtos/social-auth.dto';
|
|
4
|
+
import { SocialAuthService } from '../services/social-auth.service';
|
|
5
|
+
export declare class SocialAuthController {
|
|
6
|
+
private readonly socialAuthService;
|
|
7
|
+
constructor(socialAuthService: SocialAuthService);
|
|
8
|
+
getProviders(dto: SocialProvidersDto): Promise<SingleResponseDto<SocialProviderInfoDto[]>>;
|
|
9
|
+
authorize(dto: SocialAuthorizeDto): Promise<SingleResponseDto<SocialAuthorizeResponseDto>>;
|
|
10
|
+
callback(dto: SocialCallbackDto): Promise<SingleResponseDto<LoginResponseDto | CompanySelectionResponseDto>>;
|
|
11
|
+
}
|
package/dtos/index.d.ts
CHANGED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { IdentityResponseDto } from '@flusys/nestjs-shared/dtos';
|
|
2
|
+
import { SocialProviderTypeEnum } from '../enums/social-provider-type.enum';
|
|
3
|
+
export declare class CreateSocialAuthConfigDto {
|
|
4
|
+
provider: SocialProviderTypeEnum;
|
|
5
|
+
isEnabled?: boolean;
|
|
6
|
+
clientId?: string;
|
|
7
|
+
clientSecret?: string;
|
|
8
|
+
scopes?: string[];
|
|
9
|
+
extraConfig?: Record<string, unknown>;
|
|
10
|
+
}
|
|
11
|
+
declare const UpdateSocialAuthConfigDto_base: import("@nestjs/common").Type<Partial<CreateSocialAuthConfigDto>>;
|
|
12
|
+
export declare class UpdateSocialAuthConfigDto extends UpdateSocialAuthConfigDto_base {
|
|
13
|
+
id: string;
|
|
14
|
+
}
|
|
15
|
+
export declare class SocialAuthConfigResponseDto extends IdentityResponseDto {
|
|
16
|
+
provider: SocialProviderTypeEnum;
|
|
17
|
+
isEnabled: boolean;
|
|
18
|
+
clientId: string | null;
|
|
19
|
+
hasClientSecret: boolean;
|
|
20
|
+
scopes: string[] | null;
|
|
21
|
+
extraConfig: Record<string, unknown> | null;
|
|
22
|
+
}
|
|
23
|
+
export declare class SocialProviderInfoDto {
|
|
24
|
+
provider: SocialProviderTypeEnum;
|
|
25
|
+
redirectUri: string;
|
|
26
|
+
}
|
|
27
|
+
export declare class SocialProvidersDto {
|
|
28
|
+
redirectUri: string;
|
|
29
|
+
}
|
|
30
|
+
export declare class SocialAuthorizeDto extends SocialProvidersDto {
|
|
31
|
+
provider: SocialProviderTypeEnum;
|
|
32
|
+
}
|
|
33
|
+
export declare class SocialAuthorizeResponseDto {
|
|
34
|
+
provider: SocialProviderTypeEnum;
|
|
35
|
+
authorizationUrl: string;
|
|
36
|
+
state: string;
|
|
37
|
+
}
|
|
38
|
+
export declare class SocialCallbackDto extends SocialProvidersDto {
|
|
39
|
+
code: string;
|
|
40
|
+
state: string;
|
|
41
|
+
}
|
|
42
|
+
export {};
|
package/entities/index.d.ts
CHANGED
|
@@ -2,15 +2,17 @@ export * from './app-user.entity';
|
|
|
2
2
|
export * from './company-branch.entity';
|
|
3
3
|
export * from './company.entity';
|
|
4
4
|
export * from './pending-registration.entity';
|
|
5
|
+
export * from './social-auth-config.entity';
|
|
5
6
|
export { PermissionType, UserCompanyPermission } from './user-company-permission.entity';
|
|
6
7
|
export * from './user-token.entity';
|
|
7
8
|
import { AppUser } from './app-user.entity';
|
|
8
9
|
import { CompanyBranch } from './company-branch.entity';
|
|
9
10
|
import { Company } from './company.entity';
|
|
10
11
|
import { PendingRegistration } from './pending-registration.entity';
|
|
12
|
+
import { SocialAuthConfig } from './social-auth-config.entity';
|
|
11
13
|
import { UserCompanyPermission } from './user-company-permission.entity';
|
|
12
14
|
import { UserToken } from './user-token.entity';
|
|
13
|
-
declare const BaseEntities: (typeof AppUser | typeof UserToken)[];
|
|
15
|
+
declare const BaseEntities: (typeof AppUser | typeof SocialAuthConfig | typeof UserToken)[];
|
|
14
16
|
declare const EmailEntities: (typeof PendingRegistration)[];
|
|
15
17
|
declare const CompanyEntities: (typeof Company | typeof CompanyBranch | typeof UserCompanyPermission)[];
|
|
16
18
|
export interface IAuthEntitiesConfig {
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Identity } from '@flusys/nestjs-shared/entities';
|
|
2
|
+
export declare class SocialAuthConfig extends Identity {
|
|
3
|
+
provider: string;
|
|
4
|
+
isEnabled: boolean;
|
|
5
|
+
clientId: string | null;
|
|
6
|
+
clientSecret: string | null;
|
|
7
|
+
scopes: string[] | null;
|
|
8
|
+
extraConfig: Record<string, unknown> | null;
|
|
9
|
+
}
|
package/enums/index.d.ts
CHANGED
|
@@ -9,3 +9,6 @@ export const MAX_COOKIE_BYTES = 3500;
|
|
|
9
9
|
// Cache Key Prefixes
|
|
10
10
|
export const SESSION_CACHE_PREFIX = 'auth:session';
|
|
11
11
|
export const TOKEN_REVOKED_PREFIX = 'auth:revoked';
|
|
12
|
+
export const SOCIAL_STATE_PREFIX = 'auth:social:state';
|
|
13
|
+
// Social Login
|
|
14
|
+
export const SOCIAL_STATE_TTL_MS = 10 * 60 * 1000; // 10 minutes
|
|
@@ -12,13 +12,15 @@
|
|
|
12
12
|
PASSWORD_RESET: 'password-reset',
|
|
13
13
|
EMAIL_VERIFIED: 'email-verified',
|
|
14
14
|
PERMISSION_GRANTED: 'permission-granted',
|
|
15
|
-
PERMISSION_REVOKED: 'permission-revoked'
|
|
15
|
+
PERMISSION_REVOKED: 'permission-revoked',
|
|
16
|
+
SOCIAL_LOGGED_IN: 'social-logged-in'
|
|
16
17
|
};
|
|
17
18
|
/** Every entity the auth module publishes under, ApiService backed or not */ export const AUTH_EVENT_ENTITIES = {
|
|
18
19
|
USER: 'appuser',
|
|
19
20
|
COMPANY: 'company',
|
|
20
21
|
COMPANY_BRANCH: 'company_branch',
|
|
21
22
|
SESSION: 'session',
|
|
22
|
-
USER_PERMISSION: 'user_company_permission'
|
|
23
|
+
USER_PERMISSION: 'user_company_permission',
|
|
24
|
+
SOCIAL_AUTH_CONFIG: 'social_auth_config'
|
|
23
25
|
};
|
|
24
26
|
export const AUTH_EVENT_MODULE = 'auth';
|
|
@@ -13,7 +13,9 @@ export const AUTH_MESSAGES = {
|
|
|
13
13
|
SELECT_SUCCESS: 'auth.select.success',
|
|
14
14
|
SWITCH_COMPANY_SUCCESS: 'auth.switch.company.success',
|
|
15
15
|
REGISTER_EMAIL_EXISTS: 'auth.register.email.exists',
|
|
16
|
+
REGISTER_ACCOUNT_DELETED: 'auth.register.account.deleted',
|
|
16
17
|
REGISTER_COMPANY_ALREADY_ASSIGNED: 'auth.register.company.already.assigned',
|
|
18
|
+
REGISTER_SIGNUP_DISABLED: 'auth.register.signup.disabled',
|
|
17
19
|
PASSWORD_CHANGE_SUCCESS: 'auth.password.change.success',
|
|
18
20
|
PASSWORD_CHANGE_INVALID_CURRENT: 'auth.password.change.invalid.current',
|
|
19
21
|
PASSWORD_CHANGE_CURRENT_REQUIRED: 'auth.password.change.current.required',
|
|
@@ -50,6 +52,25 @@ export const EMAIL_VERIFICATION_MESSAGES = {
|
|
|
50
52
|
VERIFICATION_NOT_ENABLED: 'email.verification.not.enabled',
|
|
51
53
|
RESEND_SUCCESS: 'email.verification.resend.success'
|
|
52
54
|
};
|
|
55
|
+
export const SOCIAL_AUTH_MESSAGES = {
|
|
56
|
+
PROVIDERS_SUCCESS: 'auth.social.providers.success',
|
|
57
|
+
AUTHORIZE_SUCCESS: 'auth.social.authorize.success',
|
|
58
|
+
LOGIN_SUCCESS: 'auth.social.login.success',
|
|
59
|
+
PROVIDER_DISABLED: 'auth.social.provider.disabled',
|
|
60
|
+
PROVIDER_NOT_CONFIGURED: 'auth.social.provider.not.configured',
|
|
61
|
+
STATE_INVALID: 'auth.social.state.invalid',
|
|
62
|
+
REDIRECT_URI_MISMATCH: 'auth.social.redirect.uri.mismatch',
|
|
63
|
+
EXCHANGE_FAILED: 'auth.social.exchange.failed',
|
|
64
|
+
PROFILE_INCOMPLETE: 'auth.social.profile.incomplete',
|
|
65
|
+
ACCOUNT_NOT_LINKED: 'auth.social.account.not.linked',
|
|
66
|
+
COMPANY_REQUIRED: 'auth.social.company.required',
|
|
67
|
+
EMAIL_REQUIRED: 'auth.social.email.required',
|
|
68
|
+
EMAIL_NOT_VERIFIED: 'auth.social.email.not.verified',
|
|
69
|
+
ACCOUNT_DELETED: 'auth.social.account.deleted'
|
|
70
|
+
};
|
|
71
|
+
export const SOCIAL_AUTH_CONFIG_MESSAGES = {
|
|
72
|
+
PROVIDER_ALREADY_CONFIGURED: 'social.config.provider.already.configured'
|
|
73
|
+
};
|
|
53
74
|
export const USER_MESSAGES = {
|
|
54
75
|
NOT_FOUND: 'user.not.found',
|
|
55
76
|
PROFILE_UPDATE_SUCCESS: 'user.profile.update.success',
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export * from './authentication.controller';
|
|
2
2
|
export * from './company-selection.controller';
|
|
3
3
|
export * from './email-verification.controller';
|
|
4
|
+
export * from './social-auth.controller';
|
|
5
|
+
export * from './social-auth-config.controller';
|
|
4
6
|
export * from './user.controller';
|
|
5
7
|
export * from './company.controller';
|
|
6
8
|
export * from './branch.controller';
|