UNPKG

@bitwild/rockets-auth

Version:

Rockets Auth - Complete authentication and authorization solution for NestJS with JWT, OAuth, OTP, role-based access control, and more

433 lines 26.6 kB
"use strict"; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.ROCKETS_SERVER_MODULE_ASYNC_OPTIONS_TYPE = exports.ROCKETS_SERVER_MODULE_OPTIONS_TYPE = exports.RocketsAuthModuleClass = exports.RAW_OPTIONS_TOKEN = void 0; exports.createRocketsAuthControllers = createRocketsAuthControllers; exports.createRocketsAuthSettingsProvider = createRocketsAuthSettingsProvider; exports.createRocketsAuthImports = createRocketsAuthImports; exports.createRocketsAuthExports = createRocketsAuthExports; exports.createRocketsAuthProviders = createRocketsAuthProviders; const nestjs_access_control_1 = require("@concepta/nestjs-access-control"); const nestjs_auth_apple_1 = require("@concepta/nestjs-auth-apple"); const nestjs_auth_github_1 = require("@concepta/nestjs-auth-github"); const nestjs_auth_google_1 = require("@concepta/nestjs-auth-google"); const nestjs_auth_jwt_1 = require("@concepta/nestjs-auth-jwt"); const nestjs_auth_local_1 = require("@concepta/nestjs-auth-local"); const nestjs_auth_recovery_1 = require("@concepta/nestjs-auth-recovery"); const nestjs_auth_refresh_1 = require("@concepta/nestjs-auth-refresh"); const nestjs_auth_router_1 = require("@concepta/nestjs-auth-router"); const nestjs_auth_verify_1 = require("@concepta/nestjs-auth-verify"); const nestjs_authentication_1 = require("@concepta/nestjs-authentication"); const nestjs_common_1 = require("@concepta/nestjs-common"); const nestjs_crud_1 = require("@concepta/nestjs-crud"); const nestjs_email_1 = require("@concepta/nestjs-email"); const nestjs_federated_1 = require("@concepta/nestjs-federated"); const nestjs_jwt_1 = require("@concepta/nestjs-jwt"); const nestjs_otp_1 = require("@concepta/nestjs-otp"); const nestjs_password_1 = require("@concepta/nestjs-password"); const nestjs_role_1 = require("@concepta/nestjs-role"); const nestjs_swagger_ui_1 = require("@concepta/nestjs-swagger-ui"); const nestjs_user_1 = require("@concepta/nestjs-user"); const common_1 = require("@nestjs/common"); const config_1 = require("@nestjs/config"); const rockets_auth_options_default_config_1 = require("./shared/config/rockets-auth-options-default.config"); const auth_password_controller_1 = require("./domains/auth/controllers/auth-password.controller"); const auth_recovery_controller_1 = require("./domains/auth/controllers/auth-recovery.controller"); const auth_refresh_controller_1 = require("./domains/auth/controllers/auth-refresh.controller"); const auth_oauth_controller_1 = require("./domains/oauth/controllers/auth-oauth.controller"); const rockets_auth_otp_controller_1 = require("./domains/otp/controllers/rockets-auth-otp.controller"); const admin_guard_1 = require("./guards/admin.guard"); const rockets_auth_admin_module_1 = require("./domains/user/modules/rockets-auth-admin.module"); const rockets_auth_signup_module_1 = require("./domains/user/modules/rockets-auth-signup.module"); const rockets_auth_role_admin_module_1 = require("./domains/role/modules/rockets-auth-role-admin.module"); const rockets_auth_constants_1 = require("./shared/constants/rockets-auth.constants"); const rockets_auth_notification_service_1 = require("./domains/otp/services/rockets-auth-notification.service"); const rockets_auth_otp_service_1 = require("./domains/otp/services/rockets-auth-otp.service"); const rockets_jwt_auth_provider_1 = require("./provider/rockets-jwt-auth.provider"); exports.RAW_OPTIONS_TOKEN = Symbol('__ROCKETS_SERVER_MODULE_RAW_OPTIONS_TOKEN__'); _a = new common_1.ConfigurableModuleBuilder({ moduleName: 'RocketsAuth', optionsInjectionToken: exports.RAW_OPTIONS_TOKEN, }) .setExtras({ global: false, }, definitionTransform) .build(), exports.RocketsAuthModuleClass = _a.ConfigurableModuleClass, exports.ROCKETS_SERVER_MODULE_OPTIONS_TYPE = _a.OPTIONS_TYPE, exports.ROCKETS_SERVER_MODULE_ASYNC_OPTIONS_TYPE = _a.ASYNC_OPTIONS_TYPE; function definitionTransform(definition, extras) { const { imports = [], providers = [], exports = [] } = definition; const { controllers, userCrud, roleCrud } = extras; const baseModule = Object.assign(Object.assign({}, definition), { global: extras.global, imports: createRocketsAuthImports({ imports, extras }), controllers: createRocketsAuthControllers({ controllers, extras }) || [], providers: [...createRocketsAuthProviders({ providers, extras })], exports: createRocketsAuthExports({ exports, extras }) }); if (userCrud) { const disableController = extras.disableController || {}; baseModule.imports = [ ...(baseModule.imports || []), ...(!disableController.admin ? [rockets_auth_admin_module_1.RocketsAuthAdminModule.register(userCrud)] : []), ...(!disableController.signup ? [rockets_auth_signup_module_1.RocketsAuthSignUpModule.register(userCrud)] : []), ]; } if (roleCrud) { const disableController = extras.disableController || {}; baseModule.imports = [ ...(baseModule.imports || []), ...(!disableController.adminRoles ? [rockets_auth_role_admin_module_1.RocketsAuthRoleAdminModule.register(roleCrud)] : []), ]; } return baseModule; } function createRocketsAuthControllers(options) { return (options === null || options === void 0 ? void 0 : options.controllers) !== undefined ? options.controllers : (() => { var _a; const disableController = ((_a = options === null || options === void 0 ? void 0 : options.extras) === null || _a === void 0 ? void 0 : _a.disableController) || {}; const list = []; if (!disableController.password) list.push(auth_password_controller_1.AuthPasswordController); if (!disableController.refresh) list.push(auth_refresh_controller_1.AuthTokenRefreshController); if (!disableController.recovery) list.push(auth_recovery_controller_1.RocketsAuthRecoveryController); if (!disableController.otp) list.push(rockets_auth_otp_controller_1.RocketsAuthOtpController); if (!disableController.oAuth) list.push(auth_oauth_controller_1.AuthOAuthController); return list; })(); } function createRocketsAuthSettingsProvider(optionsOverrides) { return (0, nestjs_common_1.createSettingsProvider)({ settingsToken: rockets_auth_constants_1.ROCKETS_AUTH_MODULE_OPTIONS_DEFAULT_SETTINGS_TOKEN, optionsToken: exports.RAW_OPTIONS_TOKEN, settingsKey: rockets_auth_options_default_config_1.rocketsAuthOptionsDefaultConfig.KEY, optionsOverrides, }); } function createRocketsAuthImports(importOptions) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o; const defaultAuthRouterGuards = [ { name: 'google', guard: nestjs_auth_google_1.AuthGoogleGuard }, { name: 'github', guard: nestjs_auth_github_1.AuthGithubGuard }, { name: 'apple', guard: nestjs_auth_apple_1.AuthAppleGuard }, ]; const imports = [ ...(importOptions.imports || []), config_1.ConfigModule.forFeature(rockets_auth_options_default_config_1.rocketsAuthOptionsDefaultConfig), nestjs_crud_1.CrudModule.forRootAsync({ inject: [exports.RAW_OPTIONS_TOKEN], useFactory: (options) => { var _a; return { settings: (_a = options.crud) === null || _a === void 0 ? void 0 : _a.settings, }; }, }), nestjs_swagger_ui_1.SwaggerUiModule.registerAsync({ inject: [exports.RAW_OPTIONS_TOKEN], useFactory: (options) => { var _a, _b; return { documentBuilder: (_a = options.swagger) === null || _a === void 0 ? void 0 : _a.documentBuilder, settings: (_b = options.swagger) === null || _b === void 0 ? void 0 : _b.settings, }; }, }), nestjs_authentication_1.AuthenticationModule.forRootAsync({ inject: [exports.RAW_OPTIONS_TOKEN], useFactory: (options) => { var _a, _b, _c, _d, _e, _f, _g; return { verifyTokenService: ((_a = options.authentication) === null || _a === void 0 ? void 0 : _a.verifyTokenService) || ((_b = options.services) === null || _b === void 0 ? void 0 : _b.verifyTokenService), issueTokenService: ((_c = options.authentication) === null || _c === void 0 ? void 0 : _c.issueTokenService) || ((_d = options.services) === null || _d === void 0 ? void 0 : _d.issueTokenService), validateTokenService: ((_e = options.authentication) === null || _e === void 0 ? void 0 : _e.validateTokenService) || ((_f = options.services) === null || _f === void 0 ? void 0 : _f.validateTokenService), settings: (_g = options.authentication) === null || _g === void 0 ? void 0 : _g.settings, }; }, }), nestjs_jwt_1.JwtModule.forRootAsync({ inject: [exports.RAW_OPTIONS_TOKEN], useFactory: (options) => { var _a, _b, _c, _d, _e, _f, _g, _h; return { jwtIssueTokenService: ((_a = options.jwt) === null || _a === void 0 ? void 0 : _a.jwtIssueTokenService) || ((_b = options.services) === null || _b === void 0 ? void 0 : _b.issueTokenService), jwtVerifyTokenService: ((_c = options.jwt) === null || _c === void 0 ? void 0 : _c.jwtVerifyTokenService) || ((_d = options.services) === null || _d === void 0 ? void 0 : _d.verifyTokenService), jwtRefreshService: (_e = options.jwt) === null || _e === void 0 ? void 0 : _e.jwtRefreshService, jwtAccessService: (_f = options.jwt) === null || _f === void 0 ? void 0 : _f.jwtAccessService, jwtService: (_g = options.jwt) === null || _g === void 0 ? void 0 : _g.jwtService, settings: (_h = options.jwt) === null || _h === void 0 ? void 0 : _h.settings, }; }, }), nestjs_auth_jwt_1.AuthJwtModule.forRootAsync({ inject: [exports.RAW_OPTIONS_TOKEN, nestjs_user_1.UserModelService], useFactory: (options, userModelService) => { var _a, _b, _c, _d, _e, _f; return { appGuard: ((_a = importOptions.extras) === null || _a === void 0 ? void 0 : _a.enableGlobalJWTGuard) === true ? undefined : false, verifyTokenService: ((_b = options.authJwt) === null || _b === void 0 ? void 0 : _b.verifyTokenService) || ((_c = options.services) === null || _c === void 0 ? void 0 : _c.verifyTokenService), userModelService: ((_d = options.authJwt) === null || _d === void 0 ? void 0 : _d.userModelService) || ((_e = options.services) === null || _e === void 0 ? void 0 : _e.userModelService) || userModelService, settings: (_f = options.authJwt) === null || _f === void 0 ? void 0 : _f.settings, }; }, }), nestjs_federated_1.FederatedModule.forRootAsync({ inject: [exports.RAW_OPTIONS_TOKEN, nestjs_user_1.UserModelService], imports: [...(((_b = (_a = importOptions.extras) === null || _a === void 0 ? void 0 : _a.federated) === null || _b === void 0 ? void 0 : _b.imports) || [])], useFactory: (options, userModelService) => { var _a, _b, _c; return { userModelService: ((_a = options.federated) === null || _a === void 0 ? void 0 : _a.userModelService) || ((_b = options.services) === null || _b === void 0 ? void 0 : _b.userModelService) || userModelService, settings: (_c = options.federated) === null || _c === void 0 ? void 0 : _c.settings, }; }, }), nestjs_auth_apple_1.AuthAppleModule.forRootAsync({ inject: [exports.RAW_OPTIONS_TOKEN], useFactory: (options) => { var _a, _b, _c, _d, _e, _f, _g; return { jwtService: ((_a = options.authApple) === null || _a === void 0 ? void 0 : _a.jwtService) || ((_b = options.jwt) === null || _b === void 0 ? void 0 : _b.jwtService), authAppleService: (_c = options.authApple) === null || _c === void 0 ? void 0 : _c.authAppleService, issueTokenService: ((_d = options.authApple) === null || _d === void 0 ? void 0 : _d.issueTokenService) || ((_e = options.services) === null || _e === void 0 ? void 0 : _e.issueTokenService), settingsTransform: (_f = options.authApple) === null || _f === void 0 ? void 0 : _f.settingsTransform, settings: (_g = options.authApple) === null || _g === void 0 ? void 0 : _g.settings, }; }, }), nestjs_auth_github_1.AuthGithubModule.forRootAsync({ inject: [exports.RAW_OPTIONS_TOKEN], useFactory: (options) => { var _a, _b, _c, _d; return { issueTokenService: ((_a = options.authGithub) === null || _a === void 0 ? void 0 : _a.issueTokenService) || ((_b = options.services) === null || _b === void 0 ? void 0 : _b.issueTokenService), settingsTransform: (_c = options.authGithub) === null || _c === void 0 ? void 0 : _c.settingsTransform, settings: (_d = options.authGithub) === null || _d === void 0 ? void 0 : _d.settings, }; }, }), nestjs_auth_google_1.AuthGoogleModule.forRootAsync({ inject: [exports.RAW_OPTIONS_TOKEN], useFactory: (options) => { var _a, _b, _c, _d; return { issueTokenService: ((_a = options.authGoogle) === null || _a === void 0 ? void 0 : _a.issueTokenService) || ((_b = options.services) === null || _b === void 0 ? void 0 : _b.issueTokenService), settingsTransform: (_c = options.authGoogle) === null || _c === void 0 ? void 0 : _c.settingsTransform, settings: (_d = options.authGoogle) === null || _d === void 0 ? void 0 : _d.settings, }; }, }), nestjs_auth_router_1.AuthRouterModule.forRootAsync({ inject: [exports.RAW_OPTIONS_TOKEN], guards: ((_d = (_c = importOptions.extras) === null || _c === void 0 ? void 0 : _c.authRouter) === null || _d === void 0 ? void 0 : _d.guards) || defaultAuthRouterGuards, useFactory: (options) => { var _a; return { settings: (_a = options.authRouter) === null || _a === void 0 ? void 0 : _a.settings, }; }, }), nestjs_auth_refresh_1.AuthRefreshModule.forRootAsync({ inject: [exports.RAW_OPTIONS_TOKEN, nestjs_user_1.UserModelService], useFactory: (options, userModelService) => { var _a, _b, _c, _d, _e, _f, _g; return { verifyTokenService: ((_a = options.refresh) === null || _a === void 0 ? void 0 : _a.verifyTokenService) || ((_b = options.services) === null || _b === void 0 ? void 0 : _b.verifyTokenService), issueTokenService: ((_c = options.refresh) === null || _c === void 0 ? void 0 : _c.issueTokenService) || ((_d = options.services) === null || _d === void 0 ? void 0 : _d.issueTokenService), userModelService: ((_e = options.refresh) === null || _e === void 0 ? void 0 : _e.userModelService) || ((_f = options.services) === null || _f === void 0 ? void 0 : _f.userModelService) || userModelService, settings: (_g = options.refresh) === null || _g === void 0 ? void 0 : _g.settings, }; }, }), nestjs_auth_local_1.AuthLocalModule.forRootAsync({ inject: [exports.RAW_OPTIONS_TOKEN, nestjs_user_1.UserModelService], useFactory: (options, userModelService) => { var _a, _b, _c, _d, _e, _f, _g, _h; return { passwordValidationService: (_a = options.authLocal) === null || _a === void 0 ? void 0 : _a.passwordValidationService, validateUserService: ((_b = options.authLocal) === null || _b === void 0 ? void 0 : _b.validateUserService) || ((_c = options.services) === null || _c === void 0 ? void 0 : _c.validateUserService), issueTokenService: ((_d = options.authLocal) === null || _d === void 0 ? void 0 : _d.issueTokenService) || ((_e = options.services) === null || _e === void 0 ? void 0 : _e.issueTokenService), userModelService: ((_f = options.authLocal) === null || _f === void 0 ? void 0 : _f.userModelService) || ((_g = options.services) === null || _g === void 0 ? void 0 : _g.userModelService) || userModelService, settings: (_h = options.authLocal) === null || _h === void 0 ? void 0 : _h.settings, }; }, }), nestjs_auth_recovery_1.AuthRecoveryModule.forRootAsync({ inject: [ exports.RAW_OPTIONS_TOKEN, nestjs_email_1.EmailService, nestjs_otp_1.OtpService, nestjs_user_1.UserModelService, nestjs_user_1.UserPasswordService, ], useFactory: (options, defaultEmailService, defaultOtpService, userModelService, defaultUserPasswordService) => { var _a, _b, _c, _d, _e, _f, _g; return { emailService: defaultEmailService, otpService: defaultOtpService, userModelService: ((_a = options.authRecovery) === null || _a === void 0 ? void 0 : _a.userModelService) || ((_b = options.services) === null || _b === void 0 ? void 0 : _b.userModelService) || userModelService, userPasswordService: ((_c = options.authRecovery) === null || _c === void 0 ? void 0 : _c.userPasswordService) || ((_d = options.services) === null || _d === void 0 ? void 0 : _d.userPasswordService) || defaultUserPasswordService, notificationService: ((_e = options.authRecovery) === null || _e === void 0 ? void 0 : _e.notificationService) || ((_f = options.services) === null || _f === void 0 ? void 0 : _f.notificationService), settings: (_g = options.authRecovery) === null || _g === void 0 ? void 0 : _g.settings, }; }, }), nestjs_auth_verify_1.AuthVerifyModule.forRootAsync({ inject: [exports.RAW_OPTIONS_TOKEN, nestjs_email_1.EmailService, nestjs_user_1.UserModelService, nestjs_otp_1.OtpService], useFactory: (options, defaultEmailService, userModelService, defaultOtpService) => { var _a, _b, _c, _d, _e; return { emailService: defaultEmailService, otpService: defaultOtpService, userModelService: ((_a = options.authVerify) === null || _a === void 0 ? void 0 : _a.userModelService) || ((_b = options.services) === null || _b === void 0 ? void 0 : _b.userModelService) || userModelService, notificationService: ((_c = options.authVerify) === null || _c === void 0 ? void 0 : _c.notificationService) || ((_d = options.services) === null || _d === void 0 ? void 0 : _d.notificationService), settings: (_e = options.authVerify) === null || _e === void 0 ? void 0 : _e.settings, }; }, }), nestjs_password_1.PasswordModule.forRootAsync({ inject: [exports.RAW_OPTIONS_TOKEN], useFactory: (options) => { var _a; return { settings: (_a = options.password) === null || _a === void 0 ? void 0 : _a.settings, }; }, }), nestjs_user_1.UserModule.forRootAsync({ inject: [exports.RAW_OPTIONS_TOKEN], imports: [...(((_f = (_e = importOptions.extras) === null || _e === void 0 ? void 0 : _e.user) === null || _f === void 0 ? void 0 : _f.imports) || [])], useFactory: (options) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j; return { settings: (_a = options.user) === null || _a === void 0 ? void 0 : _a.settings, userModelService: ((_b = options.user) === null || _b === void 0 ? void 0 : _b.userModelService) || ((_c = options.services) === null || _c === void 0 ? void 0 : _c.userModelService), userPasswordService: ((_d = options.user) === null || _d === void 0 ? void 0 : _d.userPasswordService) || ((_e = options.services) === null || _e === void 0 ? void 0 : _e.userPasswordService), userAccessQueryService: ((_f = options.user) === null || _f === void 0 ? void 0 : _f.userAccessQueryService) || ((_g = options.services) === null || _g === void 0 ? void 0 : _g.userAccessQueryService), userPasswordHistoryService: ((_h = options.user) === null || _h === void 0 ? void 0 : _h.userPasswordHistoryService) || ((_j = options.services) === null || _j === void 0 ? void 0 : _j.userPasswordHistoryService), }; }, }), nestjs_otp_1.OtpModule.forRootAsync({ imports: [...(((_h = (_g = importOptions.extras) === null || _g === void 0 ? void 0 : _g.otp) === null || _h === void 0 ? void 0 : _h.imports) || [])], inject: [exports.RAW_OPTIONS_TOKEN], useFactory: (options) => { var _a; return { settings: (_a = options.otp) === null || _a === void 0 ? void 0 : _a.settings, }; }, entities: ['userOtp'], }), nestjs_email_1.EmailModule.forRootAsync({ inject: [exports.RAW_OPTIONS_TOKEN], useFactory: (options) => { var _a, _b; return { settings: (_a = options.email) === null || _a === void 0 ? void 0 : _a.settings, mailerService: ((_b = options.email) === null || _b === void 0 ? void 0 : _b.mailerService) || options.services.mailerService, }; }, }), nestjs_role_1.RoleModule.forRootAsync({ imports: [...(((_k = (_j = importOptions.extras) === null || _j === void 0 ? void 0 : _j.role) === null || _k === void 0 ? void 0 : _k.imports) || [])], inject: [exports.RAW_OPTIONS_TOKEN], useFactory: (rocketsServerAuthOptions) => { var _a, _b, _c, _d; return ({ roleModelService: (_a = rocketsServerAuthOptions.role) === null || _a === void 0 ? void 0 : _a.roleModelService, settings: Object.assign(Object.assign({}, (_b = rocketsServerAuthOptions.role) === null || _b === void 0 ? void 0 : _b.settings), { assignments: Object.assign({ user: { entityKey: 'userRole' } }, (_d = (_c = rocketsServerAuthOptions.role) === null || _c === void 0 ? void 0 : _c.settings) === null || _d === void 0 ? void 0 : _d.assignments) }), }); }, entities: ['userRole', ...(((_m = (_l = importOptions.extras) === null || _l === void 0 ? void 0 : _l.role) === null || _m === void 0 ? void 0 : _m.entities) || [])], }), ]; if ((_o = importOptions.extras) === null || _o === void 0 ? void 0 : _o.accessControl) { imports.push(nestjs_access_control_1.AccessControlModule.forRoot({ service: importOptions.extras.accessControl.service, settings: importOptions.extras.accessControl.settings, appFilter: importOptions.extras.accessControl.appFilter, appGuard: false, })); } return imports; } function createRocketsAuthExports(options) { return [ ...(options.exports || []), config_1.ConfigModule, exports.RAW_OPTIONS_TOKEN, rockets_auth_constants_1.ROCKETS_AUTH_MODULE_OPTIONS_DEFAULT_SETTINGS_TOKEN, nestjs_jwt_1.JwtModule, nestjs_auth_jwt_1.AuthJwtModule, nestjs_auth_apple_1.AuthAppleModule, nestjs_auth_github_1.AuthGithubModule, nestjs_auth_google_1.AuthGoogleModule, nestjs_auth_router_1.AuthRouterModule, nestjs_auth_refresh_1.AuthRefreshModule, nestjs_federated_1.FederatedModule, nestjs_swagger_ui_1.SwaggerUiModule, nestjs_role_1.RoleModule, admin_guard_1.AdminGuard, rockets_jwt_auth_provider_1.RocketsJwtAuthProvider, ]; } function createRocketsAuthProviders(options) { var _a; const providers = [ ...((_a = options.providers) !== null && _a !== void 0 ? _a : []), createRocketsAuthSettingsProvider(), { provide: rockets_auth_constants_1.RocketsAuthUserModelService, inject: [exports.RAW_OPTIONS_TOKEN, nestjs_user_1.UserModelService], useFactory: async (options, userModelService) => { return options.services.userModelService || userModelService; }, }, rockets_auth_otp_service_1.RocketsAuthOtpService, rockets_auth_notification_service_1.RocketsAuthNotificationService, rockets_jwt_auth_provider_1.RocketsJwtAuthProvider, admin_guard_1.AdminGuard, ]; return providers; } //# sourceMappingURL=rockets-auth.module-definition.js.map