@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
|
@@ -2,6 +2,7 @@ import { getAuthEntitiesByConfig } from './index';
|
|
|
2
2
|
import { AppUser } from './app-user.entity';
|
|
3
3
|
import { UserToken } from './user-token.entity';
|
|
4
4
|
import { PendingRegistration } from './pending-registration.entity';
|
|
5
|
+
import { SocialAuthConfig } from './social-auth-config.entity';
|
|
5
6
|
import { Company } from './company.entity';
|
|
6
7
|
import { CompanyBranch } from './company-branch.entity';
|
|
7
8
|
import { UserCompanyPermission } from './user-company-permission.entity';
|
|
@@ -16,6 +17,15 @@ describe('getAuthEntitiesByConfig', ()=>{
|
|
|
16
17
|
UserToken
|
|
17
18
|
]));
|
|
18
19
|
});
|
|
20
|
+
it('should always include the social sign-in config entity, which no flag gates', ()=>{
|
|
21
|
+
const entities = getAuthEntitiesByConfig({
|
|
22
|
+
enableCompanyFeature: false,
|
|
23
|
+
enableEmailVerification: false
|
|
24
|
+
});
|
|
25
|
+
expect(entities).toEqual(expect.arrayContaining([
|
|
26
|
+
SocialAuthConfig
|
|
27
|
+
]));
|
|
28
|
+
});
|
|
19
29
|
it('should include PendingRegistration when email verification is enabled', ()=>{
|
|
20
30
|
const entities = getAuthEntitiesByConfig({
|
|
21
31
|
enableEmailVerification: true
|
|
@@ -56,10 +66,11 @@ describe('getAuthEntitiesByConfig', ()=>{
|
|
|
56
66
|
enableCompanyFeature: true,
|
|
57
67
|
enableEmailVerification: true
|
|
58
68
|
});
|
|
59
|
-
expect(entities).toHaveLength(
|
|
69
|
+
expect(entities).toHaveLength(7);
|
|
60
70
|
expect(entities).toEqual(expect.arrayContaining([
|
|
61
71
|
AppUser,
|
|
62
72
|
UserToken,
|
|
73
|
+
SocialAuthConfig,
|
|
63
74
|
PendingRegistration,
|
|
64
75
|
Company,
|
|
65
76
|
CompanyBranch,
|
|
@@ -0,0 +1,92 @@
|
|
|
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
|
+
import { getJsonColumnType } from '@flusys/nestjs-shared';
|
|
24
|
+
import { Identity } from '@flusys/nestjs-shared/entities';
|
|
25
|
+
import { Column, Entity, Index } from 'typeorm';
|
|
26
|
+
export class SocialAuthConfig extends Identity {
|
|
27
|
+
constructor(...args){
|
|
28
|
+
super(...args), _define_property(this, "provider", void 0), _define_property(this, "isEnabled", void 0), _define_property(this, "clientId", void 0), _define_property(this, "clientSecret", void 0), _define_property(this, "scopes", void 0), _define_property(this, "extraConfig", void 0);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
_ts_decorate([
|
|
32
|
+
Column({
|
|
33
|
+
type: 'varchar',
|
|
34
|
+
length: 32,
|
|
35
|
+
nullable: false
|
|
36
|
+
}),
|
|
37
|
+
_ts_metadata("design:type", String)
|
|
38
|
+
], SocialAuthConfig.prototype, "provider", void 0);
|
|
39
|
+
_ts_decorate([
|
|
40
|
+
Column({
|
|
41
|
+
type: 'boolean',
|
|
42
|
+
default: false,
|
|
43
|
+
name: 'is_enabled'
|
|
44
|
+
}),
|
|
45
|
+
_ts_metadata("design:type", Boolean)
|
|
46
|
+
], SocialAuthConfig.prototype, "isEnabled", void 0);
|
|
47
|
+
_ts_decorate([
|
|
48
|
+
Column({
|
|
49
|
+
type: 'varchar',
|
|
50
|
+
length: 512,
|
|
51
|
+
nullable: true,
|
|
52
|
+
name: 'client_id'
|
|
53
|
+
}),
|
|
54
|
+
_ts_metadata("design:type", Object)
|
|
55
|
+
], SocialAuthConfig.prototype, "clientId", void 0);
|
|
56
|
+
_ts_decorate([
|
|
57
|
+
Column({
|
|
58
|
+
type: 'text',
|
|
59
|
+
nullable: true,
|
|
60
|
+
name: 'client_secret'
|
|
61
|
+
}),
|
|
62
|
+
_ts_metadata("design:type", Object)
|
|
63
|
+
], SocialAuthConfig.prototype, "clientSecret", void 0);
|
|
64
|
+
_ts_decorate([
|
|
65
|
+
Column({
|
|
66
|
+
type: 'simple-array',
|
|
67
|
+
nullable: true
|
|
68
|
+
}),
|
|
69
|
+
_ts_metadata("design:type", Object)
|
|
70
|
+
], SocialAuthConfig.prototype, "scopes", void 0);
|
|
71
|
+
_ts_decorate([
|
|
72
|
+
Column({
|
|
73
|
+
type: getJsonColumnType(),
|
|
74
|
+
nullable: true,
|
|
75
|
+
name: 'extra_config'
|
|
76
|
+
}),
|
|
77
|
+
_ts_metadata("design:type", Object)
|
|
78
|
+
], SocialAuthConfig.prototype, "extraConfig", void 0);
|
|
79
|
+
SocialAuthConfig = _ts_decorate([
|
|
80
|
+
Entity({
|
|
81
|
+
name: 'social_auth_config'
|
|
82
|
+
}),
|
|
83
|
+
Index('IDX_social_auth_config_provider', [
|
|
84
|
+
'provider'
|
|
85
|
+
], {
|
|
86
|
+
unique: true,
|
|
87
|
+
where: '"deleted_at" IS NULL'
|
|
88
|
+
}),
|
|
89
|
+
Index([
|
|
90
|
+
'isEnabled'
|
|
91
|
+
])
|
|
92
|
+
], SocialAuthConfig);
|
package/fesm/enums/index.js
CHANGED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export var SocialProviderTypeEnum = /*#__PURE__*/ function(SocialProviderTypeEnum) {
|
|
2
|
+
SocialProviderTypeEnum["GOOGLE"] = "google";
|
|
3
|
+
SocialProviderTypeEnum["FACEBOOK"] = "facebook";
|
|
4
|
+
SocialProviderTypeEnum["LINKEDIN"] = "linkedin";
|
|
5
|
+
SocialProviderTypeEnum["MICROSOFT"] = "microsoft";
|
|
6
|
+
return SocialProviderTypeEnum;
|
|
7
|
+
}({});
|
package/fesm/index.js
CHANGED
package/fesm/interfaces/index.js
CHANGED
|
@@ -2,6 +2,7 @@ export * from './authentication.interface';
|
|
|
2
2
|
export * from './auth-email-provider.interface';
|
|
3
3
|
export * from './company-branch.interface';
|
|
4
4
|
export * from './company.interface';
|
|
5
|
+
export * from './social-auth.interface';
|
|
5
6
|
export * from './user.interface';
|
|
6
7
|
export * from './user-enricher.interface';
|
|
7
8
|
export * from './auth-module-options.interface';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -10,11 +10,11 @@ import { JwtModule } from '@nestjs/jwt';
|
|
|
10
10
|
import { PassportModule } from '@nestjs/passport';
|
|
11
11
|
import { JwtAuthGuard } from '@flusys/nestjs-shared/guards';
|
|
12
12
|
import { AUTH_MODULE_OPTIONS } from '../config/auth.constants';
|
|
13
|
-
import { AuthenticationController, BranchController, CompanyController, CompanySelectionController, EmailVerificationController, UserController, UserPermissionController } from '../controllers';
|
|
13
|
+
import { AuthenticationController, BranchController, CompanyController, CompanySelectionController, EmailVerificationController, SocialAuthConfigController, SocialAuthController, UserController, UserPermissionController } from '../controllers';
|
|
14
14
|
import { getAuthEntitiesByConfig } from '../entities';
|
|
15
15
|
import { ClearToken } from '../interceptors/clear-token.interceptor';
|
|
16
16
|
import { SetToken } from '../interceptors/set-token.interceptor';
|
|
17
|
-
import { AuthEmailService, AuthenticationService, BranchService, CompanySelectionSessionService, CompanyService, UserPermissionService, UserService } from '../services';
|
|
17
|
+
import { AuthEmailService, AuthenticationService, BranchService, CompanySelectionSessionService, CompanyService, SocialAuthConfigService, SocialAuthService, UserPermissionService, UserService } from '../services';
|
|
18
18
|
import { AuthConfigService } from '../services/auth-config.service';
|
|
19
19
|
import { AuthDataSourceProvider } from '../services/auth-datasource.provider';
|
|
20
20
|
import { JwtStrategy } from '../strategies/jwt.strategy';
|
|
@@ -74,6 +74,8 @@ export class AuthModule {
|
|
|
74
74
|
if (!includeController) return [];
|
|
75
75
|
const controllers = [
|
|
76
76
|
AuthenticationController,
|
|
77
|
+
SocialAuthController,
|
|
78
|
+
SocialAuthConfigController,
|
|
77
79
|
UserController
|
|
78
80
|
];
|
|
79
81
|
if (flags.enableEmailVerification) {
|
|
@@ -91,6 +93,8 @@ export class AuthModule {
|
|
|
91
93
|
JwtStrategy,
|
|
92
94
|
JwtAuthGuard,
|
|
93
95
|
AuthenticationService,
|
|
96
|
+
SocialAuthService,
|
|
97
|
+
SocialAuthConfigService,
|
|
94
98
|
SetToken,
|
|
95
99
|
ClearToken,
|
|
96
100
|
UserService,
|
|
@@ -113,6 +117,8 @@ export class AuthModule {
|
|
|
113
117
|
JwtStrategy,
|
|
114
118
|
JwtAuthGuard,
|
|
115
119
|
AuthenticationService,
|
|
120
|
+
SocialAuthService,
|
|
121
|
+
SocialAuthConfigService,
|
|
116
122
|
UserService
|
|
117
123
|
];
|
|
118
124
|
if (flags.enableEmailVerification) {
|
|
@@ -13,8 +13,8 @@ function _define_property(obj, key, value) {
|
|
|
13
13
|
}
|
|
14
14
|
import { AuthModule } from './auth.module';
|
|
15
15
|
import { AUTH_MODULE_OPTIONS } from '../config/auth.constants';
|
|
16
|
-
import { AuthenticationController, BranchController, CompanyController, CompanySelectionController, EmailVerificationController, UserController, UserPermissionController } from '../controllers';
|
|
17
|
-
import { AuthEmailService, BranchService, CompanySelectionSessionService, CompanyService, UserPermissionService } from '../services';
|
|
16
|
+
import { AuthenticationController, BranchController, CompanyController, CompanySelectionController, EmailVerificationController, SocialAuthConfigController, SocialAuthController, UserController, UserPermissionController } from '../controllers';
|
|
17
|
+
import { AuthEmailService, BranchService, CompanySelectionSessionService, CompanyService, SocialAuthConfigService, SocialAuthService, UserPermissionService } from '../services';
|
|
18
18
|
function providerTokens(providers) {
|
|
19
19
|
return providers.map((p)=>typeof p === 'function' ? p : p.provide);
|
|
20
20
|
}
|
|
@@ -30,11 +30,26 @@ describe('AuthModule', ()=>{
|
|
|
30
30
|
expect(dynamicModule.global).toBe(false);
|
|
31
31
|
expect(dynamicModule.controllers).toEqual([
|
|
32
32
|
AuthenticationController,
|
|
33
|
+
SocialAuthController,
|
|
34
|
+
SocialAuthConfigController,
|
|
33
35
|
UserController
|
|
34
36
|
]);
|
|
35
37
|
expect(providerTokens(dynamicModule.providers)).not.toContain(AuthEmailService);
|
|
36
38
|
expect(providerTokens(dynamicModule.providers)).not.toContain(CompanyService);
|
|
37
39
|
});
|
|
40
|
+
it('should always register social sign-in, whatever the feature flags say', ()=>{
|
|
41
|
+
const dynamicModule = AuthModule.forRoot({
|
|
42
|
+
bootstrapAppConfig: {
|
|
43
|
+
enableCompanyFeature: false
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
expect(dynamicModule.controllers).toContain(SocialAuthController);
|
|
47
|
+
expect(dynamicModule.controllers).toContain(SocialAuthConfigController);
|
|
48
|
+
expect(providerTokens(dynamicModule.providers)).toContain(SocialAuthService);
|
|
49
|
+
expect(providerTokens(dynamicModule.providers)).toContain(SocialAuthConfigService);
|
|
50
|
+
expect(dynamicModule.exports).toContain(SocialAuthService);
|
|
51
|
+
expect(dynamicModule.exports).toContain(SocialAuthConfigService);
|
|
52
|
+
});
|
|
38
53
|
it('should respect global: true', ()=>{
|
|
39
54
|
const dynamicModule = AuthModule.forRoot({
|
|
40
55
|
global: true
|
|
@@ -0,0 +1,63 @@
|
|
|
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
|
+
import { SocialProviderTypeEnum } from '../enums/social-provider-type.enum';
|
|
15
|
+
import { buildQuery, getJson, postForm, readAccessToken, readString, resolveScopes } from './oauth-http.util';
|
|
16
|
+
const GRAPH_VERSION = 'v21.0';
|
|
17
|
+
const AUTHORIZE_URL = `https://www.facebook.com/${GRAPH_VERSION}/dialog/oauth`;
|
|
18
|
+
const TOKEN_URL = `https://graph.facebook.com/${GRAPH_VERSION}/oauth/access_token`;
|
|
19
|
+
const PROFILE_URL = `https://graph.facebook.com/${GRAPH_VERSION}/me?fields=id,name,email,picture.type(large)`;
|
|
20
|
+
function readAvatarUrl(profile) {
|
|
21
|
+
const picture = profile['picture'];
|
|
22
|
+
if (typeof picture !== 'object' || picture === null) return null;
|
|
23
|
+
const data = picture['data'];
|
|
24
|
+
if (typeof data !== 'object' || data === null) return null;
|
|
25
|
+
return readString(data, 'url');
|
|
26
|
+
}
|
|
27
|
+
let FacebookSocialProvider = class FacebookSocialProvider {
|
|
28
|
+
buildAuthorizationUrl(credentials, state) {
|
|
29
|
+
const query = buildQuery({
|
|
30
|
+
client_id: credentials.clientId,
|
|
31
|
+
redirect_uri: credentials.redirectUri,
|
|
32
|
+
response_type: 'code',
|
|
33
|
+
scope: resolveScopes(credentials, this.defaultScopes, ','),
|
|
34
|
+
state
|
|
35
|
+
});
|
|
36
|
+
return `${AUTHORIZE_URL}?${query}`;
|
|
37
|
+
}
|
|
38
|
+
async fetchProfile(credentials, code) {
|
|
39
|
+
const token = await postForm(this.provider, TOKEN_URL, {
|
|
40
|
+
code,
|
|
41
|
+
client_id: credentials.clientId,
|
|
42
|
+
client_secret: credentials.clientSecret,
|
|
43
|
+
redirect_uri: credentials.redirectUri
|
|
44
|
+
});
|
|
45
|
+
const profile = await getJson(this.provider, PROFILE_URL, readAccessToken(this.provider, token));
|
|
46
|
+
const email = readString(profile, 'email');
|
|
47
|
+
return {
|
|
48
|
+
providerUserId: readString(profile, 'id') ?? '',
|
|
49
|
+
email,
|
|
50
|
+
emailVerified: !!email,
|
|
51
|
+
name: readString(profile, 'name'),
|
|
52
|
+
avatarUrl: readAvatarUrl(profile)
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
constructor(){
|
|
56
|
+
_define_property(this, "provider", SocialProviderTypeEnum.FACEBOOK);
|
|
57
|
+
_define_property(this, "defaultScopes", [
|
|
58
|
+
'email',
|
|
59
|
+
'public_profile'
|
|
60
|
+
]);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
export const facebookSocialProvider = new FacebookSocialProvider();
|
|
@@ -0,0 +1,58 @@
|
|
|
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
|
+
import { SocialProviderTypeEnum } from '../enums/social-provider-type.enum';
|
|
15
|
+
import { buildQuery, getJson, postForm, readAccessToken, readString, resolveScopes } from './oauth-http.util';
|
|
16
|
+
const AUTHORIZE_URL = 'https://accounts.google.com/o/oauth2/v2/auth';
|
|
17
|
+
const TOKEN_URL = 'https://oauth2.googleapis.com/token';
|
|
18
|
+
const USERINFO_URL = 'https://openidconnect.googleapis.com/v1/userinfo';
|
|
19
|
+
let GoogleSocialProvider = class GoogleSocialProvider {
|
|
20
|
+
buildAuthorizationUrl(credentials, state) {
|
|
21
|
+
const query = buildQuery({
|
|
22
|
+
client_id: credentials.clientId,
|
|
23
|
+
redirect_uri: credentials.redirectUri,
|
|
24
|
+
response_type: 'code',
|
|
25
|
+
scope: resolveScopes(credentials, this.defaultScopes),
|
|
26
|
+
state,
|
|
27
|
+
access_type: 'online',
|
|
28
|
+
prompt: 'select_account'
|
|
29
|
+
});
|
|
30
|
+
return `${AUTHORIZE_URL}?${query}`;
|
|
31
|
+
}
|
|
32
|
+
async fetchProfile(credentials, code) {
|
|
33
|
+
const token = await postForm(this.provider, TOKEN_URL, {
|
|
34
|
+
code,
|
|
35
|
+
client_id: credentials.clientId,
|
|
36
|
+
client_secret: credentials.clientSecret,
|
|
37
|
+
redirect_uri: credentials.redirectUri,
|
|
38
|
+
grant_type: 'authorization_code'
|
|
39
|
+
});
|
|
40
|
+
const profile = await getJson(this.provider, USERINFO_URL, readAccessToken(this.provider, token));
|
|
41
|
+
return {
|
|
42
|
+
providerUserId: readString(profile, 'sub') ?? '',
|
|
43
|
+
email: readString(profile, 'email'),
|
|
44
|
+
emailVerified: profile['email_verified'] === true,
|
|
45
|
+
name: readString(profile, 'name'),
|
|
46
|
+
avatarUrl: readString(profile, 'picture')
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
constructor(){
|
|
50
|
+
_define_property(this, "provider", SocialProviderTypeEnum.GOOGLE);
|
|
51
|
+
_define_property(this, "defaultScopes", [
|
|
52
|
+
'openid',
|
|
53
|
+
'email',
|
|
54
|
+
'profile'
|
|
55
|
+
]);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
export const googleSocialProvider = new GoogleSocialProvider();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './social-provider.registry';
|
|
@@ -0,0 +1,56 @@
|
|
|
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
|
+
import { SocialProviderTypeEnum } from '../enums/social-provider-type.enum';
|
|
15
|
+
import { buildQuery, getJson, postForm, readAccessToken, readString, resolveScopes } from './oauth-http.util';
|
|
16
|
+
const AUTHORIZE_URL = 'https://www.linkedin.com/oauth/v2/authorization';
|
|
17
|
+
const TOKEN_URL = 'https://www.linkedin.com/oauth/v2/accessToken';
|
|
18
|
+
const USERINFO_URL = 'https://api.linkedin.com/v2/userinfo';
|
|
19
|
+
let LinkedInSocialProvider = class LinkedInSocialProvider {
|
|
20
|
+
buildAuthorizationUrl(credentials, state) {
|
|
21
|
+
const query = buildQuery({
|
|
22
|
+
response_type: 'code',
|
|
23
|
+
client_id: credentials.clientId,
|
|
24
|
+
redirect_uri: credentials.redirectUri,
|
|
25
|
+
scope: resolveScopes(credentials, this.defaultScopes),
|
|
26
|
+
state
|
|
27
|
+
});
|
|
28
|
+
return `${AUTHORIZE_URL}?${query}`;
|
|
29
|
+
}
|
|
30
|
+
async fetchProfile(credentials, code) {
|
|
31
|
+
const token = await postForm(this.provider, TOKEN_URL, {
|
|
32
|
+
grant_type: 'authorization_code',
|
|
33
|
+
code,
|
|
34
|
+
client_id: credentials.clientId,
|
|
35
|
+
client_secret: credentials.clientSecret,
|
|
36
|
+
redirect_uri: credentials.redirectUri
|
|
37
|
+
});
|
|
38
|
+
const profile = await getJson(this.provider, USERINFO_URL, readAccessToken(this.provider, token));
|
|
39
|
+
return {
|
|
40
|
+
providerUserId: readString(profile, 'sub') ?? '',
|
|
41
|
+
email: readString(profile, 'email'),
|
|
42
|
+
emailVerified: profile['email_verified'] === true,
|
|
43
|
+
name: readString(profile, 'name'),
|
|
44
|
+
avatarUrl: readString(profile, 'picture')
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
constructor(){
|
|
48
|
+
_define_property(this, "provider", SocialProviderTypeEnum.LINKEDIN);
|
|
49
|
+
_define_property(this, "defaultScopes", [
|
|
50
|
+
'openid',
|
|
51
|
+
'profile',
|
|
52
|
+
'email'
|
|
53
|
+
]);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
export const linkedInSocialProvider = new LinkedInSocialProvider();
|
|
@@ -0,0 +1,68 @@
|
|
|
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
|
+
import { SocialProviderTypeEnum } from '../enums/social-provider-type.enum';
|
|
15
|
+
import { buildQuery, getJson, postForm, readAccessToken, readString, resolveScopes } from './oauth-http.util';
|
|
16
|
+
const GRAPH_ME_URL = 'https://graph.microsoft.com/v1.0/me';
|
|
17
|
+
const DEFAULT_TENANT = 'common';
|
|
18
|
+
function resolveTenant(credentials) {
|
|
19
|
+
const tenantId = credentials.extraConfig?.['tenantId'];
|
|
20
|
+
return typeof tenantId === 'string' && tenantId ? tenantId : DEFAULT_TENANT;
|
|
21
|
+
}
|
|
22
|
+
function endpoint(credentials, action) {
|
|
23
|
+
return `https://login.microsoftonline.com/${resolveTenant(credentials)}/oauth2/v2.0/${action}`;
|
|
24
|
+
}
|
|
25
|
+
let MicrosoftSocialProvider = class MicrosoftSocialProvider {
|
|
26
|
+
buildAuthorizationUrl(credentials, state) {
|
|
27
|
+
const query = buildQuery({
|
|
28
|
+
client_id: credentials.clientId,
|
|
29
|
+
response_type: 'code',
|
|
30
|
+
redirect_uri: credentials.redirectUri,
|
|
31
|
+
response_mode: 'query',
|
|
32
|
+
scope: resolveScopes(credentials, this.defaultScopes),
|
|
33
|
+
state
|
|
34
|
+
});
|
|
35
|
+
return `${endpoint(credentials, 'authorize')}?${query}`;
|
|
36
|
+
}
|
|
37
|
+
async fetchProfile(credentials, code) {
|
|
38
|
+
const token = await postForm(this.provider, endpoint(credentials, 'token'), {
|
|
39
|
+
client_id: credentials.clientId,
|
|
40
|
+
client_secret: credentials.clientSecret,
|
|
41
|
+
code,
|
|
42
|
+
redirect_uri: credentials.redirectUri,
|
|
43
|
+
grant_type: 'authorization_code',
|
|
44
|
+
scope: resolveScopes(credentials, this.defaultScopes)
|
|
45
|
+
});
|
|
46
|
+
const profile = await getJson(this.provider, GRAPH_ME_URL, readAccessToken(this.provider, token));
|
|
47
|
+
// `mail` is Microsoft's confirmed mailbox claim; `userPrincipalName` is a sign-in
|
|
48
|
+
// identifier that is not guaranteed to be a verified, owned mailbox address.
|
|
49
|
+
const verifiedEmail = readString(profile, 'mail');
|
|
50
|
+
return {
|
|
51
|
+
providerUserId: readString(profile, 'id') ?? '',
|
|
52
|
+
email: verifiedEmail ?? readString(profile, 'userPrincipalName'),
|
|
53
|
+
emailVerified: !!verifiedEmail,
|
|
54
|
+
name: readString(profile, 'displayName'),
|
|
55
|
+
avatarUrl: null
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
constructor(){
|
|
59
|
+
_define_property(this, "provider", SocialProviderTypeEnum.MICROSOFT);
|
|
60
|
+
_define_property(this, "defaultScopes", [
|
|
61
|
+
'openid',
|
|
62
|
+
'profile',
|
|
63
|
+
'email',
|
|
64
|
+
'User.Read'
|
|
65
|
+
]);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
export const microsoftSocialProvider = new MicrosoftSocialProvider();
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { UnauthorizedException } from '@nestjs/common';
|
|
2
|
+
import { SOCIAL_AUTH_MESSAGES } from '../config/message-keys';
|
|
3
|
+
const OAUTH_HTTP_TIMEOUT_MS = 10_000;
|
|
4
|
+
function failExchange(provider, detail) {
|
|
5
|
+
throw new UnauthorizedException({
|
|
6
|
+
message: `Social sign-in with ${provider} failed: ${detail}`,
|
|
7
|
+
messageKey: SOCIAL_AUTH_MESSAGES.EXCHANGE_FAILED,
|
|
8
|
+
messageVariables: {
|
|
9
|
+
provider
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
async function fetchWithTimeout(provider, url, init) {
|
|
14
|
+
try {
|
|
15
|
+
return await fetch(url, {
|
|
16
|
+
...init,
|
|
17
|
+
signal: AbortSignal.timeout(OAUTH_HTTP_TIMEOUT_MS)
|
|
18
|
+
});
|
|
19
|
+
} catch (error) {
|
|
20
|
+
if (error instanceof Error && error.name === 'TimeoutError') {
|
|
21
|
+
failExchange(provider, 'request timed out');
|
|
22
|
+
}
|
|
23
|
+
throw error;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
async function readJson(provider, response) {
|
|
27
|
+
const body = await response.text();
|
|
28
|
+
if (!response.ok) {
|
|
29
|
+
failExchange(provider, `${response.status} ${body.slice(0, 200)}`);
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
return JSON.parse(body);
|
|
33
|
+
} catch {
|
|
34
|
+
return failExchange(provider, 'response was not JSON');
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export async function postForm(provider, url, form) {
|
|
38
|
+
const response = await fetchWithTimeout(provider, url, {
|
|
39
|
+
method: 'POST',
|
|
40
|
+
headers: {
|
|
41
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
42
|
+
Accept: 'application/json'
|
|
43
|
+
},
|
|
44
|
+
body: new URLSearchParams(form).toString()
|
|
45
|
+
});
|
|
46
|
+
return readJson(provider, response);
|
|
47
|
+
}
|
|
48
|
+
export async function getJson(provider, url, accessToken) {
|
|
49
|
+
const response = await fetchWithTimeout(provider, url, {
|
|
50
|
+
headers: {
|
|
51
|
+
Authorization: `Bearer ${accessToken}`,
|
|
52
|
+
Accept: 'application/json'
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
return readJson(provider, response);
|
|
56
|
+
}
|
|
57
|
+
export function readAccessToken(provider, token) {
|
|
58
|
+
const accessToken = token['access_token'];
|
|
59
|
+
if (typeof accessToken !== 'string' || !accessToken) {
|
|
60
|
+
failExchange(provider, 'no access token returned');
|
|
61
|
+
}
|
|
62
|
+
return accessToken;
|
|
63
|
+
}
|
|
64
|
+
export function readString(source, key) {
|
|
65
|
+
const value = source[key];
|
|
66
|
+
return typeof value === 'string' && value ? value : null;
|
|
67
|
+
}
|
|
68
|
+
export function buildQuery(params) {
|
|
69
|
+
const query = new URLSearchParams();
|
|
70
|
+
for (const [key, value] of Object.entries(params)){
|
|
71
|
+
if (value) query.append(key, value);
|
|
72
|
+
}
|
|
73
|
+
return query.toString();
|
|
74
|
+
}
|
|
75
|
+
export function resolveScopes(credentials, defaults, separator = ' ') {
|
|
76
|
+
return (credentials.scopes?.length ? credentials.scopes : defaults).join(separator);
|
|
77
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { SocialProviderTypeEnum } from '../enums/social-provider-type.enum';
|
|
2
|
+
import { facebookSocialProvider } from './facebook.social-provider';
|
|
3
|
+
import { googleSocialProvider } from './google.social-provider';
|
|
4
|
+
import { linkedInSocialProvider } from './linkedin.social-provider';
|
|
5
|
+
import { microsoftSocialProvider } from './microsoft.social-provider';
|
|
6
|
+
const PROVIDERS = {
|
|
7
|
+
[SocialProviderTypeEnum.GOOGLE]: googleSocialProvider,
|
|
8
|
+
[SocialProviderTypeEnum.FACEBOOK]: facebookSocialProvider,
|
|
9
|
+
[SocialProviderTypeEnum.LINKEDIN]: linkedInSocialProvider,
|
|
10
|
+
[SocialProviderTypeEnum.MICROSOFT]: microsoftSocialProvider
|
|
11
|
+
};
|
|
12
|
+
export function getSocialProvider(provider) {
|
|
13
|
+
return PROVIDERS[provider];
|
|
14
|
+
}
|