@koalarx/nest 3.1.50 → 4.0.2
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 +126 -439
- package/cli/commands/add/index.js +97 -0
- package/cli/commands/help.js +44 -0
- package/cli/commands/new/configure-test-runner.js +13 -0
- package/cli/commands/new/create-ddd-structure.js +52 -0
- package/cli/commands/new/create-empty-nest-project.js +58 -0
- package/cli/commands/new/fix-lint-config.js +24 -0
- package/cli/commands/new/index.js +202 -0
- package/cli/commands/version.js +5 -0
- package/cli/constants/auth-strategy-artifacts.js +56 -0
- package/cli/constants/auth-strategy-checklist.js +314 -0
- package/cli/constants/cli-commands.js +24 -0
- package/cli/constants/cli-project-checklist.js +289 -0
- package/cli/constants/core-packages.js +34 -0
- package/cli/constants/domain.js +171 -0
- package/cli/constants/package-manager.js +5 -0
- package/cli/constants/version.js +5 -0
- package/cli/index.js +55 -0
- package/cli/types/index.js +0 -0
- package/cli/utils/add-project-features.js +170 -0
- package/cli/utils/apply-optional-features.js +48 -0
- package/cli/utils/auth-strategy-validation.js +179 -0
- package/cli/utils/cancel.js +8 -0
- package/cli/utils/cli-options.js +27 -0
- package/cli/utils/cli-project-validation.js +157 -0
- package/cli/utils/detect-project-state.js +131 -0
- package/cli/utils/format-code.js +9 -0
- package/cli/utils/get-package-manager.js +12 -0
- package/cli/utils/get-package-root.js +19 -0
- package/cli/utils/get-source-code-path.js +15 -0
- package/cli/utils/install-module.js +258 -0
- package/cli/utils/normalize-add-args.js +25 -0
- package/cli/utils/parse-new-args.js +127 -0
- package/cli/utils/patch-auth-install.js +224 -0
- package/cli/utils/patch-define-documentation.js +222 -0
- package/cli/utils/patch-env.js +106 -0
- package/cli/utils/patch-generated-project.js +21 -0
- package/cli/utils/patch-health-module.js +80 -0
- package/cli/utils/patch-infra-module.js +62 -0
- package/cli/utils/patch-jobs-module.js +103 -0
- package/cli/utils/patch-main.js +15 -0
- package/cli/utils/patch-person-features.js +127 -0
- package/cli/utils/project-files.js +11 -0
- package/cli/utils/prune-auth-strategies.js +116 -0
- package/cli/utils/prune-core-auth.js +17 -0
- package/cli/utils/remove-sample-parts.js +203 -0
- package/cli/utils/resolve-project-path.js +19 -0
- package/cli/utils/restore-person-features.js +64 -0
- package/cli/utils/run-command.js +31 -0
- package/cli/utils/sync-auth-strategy-files.js +63 -0
- package/koala-nest/.env.example +34 -0
- package/koala-nest/.prettierrc +4 -0
- package/koala-nest/README.md +51 -0
- package/koala-nest/bunfig.toml +7 -0
- package/koala-nest/eslint.config.mjs +55 -0
- package/koala-nest/nest-cli.json +9 -0
- package/koala-nest/package.json +79 -0
- package/koala-nest/src/application/auth/common/auth-token.response.ts +15 -0
- package/koala-nest/src/application/auth/common/user-claims.ts +6 -0
- package/koala-nest/src/application/auth/login/login.handler.ts +42 -0
- package/koala-nest/src/application/auth/login/login.request.ts +9 -0
- package/koala-nest/src/application/auth/login/login.response.ts +3 -0
- package/koala-nest/src/application/auth/login/login.validator.ts +12 -0
- package/koala-nest/src/application/auth/oauth2/auth-link/auth-link.handler.ts +24 -0
- package/koala-nest/src/application/auth/oauth2/auth-link/auth-link.request.ts +10 -0
- package/koala-nest/src/application/auth/oauth2/auth-link/auth-link.response.ts +6 -0
- package/koala-nest/src/application/auth/oauth2/auth-link/auth-link.validator.ts +12 -0
- package/koala-nest/src/application/auth/oauth2/callback/oauth-callback.response.ts +10 -0
- package/koala-nest/src/application/auth/oauth2/exchange-code/exchange-code.handler.ts +73 -0
- package/koala-nest/src/application/auth/oauth2/exchange-code/exchange-code.request.ts +16 -0
- package/koala-nest/src/application/auth/oauth2/exchange-code/exchange-code.validator.ts +14 -0
- package/koala-nest/src/application/auth/oauth2/scalar-token/scalar-oauth-token.handler.ts +35 -0
- package/koala-nest/src/application/auth/oauth2/scalar-token/scalar-token.types.ts +1 -0
- package/koala-nest/src/application/auth/refresh-token/refresh-token.handler.ts +26 -0
- package/koala-nest/src/application/auth/user-info/user-info.handler.ts +30 -0
- package/koala-nest/src/application/auth/user-info/user-info.response.ts +20 -0
- package/koala-nest/src/application/common/created-registre.response.ts +20 -0
- package/koala-nest/src/application/common/pagination.request.ts +41 -0
- package/koala-nest/src/application/common/request-handler.base.ts +3 -0
- package/koala-nest/src/application/common/request-validator.base.ts +33 -0
- package/koala-nest/src/application/mapping/mapping.provider.ts +9 -0
- package/koala-nest/src/application/mapping/person.mapper.ts +44 -0
- package/koala-nest/src/application/person/create/create-person.handler.ts +34 -0
- package/koala-nest/src/application/person/create/create-person.request.ts +29 -0
- package/koala-nest/src/application/person/create/create-person.response.ts +3 -0
- package/koala-nest/src/application/person/create/create-person.validator.ts +9 -0
- package/koala-nest/src/application/person/delete/delete-person.handler.ts +22 -0
- package/koala-nest/src/application/person/find-person-or-throw.ts +16 -0
- package/koala-nest/src/application/person/jobs/cron/create-person.job.ts +58 -0
- package/koala-nest/src/application/person/jobs/cron/delete-inactive.job.ts +57 -0
- package/koala-nest/src/application/person/jobs/events/person/inactive-person/inactive-person.event.ts +4 -0
- package/koala-nest/src/application/person/jobs/events/person/inactive-person/inactive-person.handler.ts +54 -0
- package/koala-nest/src/application/person/jobs/events/person/person-event.job.ts +19 -0
- package/koala-nest/src/application/person/person.schemas.ts +21 -0
- package/koala-nest/src/application/person/read/read-person.handler.ts +22 -0
- package/koala-nest/src/application/person/read/read-person.response.ts +44 -0
- package/koala-nest/src/application/person/read-many/read-many-person.handler.ts +63 -0
- package/koala-nest/src/application/person/read-many/read-many-person.request.ts +13 -0
- package/koala-nest/src/application/person/read-many/read-many-person.response.ts +30 -0
- package/koala-nest/src/application/person/read-many/read-many-person.validator.ts +15 -0
- package/koala-nest/src/application/person/update/update-person.handler.ts +51 -0
- package/koala-nest/src/application/person/update/update-person.request.ts +41 -0
- package/koala-nest/src/application/person/update/update-person.validator.ts +9 -0
- package/koala-nest/src/core/auth/assert-user-active.ts +18 -0
- package/koala-nest/src/core/auth/auth-profile.enum.ts +7 -0
- package/koala-nest/src/core/auth/auth-routes.ts +5 -0
- package/koala-nest/src/core/auth/auth.constants.ts +6 -0
- package/koala-nest/src/core/auth/jwt-claims.ts +23 -0
- package/koala-nest/src/core/auth/oauth-provider.registry.ts +94 -0
- package/koala-nest/src/core/auth/parse-jwt-expires-in.ts +30 -0
- package/koala-nest/src/core/auth/parse-oauth2-provider-env.ts +56 -0
- package/koala-nest/src/core/auth/resolve-refresh-token.ts +40 -0
- package/koala-nest/src/core/background-services/cron-service/cron-job.handler.base.ts +65 -0
- package/koala-nest/src/core/background-services/event-service/event-class.ts +5 -0
- package/koala-nest/src/core/background-services/event-service/event-handler.base.ts +17 -0
- package/koala-nest/src/core/background-services/event-service/event-job.ts +31 -0
- package/koala-nest/src/core/background-services/event-service/event-queue.ts +107 -0
- package/koala-nest/src/core/base/entity.base.ts +3 -0
- package/koala-nest/src/core/base/object-class.ts +14 -0
- package/koala-nest/src/core/constants/cache.constants.ts +16 -0
- package/koala-nest/src/core/constants/cron.constants.ts +14 -0
- package/koala-nest/src/core/constants/query-params.ts +7 -0
- package/koala-nest/src/core/env.ts +42 -0
- package/koala-nest/src/core/schemas/boolean.schema.ts +24 -0
- package/koala-nest/src/core/schemas/document-number-mask.ts +22 -0
- package/koala-nest/src/core/schemas/document-number.schema.ts +21 -0
- package/koala-nest/src/core/schemas/document-number.utils.ts +15 -0
- package/koala-nest/src/core/schemas/email.schema.ts +13 -0
- package/koala-nest/src/core/schemas/index.ts +6 -0
- package/koala-nest/src/core/schemas/list-query.schema.ts +18 -0
- package/koala-nest/src/core/schemas/native-enum.schema.ts +36 -0
- package/koala-nest/src/core/tools/mapping/auto-map.ts +14 -0
- package/koala-nest/src/core/tools/mapping/auto-mapper.ts +102 -0
- package/koala-nest/src/core/tools/mapping/create-map.ts +11 -0
- package/koala-nest/src/core/tools/mapping/for-member.ts +16 -0
- package/koala-nest/src/core/tools/mapping/index.ts +4 -0
- package/koala-nest/src/core/tools/mapping/mapping-store.ts +121 -0
- package/koala-nest/src/core/types/auth-provider-config-response.type.ts +5 -0
- package/koala-nest/src/core/types/index.ts +5 -0
- package/koala-nest/src/core/utils/build-list-cache-key.ts +19 -0
- package/koala-nest/src/core/utils/cron-expression-to-boolean.ts +65 -0
- package/koala-nest/src/core/utils/env.config.ts +17 -0
- package/koala-nest/src/core/utils/filter-request-params.ts +29 -0
- package/koala-nest/src/core/utils/format-typeorm-error.ts +173 -0
- package/koala-nest/src/core/utils/format-zod-error.ts +201 -0
- package/koala-nest/src/core/utils/hash-password.ts +5 -0
- package/koala-nest/src/core/utils/icomparable.ts +1 -0
- package/koala-nest/src/core/utils/is-provider-registered.ts +40 -0
- package/koala-nest/src/core/utils/name-to-login.ts +25 -0
- package/koala-nest/src/core/utils/person-list-cache.ts +7 -0
- package/koala-nest/src/core/utils/report-error.ts +18 -0
- package/koala-nest/src/core/utils/resolve-api-host.ts +8 -0
- package/koala-nest/src/core/utils/time.constants.ts +5 -0
- package/koala-nest/src/domain/auth/dtos/auth-provider-config.dto.ts +12 -0
- package/koala-nest/src/domain/auth/dtos/oauth-user-info.dto.ts +9 -0
- package/koala-nest/src/domain/auth/services/iauth.service.ts +37 -0
- package/koala-nest/src/domain/common/icache.service.ts +11 -0
- package/koala-nest/src/domain/common/ilogging.service.ts +9 -0
- package/koala-nest/src/domain/common/ired-lock.service.ts +4 -0
- package/koala-nest/src/domain/dtos/logged-user-info.dto.ts +30 -0
- package/koala-nest/src/domain/dtos/pagination.dto.ts +35 -0
- package/koala-nest/src/domain/dtos/person-query.dto.ts +10 -0
- package/koala-nest/src/domain/entities/person/person-address.ts +14 -0
- package/koala-nest/src/domain/entities/person/person-contact.ts +28 -0
- package/koala-nest/src/domain/entities/person/person.ts +44 -0
- package/koala-nest/src/domain/entities/user/enums/user-status.enum.ts +6 -0
- package/koala-nest/src/domain/entities/user/user.ts +49 -0
- package/koala-nest/src/domain/repositories/iperson.repository.ts +10 -0
- package/koala-nest/src/domain/repositories/iuser.repository.ts +8 -0
- package/koala-nest/src/domain/services/ilogged-user-info.service.ts +5 -0
- package/koala-nest/src/host/app.module.ts +27 -0
- package/koala-nest/src/host/controllers/auth/auth.module.ts +38 -0
- package/koala-nest/src/host/controllers/auth/login.controller.ts +41 -0
- package/koala-nest/src/host/controllers/auth/refresh-token.controller.ts +24 -0
- package/koala-nest/src/host/controllers/auth/router.config.ts +9 -0
- package/koala-nest/src/host/controllers/auth/user-info.controller.ts +19 -0
- package/koala-nest/src/host/controllers/common/controller.base.ts +9 -0
- package/koala-nest/src/host/controllers/common/controller.module.ts +10 -0
- package/koala-nest/src/host/controllers/common/router-config.base.ts +14 -0
- package/koala-nest/src/host/controllers/health-check/health-check.controller.ts +29 -0
- package/koala-nest/src/host/controllers/health-check/health-check.module.ts +22 -0
- package/koala-nest/src/host/controllers/oauth2/auth-link.controller.ts +29 -0
- package/koala-nest/src/host/controllers/oauth2/exchange-code.controller.ts +29 -0
- package/koala-nest/src/host/controllers/oauth2/oauth-callback.controller.ts +28 -0
- package/koala-nest/src/host/controllers/oauth2/router.config.ts +9 -0
- package/koala-nest/src/host/controllers/oauth2/scalar-token.controller.ts +16 -0
- package/koala-nest/src/host/controllers/person/create-person.controller.ts +23 -0
- package/koala-nest/src/host/controllers/person/delete-person.controller.ts +23 -0
- package/koala-nest/src/host/controllers/person/person.module.ts +39 -0
- package/koala-nest/src/host/controllers/person/read-many-person.controller.ts +24 -0
- package/koala-nest/src/host/controllers/person/read-person.controller.ts +21 -0
- package/koala-nest/src/host/controllers/person/router.config.ts +9 -0
- package/koala-nest/src/host/controllers/person/update-person.controller.ts +22 -0
- package/koala-nest/src/host/decorators/api-exclude-endpoint-diff-develop.decorator.ts +9 -0
- package/koala-nest/src/host/decorators/api-property-enum.decorator.ts +49 -0
- package/koala-nest/src/host/decorators/api-property-only-develop.decorator.ts +19 -0
- package/koala-nest/src/host/decorators/controller.decorator.ts +10 -0
- package/koala-nest/src/host/decorators/is-public.decorator.ts +82 -0
- package/koala-nest/src/host/decorators/restriction-by-profile.decorator.ts +13 -0
- package/koala-nest/src/host/decorators/scalar-token-endpoint.decorator.ts +14 -0
- package/koala-nest/src/host/filters/errors.filter.ts +99 -0
- package/koala-nest/src/host/jobs/jobs-bootstrap.service.ts +61 -0
- package/koala-nest/src/host/jobs/jobs.module.ts +41 -0
- package/koala-nest/src/host/jobs/jobs.tokens.ts +2 -0
- package/koala-nest/src/host/main.ts +43 -0
- package/koala-nest/src/host/open-api/define-documentation.ts +210 -0
- package/koala-nest/src/host/security/guards/auth.guard.ts +107 -0
- package/koala-nest/src/host/security/guards/profiles.guard.ts +32 -0
- package/koala-nest/src/host/security/security.module.ts +62 -0
- package/koala-nest/src/host/security/strategies/jwt.strategy.ts +61 -0
- package/koala-nest/src/infra/auth/jwt-token.service.ts +48 -0
- package/koala-nest/src/infra/auth/oauth2-auth.service.ts +208 -0
- package/koala-nest/src/infra/common/cache-service.provider.ts +47 -0
- package/koala-nest/src/infra/common/env.service.ts +12 -0
- package/koala-nest/src/infra/common/in-memory-cache.service.ts +71 -0
- package/koala-nest/src/infra/common/logging.service.ts +19 -0
- package/koala-nest/src/infra/common/red-lock.service.ts +44 -0
- package/koala-nest/src/infra/common/redis-cache.service.ts +72 -0
- package/koala-nest/src/infra/database/data-source-factory.ts +23 -0
- package/koala-nest/src/infra/database/database.module.ts +19 -0
- package/koala-nest/src/infra/database/migrations/1781281330533-Init.ts +67 -0
- package/koala-nest/src/infra/database/migrations/generate-migration.ts +26 -0
- package/koala-nest/src/infra/database/migrations/migration-datasource.ts +14 -0
- package/koala-nest/src/infra/infra.module.ts +29 -0
- package/koala-nest/src/infra/repositories/person.repository.ts +49 -0
- package/koala-nest/src/infra/repositories/repository.base.ts +20 -0
- package/koala-nest/src/infra/repositories/repository.module.ts +16 -0
- package/koala-nest/src/infra/repositories/user.repository.ts +38 -0
- package/koala-nest/src/infra/services/database.indicator.service.ts +17 -0
- package/koala-nest/src/infra/services/logged-user-info.service.ts +32 -0
- package/koala-nest/src/infra/services/redis.indicator.service.ts +66 -0
- package/koala-nest/src/test/app-auth-test.module.ts +32 -0
- package/koala-nest/src/test/app-test.module.ts +22 -0
- package/koala-nest/src/test/application/auth-link.handler.spec.ts +22 -0
- package/koala-nest/src/test/application/create-person.handler.spec.ts +39 -0
- package/koala-nest/src/test/application/create-person.job.spec.ts +55 -0
- package/koala-nest/src/test/application/delete-inactive.job.spec.ts +35 -0
- package/koala-nest/src/test/application/delete-person.handler.spec.ts +46 -0
- package/koala-nest/src/test/application/exchange-code.handler.spec.ts +67 -0
- package/koala-nest/src/test/application/inactive-person.handler.spec.ts +48 -0
- package/koala-nest/src/test/application/login.handler.spec.ts +55 -0
- package/koala-nest/src/test/application/read-many-person.handler.spec.ts +64 -0
- package/koala-nest/src/test/application/read-person.handler.spec.ts +44 -0
- package/koala-nest/src/test/application/refresh-token.handler.spec.ts +34 -0
- package/koala-nest/src/test/application/scalar-oauth-token.handler.spec.ts +63 -0
- package/koala-nest/src/test/application/update-person.handler.spec.ts +78 -0
- package/koala-nest/src/test/bun-test-globals.d.ts +1 -0
- package/koala-nest/src/test/core/auth.guard.spec.ts +77 -0
- package/koala-nest/src/test/core/build-list-cache-key.spec.ts +29 -0
- package/koala-nest/src/test/core/cron-expression-to-boolean.spec.ts +22 -0
- package/koala-nest/src/test/core/cron-job.handler.spec.ts +40 -0
- package/koala-nest/src/test/core/env.config.spec.ts +14 -0
- package/koala-nest/src/test/core/env.spec.ts +61 -0
- package/koala-nest/src/test/core/event-queue.spec.ts +53 -0
- package/koala-nest/src/test/core/format-typeorm-error.spec.ts +42 -0
- package/koala-nest/src/test/core/format-zod-error.spec.ts +38 -0
- package/koala-nest/src/test/core/is-provider-registered.spec.ts +35 -0
- package/koala-nest/src/test/core/jwt.strategy.spec.ts +38 -0
- package/koala-nest/src/test/core/mapping.spec.ts +177 -0
- package/koala-nest/src/test/core/oauth-provider.registry.spec.ts +79 -0
- package/koala-nest/src/test/core/profiles.guard.spec.ts +42 -0
- package/koala-nest/src/test/core/resolve-api-host.spec.ts +20 -0
- package/koala-nest/src/test/core/schemas.spec.ts +95 -0
- package/koala-nest/src/test/create-auth-e2e-test-app.ts +22 -0
- package/koala-nest/src/test/create-e2e-test-app.ts +14 -0
- package/koala-nest/src/test/e2e-context.ts +7 -0
- package/koala-nest/src/test/host/controllers/auth/auth.controller.e2e.spec.ts +87 -0
- package/koala-nest/src/test/host/controllers/person/lazy-loading.e2e.spec.ts +181 -0
- package/koala-nest/src/test/host/controllers/person/person.controller.e2e.spec.ts +148 -0
- package/koala-nest/src/test/host/errors.filter.spec.ts +75 -0
- package/koala-nest/src/test/host/is-public-open-api.spec.ts +60 -0
- package/koala-nest/src/test/host/oauth-callback.controller.spec.ts +18 -0
- package/koala-nest/src/test/infra/cache-service.provider.spec.ts +21 -0
- package/koala-nest/src/test/infra/in-memory-cache.service.spec.ts +49 -0
- package/koala-nest/src/test/infra/jwt-token.service.spec.ts +28 -0
- package/koala-nest/src/test/infra/logged-user-info.service.spec.ts +33 -0
- package/koala-nest/src/test/infra/oauth2-auth.service.spec.ts +112 -0
- package/koala-nest/src/test/infra/red-lock.service.spec.ts +43 -0
- package/koala-nest/src/test/infra/redis-cache.service.spec.ts +100 -0
- package/koala-nest/src/test/infra/redis-indicator.service.spec.ts +75 -0
- package/koala-nest/src/test/mockup/person/person.entities.ts +35 -0
- package/koala-nest/src/test/mockup/person/person.requests.ts +23 -0
- package/koala-nest/src/test/mockup/person/person.responses.ts +31 -0
- package/koala-nest/src/test/services/cache.stub.ts +41 -0
- package/koala-nest/src/test/services/fake-logging.service.ts +13 -0
- package/koala-nest/src/test/services/fake-red-lock.service.ts +11 -0
- package/koala-nest/src/test/services/logged-user-info.fake-service.ts +18 -0
- package/koala-nest/src/test/setup-e2e.ts +66 -0
- package/koala-nest/src/test/setup.ts +1 -0
- package/koala-nest/src/test/utils/configure-test-app.ts +24 -0
- package/koala-nest/src/test/utils/create-e2e-database.ts +50 -0
- package/koala-nest/src/test/utils/e2e-database-client.ts +9 -0
- package/koala-nest/src/test/utils/guard-test-context.ts +28 -0
- package/koala-nest/src/test/utils/jwt-test-keys.ts +27 -0
- package/koala-nest/tsconfig.build.json +8 -0
- package/koala-nest/tsconfig.json +36 -0
- package/koala-nest/tsconfig.spec.json +11 -0
- package/package.json +19 -29
- package/core/backgroud-services/cron-service/cron-job.handler.base.d.ts +0 -16
- package/core/backgroud-services/cron-service/cron-job.handler.base.js +0 -49
- package/core/backgroud-services/event-service/event-class.d.ts +0 -5
- package/core/backgroud-services/event-service/event-class.js +0 -11
- package/core/backgroud-services/event-service/event-handler.base.d.ts +0 -8
- package/core/backgroud-services/event-service/event-handler.base.js +0 -14
- package/core/backgroud-services/event-service/event-is-trigger.d.ts +0 -3
- package/core/backgroud-services/event-service/event-is-trigger.js +0 -7
- package/core/backgroud-services/event-service/event-job.d.ts +0 -13
- package/core/backgroud-services/event-service/event-job.js +0 -21
- package/core/backgroud-services/event-service/event-queue.d.ts +0 -18
- package/core/backgroud-services/event-service/event-queue.js +0 -65
- package/core/constants/query-params.d.ts +0 -6
- package/core/constants/query-params.js +0 -8
- package/core/controllers/base.controller.d.ts +0 -4
- package/core/controllers/base.controller.js +0 -6
- package/core/controllers/controller.decorator.d.ts +0 -2
- package/core/controllers/controller.decorator.js +0 -11
- package/core/controllers/created-registre-response.base.d.ts +0 -10
- package/core/controllers/created-registre-response.base.js +0 -35
- package/core/controllers/list-response.base.d.ts +0 -4
- package/core/controllers/list-response.base.js +0 -21
- package/core/controllers/pagination.request.d.ts +0 -10
- package/core/controllers/pagination.request.js +0 -56
- package/core/controllers/router-config.base.d.ts +0 -7
- package/core/controllers/router-config.base.js +0 -18
- package/core/controllers/schemas/boolean.schema.d.ts +0 -2
- package/core/controllers/schemas/boolean.schema.js +0 -12
- package/core/controllers/schemas/document-number.schema.d.ts +0 -1
- package/core/controllers/schemas/document-number.schema.js +0 -26
- package/core/controllers/schemas/email.schema.d.ts +0 -1
- package/core/controllers/schemas/email.schema.js +0 -13
- package/core/controllers/schemas/list-query.schema.d.ts +0 -17
- package/core/controllers/schemas/list-query.schema.js +0 -19
- package/core/controllers/schemas/native-enum.schema.d.ts +0 -7
- package/core/controllers/schemas/native-enum.schema.js +0 -28
- package/core/controllers/schemas/set-mask-document-number.schema.d.ts +0 -1
- package/core/controllers/schemas/set-mask-document-number.schema.js +0 -13
- package/core/database/entity.base.d.ts +0 -27
- package/core/database/entity.base.js +0 -145
- package/core/database/entity.decorator.d.ts +0 -12
- package/core/database/entity.decorator.js +0 -37
- package/core/database/prisma-client-with-custom-transaction.interface.d.ts +0 -8
- package/core/database/prisma-client-with-custom-transaction.interface.js +0 -2
- package/core/database/prisma-resolver.d.ts +0 -2
- package/core/database/prisma-resolver.js +0 -74
- package/core/database/prisma-transactional-client.d.ts +0 -11
- package/core/database/prisma-transactional-client.js +0 -25
- package/core/database/prisma.service.d.ts +0 -24
- package/core/database/prisma.service.js +0 -104
- package/core/database/repository.base.d.ts +0 -83
- package/core/database/repository.base.js +0 -668
- package/core/dtos/pagination.dto.d.ts +0 -9
- package/core/dtos/pagination.dto.js +0 -49
- package/core/errors/bad-request.error.d.ts +0 -5
- package/core/errors/bad-request.error.js +0 -10
- package/core/errors/conflict.error.d.ts +0 -4
- package/core/errors/conflict.error.js +0 -10
- package/core/errors/error.base.d.ts +0 -4
- package/core/errors/error.base.js +0 -11
- package/core/errors/no-content.error.d.ts +0 -5
- package/core/errors/no-content.error.js +0 -10
- package/core/errors/not-allowed.error.d.ts +0 -5
- package/core/errors/not-allowed.error.js +0 -10
- package/core/errors/resource-not-found.error.d.ts +0 -5
- package/core/errors/resource-not-found.error.js +0 -10
- package/core/errors/use-case-error.d.ts +0 -3
- package/core/errors/use-case-error.js +0 -2
- package/core/errors/user-already-exist.error.d.ts +0 -4
- package/core/errors/user-already-exist.error.js +0 -10
- package/core/errors/wrong-credentials.error.d.ts +0 -4
- package/core/errors/wrong-credentials.error.js +0 -10
- package/core/health-check/health-check.controller.d.ts +0 -5
- package/core/health-check/health-check.controller.js +0 -32
- package/core/health-check/health-check.module.d.ts +0 -2
- package/core/health-check/health-check.module.js +0 -19
- package/core/index.d.ts +0 -18
- package/core/index.js +0 -7
- package/core/koala-app.d.ts +0 -64
- package/core/koala-app.js +0 -258
- package/core/koala-global-vars.d.ts +0 -7
- package/core/koala-global-vars.js +0 -9
- package/core/koala-nest-database.module.d.ts +0 -16
- package/core/koala-nest-database.module.js +0 -52
- package/core/koala-nest-http.module.d.ts +0 -13
- package/core/koala-nest-http.module.js +0 -37
- package/core/koala-nest.module.d.ts +0 -17
- package/core/koala-nest.module.js +0 -66
- package/core/mapping/auto-mapping-class-context.d.ts +0 -16
- package/core/mapping/auto-mapping-class-context.js +0 -18
- package/core/mapping/auto-mapping-context.d.ts +0 -11
- package/core/mapping/auto-mapping-context.js +0 -24
- package/core/mapping/auto-mapping-list.d.ts +0 -28
- package/core/mapping/auto-mapping-list.js +0 -99
- package/core/mapping/auto-mapping-profile.d.ts +0 -3
- package/core/mapping/auto-mapping-profile.js +0 -6
- package/core/mapping/auto-mapping.decorator.d.ts +0 -9
- package/core/mapping/auto-mapping.decorator.js +0 -27
- package/core/mapping/auto-mapping.module.d.ts +0 -5
- package/core/mapping/auto-mapping.module.js +0 -29
- package/core/mapping/auto-mapping.service.d.ts +0 -20
- package/core/mapping/auto-mapping.service.js +0 -197
- package/core/mapping/create-map.d.ts +0 -3
- package/core/mapping/create-map.js +0 -7
- package/core/mapping/for-member.d.ts +0 -5
- package/core/mapping/for-member.js +0 -8
- package/core/request-overflow/request-handler.base.d.ts +0 -4
- package/core/request-overflow/request-handler.base.js +0 -6
- package/core/request-overflow/request-result.d.ts +0 -15
- package/core/request-overflow/request-result.js +0 -37
- package/core/request-overflow/request-validator.base.d.ts +0 -7
- package/core/request-overflow/request-validator.base.js +0 -26
- package/core/security/strategies/api-key.strategy.d.ts +0 -16
- package/core/security/strategies/api-key.strategy.js +0 -31
- package/core/utils/assing-object.d.ts +0 -5
- package/core/utils/assing-object.js +0 -6
- package/core/utils/automap-cycle-context.d.ts +0 -6
- package/core/utils/automap-cycle-context.js +0 -33
- package/core/utils/env.config.d.ts +0 -6
- package/core/utils/env.config.js +0 -18
- package/core/utils/filter-request-params.d.ts +0 -13
- package/core/utils/filter-request-params.js +0 -22
- package/core/utils/find-on-list.d.ts +0 -2
- package/core/utils/find-on-list.js +0 -13
- package/core/utils/generate-prisma-include-schema.d.ts +0 -9
- package/core/utils/generate-prisma-include-schema.js +0 -60
- package/core/utils/get-type-by-prop.d.ts +0 -2
- package/core/utils/get-type-by-prop.js +0 -11
- package/core/utils/hydrate-entity-from-cache.d.ts +0 -22
- package/core/utils/hydrate-entity-from-cache.js +0 -76
- package/core/utils/instanciate-class-with-dependencies-injection.d.ts +0 -2
- package/core/utils/instanciate-class-with-dependencies-injection.js +0 -10
- package/core/utils/interfaces/icomparable.d.ts +0 -5
- package/core/utils/interfaces/icomparable.js +0 -6
- package/core/utils/is-plain-object.d.ts +0 -1
- package/core/utils/is-plain-object.js +0 -8
- package/core/utils/list.d.ts +0 -39
- package/core/utils/list.js +0 -167
- package/core/utils/promise-all.d.ts +0 -7
- package/core/utils/promise-all.js +0 -19
- package/core/utils/proxy.d.ts +0 -1
- package/core/utils/proxy.js +0 -27
- package/core/utils/set-mask-document-number.d.ts +0 -1
- package/core/utils/set-mask-document-number.js +0 -13
- package/core/validators/file-validator.d.ts +0 -27
- package/core/validators/file-validator.js +0 -94
- package/decorators/api-exclude-endpoint-diff-develop.decorator.d.ts +0 -1
- package/decorators/api-exclude-endpoint-diff-develop.decorator.js +0 -9
- package/decorators/api-property-enum.decorator.d.ts +0 -8
- package/decorators/api-property-enum.decorator.js +0 -21
- package/decorators/api-property-only-develop.decorator.d.ts +0 -2
- package/decorators/api-property-only-develop.decorator.js +0 -9
- package/decorators/cookies.decorator.d.ts +0 -1
- package/decorators/cookies.decorator.js +0 -8
- package/decorators/is-public.decorator.d.ts +0 -2
- package/decorators/is-public.decorator.js +0 -7
- package/decorators/upload.decorator.d.ts +0 -1
- package/decorators/upload.decorator.js +0 -18
- package/env/env.d.ts +0 -25
- package/env/env.js +0 -14
- package/env/env.module.d.ts +0 -2
- package/env/env.module.js +0 -20
- package/env/env.service.d.ts +0 -7
- package/env/env.service.js +0 -28
- package/filters/domain-errors.filter.d.ts +0 -18
- package/filters/domain-errors.filter.js +0 -92
- package/filters/global-exception.filter.d.ts +0 -8
- package/filters/global-exception.filter.js +0 -68
- package/filters/prisma-validation-exception.filter.d.ts +0 -10
- package/filters/prisma-validation-exception.filter.js +0 -82
- package/filters/zod-errors.filter.d.ts +0 -9
- package/filters/zod-errors.filter.js +0 -60
- package/services/logging/ilogging.service.d.ts +0 -16
- package/services/logging/ilogging.service.js +0 -6
- package/services/logging/logging.service.d.ts +0 -4
- package/services/logging/logging.service.js +0 -20
- package/services/redis/iredis.service.d.ts +0 -6
- package/services/redis/iredis.service.js +0 -6
- package/services/redis/redis.service.d.ts +0 -14
- package/services/redis/redis.service.js +0 -65
- package/services/redlock/ired-lock.service.d.ts +0 -4
- package/services/redlock/ired-lock.service.js +0 -6
- package/services/redlock/red-lock.service.d.ts +0 -9
- package/services/redlock/red-lock.service.js +0 -46
- package/test/koala-app-test-dependencies.d.ts +0 -10
- package/test/koala-app-test-dependencies.js +0 -13
- package/test/koala-app-test.d.ts +0 -22
- package/test/koala-app-test.js +0 -77
- package/test/repositories/in-memory-base.repository.d.ts +0 -17
- package/test/repositories/in-memory-base.repository.js +0 -67
- package/test/services/fake-logging.service.d.ts +0 -4
- package/test/services/fake-logging.service.js +0 -9
- package/test/services/fake-red-lock.service.d.ts +0 -5
- package/test/services/fake-red-lock.service.js +0 -11
- package/test/utils/create-e2e-database.d.ts +0 -7
- package/test/utils/create-e2e-database.js +0 -38
- package/test/utils/e2e-database-client.d.ts +0 -7
- package/test/utils/e2e-database-client.js +0 -12
- package/test/utils/wait-for.d.ts +0 -1
- package/test/utils/wait-for.js +0 -21
- package/tsconfig.lib.tsbuildinfo +0 -1
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { AuthProfile } from '@/core/auth/auth-profile.enum';
|
|
2
|
+
import { AuthenticatedUser } from '@/core/auth/jwt-claims';
|
|
3
|
+
import { PROFILES_KEY } from '@/host/decorators/restriction-by-profile.decorator';
|
|
4
|
+
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
|
|
5
|
+
import { Reflector } from '@nestjs/core';
|
|
6
|
+
|
|
7
|
+
@Injectable()
|
|
8
|
+
export class ProfilesGuard implements CanActivate {
|
|
9
|
+
constructor(private readonly reflector: Reflector) {}
|
|
10
|
+
|
|
11
|
+
canActivate(context: ExecutionContext) {
|
|
12
|
+
const profiles = this.reflector.getAllAndOverride<AuthProfile[]>(
|
|
13
|
+
PROFILES_KEY,
|
|
14
|
+
[context.getHandler(), context.getClass()],
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
if (!profiles?.length) {
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const request = context
|
|
22
|
+
.switchToHttp()
|
|
23
|
+
.getRequest<{ user?: AuthenticatedUser }>();
|
|
24
|
+
const profile = request.user?.profile;
|
|
25
|
+
|
|
26
|
+
if (!profile) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return profiles.includes(profile);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { AuthHttp } from '@/core/auth/auth.constants';
|
|
2
|
+
import { Env } from '@/core/env';
|
|
3
|
+
import { OAuthProviderRegistry } from '@/core/auth/oauth-provider.registry';
|
|
4
|
+
import {
|
|
5
|
+
IJwtTokenService,
|
|
6
|
+
IOAuth2Service,
|
|
7
|
+
} from '@/domain/auth/services/iauth.service';
|
|
8
|
+
import { JwtTokenService } from '@/infra/auth/jwt-token.service';
|
|
9
|
+
import { OAuth2AuthService } from '@/infra/auth/oauth2-auth.service';
|
|
10
|
+
import { InfraModule } from '@/infra/infra.module';
|
|
11
|
+
import { Module } from '@nestjs/common';
|
|
12
|
+
import { ConfigModule, ConfigService } from '@nestjs/config';
|
|
13
|
+
import { JwtModule } from '@nestjs/jwt';
|
|
14
|
+
import { PassportModule } from '@nestjs/passport';
|
|
15
|
+
import { JwtStrategy } from './strategies/jwt.strategy';
|
|
16
|
+
import { AuthGuard } from './guards/auth.guard';
|
|
17
|
+
import { ProfilesGuard } from './guards/profiles.guard';
|
|
18
|
+
|
|
19
|
+
@Module({
|
|
20
|
+
imports: [
|
|
21
|
+
InfraModule,
|
|
22
|
+
PassportModule.register({ defaultStrategy: 'jwt' }),
|
|
23
|
+
JwtModule.registerAsync({
|
|
24
|
+
imports: [ConfigModule],
|
|
25
|
+
inject: [ConfigService],
|
|
26
|
+
global: true,
|
|
27
|
+
useFactory(config: ConfigService<Env, true>) {
|
|
28
|
+
const privateKey = config.get('JWT_PRIVATE_KEY', { infer: true });
|
|
29
|
+
const publicKey = config.get('JWT_PUBLIC_KEY', { infer: true });
|
|
30
|
+
|
|
31
|
+
if (!privateKey || !publicKey) {
|
|
32
|
+
throw new Error(
|
|
33
|
+
'JWT_PRIVATE_KEY e JWT_PUBLIC_KEY são obrigatórios quando o módulo de autenticação está ativo',
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
signOptions: { algorithm: AuthHttp.JWT_ALGORITHM },
|
|
39
|
+
privateKey: Buffer.from(privateKey, 'base64'),
|
|
40
|
+
publicKey: Buffer.from(publicKey, 'base64'),
|
|
41
|
+
};
|
|
42
|
+
},
|
|
43
|
+
}),
|
|
44
|
+
],
|
|
45
|
+
providers: [
|
|
46
|
+
OAuthProviderRegistry,
|
|
47
|
+
JwtStrategy,
|
|
48
|
+
AuthGuard,
|
|
49
|
+
ProfilesGuard,
|
|
50
|
+
{ provide: IJwtTokenService, useClass: JwtTokenService },
|
|
51
|
+
{ provide: IOAuth2Service, useClass: OAuth2AuthService },
|
|
52
|
+
],
|
|
53
|
+
exports: [
|
|
54
|
+
IJwtTokenService,
|
|
55
|
+
IOAuth2Service,
|
|
56
|
+
OAuthProviderRegistry,
|
|
57
|
+
JwtModule,
|
|
58
|
+
AuthGuard,
|
|
59
|
+
ProfilesGuard,
|
|
60
|
+
],
|
|
61
|
+
})
|
|
62
|
+
export class SecurityModule {}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { AuthHttp } from '@/core/auth/auth.constants';
|
|
2
|
+
import { isAuthRefreshRoute } from '@/core/auth/auth-routes';
|
|
3
|
+
import { jwtPayloadSchema } from '@/core/auth/jwt-claims';
|
|
4
|
+
import { parseJwtExpiresInToSeconds } from '@/core/auth/parse-jwt-expires-in';
|
|
5
|
+
import { EnvService } from '@/infra/common/env.service';
|
|
6
|
+
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
|
7
|
+
import { PassportStrategy } from '@nestjs/passport';
|
|
8
|
+
import { Request } from 'express';
|
|
9
|
+
import { ExtractJwt, Strategy } from 'passport-jwt';
|
|
10
|
+
|
|
11
|
+
@Injectable()
|
|
12
|
+
export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
|
|
13
|
+
private readonly accessTokenTtlSeconds: number;
|
|
14
|
+
|
|
15
|
+
constructor(env: EnvService) {
|
|
16
|
+
const publicKey = env.get('JWT_PUBLIC_KEY');
|
|
17
|
+
|
|
18
|
+
if (!publicKey) {
|
|
19
|
+
throw new Error(
|
|
20
|
+
'JWT_PUBLIC_KEY é obrigatório quando o módulo de autenticação está ativo',
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
super({
|
|
25
|
+
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
|
26
|
+
secretOrKey: Buffer.from(publicKey, 'base64'),
|
|
27
|
+
algorithms: [AuthHttp.JWT_ALGORITHM],
|
|
28
|
+
passReqToCallback: true,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
this.accessTokenTtlSeconds = parseJwtExpiresInToSeconds(
|
|
32
|
+
env.get('JWT_ACCESS_TOKEN_EXPIRES_IN'),
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
private isLongLivedToken(payload: { exp?: number; iat?: number }): boolean {
|
|
37
|
+
if (!payload.exp || !payload.iat) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return payload.exp - payload.iat > this.accessTokenTtlSeconds;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
validate(req: Request, payload: { sub: string; exp?: number; iat?: number }) {
|
|
45
|
+
const isRefreshRoute = isAuthRefreshRoute(req.url);
|
|
46
|
+
const parsed = jwtPayloadSchema.parse(payload);
|
|
47
|
+
|
|
48
|
+
if (this.isLongLivedToken(payload) && !isRefreshRoute) {
|
|
49
|
+
throw new UnauthorizedException(
|
|
50
|
+
'Refresh token não pode ser usado como access token',
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const token = req
|
|
55
|
+
.get('Authorization')
|
|
56
|
+
?.replace(AuthHttp.BEARER_PREFIX, '')
|
|
57
|
+
.trim();
|
|
58
|
+
|
|
59
|
+
return { sub: parsed.sub, refreshToken: token };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { JwtClaims } from '@/core/auth/jwt-claims';
|
|
2
|
+
import { IJwtTokenService } from '@/domain/auth/services/iauth.service';
|
|
3
|
+
import { Injectable } from '@nestjs/common';
|
|
4
|
+
import { JwtService, type JwtSignOptions } from '@nestjs/jwt';
|
|
5
|
+
import { randomUUID } from 'node:crypto';
|
|
6
|
+
import { EnvService } from '@/infra/common/env.service';
|
|
7
|
+
|
|
8
|
+
@Injectable()
|
|
9
|
+
export class JwtTokenService implements IJwtTokenService {
|
|
10
|
+
constructor(
|
|
11
|
+
private readonly jwtService: JwtService,
|
|
12
|
+
private readonly env: EnvService,
|
|
13
|
+
) {}
|
|
14
|
+
|
|
15
|
+
private signOptions(
|
|
16
|
+
key: 'JWT_ACCESS_TOKEN_EXPIRES_IN' | 'JWT_REFRESH_TOKEN_EXPIRES_IN',
|
|
17
|
+
): JwtSignOptions {
|
|
18
|
+
return {
|
|
19
|
+
expiresIn: this.env.get(key) as JwtSignOptions['expiresIn'],
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
signAccessToken(claims: JwtClaims): string {
|
|
24
|
+
return this.jwtService.sign(
|
|
25
|
+
{ ...claims, jti: randomUUID() },
|
|
26
|
+
this.signOptions('JWT_ACCESS_TOKEN_EXPIRES_IN'),
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
signRefreshToken(claims: JwtClaims): string {
|
|
31
|
+
return this.jwtService.sign(
|
|
32
|
+
{ ...claims, jti: randomUUID() },
|
|
33
|
+
this.signOptions('JWT_REFRESH_TOKEN_EXPIRES_IN'),
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
signTokenPair(claims: JwtClaims) {
|
|
38
|
+
const accessToken = this.signAccessToken(claims);
|
|
39
|
+
const refreshToken = this.signRefreshToken(claims);
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
accessToken,
|
|
43
|
+
access_token: accessToken,
|
|
44
|
+
refreshToken,
|
|
45
|
+
refresh_token: refreshToken,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { AuthHttp } from '@/core/auth/auth.constants';
|
|
2
|
+
import {
|
|
3
|
+
CacheKeyPrefix,
|
|
4
|
+
CacheTtlSeconds,
|
|
5
|
+
} from '@/core/constants/cache.constants';
|
|
6
|
+
import { randomBytes } from 'node:crypto';
|
|
7
|
+
import { AuthProfile } from '@/core/auth/auth-profile.enum';
|
|
8
|
+
import { OAuthProviderRegistry } from '@/core/auth/oauth-provider.registry';
|
|
9
|
+
import type { OAuthProviderEnvConfig } from '@/core/auth/oauth-provider.registry';
|
|
10
|
+
import { AuthProviderConfigResponseType } from '@/core/types/auth-provider-config-response.type';
|
|
11
|
+
import { AuthProviderConfigDto } from '@/domain/auth/dtos/auth-provider-config.dto';
|
|
12
|
+
import { OAuthUserInfoDto } from '@/domain/auth/dtos/oauth-user-info.dto';
|
|
13
|
+
import { IOAuth2Service } from '@/domain/auth/services/iauth.service';
|
|
14
|
+
import { EnvService } from '@/infra/common/env.service';
|
|
15
|
+
import { ICacheService } from '@/domain/common/icache.service';
|
|
16
|
+
import { resolveApiHost } from '@/core/utils/resolve-api-host';
|
|
17
|
+
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
|
18
|
+
|
|
19
|
+
const OAUTH_STATE_TTL_SECONDS = CacheTtlSeconds.OAUTH2_STATE;
|
|
20
|
+
|
|
21
|
+
@Injectable()
|
|
22
|
+
export class OAuth2AuthService implements IOAuth2Service {
|
|
23
|
+
constructor(
|
|
24
|
+
private readonly env: EnvService,
|
|
25
|
+
private readonly providerRegistry: OAuthProviderRegistry,
|
|
26
|
+
private readonly cache: ICacheService,
|
|
27
|
+
) {}
|
|
28
|
+
|
|
29
|
+
private generateState() {
|
|
30
|
+
return randomBytes(24).toString('hex');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
private stateCacheKey(state: string) {
|
|
34
|
+
return `${CacheKeyPrefix.OAUTH2_STATE}${state}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
private getApiHost() {
|
|
38
|
+
return resolveApiHost(this.env.get('API_HOST'), this.env.get('PORT'));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Grava o state temporariamente para validar autenticidade no POST /oauth2/token (anti-CSRF). */
|
|
42
|
+
private async rememberState(provider: string, state: string) {
|
|
43
|
+
await this.cache.set(
|
|
44
|
+
this.stateCacheKey(state),
|
|
45
|
+
JSON.stringify({ provider }),
|
|
46
|
+
OAUTH_STATE_TTL_SECONDS,
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
private async validateState(provider: string, state: string) {
|
|
51
|
+
const raw = await this.cache.get(this.stateCacheKey(state));
|
|
52
|
+
|
|
53
|
+
if (!raw) {
|
|
54
|
+
throw new UnauthorizedException('State OAuth2 inválido ou expirado');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const entry = JSON.parse(raw) as { provider: string };
|
|
58
|
+
|
|
59
|
+
if (entry.provider !== provider) {
|
|
60
|
+
throw new UnauthorizedException('State OAuth2 inválido ou expirado');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
await this.cache.invalidate(this.stateCacheKey(state));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private async resolveProviderConfig(
|
|
67
|
+
provider: string,
|
|
68
|
+
): Promise<AuthProviderConfigDto> {
|
|
69
|
+
const providerConfig = this.providerRegistry.getProvider(provider);
|
|
70
|
+
const endpoints = await this.resolveOAuthEndpoints(providerConfig);
|
|
71
|
+
|
|
72
|
+
return AuthProviderConfigDto.from({
|
|
73
|
+
...endpoints,
|
|
74
|
+
clientId: providerConfig.clientId,
|
|
75
|
+
clientSecret: providerConfig.clientSecret,
|
|
76
|
+
state: '',
|
|
77
|
+
redirectUri: `${this.getApiHost()}${providerConfig.redirectPath}`,
|
|
78
|
+
scope: providerConfig.scope,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private async resolveOAuthEndpoints(providerConfig: OAuthProviderEnvConfig) {
|
|
83
|
+
if (
|
|
84
|
+
providerConfig.authorizationUrl &&
|
|
85
|
+
providerConfig.tokenUrl &&
|
|
86
|
+
providerConfig.userInfoUrl
|
|
87
|
+
) {
|
|
88
|
+
return {
|
|
89
|
+
authorizationUrl: providerConfig.authorizationUrl,
|
|
90
|
+
tokenUrl: providerConfig.tokenUrl,
|
|
91
|
+
userInfoUrl: providerConfig.userInfoUrl,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const discovery = await fetch(
|
|
96
|
+
`${providerConfig.domain}/.well-known/openid-configuration`,
|
|
97
|
+
).then(
|
|
98
|
+
(response) => response.json() as Promise<AuthProviderConfigResponseType>,
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
authorizationUrl: discovery.authorization_endpoint,
|
|
103
|
+
tokenUrl: discovery.token_endpoint,
|
|
104
|
+
userInfoUrl: discovery.userinfo_endpoint,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
private async userInfo(
|
|
109
|
+
config: AuthProviderConfigDto,
|
|
110
|
+
accessToken: string,
|
|
111
|
+
): Promise<OAuthUserInfoDto> {
|
|
112
|
+
const data = await fetch(config.userInfoUrl, {
|
|
113
|
+
headers: { Authorization: `${AuthHttp.BEARER_PREFIX}${accessToken}` },
|
|
114
|
+
}).then((response) => response.json() as Promise<Record<string, string>>);
|
|
115
|
+
|
|
116
|
+
const email = data.email ?? data.unique_name ?? data.upn ?? '';
|
|
117
|
+
const login =
|
|
118
|
+
data.samaccountname ??
|
|
119
|
+
data.preferred_username ??
|
|
120
|
+
(email ? email.split('@')[0] : (data.sub ?? ''));
|
|
121
|
+
|
|
122
|
+
return OAuthUserInfoDto.from({
|
|
123
|
+
login,
|
|
124
|
+
email,
|
|
125
|
+
name: data.name,
|
|
126
|
+
profile: AuthProfile.user,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
private async exchangeAuthorizationCode(
|
|
131
|
+
provider: string,
|
|
132
|
+
code: string,
|
|
133
|
+
redirectUri?: string,
|
|
134
|
+
): Promise<OAuthUserInfoDto> {
|
|
135
|
+
const config = await this.resolveProviderConfig(provider);
|
|
136
|
+
|
|
137
|
+
const formData = new URLSearchParams();
|
|
138
|
+
formData.append('code', code);
|
|
139
|
+
formData.append('redirect_uri', redirectUri ?? config.redirectUri);
|
|
140
|
+
formData.append('client_id', config.clientId);
|
|
141
|
+
formData.append('client_secret', config.clientSecret);
|
|
142
|
+
formData.append('grant_type', 'authorization_code');
|
|
143
|
+
|
|
144
|
+
const tokenResponse = await fetch(config.tokenUrl, {
|
|
145
|
+
method: 'POST',
|
|
146
|
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
147
|
+
body: formData.toString(),
|
|
148
|
+
}).then((response) => response.json() as Promise<Record<string, string>>);
|
|
149
|
+
|
|
150
|
+
if (tokenResponse.error || !tokenResponse.access_token) {
|
|
151
|
+
throw new UnauthorizedException(
|
|
152
|
+
tokenResponse.error_description ??
|
|
153
|
+
tokenResponse.error ??
|
|
154
|
+
'Falha ao trocar código OAuth2 por token',
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return this.userInfo(config, tokenResponse.access_token);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async providerConfig(provider: string): Promise<AuthProviderConfigDto> {
|
|
162
|
+
const providerConfig = this.providerRegistry.getProvider(provider);
|
|
163
|
+
const config = await this.resolveProviderConfig(provider);
|
|
164
|
+
const state = this.generateState();
|
|
165
|
+
await this.rememberState(providerConfig.key, state);
|
|
166
|
+
|
|
167
|
+
return AuthProviderConfigDto.from({
|
|
168
|
+
...config,
|
|
169
|
+
state,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async resolveScalarOAuthFlow(provider: string) {
|
|
174
|
+
const config = await this.resolveProviderConfig(provider);
|
|
175
|
+
|
|
176
|
+
return {
|
|
177
|
+
authorizationUrl: `${config.authorizationUrl}?scope=${encodeURIComponent(config.scope)}`,
|
|
178
|
+
redirectUri: config.redirectUri,
|
|
179
|
+
clientId: config.clientId,
|
|
180
|
+
clientSecret: config.clientSecret,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async authLink(provider: string, redirectUri?: string): Promise<string> {
|
|
185
|
+
const config = await this.providerConfig(provider);
|
|
186
|
+
const targetRedirect = redirectUri ?? config.redirectUri;
|
|
187
|
+
|
|
188
|
+
return `${config.authorizationUrl}?response_type=code&client_id=${config.clientId}&state=${config.state}&redirect_uri=${encodeURIComponent(targetRedirect)}&scope=${encodeURIComponent(config.scope)}`;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
async exchangeScalarCode(
|
|
192
|
+
provider: string,
|
|
193
|
+
code: string,
|
|
194
|
+
redirectUri?: string,
|
|
195
|
+
): Promise<OAuthUserInfoDto> {
|
|
196
|
+
return this.exchangeAuthorizationCode(provider, code, redirectUri);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async exchangeCode(
|
|
200
|
+
provider: string,
|
|
201
|
+
code: string,
|
|
202
|
+
state: string,
|
|
203
|
+
redirectUri?: string,
|
|
204
|
+
): Promise<OAuthUserInfoDto> {
|
|
205
|
+
await this.validateState(provider, state);
|
|
206
|
+
return this.exchangeAuthorizationCode(provider, code, redirectUri);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import packageJson from '../../../package.json';
|
|
2
|
+
import { ICacheService } from '@/domain/common/icache.service';
|
|
3
|
+
import { EnvService } from '@/infra/common/env.service';
|
|
4
|
+
import { InMemoryCacheService } from '@/infra/common/in-memory-cache.service';
|
|
5
|
+
import { RedisCacheService } from '@/infra/common/redis-cache.service';
|
|
6
|
+
import { Injectable, OnModuleDestroy } from '@nestjs/common';
|
|
7
|
+
|
|
8
|
+
@Injectable()
|
|
9
|
+
export class CacheServiceProvider implements ICacheService, OnModuleDestroy {
|
|
10
|
+
private readonly delegate: ICacheService & Partial<OnModuleDestroy>;
|
|
11
|
+
|
|
12
|
+
constructor(env: EnvService) {
|
|
13
|
+
const redisUrl = env.get('REDIS_CONNECTION_STRING');
|
|
14
|
+
|
|
15
|
+
this.delegate = redisUrl
|
|
16
|
+
? new RedisCacheService(redisUrl, this.resolveKeyPrefix(env))
|
|
17
|
+
: new InMemoryCacheService();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
private resolveKeyPrefix(env: EnvService) {
|
|
21
|
+
return env.get('CACHE_KEY_PREFIX') ?? packageJson.name;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
get(key: string): Promise<string | null> {
|
|
25
|
+
return this.delegate.get(key);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
set(key: string, value: string, ttl?: number): Promise<void> {
|
|
29
|
+
return this.delegate.set(key, value, ttl);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
setIfNotExists(key: string, value: string, ttl: number): Promise<boolean> {
|
|
33
|
+
return this.delegate.setIfNotExists(key, value, ttl);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
invalidate(key: string): Promise<void> {
|
|
37
|
+
return this.delegate.invalidate(key);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
invalidateByPrefix(prefix: string): Promise<void> {
|
|
41
|
+
return this.delegate.invalidateByPrefix(prefix);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
onModuleDestroy() {
|
|
45
|
+
this.delegate.onModuleDestroy?.();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Env } from '@/core/env';
|
|
2
|
+
import { Injectable } from '@nestjs/common';
|
|
3
|
+
import { ConfigService } from '@nestjs/config';
|
|
4
|
+
|
|
5
|
+
@Injectable()
|
|
6
|
+
export class EnvService {
|
|
7
|
+
constructor(private readonly configService: ConfigService<Env, true>) {}
|
|
8
|
+
|
|
9
|
+
get<T extends keyof Env>(key: T) {
|
|
10
|
+
return this.configService.get(key, { infer: true });
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { ICacheService } from '@/domain/common/icache.service';
|
|
2
|
+
import { Injectable } from '@nestjs/common';
|
|
3
|
+
|
|
4
|
+
type CacheEntry = {
|
|
5
|
+
value: string;
|
|
6
|
+
expiresAt?: number;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Cache em memória usado quando `REDIS_CONNECTION_STRING` não está configurado.
|
|
11
|
+
*/
|
|
12
|
+
@Injectable()
|
|
13
|
+
export class InMemoryCacheService implements ICacheService {
|
|
14
|
+
private readonly store = new Map<string, CacheEntry>();
|
|
15
|
+
|
|
16
|
+
get(key: string): Promise<string | null> {
|
|
17
|
+
const entry = this.store.get(key);
|
|
18
|
+
|
|
19
|
+
if (!entry) {
|
|
20
|
+
return Promise.resolve(null);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (entry.expiresAt !== undefined && entry.expiresAt <= Date.now()) {
|
|
24
|
+
this.store.delete(key);
|
|
25
|
+
return Promise.resolve(null);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return Promise.resolve(entry.value);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
set(key: string, value: string, ttl?: number): Promise<void> {
|
|
32
|
+
this.store.set(key, {
|
|
33
|
+
value,
|
|
34
|
+
expiresAt: ttl ? Date.now() + ttl * 1000 : undefined,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
return Promise.resolve();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async setIfNotExists(
|
|
41
|
+
key: string,
|
|
42
|
+
value: string,
|
|
43
|
+
ttl: number,
|
|
44
|
+
): Promise<boolean> {
|
|
45
|
+
const existing = await this.get(key);
|
|
46
|
+
|
|
47
|
+
if (existing) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
await this.set(key, value, ttl);
|
|
52
|
+
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
invalidate(key: string): Promise<void> {
|
|
57
|
+
this.store.delete(key);
|
|
58
|
+
|
|
59
|
+
return Promise.resolve();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
invalidateByPrefix(prefix: string): Promise<void> {
|
|
63
|
+
for (const key of this.store.keys()) {
|
|
64
|
+
if (key.startsWith(prefix)) {
|
|
65
|
+
this.store.delete(key);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return Promise.resolve();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ILoggingService,
|
|
3
|
+
LoggingReportProps,
|
|
4
|
+
} from '@/domain/common/ilogging.service';
|
|
5
|
+
import { Injectable, Logger } from '@nestjs/common';
|
|
6
|
+
|
|
7
|
+
@Injectable()
|
|
8
|
+
export class LoggingService implements ILoggingService {
|
|
9
|
+
private readonly logger = new Logger(LoggingService.name);
|
|
10
|
+
|
|
11
|
+
report(data: LoggingReportProps): Promise<void> {
|
|
12
|
+
this.logger.error(
|
|
13
|
+
`[${data.packageName}] ${data.loggedUsername}: ${data.error.message}`,
|
|
14
|
+
data.error.stack,
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
return Promise.resolve();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { CacheKeyPrefix } from '@/core/constants/cache.constants';
|
|
2
|
+
import { ICacheService } from '@/domain/common/icache.service';
|
|
3
|
+
import { IRedLockService } from '@/domain/common/ired-lock.service';
|
|
4
|
+
import { EnvService } from '@/infra/common/env.service';
|
|
5
|
+
import { Injectable } from '@nestjs/common';
|
|
6
|
+
|
|
7
|
+
@Injectable()
|
|
8
|
+
export class RedLockService implements IRedLockService {
|
|
9
|
+
constructor(
|
|
10
|
+
private readonly cache: ICacheService,
|
|
11
|
+
private readonly env: EnvService,
|
|
12
|
+
) {}
|
|
13
|
+
|
|
14
|
+
private shouldBypassDistributedLock() {
|
|
15
|
+
return (
|
|
16
|
+
this.env.get('NODE_ENV') === 'test' ||
|
|
17
|
+
!this.env.get('REDIS_CONNECTION_STRING')
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
private getLockKey(key: string) {
|
|
22
|
+
return `${CacheKeyPrefix.RED_LOCK}${key}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async acquiredLock(key: string, ttlSecondsLock: number): Promise<boolean> {
|
|
26
|
+
if (this.shouldBypassDistributedLock()) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return this.cache.setIfNotExists(
|
|
31
|
+
this.getLockKey(key),
|
|
32
|
+
'RedLockService',
|
|
33
|
+
ttlSecondsLock,
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async releaseLock(key: string): Promise<void> {
|
|
38
|
+
if (this.shouldBypassDistributedLock()) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
await this.cache.invalidate(this.getLockKey(key));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { ICacheService } from '@/domain/common/icache.service';
|
|
2
|
+
import Redis from 'ioredis';
|
|
3
|
+
|
|
4
|
+
export class RedisCacheService implements ICacheService {
|
|
5
|
+
private readonly client: Redis;
|
|
6
|
+
|
|
7
|
+
constructor(
|
|
8
|
+
connectionString: string,
|
|
9
|
+
private readonly keyPrefix: string,
|
|
10
|
+
client?: Redis,
|
|
11
|
+
) {
|
|
12
|
+
this.client =
|
|
13
|
+
client ??
|
|
14
|
+
new Redis(connectionString, {
|
|
15
|
+
maxRetriesPerRequest: 3,
|
|
16
|
+
lazyConnect: false,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
private buildKey(key: string) {
|
|
21
|
+
return `${this.keyPrefix}:${key}`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
get(key: string): Promise<string | null> {
|
|
25
|
+
return this.client.get(this.buildKey(key));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async set(key: string, value: string, ttl?: number): Promise<void> {
|
|
29
|
+
const redisKey = this.buildKey(key);
|
|
30
|
+
|
|
31
|
+
if (ttl !== undefined && ttl > 0) {
|
|
32
|
+
await this.client.set(redisKey, value, 'EX', Math.ceil(ttl));
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
await this.client.set(redisKey, value);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async setIfNotExists(
|
|
40
|
+
key: string,
|
|
41
|
+
value: string,
|
|
42
|
+
ttl: number,
|
|
43
|
+
): Promise<boolean> {
|
|
44
|
+
const redisKey = this.buildKey(key);
|
|
45
|
+
const result = await this.client.set(
|
|
46
|
+
redisKey,
|
|
47
|
+
value,
|
|
48
|
+
'EX',
|
|
49
|
+
Math.ceil(ttl),
|
|
50
|
+
'NX',
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
return result === 'OK';
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
invalidate(key: string): Promise<void> {
|
|
57
|
+
return this.client.del(this.buildKey(key)).then(() => undefined);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async invalidateByPrefix(prefix: string): Promise<void> {
|
|
61
|
+
const pattern = `${this.buildKey(prefix)}*`;
|
|
62
|
+
const keys = await this.client.keys(pattern);
|
|
63
|
+
|
|
64
|
+
if (keys.length > 0) {
|
|
65
|
+
await this.client.del(...keys);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
onModuleDestroy() {
|
|
70
|
+
this.client.disconnect();
|
|
71
|
+
}
|
|
72
|
+
}
|