@atlantjs/arch 2.5.12 → 2.6.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.
Files changed (140) hide show
  1. package/README.md +8 -8
  2. package/external-files/biome.json +47 -47
  3. package/external-files/typescript/tsconfig-backend.json +3 -3
  4. package/external-files/typescript/tsconfig-default.json +45 -45
  5. package/external-files/typescript/tsconfig-frontend.json +3 -3
  6. package/external-files/typescript/tsconfig-lib.json +6 -6
  7. package/external-files/typescript/tsconfig-mobile.json +3 -3
  8. package/index.d.ts +1 -33
  9. package/index.js +3 -62
  10. package/objects/arch/application/errors/failure.abstract.js +1 -1
  11. package/objects/arch/application/errors/unknown-application-error.js +1 -1
  12. package/package.json +2 -1
  13. package/tsconfig.tsbuildinfo +1 -1
  14. package/utils/datatypes/string-utils.d.ts +13 -1
  15. package/utils/datatypes/string-utils.js +41 -1
  16. package/utils/http-client/http-client-exception.d.ts +3 -0
  17. package/utils/http-client/http-client-exception.js +12 -0
  18. package/utils/http-client/http-client-request.d.ts +9 -0
  19. package/utils/http-client/http-client-response.d.ts +18 -0
  20. package/utils/http-client/http-client-response.get-header.failure.d.ts +4 -0
  21. package/utils/http-client/http-client-response.get-header.failure.js +14 -0
  22. package/utils/http-client/http-client-response.js +39 -0
  23. package/utils/http-client/http-client.d.ts +9 -0
  24. package/utils/http-client/http-client.js +21 -0
  25. package/objects/auth/entities/client/client-credentials.abstract.d.ts +0 -13
  26. package/objects/auth/entities/client/client-credentials.abstract.js +0 -27
  27. package/objects/auth/entities/client/client-credentials.abstract.type.d.ts +0 -7
  28. package/objects/auth/entities/client/realm-credentials.abstract.d.ts +0 -16
  29. package/objects/auth/entities/client/realm-credentials.abstract.js +0 -23
  30. package/objects/auth/entities/token/access-token-signature.d.ts +0 -10
  31. package/objects/auth/entities/token/access-token-signature.js +0 -47
  32. package/objects/auth/entities/token/access-token.abstract.d.ts +0 -10
  33. package/objects/auth/entities/token/access-token.abstract.has-role.failure.d.ts +0 -4
  34. package/objects/auth/entities/token/access-token.abstract.has-role.failure.js +0 -14
  35. package/objects/auth/entities/token/access-token.abstract.js +0 -25
  36. package/objects/auth/entities/token/access-token.abstract.signature-verify.failure.d.ts +0 -4
  37. package/objects/auth/entities/token/access-token.abstract.signature-verify.failure.js +0 -10
  38. package/objects/auth/entities/token/access-token.abstract.type.d.ts +0 -44
  39. package/objects/auth/entities/token/access-token.abstract.type.js +0 -2
  40. package/objects/auth/entities/token/rotation.d.ts +0 -12
  41. package/objects/auth/entities/token/rotation.js +0 -97
  42. package/objects/auth/entities/token/rotation.type.d.ts +0 -5
  43. package/objects/auth/entities/token/rotation.type.js +0 -2
  44. package/objects/auth/entities/token/token.d.ts +0 -16
  45. package/objects/auth/entities/token/token.js +0 -50
  46. package/objects/auth/entities/token/token.type.d.ts +0 -5
  47. package/objects/auth/entities/token/token.type.js +0 -2
  48. package/objects/auth/entities/user/enums/user-status.enum.d.ts +0 -4
  49. package/objects/auth/entities/user/enums/user-status.enum.js +0 -8
  50. package/objects/auth/entities/user/password/password.abstract.d.ts +0 -8
  51. package/objects/auth/entities/user/password/password.abstract.js +0 -41
  52. package/objects/auth/entities/user/password/password.abstract.type.d.ts +0 -5
  53. package/objects/auth/entities/user/password/password.abstract.type.js +0 -2
  54. package/objects/auth/entities/user/password/steps/not-allowing-consecutive-chars-step.d.ts +0 -11
  55. package/objects/auth/entities/user/password/steps/not-allowing-consecutive-chars-step.js +0 -34
  56. package/objects/auth/entities/user/password/steps/not-allowing-date-step.d.ts +0 -11
  57. package/objects/auth/entities/user/password/steps/not-allowing-date-step.js +0 -61
  58. package/objects/auth/entities/user/password/steps/not-allowing-phone-step.d.ts +0 -7
  59. package/objects/auth/entities/user/password/steps/not-allowing-phone-step.js +0 -27
  60. package/objects/auth/entities/user/password/steps/not-allowing-repeated-chars-step.d.ts +0 -11
  61. package/objects/auth/entities/user/password/steps/not-allowing-repeated-chars-step.js +0 -34
  62. package/objects/auth/entities/user/password/steps/not-allowing-string-step.d.ts +0 -7
  63. package/objects/auth/entities/user/password/steps/not-allowing-string-step.js +0 -19
  64. package/objects/auth/entities/user/password/steps/not-allowing-these-chars-step.d.ts +0 -10
  65. package/objects/auth/entities/user/password/steps/not-allowing-these-chars-step.js +0 -17
  66. package/objects/auth/entities/user/password/steps/numeric-step.d.ts +0 -5
  67. package/objects/auth/entities/user/password/steps/numeric-step.js +0 -13
  68. package/objects/auth/entities/user/password/steps/with-at-least-one-letter-step.d.ts +0 -5
  69. package/objects/auth/entities/user/password/steps/with-at-least-one-letter-step.js +0 -14
  70. package/objects/auth/entities/user/password/steps/with-at-least-one-number-digit-step.d.ts +0 -5
  71. package/objects/auth/entities/user/password/steps/with-at-least-one-number-digit-step.js +0 -14
  72. package/objects/auth/entities/user/password/steps/with-at-least-one-special-char-step.d.ts +0 -5
  73. package/objects/auth/entities/user/password/steps/with-at-least-one-special-char-step.js +0 -14
  74. package/objects/auth/entities/user/password/steps/with-length-step.d.ts +0 -11
  75. package/objects/auth/entities/user/password/steps/with-length-step.js +0 -18
  76. package/objects/auth/entities/user/password/validation-step.d.ts +0 -5
  77. package/objects/auth/entities/user/password/validation-step.js +0 -9
  78. package/objects/auth/entities/user/password/validation-step.type.d.ts +0 -4
  79. package/objects/auth/entities/user/password/validation-step.type.js +0 -2
  80. package/objects/auth/entities/user/user-credentials.abstract.d.ts +0 -12
  81. package/objects/auth/entities/user/user-credentials.abstract.js +0 -9
  82. package/objects/auth/entities/user/user-tokens.abstract.d.ts +0 -14
  83. package/objects/auth/entities/user/user-tokens.abstract.js +0 -27
  84. package/objects/auth/entities/user/user-tokens.abstract.type.d.ts +0 -5
  85. package/objects/auth/entities/user/user-tokens.abstract.type.js +0 -2
  86. package/objects/auth/entities/user/user.abstract.d.ts +0 -24
  87. package/objects/auth/entities/user/user.abstract.js +0 -32
  88. package/objects/auth/entities/user/value-objects/email.d.ts +0 -6
  89. package/objects/auth/entities/user/value-objects/email.js +0 -17
  90. package/objects/auth/entities/user/value-objects/person-name.d.ts +0 -7
  91. package/objects/auth/entities/user/value-objects/person-name.js +0 -17
  92. package/objects/auth/entities/user/value-objects/user-id.d.ts +0 -3
  93. package/objects/auth/entities/user/value-objects/user-id.js +0 -7
  94. package/objects/auth/entities/user/value-objects/username.d.ts +0 -3
  95. package/objects/auth/entities/user/value-objects/username.js +0 -7
  96. package/objects/auth/enums/client-role-group.enum.d.ts +0 -3
  97. package/objects/auth/enums/client-role-group.enum.js +0 -7
  98. package/objects/auth/enums/client-role.enum.d.ts +0 -3
  99. package/objects/auth/enums/client-role.enum.js +0 -7
  100. package/objects/auth/enums/client.enum.d.ts +0 -3
  101. package/objects/auth/enums/client.enum.js +0 -7
  102. package/objects/auth/enums/realm.enum.d.ts +0 -3
  103. package/objects/auth/enums/realm.enum.js +0 -7
  104. package/objects/auth/enums/role-group.type.d.ts +0 -3
  105. package/objects/auth/enums/role-group.type.js +0 -2
  106. package/objects/auth/enums/role.type.d.ts +0 -3
  107. package/objects/auth/enums/role.type.js +0 -2
  108. package/objects/auth/enums/token-name-header.enum.d.ts +0 -6
  109. package/objects/auth/enums/token-name-header.enum.js +0 -10
  110. package/objects/auth/enums/user-role-group.enum.d.ts +0 -4
  111. package/objects/auth/enums/user-role-group.enum.js +0 -8
  112. package/objects/auth/enums/user-role.enum.d.ts +0 -17
  113. package/objects/auth/enums/user-role.enum.js +0 -21
  114. package/tests/builders/auth/access-token-signature.builder.d.ts +0 -6
  115. package/tests/builders/auth/access-token-signature.builder.js +0 -19
  116. package/tests/builders/auth/access-token.builder.d.ts +0 -6
  117. package/tests/builders/auth/access-token.builder.js +0 -15
  118. package/tests/builders/auth/rotation.builder.d.ts +0 -6
  119. package/tests/builders/auth/rotation.builder.js +0 -19
  120. package/tests/builders/auth/token.builder.d.ts +0 -7
  121. package/tests/builders/auth/token.builder.js +0 -15
  122. package/tests/builders/auth/value-objects/client-credentials.builder.d.ts +0 -6
  123. package/tests/builders/auth/value-objects/client-credentials.builder.js +0 -18
  124. package/tests/builders/auth/value-objects/dummies/access-token.dummy.d.ts +0 -3
  125. package/tests/builders/auth/value-objects/dummies/access-token.dummy.js +0 -7
  126. package/tests/builders/auth/value-objects/dummies/client-credentials.dummy.d.ts +0 -3
  127. package/tests/builders/auth/value-objects/dummies/client-credentials.dummy.js +0 -7
  128. package/tests/builders/auth/value-objects/dummies/realm-credentials.dummy.d.ts +0 -6
  129. package/tests/builders/auth/value-objects/dummies/realm-credentials.dummy.js +0 -12
  130. package/tests/builders/auth/value-objects/dummies/user-tokens.dummy.d.ts +0 -3
  131. package/tests/builders/auth/value-objects/dummies/user-tokens.dummy.js +0 -7
  132. package/tests/builders/auth/value-objects/realm-credentials.builder.d.ts +0 -6
  133. package/tests/builders/auth/value-objects/realm-credentials.builder.js +0 -17
  134. package/tests/builders/auth/value-objects/user-tokens.builder.d.ts +0 -7
  135. package/tests/builders/auth/value-objects/user-tokens.builder.js +0 -18
  136. package/tests/builders/user/person-name.builder.d.ts +0 -6
  137. package/tests/builders/user/person-name.builder.js +0 -15
  138. package/tests/builders/user/user-id.builder.d.ts +0 -6
  139. package/tests/builders/user/user-id.builder.js +0 -15
  140. /package/{objects/auth/entities/client/client-credentials.abstract.type.js → utils/http-client/http-client-request.js} +0 -0
