@atlantjs/arch 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -0
- package/configs/env/env-vars.d.ts +3 -0
- package/configs/env/env-vars.js +20 -0
- package/external-files/biome.json +47 -0
- package/external-files/typescript/tsconfig-backend.json +3 -0
- package/external-files/typescript/tsconfig-default.json +42 -0
- package/external-files/typescript/tsconfig-frontend.json +3 -0
- package/external-files/typescript/tsconfig-lib.json +6 -0
- package/external-files/typescript/tsconfig-mobile.json +3 -0
- package/index.d.ts +51 -0
- package/index.js +106 -0
- package/objects/arch/application/errors/application-error.abstract.d.ts +8 -0
- package/objects/arch/application/errors/application-error.abstract.js +25 -0
- package/objects/arch/application/errors/application-error.abstract.type.d.ts +9 -0
- package/objects/arch/application/errors/application-error.abstract.type.js +2 -0
- package/objects/arch/application/errors/unknown-application-error.d.ts +15 -0
- package/objects/arch/application/errors/unknown-application-error.js +16 -0
- package/objects/arch/domain/entity.abstract.d.ts +3 -0
- package/objects/arch/domain/entity.abstract.js +6 -0
- package/objects/arch/domain/usecase.abstract.d.ts +4 -0
- package/objects/arch/domain/usecase.abstract.js +6 -0
- package/objects/arch/infrastructure/controller.abstract.d.ts +6 -0
- package/objects/arch/infrastructure/controller.abstract.js +6 -0
- package/objects/auth/entities/client/client-credentials.abstract.d.ts +13 -0
- package/objects/auth/entities/client/client-credentials.abstract.js +27 -0
- package/objects/auth/entities/client/client-credentials.abstract.type.d.ts +7 -0
- package/objects/auth/entities/client/client-credentials.abstract.type.js +2 -0
- package/objects/auth/entities/client/realm-credentials.abstract.d.ts +13 -0
- package/objects/auth/entities/client/realm-credentials.abstract.js +20 -0
- package/objects/auth/entities/token/access-token-signature.d.ts +10 -0
- package/objects/auth/entities/token/access-token-signature.js +58 -0
- package/objects/auth/entities/token/access-token.abstract.d.ts +10 -0
- package/objects/auth/entities/token/access-token.abstract.has-role.error.d.ts +4 -0
- package/objects/auth/entities/token/access-token.abstract.has-role.error.js +14 -0
- package/objects/auth/entities/token/access-token.abstract.js +25 -0
- package/objects/auth/entities/token/access-token.abstract.signature-verify.error.d.ts +4 -0
- package/objects/auth/entities/token/access-token.abstract.signature-verify.error.js +10 -0
- package/objects/auth/entities/token/access-token.abstract.type.d.ts +44 -0
- package/objects/auth/entities/token/access-token.abstract.type.js +2 -0
- package/objects/auth/entities/token/rotation.d.ts +12 -0
- package/objects/auth/entities/token/rotation.js +108 -0
- package/objects/auth/entities/token/rotation.type.d.ts +5 -0
- package/objects/auth/entities/token/rotation.type.js +2 -0
- package/objects/auth/entities/token/token.d.ts +16 -0
- package/objects/auth/entities/token/token.js +50 -0
- package/objects/auth/entities/token/token.type.d.ts +5 -0
- package/objects/auth/entities/token/token.type.js +2 -0
- package/objects/auth/entities/user/enums/user-status.enum.d.ts +4 -0
- package/objects/auth/entities/user/enums/user-status.enum.js +8 -0
- package/objects/auth/entities/user/password/password.abstract.d.ts +8 -0
- package/objects/auth/entities/user/password/password.abstract.js +41 -0
- package/objects/auth/entities/user/password/password.abstract.type.d.ts +5 -0
- package/objects/auth/entities/user/password/password.abstract.type.js +2 -0
- package/objects/auth/entities/user/password/steps/not-allowing-consecutive-chars-step.d.ts +11 -0
- package/objects/auth/entities/user/password/steps/not-allowing-consecutive-chars-step.js +34 -0
- package/objects/auth/entities/user/password/steps/not-allowing-date-step.d.ts +11 -0
- package/objects/auth/entities/user/password/steps/not-allowing-date-step.js +61 -0
- package/objects/auth/entities/user/password/steps/not-allowing-phone-step.d.ts +7 -0
- package/objects/auth/entities/user/password/steps/not-allowing-phone-step.js +27 -0
- package/objects/auth/entities/user/password/steps/not-allowing-repeated-chars-step.d.ts +11 -0
- package/objects/auth/entities/user/password/steps/not-allowing-repeated-chars-step.js +34 -0
- package/objects/auth/entities/user/password/steps/not-allowing-string-step.d.ts +7 -0
- package/objects/auth/entities/user/password/steps/not-allowing-string-step.js +19 -0
- package/objects/auth/entities/user/password/steps/not-allowing-these-chars-step.d.ts +10 -0
- package/objects/auth/entities/user/password/steps/not-allowing-these-chars-step.js +17 -0
- package/objects/auth/entities/user/password/steps/numeric-step.d.ts +5 -0
- package/objects/auth/entities/user/password/steps/numeric-step.js +13 -0
- package/objects/auth/entities/user/password/steps/with-at-least-one-letter-step.d.ts +5 -0
- package/objects/auth/entities/user/password/steps/with-at-least-one-letter-step.js +14 -0
- package/objects/auth/entities/user/password/steps/with-at-least-one-number-digit-step.d.ts +5 -0
- package/objects/auth/entities/user/password/steps/with-at-least-one-number-digit-step.js +14 -0
- package/objects/auth/entities/user/password/steps/with-at-least-one-special-char-step.d.ts +5 -0
- package/objects/auth/entities/user/password/steps/with-at-least-one-special-char-step.js +14 -0
- package/objects/auth/entities/user/password/steps/with-length-step.d.ts +11 -0
- package/objects/auth/entities/user/password/steps/with-length-step.js +18 -0
- package/objects/auth/entities/user/password/validation-step.d.ts +5 -0
- package/objects/auth/entities/user/password/validation-step.js +9 -0
- package/objects/auth/entities/user/password/validation-step.type.d.ts +4 -0
- package/objects/auth/entities/user/password/validation-step.type.js +2 -0
- package/objects/auth/entities/user/user-credentials.abstract.d.ts +9 -0
- package/objects/auth/entities/user/user-credentials.abstract.js +6 -0
- package/objects/auth/entities/user/user-tokens.abstract.d.ts +13 -0
- package/objects/auth/entities/user/user-tokens.abstract.js +27 -0
- package/objects/auth/entities/user/user-tokens.abstract.type.d.ts +5 -0
- package/objects/auth/entities/user/user-tokens.abstract.type.js +2 -0
- package/objects/auth/entities/user/user.abstract.d.ts +21 -0
- package/objects/auth/entities/user/user.abstract.js +29 -0
- package/objects/auth/entities/user/value-objects/email.d.ts +6 -0
- package/objects/auth/entities/user/value-objects/email.js +17 -0
- package/objects/auth/entities/user/value-objects/person-name.d.ts +7 -0
- package/objects/auth/entities/user/value-objects/person-name.js +17 -0
- package/objects/auth/entities/user/value-objects/user-id.d.ts +3 -0
- package/objects/auth/entities/user/value-objects/user-id.js +7 -0
- package/objects/auth/entities/user/value-objects/username.d.ts +3 -0
- package/objects/auth/entities/user/value-objects/username.js +7 -0
- package/objects/auth/enums/client.enum.d.ts +3 -0
- package/objects/auth/enums/client.enum.js +7 -0
- package/objects/auth/enums/realm.enum.d.ts +3 -0
- package/objects/auth/enums/realm.enum.js +7 -0
- package/objects/auth/enums/role-group.enum.d.ts +4 -0
- package/objects/auth/enums/role-group.enum.js +8 -0
- package/objects/auth/enums/role.enum.d.ts +17 -0
- package/objects/auth/enums/role.enum.js +21 -0
- package/objects/auth/enums/token-name-header.enum.d.ts +4 -0
- package/objects/auth/enums/token-name-header.enum.js +8 -0
- package/objects/configs/enums/node-envs.enum.d.ts +6 -0
- package/objects/configs/enums/node-envs.enum.js +10 -0
- package/objects/datatypes/entities/$boolean.abstract.d.ts +7 -0
- package/objects/datatypes/entities/$boolean.abstract.js +21 -0
- package/objects/datatypes/entities/$number.abstract.d.ts +13 -0
- package/objects/datatypes/entities/$number.abstract.js +60 -0
- package/objects/datatypes/entities/$string.abstract.d.ts +10 -0
- package/objects/datatypes/entities/$string.abstract.js +34 -0
- package/objects/datatypes/entities/uuid.abstract.d.ts +7 -0
- package/objects/datatypes/entities/uuid.abstract.js +20 -0
- package/package.json +66 -0
- package/tests/builders/auth/access-token-signature.builder.d.ts +6 -0
- package/tests/builders/auth/access-token-signature.builder.js +19 -0
- package/tests/builders/auth/access-token.builder.d.ts +6 -0
- package/tests/builders/auth/access-token.builder.js +15 -0
- package/tests/builders/auth/rotation.builder.d.ts +6 -0
- package/tests/builders/auth/rotation.builder.js +19 -0
- package/tests/builders/auth/token.builder.d.ts +7 -0
- package/tests/builders/auth/token.builder.js +15 -0
- package/tests/builders/auth/value-objects/client-credentials.builder.d.ts +6 -0
- package/tests/builders/auth/value-objects/client-credentials.builder.js +18 -0
- package/tests/builders/auth/value-objects/dummies/access-token.dummy.d.ts +3 -0
- package/tests/builders/auth/value-objects/dummies/access-token.dummy.js +7 -0
- package/tests/builders/auth/value-objects/dummies/client-credentials.dummy.d.ts +3 -0
- package/tests/builders/auth/value-objects/dummies/client-credentials.dummy.js +7 -0
- package/tests/builders/auth/value-objects/dummies/realm-credentials.dummy.d.ts +6 -0
- package/tests/builders/auth/value-objects/dummies/realm-credentials.dummy.js +12 -0
- package/tests/builders/auth/value-objects/dummies/user-tokens.dummy.d.ts +3 -0
- package/tests/builders/auth/value-objects/dummies/user-tokens.dummy.js +7 -0
- package/tests/builders/auth/value-objects/realm-credentials.builder.d.ts +6 -0
- package/tests/builders/auth/value-objects/realm-credentials.builder.js +17 -0
- package/tests/builders/auth/value-objects/user-tokens.builder.d.ts +7 -0
- package/tests/builders/auth/value-objects/user-tokens.builder.js +18 -0
- package/tests/builders/errors/application-error.builder.d.ts +6 -0
- package/tests/builders/errors/application-error.builder.js +21 -0
- package/tests/builders/errors/dummies/application-error.dummy.d.ts +3 -0
- package/tests/builders/errors/dummies/application-error.dummy.js +7 -0
- package/tests/builders/generic/dummies/uuid.dummy.d.ts +3 -0
- package/tests/builders/generic/dummies/uuid.dummy.js +7 -0
- package/tests/builders/generic/uuid.builder.d.ts +6 -0
- package/tests/builders/generic/uuid.builder.js +14 -0
- package/tests/builders/user/user-id.builder.d.ts +6 -0
- package/tests/builders/user/user-id.builder.js +15 -0
- package/tests/create-fake-stub.d.ts +8 -0
- package/tests/create-fake-stub.js +37 -0
- package/tests/entity-builder.util.d.ts +13 -0
- package/tests/entity-builder.util.js +39 -0
- package/tests/faker/faker.d.ts +2 -0
- package/tests/faker/faker.js +6 -0
- package/utils/arrays/extended-map.d.ts +5 -0
- package/utils/arrays/extended-map.js +23 -0
- package/utils/datatypes/generic-types.d.ts +7 -0
- package/utils/datatypes/generic-types.js +3 -0
- package/utils/datatypes/string-utils.d.ts +3 -0
- package/utils/datatypes/string-utils.js +4 -0
- package/utils/ducts/common.d.ts +9 -0
- package/utils/ducts/common.js +14 -0
- package/utils/ducts/optional-type.d.ts +25 -0
- package/utils/ducts/optional-type.js +50 -0
- package/utils/ducts/return-type.d.ts +22 -0
- package/utils/ducts/return-type.js +60 -0
- package/utils/randoms/random-enum-value.d.ts +2 -0
- package/utils/randoms/random-enum-value.js +20 -0
- package/utils/randoms/random-number.d.ts +6 -0
- package/utils/randoms/random-number.js +6 -0
- package/utils/type-guard/guardian-exception.d.ts +2 -0
- package/utils/type-guard/guardian-exception.js +6 -0
- package/utils/type-guard/guardian.d.ts +29 -0
- package/utils/type-guard/guardian.js +132 -0
- package/utils/type-guard/guardian.type.d.ts +1 -0
- package/utils/type-guard/guardian.type.js +2 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TokenHasRoleError = void 0;
|
|
4
|
+
const application_error_abstract_1 = require("../../../arch/application/errors/application-error.abstract");
|
|
5
|
+
class TokenHasRoleError extends application_error_abstract_1.ApplicationErrorAbstract {
|
|
6
|
+
constructor(error) {
|
|
7
|
+
super({
|
|
8
|
+
className: TokenHasRoleError.name,
|
|
9
|
+
message: "É necessário informar o clientId para verificar se possui a role",
|
|
10
|
+
error,
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.TokenHasRoleError = TokenHasRoleError;
|
|
@@ -0,0 +1,25 @@
|
|
|
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;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SignatureVerifyError = void 0;
|
|
4
|
+
const application_error_abstract_1 = require("../../../arch/application/errors/application-error.abstract");
|
|
5
|
+
class SignatureVerifyError extends application_error_abstract_1.ApplicationErrorAbstract {
|
|
6
|
+
constructor(message, error) {
|
|
7
|
+
super({ className: SignatureVerifyError.name, message, error });
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.SignatureVerifyError = SignatureVerifyError;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ClientEnum } from "../../enums/client.enum";
|
|
2
|
+
import { RoleEnum } from "../../enums/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: RoleEnum[];
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
roles?: RoleEnum[];
|
|
21
|
+
realm_access: {
|
|
22
|
+
roles: RoleEnum[];
|
|
23
|
+
};
|
|
24
|
+
auth_time: number;
|
|
25
|
+
name: string;
|
|
26
|
+
authorization?: {
|
|
27
|
+
roles?: RoleEnum[];
|
|
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
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.Rotation = void 0;
|
|
16
|
+
const node_url_1 = require("node:url");
|
|
17
|
+
const jwk_to_pem_1 = __importDefault(require("jwk-to-pem"));
|
|
18
|
+
const guardian_1 = require("../../../../utils/type-guard/guardian");
|
|
19
|
+
class Rotation {
|
|
20
|
+
constructor(config) {
|
|
21
|
+
this.realmUrl = config.realmUrl;
|
|
22
|
+
this.minTimeBetweenJwksRequests = config.minTimeBetweenJwksRequests;
|
|
23
|
+
this.jwks = [];
|
|
24
|
+
this.lastTimeRequesTime = 0;
|
|
25
|
+
}
|
|
26
|
+
retrieveJWKs(callback) {
|
|
27
|
+
const urlString = `${this.realmUrl}/protocol/openid-connect/certs`;
|
|
28
|
+
const url = new node_url_1.URL(urlString);
|
|
29
|
+
// Prepare options for the HTTP request
|
|
30
|
+
const options = {
|
|
31
|
+
hostname: url.hostname,
|
|
32
|
+
port: url.port ? Number(url.port) : url.protocol === "https:" ? 443 : 80,
|
|
33
|
+
path: url.pathname + url.search,
|
|
34
|
+
method: "GET",
|
|
35
|
+
};
|
|
36
|
+
// Choose http or https based on the URL protocol
|
|
37
|
+
const protocol = url.protocol === "https:" ? require("node:https") : require("node:http");
|
|
38
|
+
const promise = new Promise((resolve, reject) => {
|
|
39
|
+
const req = protocol.request(options, (response) => {
|
|
40
|
+
if (response.statusCode &&
|
|
41
|
+
(response.statusCode < 200 || response.statusCode >= 300)) {
|
|
42
|
+
return reject(new Error("Error fetching JWK Keys"));
|
|
43
|
+
}
|
|
44
|
+
let json = "";
|
|
45
|
+
response.on("data", (chunk) => {
|
|
46
|
+
json += chunk.toString();
|
|
47
|
+
});
|
|
48
|
+
response.on("end", () => {
|
|
49
|
+
try {
|
|
50
|
+
const data = JSON.parse(json);
|
|
51
|
+
if (data.error) {
|
|
52
|
+
reject(data);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
resolve(data);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
catch (e) {
|
|
59
|
+
reject(new Error("Failed to parse JSON"));
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
req.on("error", reject);
|
|
64
|
+
req.end();
|
|
65
|
+
});
|
|
66
|
+
return this.nodeify(promise, callback);
|
|
67
|
+
}
|
|
68
|
+
getJWK(kid) {
|
|
69
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
const key = this.jwks.find((key) => {
|
|
71
|
+
return key.kid === kid;
|
|
72
|
+
});
|
|
73
|
+
if (guardian_1.Guardian.isNotEmpty(key)) {
|
|
74
|
+
return new Promise((resolve, reject) => {
|
|
75
|
+
resolve((0, jwk_to_pem_1.default)(key));
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
const currentTime = new Date().getTime() / 1000;
|
|
79
|
+
if (currentTime >
|
|
80
|
+
this.lastTimeRequesTime + this.minTimeBetweenJwksRequests) {
|
|
81
|
+
return this.retrieveJWKs()
|
|
82
|
+
.then((publicKeys) => {
|
|
83
|
+
this.lastTimeRequesTime = currentTime;
|
|
84
|
+
this.jwks = publicKeys.keys;
|
|
85
|
+
const convertedKey = (0, jwk_to_pem_1.default)(this.jwks.find((key) => {
|
|
86
|
+
return key.kid === kid;
|
|
87
|
+
}));
|
|
88
|
+
return convertedKey;
|
|
89
|
+
})
|
|
90
|
+
.catch(() => {
|
|
91
|
+
return undefined;
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
console.error("Not enough time elapsed since the last request, blocking the request");
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
clearCache() {
|
|
98
|
+
this.jwks.length = 0;
|
|
99
|
+
}
|
|
100
|
+
nodeify(promise, cb) {
|
|
101
|
+
if (typeof cb !== "function")
|
|
102
|
+
return promise;
|
|
103
|
+
return promise
|
|
104
|
+
.then((res) => cb(null, res))
|
|
105
|
+
.catch((err) => cb(err));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
exports.Rotation = Rotation;
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
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.Token = void 0;
|
|
7
|
+
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
8
|
+
const optional_type_1 = require("../../../../utils/ducts/optional-type");
|
|
9
|
+
const return_type_1 = require("../../../../utils/ducts/return-type");
|
|
10
|
+
const guardian_1 = require("../../../../utils/type-guard/guardian");
|
|
11
|
+
class Token {
|
|
12
|
+
constructor(token) {
|
|
13
|
+
this._token = token.replace(/^bearer/i, "").trim();
|
|
14
|
+
const tokenParts = this._token.split(".");
|
|
15
|
+
this._header = JSON.parse(Buffer.from(tokenParts[0], "base64").toString());
|
|
16
|
+
const payload = jsonwebtoken_1.default.decode(this._token);
|
|
17
|
+
if (guardian_1.Guardian.isEmpty(payload) || guardian_1.Guardian.isTypeOf(payload, "string")) {
|
|
18
|
+
throw new Error("Invalid token");
|
|
19
|
+
}
|
|
20
|
+
this._payload = payload;
|
|
21
|
+
}
|
|
22
|
+
static isValid(token) {
|
|
23
|
+
try {
|
|
24
|
+
new Token(token);
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
catch (_a) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
getClaim(key) {
|
|
32
|
+
return (0, optional_type_1.Optional)(this._payload[key]);
|
|
33
|
+
}
|
|
34
|
+
get header() {
|
|
35
|
+
return this._header;
|
|
36
|
+
}
|
|
37
|
+
get payload() {
|
|
38
|
+
return this._payload;
|
|
39
|
+
}
|
|
40
|
+
isExpired() {
|
|
41
|
+
if (guardian_1.Guardian.isEmpty(this._payload.exp)) {
|
|
42
|
+
return (0, return_type_1.Success)(false);
|
|
43
|
+
}
|
|
44
|
+
return (0, return_type_1.Success)(this._payload.exp * 1000 < Date.now());
|
|
45
|
+
}
|
|
46
|
+
toString() {
|
|
47
|
+
return this._token;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.Token = Token;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UserStatusEnum = void 0;
|
|
4
|
+
var UserStatusEnum;
|
|
5
|
+
(function (UserStatusEnum) {
|
|
6
|
+
UserStatusEnum["active"] = "active";
|
|
7
|
+
UserStatusEnum["inactive"] = "inactive";
|
|
8
|
+
})(UserStatusEnum || (exports.UserStatusEnum = UserStatusEnum = {}));
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { PasswordPolicyResult } from "./password.abstract.type";
|
|
2
|
+
export declare abstract class PasswordAbstract {
|
|
3
|
+
private readonly password;
|
|
4
|
+
private readonly validationSteps;
|
|
5
|
+
constructor(password: string);
|
|
6
|
+
protected validate(password: string): PasswordPolicyResult;
|
|
7
|
+
toString(): string;
|
|
8
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PasswordAbstract = void 0;
|
|
4
|
+
const with_at_least_one_letter_step_1 = require("./steps/with-at-least-one-letter-step");
|
|
5
|
+
const with_at_least_one_number_digit_step_1 = require("./steps/with-at-least-one-number-digit-step");
|
|
6
|
+
const with_at_least_one_special_char_step_1 = require("./steps/with-at-least-one-special-char-step");
|
|
7
|
+
const with_length_step_1 = require("./steps/with-length-step");
|
|
8
|
+
const guardian_1 = require("../../../../../utils/type-guard/guardian");
|
|
9
|
+
class PasswordAbstract {
|
|
10
|
+
constructor(password) {
|
|
11
|
+
this.password = password;
|
|
12
|
+
this.validationSteps = [
|
|
13
|
+
new with_at_least_one_letter_step_1.WithAtLeastOneLetterStep(),
|
|
14
|
+
new with_at_least_one_number_digit_step_1.WithAtLeastOneNumberDigitStep(),
|
|
15
|
+
new with_at_least_one_special_char_step_1.WithAtLeastOneSpecialCharStep(),
|
|
16
|
+
new with_length_step_1.WithLengthStep({ min: 6, max: 999 }),
|
|
17
|
+
];
|
|
18
|
+
const { valid, validationStepsResults } = this.validate(this.password);
|
|
19
|
+
if (guardian_1.Guardian.isFalsy(valid)) {
|
|
20
|
+
throw new Error(`Password is invalid: ${validationStepsResults.map((result) => result.name)}`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
validate(password) {
|
|
24
|
+
if (guardian_1.Guardian.isEqual(this.validationSteps.length, 0)) {
|
|
25
|
+
throw new Error("No validations found");
|
|
26
|
+
}
|
|
27
|
+
const validationStepsResults = [];
|
|
28
|
+
for (const step of this.validationSteps) {
|
|
29
|
+
validationStepsResults.push(step.validate(password));
|
|
30
|
+
}
|
|
31
|
+
const valid = validationStepsResults.every((result) => result.valid);
|
|
32
|
+
return {
|
|
33
|
+
valid,
|
|
34
|
+
validationStepsResults,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
toString() {
|
|
38
|
+
return this.password;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.PasswordAbstract = PasswordAbstract;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ValidationStep } from "../validation-step";
|
|
2
|
+
import { ValidationStepResult } from "../validation-step.type";
|
|
3
|
+
export interface NotAllowingConsecutiveCharsStepOptions {
|
|
4
|
+
maxAllowedConsecutiveChars: number;
|
|
5
|
+
}
|
|
6
|
+
export declare class NotAllowingConsecutiveCharsStep extends ValidationStep {
|
|
7
|
+
private readonly params;
|
|
8
|
+
constructor(params: NotAllowingConsecutiveCharsStepOptions);
|
|
9
|
+
validate(password: string): ValidationStepResult;
|
|
10
|
+
private getMaxNumberOfConsecutiveChars;
|
|
11
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NotAllowingConsecutiveCharsStep = void 0;
|
|
4
|
+
const validation_step_1 = require("../validation-step");
|
|
5
|
+
class NotAllowingConsecutiveCharsStep extends validation_step_1.ValidationStep {
|
|
6
|
+
constructor(params) {
|
|
7
|
+
super();
|
|
8
|
+
this.params = params;
|
|
9
|
+
}
|
|
10
|
+
validate(password) {
|
|
11
|
+
return {
|
|
12
|
+
valid: this.getMaxNumberOfConsecutiveChars(password) <=
|
|
13
|
+
this.params.maxAllowedConsecutiveChars,
|
|
14
|
+
name: this.name,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
getMaxNumberOfConsecutiveChars(password) {
|
|
18
|
+
let numberOfConsecutiveChars = 1;
|
|
19
|
+
let maxNumberOfConsecutiveChars = 1;
|
|
20
|
+
for (let i = 1; i < password.length; i++) {
|
|
21
|
+
const previousChar = password[i - 1];
|
|
22
|
+
const char = password[i];
|
|
23
|
+
if (Math.abs(previousChar.charCodeAt(0) - char.charCodeAt(0)) === 1) {
|
|
24
|
+
numberOfConsecutiveChars += 1;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
maxNumberOfConsecutiveChars = Math.max(numberOfConsecutiveChars, maxNumberOfConsecutiveChars);
|
|
28
|
+
numberOfConsecutiveChars = 1;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return Math.max(numberOfConsecutiveChars, maxNumberOfConsecutiveChars);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.NotAllowingConsecutiveCharsStep = NotAllowingConsecutiveCharsStep;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ValidationStep } from "../validation-step";
|
|
2
|
+
import { ValidationStepResult } from "../validation-step.type";
|
|
3
|
+
export declare class NotAllowingDateStep extends ValidationStep {
|
|
4
|
+
private readonly notAllowedDate;
|
|
5
|
+
constructor(notAllowedDate: Date);
|
|
6
|
+
validate(password: string): ValidationStepResult;
|
|
7
|
+
private possibleDateCombinations;
|
|
8
|
+
private buildDayFormats;
|
|
9
|
+
private buildMonthFormats;
|
|
10
|
+
private buildYearFormat;
|
|
11
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NotAllowingDateStep = void 0;
|
|
4
|
+
const validation_step_1 = require("../validation-step");
|
|
5
|
+
class NotAllowingDateStep extends validation_step_1.ValidationStep {
|
|
6
|
+
constructor(notAllowedDate) {
|
|
7
|
+
super();
|
|
8
|
+
this.notAllowedDate = notAllowedDate;
|
|
9
|
+
}
|
|
10
|
+
validate(password) {
|
|
11
|
+
const cleanPassword = password.replace(/[^\w]/gi, "");
|
|
12
|
+
const result = this.possibleDateCombinations().every((dateFormat) => !cleanPassword.includes(dateFormat.toLowerCase()));
|
|
13
|
+
return {
|
|
14
|
+
valid: result,
|
|
15
|
+
name: this.name,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
possibleDateCombinations() {
|
|
19
|
+
const formats = [];
|
|
20
|
+
this.buildDayFormats().forEach((day) => {
|
|
21
|
+
this.buildMonthFormats().forEach((month) => {
|
|
22
|
+
this.buildYearFormat().forEach((year) => {
|
|
23
|
+
formats.push(`${day}${month}`);
|
|
24
|
+
formats.push(`${month}${day}`);
|
|
25
|
+
formats.push(`${day}${month}${year}`);
|
|
26
|
+
formats.push(`${month}${day}${year}`);
|
|
27
|
+
formats.push(`${year}${month}${day}`);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
return [...new Set(formats)];
|
|
32
|
+
}
|
|
33
|
+
buildDayFormats() {
|
|
34
|
+
const day = this.notAllowedDate.getUTCDate();
|
|
35
|
+
const dayLength = 2;
|
|
36
|
+
return [
|
|
37
|
+
...new Set([day.toString(), day.toString().padStart(dayLength, "0")]),
|
|
38
|
+
];
|
|
39
|
+
}
|
|
40
|
+
buildMonthFormats() {
|
|
41
|
+
const month = this.notAllowedDate.getUTCMonth() + 1;
|
|
42
|
+
const monthLength = 2;
|
|
43
|
+
return [
|
|
44
|
+
...new Set([
|
|
45
|
+
month.toString(),
|
|
46
|
+
month.toString().padStart(monthLength, "0"),
|
|
47
|
+
]),
|
|
48
|
+
];
|
|
49
|
+
}
|
|
50
|
+
buildYearFormat() {
|
|
51
|
+
const year = this.notAllowedDate.getUTCFullYear();
|
|
52
|
+
const abbreviateYearLength = 2;
|
|
53
|
+
return [
|
|
54
|
+
...new Set([
|
|
55
|
+
year.toString(),
|
|
56
|
+
year.toString().slice(-abbreviateYearLength),
|
|
57
|
+
]),
|
|
58
|
+
];
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.NotAllowingDateStep = NotAllowingDateStep;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ValidationStep } from "../validation-step";
|
|
2
|
+
import { ValidationStepResult } from "../validation-step.type";
|
|
3
|
+
export declare class NotAllowingPhoneStep extends ValidationStep {
|
|
4
|
+
private readonly notAllowedPhone?;
|
|
5
|
+
constructor(phone: string);
|
|
6
|
+
validate(password: string): ValidationStepResult;
|
|
7
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NotAllowingPhoneStep = void 0;
|
|
4
|
+
const validation_step_1 = require("../validation-step");
|
|
5
|
+
class NotAllowingPhoneStep extends validation_step_1.ValidationStep {
|
|
6
|
+
constructor(phone) {
|
|
7
|
+
super();
|
|
8
|
+
const matches = phone.match(/\d{4}\-?\d{4}$/);
|
|
9
|
+
if (matches) {
|
|
10
|
+
this.notAllowedPhone = matches[0].replace(/\-/, "");
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
validate(password) {
|
|
14
|
+
if (!this.notAllowedPhone) {
|
|
15
|
+
return {
|
|
16
|
+
valid: true,
|
|
17
|
+
name: this.name,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
const cleanPassword = password.replace(/[\-\s]/gi, "");
|
|
21
|
+
return {
|
|
22
|
+
valid: !cleanPassword.includes(this.notAllowedPhone),
|
|
23
|
+
name: this.name,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.NotAllowingPhoneStep = NotAllowingPhoneStep;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ValidationStep } from "../validation-step";
|
|
2
|
+
import { ValidationStepResult } from "../validation-step.type";
|
|
3
|
+
export interface NotAllowingRepeatedCharsStepOptions {
|
|
4
|
+
maxAllowedRepeatedChars: number;
|
|
5
|
+
}
|
|
6
|
+
export declare class NotAllowingRepeatedCharsStep extends ValidationStep {
|
|
7
|
+
private readonly options;
|
|
8
|
+
constructor(options: NotAllowingRepeatedCharsStepOptions);
|
|
9
|
+
validate(password: string): ValidationStepResult;
|
|
10
|
+
private getMaxNumberOfRepeatedChars;
|
|
11
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NotAllowingRepeatedCharsStep = void 0;
|
|
4
|
+
const validation_step_1 = require("../validation-step");
|
|
5
|
+
class NotAllowingRepeatedCharsStep extends validation_step_1.ValidationStep {
|
|
6
|
+
constructor(options) {
|
|
7
|
+
super();
|
|
8
|
+
this.options = options;
|
|
9
|
+
}
|
|
10
|
+
validate(password) {
|
|
11
|
+
return {
|
|
12
|
+
valid: this.getMaxNumberOfRepeatedChars(password) <=
|
|
13
|
+
this.options.maxAllowedRepeatedChars,
|
|
14
|
+
name: this.name,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
getMaxNumberOfRepeatedChars(password) {
|
|
18
|
+
let numberOfRepeatedChars = 1;
|
|
19
|
+
let maxNumberOfRepeatedChars = 1;
|
|
20
|
+
for (let i = 1; i < password.length; i++) {
|
|
21
|
+
const previousChar = password[i - 1];
|
|
22
|
+
const char = password[i];
|
|
23
|
+
if (previousChar === char) {
|
|
24
|
+
numberOfRepeatedChars += 1;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
maxNumberOfRepeatedChars = Math.max(numberOfRepeatedChars, maxNumberOfRepeatedChars);
|
|
28
|
+
numberOfRepeatedChars = 1;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return Math.max(numberOfRepeatedChars, maxNumberOfRepeatedChars);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.NotAllowingRepeatedCharsStep = NotAllowingRepeatedCharsStep;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ValidationStep } from "../validation-step";
|
|
2
|
+
import { ValidationStepResult } from "../validation-step.type";
|
|
3
|
+
export declare class NotAllowingStringStep extends ValidationStep {
|
|
4
|
+
private readonly forbiddenString;
|
|
5
|
+
constructor(forbiddenString: string);
|
|
6
|
+
validate(password: string): ValidationStepResult;
|
|
7
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NotAllowingStringStep = void 0;
|
|
4
|
+
const validation_step_1 = require("../validation-step");
|
|
5
|
+
class NotAllowingStringStep extends validation_step_1.ValidationStep {
|
|
6
|
+
constructor(forbiddenString) {
|
|
7
|
+
super();
|
|
8
|
+
this.forbiddenString = forbiddenString;
|
|
9
|
+
}
|
|
10
|
+
validate(password) {
|
|
11
|
+
return {
|
|
12
|
+
valid: !password
|
|
13
|
+
.toLowerCase()
|
|
14
|
+
.includes(this.forbiddenString.toLowerCase()),
|
|
15
|
+
name: this.name,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.NotAllowingStringStep = NotAllowingStringStep;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ValidationStep } from "../validation-step";
|
|
2
|
+
import { ValidationStepResult } from "../validation-step.type";
|
|
3
|
+
export interface NotAllowingTheseCharsStepOptions {
|
|
4
|
+
notAllowedCharsList: string[];
|
|
5
|
+
}
|
|
6
|
+
export declare class NotAllowingTheseCharsStep extends ValidationStep {
|
|
7
|
+
private readonly options;
|
|
8
|
+
constructor(options: NotAllowingTheseCharsStepOptions);
|
|
9
|
+
validate(password: string): ValidationStepResult;
|
|
10
|
+
}
|