@@ -1,3 +1,15 @@
1
1
  interface String {
2
- toKebab(): string;
2
+ toKebabCase(): string;
3
+ toSnakeCase(): string;
4
+ toPascalCase(): string;
5
+ reverse(): string;
6
+ trimSpaces(): string;
7
+ capitalize(): string;
8
+ toTitleCase(): string;
9
+ contains(substring: string): boolean;
10
+ truncate(length: number): String;
11
+ removeWhitespace(): string;
12
+ toArray(): string[];
13
+ isEmpty(): boolean;
14
+ fromBase64(): string;
3
15
  }
@@ -1,4 +1,44 @@
1
1
  "use strict";
2
- String.prototype.toKebab = function () {
2
+ String.prototype.toKebabCase = function () {
3
3
  return this.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
4
4
  };
5
+ String.prototype.toSnakeCase = function () {
6
+ return this.replace(/([a-z])([A-Z])/g, "$1_$2").toLowerCase();
7
+ };
8
+ String.prototype.toPascalCase = function () {
9
+ return this.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, (match) => match.toUpperCase()).replace(/\s+/g, "");
10
+ };
11
+ String.prototype.reverse = function () {
12
+ return this.split("").reverse().join("");
13
+ };
14
+ String.prototype.trimSpaces = function () {
15
+ return this.replace(/\s+/g, " ").trim();
16
+ };
17
+ String.prototype.capitalize = function () {
18
+ return this.split(" ")
19
+ .map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
20
+ .join(" ");
21
+ };
22
+ String.prototype.toTitleCase = function () {
23
+ return this.replace(/\w\S*/g, (word) => word.charAt(0).toUpperCase() + word.substr(1).toLowerCase());
24
+ };
25
+ String.prototype.contains = function (substring) {
26
+ return this.indexOf(substring) !== -1;
27
+ };
28
+ String.prototype.truncate = function (length) {
29
+ if (this.length <= length)
30
+ return this;
31
+ return `${this.substring(0, length - 3)}...`;
32
+ };
33
+ String.prototype.removeWhitespace = function () {
34
+ return this.replace(/\s+/g, "");
35
+ };
36
+ String.prototype.toArray = function () {
37
+ return this.split("");
38
+ };
39
+ String.prototype.isEmpty = function () {
40
+ return this.length === 0;
41
+ };
42
+ String.prototype.fromBase64 = function () {
43
+ return atob(this.toString());
44
+ };
@@ -0,0 +1,3 @@
1
+ export declare class HttpClientException extends Error {
2
+ constructor(message: string, name?: string, stack?: string);
3
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpClientException = void 0;
4
+ class HttpClientException extends Error {
5
+ constructor(message, name, stack) {
6
+ super(message);
7
+ this.stack = stack;
8
+ this.name = name ?? HttpClientException.name;
9
+ this.message = message;
10
+ }
11
+ }
12
+ exports.HttpClientException = HttpClientException;
@@ -0,0 +1,9 @@
1
+ import { ResponseType } from "axios";
2
+ export interface HttpClientRequest {
3
+ url: string;
4
+ method: "get" | "post" | "put" | "patch" | "delete";
5
+ params?: unknown;
6
+ data?: unknown;
7
+ headers?: Record<string, string>;
8
+ responseType?: ResponseType;
9
+ }
@@ -0,0 +1,18 @@
1
+ import { AxiosResponseHeaders, RawAxiosResponseHeaders } from "axios";
2
+ import { FailureAbstract } from "../../objects/arch/application/errors/failure.abstract";
3
+ import { Newable, ObjectType } from "../datatypes/generic-types";
4
+ import { Optional } from "../ducts/optional-type";
5
+ import { Return } from "./../ducts/return-type";
6
+ type Header = RawAxiosResponseHeaders | AxiosResponseHeaders;
7
+ export declare class HttpClientResponse {
8
+ readonly statusCode: number;
9
+ private readonly data;
10
+ private readonly headers?;
11
+ constructor(statusCode: number, data: unknown, headers?: Header | undefined);
12
+ hasFailure(): boolean;
13
+ hasSuccess(): boolean;
14
+ hasStatus(status: number): boolean;
15
+ getData<T extends ObjectType>(dataType: Newable<T>): Promise<T>;
16
+ getHeader(key: string): Return<Optional<string>, FailureAbstract>;
17
+ }
18
+ export {};
@@ -0,0 +1,4 @@
1
+ import { FailureAbstract } from "../../objects/arch/application/errors/failure.abstract";
2
+ export declare class HttpClientResponseGetHeaderFailure extends FailureAbstract {
3
+ constructor(keyName: string, error?: any);
4
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpClientResponseGetHeaderFailure = void 0;
4
+ const failure_abstract_1 = require("../../objects/arch/application/errors/failure.abstract");
5
+ class HttpClientResponseGetHeaderFailure extends failure_abstract_1.FailureAbstract {
6
+ constructor(keyName, error) {
7
+ super({
8
+ className: HttpClientResponseGetHeaderFailure.name,
9
+ message: `Header does not contain defined ${keyName} key`,
10
+ error,
11
+ });
12
+ }
13
+ }
14
+ exports.HttpClientResponseGetHeaderFailure = HttpClientResponseGetHeaderFailure;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpClientResponse = void 0;
4
+ const class_transformer_1 = require("class-transformer");
5
+ const class_validator_1 = require("class-validator");
6
+ const http_status_codes_enum_1 = require("../../objects/arch/infrastructure/http-status-codes.enum");
7
+ const optional_type_1 = require("../ducts/optional-type");
8
+ const http_client_response_get_header_failure_1 = require("./http-client-response.get-header.failure");
9
+ const guardian_1 = require("../type-guard/guardian");
10
+ const return_type_1 = require("./../ducts/return-type");
11
+ class HttpClientResponse {
12
+ constructor(statusCode, data, headers) {
13
+ this.statusCode = statusCode;
14
+ this.data = data;
15
+ this.headers = headers;
16
+ }
17
+ hasFailure() {
18
+ return (this.statusCode >= http_status_codes_enum_1.HttpStatusCodes.BAD_REQUEST &&
19
+ this.statusCode < http_status_codes_enum_1.HttpStatusCodes.INTERNAL_SERVER_ERROR);
20
+ }
21
+ hasSuccess() {
22
+ return guardian_1.Guardian.isFalsy(this.hasFailure());
23
+ }
24
+ hasStatus(status) {
25
+ return this.statusCode === status;
26
+ }
27
+ async getData(dataType) {
28
+ const data = (0, class_transformer_1.plainToClass)(dataType, this.data);
29
+ await (0, class_validator_1.validateOrReject)(data);
30
+ return data;
31
+ }
32
+ getHeader(key) {
33
+ if (guardian_1.Guardian.isUndefined(this.headers))
34
+ return (0, return_type_1.Failure)(new http_client_response_get_header_failure_1.HttpClientResponseGetHeaderFailure(key));
35
+ const header = this.headers[key];
36
+ return (0, return_type_1.Success)((0, optional_type_1.Optional)(header));
37
+ }
38
+ }
39
+ exports.HttpClientResponse = HttpClientResponse;
@@ -0,0 +1,9 @@
1
+ import { HttpClientRequest } from "./http-client-request";
2
+ import { HttpClientResponse } from "./http-client-response";
3
+ import { AxiosInstance } from "axios";
4
+ export declare class HttpClient {
5
+ readonly baseUrl: string;
6
+ protected readonly axios: AxiosInstance;
7
+ constructor(baseUrl: string);
8
+ request(request: HttpClientRequest): Promise<HttpClientResponse>;
9
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.HttpClient = void 0;
7
+ const http_client_response_1 = require("./http-client-response");
8
+ const axios_1 = __importDefault(require("axios"));
9
+ class HttpClient {
10
+ constructor(baseUrl) {
11
+ this.baseUrl = baseUrl;
12
+ this.axios = axios_1.default.create({
13
+ baseURL: baseUrl,
14
+ });
15
+ }
16
+ async request(request) {
17
+ const response = await this.axios.request(request);
18
+ return new http_client_response_1.HttpClientResponse(response.status, response.data, response.headers);
19
+ }
20
+ }
21
+ exports.HttpClient = HttpClient;
@@ -1,13 +0,0 @@
1
- import { ClientCredentialsPrimitive } from "./client-credentials.abstract.type";
2
- import { ClientEnum } from "../../enums/client.enum";
3
- import { RealmEnum } from "../../enums/realm.enum";
4
- export declare class ClientCredentialsAbstract {
5
- protected _clientId: ClientEnum;
6
- protected _clientSecret: string;
7
- protected _realm: RealmEnum;
8
- constructor(_clientId: ClientEnum, _clientSecret: string, _realm: RealmEnum);
9
- get clientId(): ClientEnum;
10
- get clientSecret(): string;
11
- get realm(): RealmEnum;
12
- toPlain(): ClientCredentialsPrimitive;
13
- }
@@ -1,27 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ClientCredentialsAbstract = void 0;
4
- class ClientCredentialsAbstract {
5
- constructor(_clientId, _clientSecret, _realm) {
6
- this._clientId = _clientId;
7
- this._clientSecret = _clientSecret;
8
- this._realm = _realm;
9
- }
10
- get clientId() {
11
- return this._clientId;
12
- }
13
- get clientSecret() {
14
- return this._clientSecret;
15
- }
16
- get realm() {
17
- return this._realm;
18
- }
19
- toPlain() {
20
- return {
21
- clientId: this.clientId,
22
- clientSecret: this.clientSecret,
23
- realm: this.realm,
24
- };
25
- }
26
- }
27
- exports.ClientCredentialsAbstract = ClientCredentialsAbstract;
@@ -1,7 +0,0 @@
1
- import { ClientEnum } from "../../enums/client.enum";
2
- import { RealmEnum } from "../../enums/realm.enum";
3
- export interface ClientCredentialsPrimitive {
4
- clientId: ClientEnum;
5
- clientSecret: string;
6
- realm: RealmEnum;
7
- }
@@ -1,16 +0,0 @@
1
- import { SolidAbstract } from "../../../arch/domain/solid.abstract";
2
- import { ClientCredentialsAbstract } from "./client-credentials.abstract";
3
- import { RealmEnum } from "../../enums/realm.enum";
4
- import { ObjectType } from "../../../../utils/datatypes/generic-types";
5
- import { Optional } from "../../../../utils/ducts/optional-type";
6
- export declare abstract class RealmCredentialsAbstract implements SolidAbstract {
7
- protected _realm: RealmEnum;
8
- protected _adminUsername: string;
9
- protected _adminPassword: string;
10
- abstract clientPacked: Optional<ClientCredentialsAbstract>;
11
- constructor(_realm: RealmEnum, _adminUsername: string, _adminPassword: string);
12
- get realm(): RealmEnum;
13
- get adminUsername(): string;
14
- get adminPassword(): string;
15
- toPlain(): ObjectType;
16
- }
@@ -1,23 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RealmCredentialsAbstract = void 0;
4
- class RealmCredentialsAbstract {
5
- constructor(_realm, _adminUsername, _adminPassword) {
6
- this._realm = _realm;
7
- this._adminUsername = _adminUsername;
8
- this._adminPassword = _adminPassword;
9
- }
10
- get realm() {
11
- return this._realm;
12
- }
13
- get adminUsername() {
14
- return this._adminUsername;
15
- }
16
- get adminPassword() {
17
- return this._adminPassword;
18
- }
19
- toPlain() {
20
- throw new Error("Method not implemented.");
21
- }
22
- }
23
- exports.RealmCredentialsAbstract = RealmCredentialsAbstract;
@@ -1,10 +0,0 @@
1
- import { SignatureVerifyFailure } from "./access-token.abstract.signature-verify.failure";
2
- import { AuthTokenPayload } from "./access-token.abstract.type";
3
- import { RotationConfig } from "./rotation.type";
4
- import { Token } from "./token";
5
- import { Return } from "../../../../utils/ducts/return-type";
6
- export declare class AccessTokenSignature {
7
- private readonly config;
8
- constructor(config: RotationConfig);
9
- verify(token: Token<AuthTokenPayload>): Promise<Return<Token<AuthTokenPayload>, SignatureVerifyFailure>>;
10
- }
@@ -1,47 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.AccessTokenSignature = void 0;
7
- const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
8
- const env_vars_1 = require("../../../../configs/env/env-vars");
9
- const access_token_abstract_signature_verify_failure_1 = require("./access-token.abstract.signature-verify.failure");
10
- const rotation_1 = require("./rotation");
11
- const return_type_1 = require("../../../../utils/ducts/return-type");
12
- const guardian_1 = require("../../../../utils/type-guard/guardian");
13
- class AccessTokenSignature {
14
- constructor(config) {
15
- this.config = config;
16
- }
17
- async verify(token) {
18
- try {
19
- const rotation = new rotation_1.Rotation(this.config);
20
- const publicKey = await rotation.getJWK(token.header.kid);
21
- if (guardian_1.Guardian.isEmpty(publicKey)) {
22
- return (0, return_type_1.Failure)(new access_token_abstract_signature_verify_failure_1.SignatureVerifyFailure("Unable to capture public key"));
23
- }
24
- const publicKeySanitized = publicKey
25
- .split("\n")
26
- .filter((line) => line && !line.includes("BEGIN") && !line.includes("END"))
27
- .join("\n")
28
- .replace(/\n/g, "");
29
- if (guardian_1.Guardian.isDifferent(env_vars_1.IAM_MOVINGAPP_REALM_PUBLIC_KEY_RS256, publicKeySanitized)) {
30
- return (0, return_type_1.Failure)(new access_token_abstract_signature_verify_failure_1.SignatureVerifyFailure("Token is invalid"));
31
- }
32
- if (guardian_1.Guardian.isNotEmpty(publicKey)) {
33
- const isSignatureValid = jsonwebtoken_1.default.verify(token.toString(), publicKey, {
34
- algorithms: ["RS256"],
35
- });
36
- if (guardian_1.Guardian.isEmpty(isSignatureValid)) {
37
- return (0, return_type_1.Failure)(new access_token_abstract_signature_verify_failure_1.SignatureVerifyFailure("admin request failed: invalid token (signature)"));
38
- }
39
- }
40
- return (0, return_type_1.Success)(token);
41
- }
42
- catch (error) {
43
- return (0, return_type_1.Failure)(new access_token_abstract_signature_verify_failure_1.SignatureVerifyFailure("Failed to verify token", error));
44
- }
45
- }
46
- }
47
- exports.AccessTokenSignature = AccessTokenSignature;
@@ -1,10 +0,0 @@
1
- import { TokenHasRoleFailure } from "./access-token.abstract.has-role.failure";
2
- import { AccessTokenPayload } from "./access-token.abstract.type";
3
- import { Token } from "./token";
4
- import { Role } from "../../enums/role.type";
5
- import { Return } from "../../../../utils/ducts/return-type";
6
- export declare class AccessTokenAbstract extends Token<AccessTokenPayload> {
7
- private readonly token;
8
- constructor(token: string);
9
- hasRoles(roles: Role[]): Return<boolean, TokenHasRoleFailure>;
10
- }
@@ -1,4 +0,0 @@
1
- import { FailureAbstract } from "../../../arch/application/errors/failure.abstract";
2
- export declare class TokenHasRoleFailure extends FailureAbstract {
3
- constructor(error?: any);
4
- }
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TokenHasRoleFailure = void 0;
4
- const failure_abstract_1 = require("../../../arch/application/errors/failure.abstract");
5
- class TokenHasRoleFailure extends failure_abstract_1.FailureAbstract {
6
- constructor(error) {
7
- super({
8
- className: TokenHasRoleFailure.name,
9
- message: "É necessário informar o clientId para verificar se possui a role",
10
- error,
11
- });
12
- }
13
- }
14
- exports.TokenHasRoleFailure = TokenHasRoleFailure;
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AccessTokenAbstract = void 0;
4
- const token_1 = require("./token");
5
- const return_type_1 = require("../../../../utils/ducts/return-type");
6
- const guardian_1 = require("../../../../utils/type-guard/guardian");
7
- class AccessTokenAbstract extends token_1.Token {
8
- constructor(token) {
9
- super(token);
10
- this.token = token;
11
- }
12
- hasRoles(roles) {
13
- if (guardian_1.Guardian.isEmpty(this.payload.resource_access)) {
14
- return (0, return_type_1.Success)(false);
15
- }
16
- const rolesInToken = this.payload.realm_access.roles;
17
- for (const role of roles) {
18
- if (guardian_1.Guardian.isFalsy(guardian_1.Guardian.isAnyOf(role, rolesInToken))) {
19
- return (0, return_type_1.Success)(false);
20
- }
21
- }
22
- return (0, return_type_1.Success)(true);
23
- }
24
- }
25
- exports.AccessTokenAbstract = AccessTokenAbstract;
@@ -1,4 +0,0 @@
1
- import { FailureAbstract } from "../../../arch/application/errors/failure.abstract";
2
- export declare class SignatureVerifyFailure extends FailureAbstract {
3
- constructor(message: string, error?: any);
4
- }
@@ -1,10 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SignatureVerifyFailure = void 0;
4
- const failure_abstract_1 = require("../../../arch/application/errors/failure.abstract");
5
- class SignatureVerifyFailure extends failure_abstract_1.FailureAbstract {
6
- constructor(message, error) {
7
- super({ className: SignatureVerifyFailure.name, message, error });
8
- }
9
- }
10
- exports.SignatureVerifyFailure = SignatureVerifyFailure;
@@ -1,44 +0,0 @@
1
- import { ClientEnum } from "../../enums/client.enum";
2
- import { UserRoleEnum } from "../../enums/user-role.enum";
3
- export interface AccessTokenHeader {
4
- alg: string;
5
- typ: string;
6
- kid?: string;
7
- }
8
- export type AuthTokenPayload = AccessTokenPayload | RefreshTokenPayload;
9
- export interface AccessTokenPayload {
10
- iss: string;
11
- sub: string;
12
- aud: string;
13
- exp: number;
14
- iat: number;
15
- resource_access?: {
16
- [key in ClientEnum]: {
17
- roles: UserRoleEnum[];
18
- };
19
- };
20
- roles?: UserRoleEnum[];
21
- realm_access: {
22
- roles: UserRoleEnum[];
23
- };
24
- auth_time: number;
25
- name: string;
26
- authorization?: {
27
- roles?: UserRoleEnum[];
28
- permissions?: [
29
- {
30
- rsid?: string;
31
- rsname?: string;
32
- scopes?: string;
33
- }
34
- ];
35
- };
36
- email: string;
37
- }
38
- export interface RefreshTokenPayload {
39
- sub: string;
40
- aud: string;
41
- exp: number;
42
- iat: number;
43
- scope: string;
44
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,12 +0,0 @@
1
- import { RotationConfig } from "./rotation.type";
2
- export declare class Rotation {
3
- private realmUrl;
4
- private minTimeBetweenJwksRequests;
5
- private jwks;
6
- private lastTimeRequesTime;
7
- constructor(config: RotationConfig);
8
- retrieveJWKs(callback?: unknown): Promise<any>;
9
- getJWK(kid: unknown): Promise<string | undefined>;
10
- clearCache(): void;
11
- private nodeify;
12
- }
@@ -1,97 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Rotation = void 0;
7
- const node_url_1 = require("node:url");
8
- const jwk_to_pem_1 = __importDefault(require("jwk-to-pem"));
9
- const guardian_1 = require("../../../../utils/type-guard/guardian");
10
- class Rotation {
11
- constructor(config) {
12
- this.realmUrl = config.realmUrl;
13
- this.minTimeBetweenJwksRequests = config.minTimeBetweenJwksRequests;
14
- this.jwks = [];
15
- this.lastTimeRequesTime = 0;
16
- }
17
- retrieveJWKs(callback) {
18
- const urlString = `${this.realmUrl}/protocol/openid-connect/certs`;
19
- const url = new node_url_1.URL(urlString);
20
- // Prepare options for the HTTP request
21
- const options = {
22
- hostname: url.hostname,
23
- port: url.port ? Number(url.port) : url.protocol === "https:" ? 443 : 80,
24
- path: url.pathname + url.search,
25
- method: "GET",
26
- };
27
- // Choose http or https based on the URL protocol
28
- const protocol = url.protocol === "https:" ? require("node:https") : require("node:http");
29
- const promise = new Promise((resolve, reject) => {
30
- const req = protocol.request(options, (response) => {
31
- if (response.statusCode &&
32
- (response.statusCode < 200 || response.statusCode >= 300)) {
33
- return reject(new Error("Error fetching JWK Keys"));
34
- }
35
- let json = "";
36
- response.on("data", (chunk) => {
37
- json += chunk.toString();
38
- });
39
- response.on("end", () => {
40
- try {
41
- const data = JSON.parse(json);
42
- if (data.error) {
43
- reject(data);
44
- }
45
- else {
46
- resolve(data);
47
- }
48
- }
49
- catch (e) {
50
- reject(new Error("Failed to parse JSON"));
51
- }
52
- });
53
- });
54
- req.on("error", reject);
55
- req.end();
56
- });
57
- return this.nodeify(promise, callback);
58
- }
59
- async getJWK(kid) {
60
- const key = this.jwks.find((key) => {
61
- return key.kid === kid;
62
- });
63
- if (guardian_1.Guardian.isNotEmpty(key)) {
64
- return new Promise((resolve, reject) => {
65
- resolve((0, jwk_to_pem_1.default)(key));
66
- });
67
- }
68
- const currentTime = new Date().getTime() / 1000;
69
- if (currentTime >
70
- this.lastTimeRequesTime + this.minTimeBetweenJwksRequests) {
71
- return this.retrieveJWKs()
72
- .then((publicKeys) => {
73
- this.lastTimeRequesTime = currentTime;
74
- this.jwks = publicKeys.keys;
75
- const convertedKey = (0, jwk_to_pem_1.default)(this.jwks.find((key) => {
76
- return key.kid === kid;
77
- }));
78
- return convertedKey;
79
- })
80
- .catch(() => {
81
- return undefined;
82
- });
83
- }
84
- console.error("Not enough time elapsed since the last request, blocking the request");
85
- }
86
- clearCache() {
87
- this.jwks.length = 0;
88
- }
89
- nodeify(promise, cb) {
90
- if (typeof cb !== "function")
91
- return promise;
92
- return promise
93
- .then((res) => cb(null, res))
94
- .catch((err) => cb(err));
95
- }
96
- }
97
- exports.Rotation = Rotation;
@@ -1,5 +0,0 @@
1
- export interface RotationConfig {
2
- realmUrl: string;
3
- minTimeBetweenJwksRequests: number;
4
- publicKey: string;
5
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,16 +0,0 @@
1
- import { JwtPayload } from "jsonwebtoken";
2
- import { TokenHeader } from "./token.type";
3
- import { Optional } from "../../../../utils/ducts/optional-type";
4
- import { Return } from "../../../../utils/ducts/return-type";
5
- export declare class Token<TokenPayload extends JwtPayload> {
6
- private _header;
7
- private _token;
8
- private _payload;
9
- constructor(token: string);
10
- static isValid(token: string): boolean;
11
- getClaim<T>(key: string): Optional<T>;
12
- get header(): TokenHeader;
13
- get payload(): TokenPayload;
14
- isExpired(): Return<boolean, undefined>;
15
- toString(): string;
16
- }