@seidor-cloud-produtos/orbit-backend-lib 0.0.24 → 0.0.26
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/dist/clean-arch/domain/errors/invalid-password-algorithm-error.d.ts +4 -0
- package/dist/clean-arch/domain/errors/invalid-password-algorithm-error.js +11 -0
- package/dist/clean-arch/domain/errors/invalid-password-length-error.d.ts +4 -0
- package/dist/clean-arch/domain/errors/invalid-password-length-error.js +14 -0
- package/dist/clean-arch/domain/factories/create-password-factory.d.ts +13 -0
- package/dist/clean-arch/domain/factories/create-password-factory.js +32 -0
- package/dist/clean-arch/domain/factories/random-password-temporary-factory.d.ts +8 -0
- package/dist/clean-arch/domain/factories/random-password-temporary-factory.js +55 -0
- package/dist/clean-arch/domain/value-objects/password.d.ts +19 -0
- package/dist/clean-arch/domain/value-objects/password.js +18 -0
- package/dist/clean-arch/domain/value-objects/pbkdf2-password.d.ts +10 -0
- package/dist/clean-arch/domain/value-objects/pbkdf2-password.js +47 -0
- package/dist/clean-arch/domain/value-objects/sha1-password.d.ts +10 -0
- package/dist/clean-arch/domain/value-objects/sha1-password.js +35 -0
- package/dist/clean-arch/infra/authorizations/authorization.d.ts +1 -1
- package/dist/clean-arch/infra/authorizations/authorization.js +3 -7
- package/dist/frameworks/express/authorizations/authorization-express.d.ts +1 -1
- package/dist/frameworks/express/authorizations/authorization-express.js +3 -4
- package/dist/infra/authorizations/validator/api/api-validator.d.ts +2 -0
- package/dist/infra/authorizations/validator/api/api-validator.js +9 -4
- package/dist/infra/http/api-gateway/mapping-model.spec.js +3 -3
- package/package.json +1 -1
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
const tslib_1 = require('tslib');
|
|
4
|
+
const domain_error_1 = tslib_1.__importDefault(require('./domain-error'));
|
|
5
|
+
class InvalidPasswordAlgorithmError extends domain_error_1.default {
|
|
6
|
+
constructor() {
|
|
7
|
+
super('Invalid password algorithm.', 400);
|
|
8
|
+
this.name = 'InvalidPasswordAlgorithmError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.default = InvalidPasswordAlgorithmError;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
const tslib_1 = require('tslib');
|
|
4
|
+
const domain_error_1 = tslib_1.__importDefault(require('./domain-error'));
|
|
5
|
+
class InvalidPasswordLengthError extends domain_error_1.default {
|
|
6
|
+
constructor() {
|
|
7
|
+
super(
|
|
8
|
+
'Invalid length password. Password must be at least 8 characters long.',
|
|
9
|
+
400,
|
|
10
|
+
);
|
|
11
|
+
this.name = 'InvalidPasswordLengthError';
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.default = InvalidPasswordLengthError;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { PasswordAlgorithm } from '../value-objects/password';
|
|
2
|
+
import PBKDF2Password from '../value-objects/pbkdf2-password';
|
|
3
|
+
import SHA1Password from '../value-objects/sha1-password';
|
|
4
|
+
export type CreatePasswordDomainProps = {
|
|
5
|
+
value: string;
|
|
6
|
+
algorithm: PasswordAlgorithm;
|
|
7
|
+
salt: string | null;
|
|
8
|
+
};
|
|
9
|
+
export default class CreatePasswordFactory {
|
|
10
|
+
static create(
|
|
11
|
+
password?: CreatePasswordDomainProps,
|
|
12
|
+
): PBKDF2Password | SHA1Password;
|
|
13
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
const tslib_1 = require('tslib');
|
|
4
|
+
const invalid_password_algorithm_error_1 = tslib_1.__importDefault(
|
|
5
|
+
require('../errors/invalid-password-algorithm-error'),
|
|
6
|
+
);
|
|
7
|
+
const pbkdf2_password_1 = tslib_1.__importDefault(
|
|
8
|
+
require('../value-objects/pbkdf2-password'),
|
|
9
|
+
);
|
|
10
|
+
const sha1_password_1 = tslib_1.__importDefault(
|
|
11
|
+
require('../value-objects/sha1-password'),
|
|
12
|
+
);
|
|
13
|
+
const random_password_temporary_factory_1 = tslib_1.__importDefault(
|
|
14
|
+
require('./random-password-temporary-factory'),
|
|
15
|
+
);
|
|
16
|
+
class CreatePasswordFactory {
|
|
17
|
+
static create(password) {
|
|
18
|
+
if (!password) {
|
|
19
|
+
return sha1_password_1.default.create(
|
|
20
|
+
random_password_temporary_factory_1.default.create(),
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
if (password && password.algorithm === 'sha1') {
|
|
24
|
+
return sha1_password_1.default.restore(password.value, password.salt);
|
|
25
|
+
}
|
|
26
|
+
if (password && password.algorithm === 'pbkdf2') {
|
|
27
|
+
return pbkdf2_password_1.default.restore(password.value, password.salt);
|
|
28
|
+
}
|
|
29
|
+
throw new invalid_password_algorithm_error_1.default();
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.default = CreatePasswordFactory;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export default class CreateRandomPasswordTemporaryFactory {
|
|
2
|
+
static create(password?: string): string;
|
|
3
|
+
static isValid(str: string): boolean;
|
|
4
|
+
private static hasSpecialCharacter;
|
|
5
|
+
private static hasUpperCaseLetter;
|
|
6
|
+
private static hasNumber;
|
|
7
|
+
private static getRandomCharacter;
|
|
8
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
class CreateRandomPasswordTemporaryFactory {
|
|
4
|
+
static create(password) {
|
|
5
|
+
if (password) {
|
|
6
|
+
return password;
|
|
7
|
+
}
|
|
8
|
+
const specials = '!@#$%^&*()_+-=[]{}|;:,.<>?';
|
|
9
|
+
const upperCaseLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
10
|
+
const allChars = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
11
|
+
let randomString = '';
|
|
12
|
+
randomString += this.getRandomCharacter(specials);
|
|
13
|
+
randomString += this.getRandomCharacter(upperCaseLetters);
|
|
14
|
+
randomString += this.getRandomCharacter('0123456789');
|
|
15
|
+
for (let i = 0; i < 5; i++) {
|
|
16
|
+
randomString += this.getRandomCharacter(allChars);
|
|
17
|
+
}
|
|
18
|
+
randomString = randomString
|
|
19
|
+
.split('')
|
|
20
|
+
.sort(() => Math.random() - 0.5)
|
|
21
|
+
.join('');
|
|
22
|
+
return randomString;
|
|
23
|
+
}
|
|
24
|
+
static isValid(str) {
|
|
25
|
+
const minLength = 8;
|
|
26
|
+
if (str.length < minLength) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
if (!this.hasSpecialCharacter(str)) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
if (!this.hasUpperCaseLetter(str)) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
if (!this.hasNumber(str)) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
static hasSpecialCharacter(str) {
|
|
41
|
+
const specials = '!@#$%^&*()_+-=[]{}|;:,.<>?';
|
|
42
|
+
return [...str].some(char => specials.includes(char));
|
|
43
|
+
}
|
|
44
|
+
static hasUpperCaseLetter(str) {
|
|
45
|
+
return [...str].some(char => char >= 'A' && char <= 'Z');
|
|
46
|
+
}
|
|
47
|
+
static hasNumber(str) {
|
|
48
|
+
return [...str].some(char => char >= '0' && char <= '9');
|
|
49
|
+
}
|
|
50
|
+
static getRandomCharacter(characters) {
|
|
51
|
+
const index = Math.floor(Math.random() * characters.length);
|
|
52
|
+
return characters[index];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
exports.default = CreateRandomPasswordTemporaryFactory;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare enum PasswordAlgorithm {
|
|
2
|
+
SHA1 = 'sha1',
|
|
3
|
+
PBKDF2 = 'pbkdf2',
|
|
4
|
+
}
|
|
5
|
+
type PasswordProps = {
|
|
6
|
+
algorithm: PasswordAlgorithm;
|
|
7
|
+
isTemporary: boolean;
|
|
8
|
+
};
|
|
9
|
+
export default abstract class Password {
|
|
10
|
+
value: string;
|
|
11
|
+
salt: string | null;
|
|
12
|
+
notEncryptedValue?: string;
|
|
13
|
+
algorithm: string;
|
|
14
|
+
isTemporary: boolean;
|
|
15
|
+
abstract isEqual(password: string): boolean;
|
|
16
|
+
protected static isValid(password: string): boolean;
|
|
17
|
+
protected constructor(props: PasswordProps);
|
|
18
|
+
}
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
exports.PasswordAlgorithm = void 0;
|
|
4
|
+
var PasswordAlgorithm;
|
|
5
|
+
(function (PasswordAlgorithm) {
|
|
6
|
+
PasswordAlgorithm['SHA1'] = 'sha1';
|
|
7
|
+
PasswordAlgorithm['PBKDF2'] = 'pbkdf2';
|
|
8
|
+
})(PasswordAlgorithm || (exports.PasswordAlgorithm = PasswordAlgorithm = {}));
|
|
9
|
+
class Password {
|
|
10
|
+
static isValid(password) {
|
|
11
|
+
return password.length >= 8;
|
|
12
|
+
}
|
|
13
|
+
constructor(props) {
|
|
14
|
+
this.algorithm = props.algorithm;
|
|
15
|
+
this.isTemporary = props.isTemporary;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.default = Password;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import Password from './password';
|
|
2
|
+
export default class PBKDF2Password extends Password {
|
|
3
|
+
readonly value: string;
|
|
4
|
+
readonly salt: string;
|
|
5
|
+
readonly notEncryptedValue?: string | undefined;
|
|
6
|
+
private constructor();
|
|
7
|
+
static create(password: string): PBKDF2Password;
|
|
8
|
+
static restore(password: string, salt: string): PBKDF2Password;
|
|
9
|
+
isEqual(password: string): boolean;
|
|
10
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
const tslib_1 = require('tslib');
|
|
4
|
+
const node_crypto_1 = require('node:crypto');
|
|
5
|
+
const invalid_password_length_error_1 = tslib_1.__importDefault(
|
|
6
|
+
require('../errors/invalid-password-length-error'),
|
|
7
|
+
);
|
|
8
|
+
const password_1 = tslib_1.__importStar(require('./password'));
|
|
9
|
+
class PBKDF2Password extends password_1.default {
|
|
10
|
+
constructor(value, salt, notEncryptedValue) {
|
|
11
|
+
super({
|
|
12
|
+
algorithm: password_1.PasswordAlgorithm.PBKDF2,
|
|
13
|
+
isTemporary: false,
|
|
14
|
+
});
|
|
15
|
+
this.value = value;
|
|
16
|
+
this.salt = salt;
|
|
17
|
+
this.notEncryptedValue = notEncryptedValue;
|
|
18
|
+
}
|
|
19
|
+
static create(password) {
|
|
20
|
+
if (!this.isValid(password)) {
|
|
21
|
+
throw new invalid_password_length_error_1.default();
|
|
22
|
+
}
|
|
23
|
+
const salt = (0, node_crypto_1.randomBytes)(20).toString('hex');
|
|
24
|
+
const value = (0, node_crypto_1.pbkdf2Sync)(
|
|
25
|
+
password,
|
|
26
|
+
salt,
|
|
27
|
+
100,
|
|
28
|
+
64,
|
|
29
|
+
'sha512',
|
|
30
|
+
).toString('hex');
|
|
31
|
+
return new PBKDF2Password(value, salt, password);
|
|
32
|
+
}
|
|
33
|
+
static restore(password, salt) {
|
|
34
|
+
return new PBKDF2Password(password, salt);
|
|
35
|
+
}
|
|
36
|
+
isEqual(password) {
|
|
37
|
+
const value = (0, node_crypto_1.pbkdf2Sync)(
|
|
38
|
+
password,
|
|
39
|
+
this.salt,
|
|
40
|
+
100,
|
|
41
|
+
64,
|
|
42
|
+
'sha512',
|
|
43
|
+
).toString('hex');
|
|
44
|
+
return this.value === value;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.default = PBKDF2Password;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import Password from './password';
|
|
2
|
+
export default class SHA1Password extends Password {
|
|
3
|
+
readonly value: string;
|
|
4
|
+
readonly salt: string | null;
|
|
5
|
+
readonly notEncryptedValue?: string | undefined;
|
|
6
|
+
private constructor();
|
|
7
|
+
static create(password: string): SHA1Password;
|
|
8
|
+
static restore(password: string, salt: string | null): SHA1Password;
|
|
9
|
+
isEqual(password: string): boolean;
|
|
10
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
const tslib_1 = require('tslib');
|
|
4
|
+
const node_crypto_1 = require('node:crypto');
|
|
5
|
+
const invalid_password_length_error_1 = tslib_1.__importDefault(
|
|
6
|
+
require('../errors/invalid-password-length-error'),
|
|
7
|
+
);
|
|
8
|
+
const password_1 = tslib_1.__importStar(require('./password'));
|
|
9
|
+
class SHA1Password extends password_1.default {
|
|
10
|
+
constructor(value, salt, notEncryptedValue) {
|
|
11
|
+
super({ algorithm: password_1.PasswordAlgorithm.SHA1, isTemporary: true });
|
|
12
|
+
this.value = value;
|
|
13
|
+
this.salt = salt;
|
|
14
|
+
this.notEncryptedValue = notEncryptedValue;
|
|
15
|
+
}
|
|
16
|
+
static create(password) {
|
|
17
|
+
if (!this.isValid(password)) {
|
|
18
|
+
throw new invalid_password_length_error_1.default();
|
|
19
|
+
}
|
|
20
|
+
const value = (0, node_crypto_1.createHash)('sha1')
|
|
21
|
+
.update(password)
|
|
22
|
+
.digest('hex');
|
|
23
|
+
return new SHA1Password(value, null, password);
|
|
24
|
+
}
|
|
25
|
+
static restore(password, salt) {
|
|
26
|
+
return new SHA1Password(password, salt, undefined);
|
|
27
|
+
}
|
|
28
|
+
isEqual(password) {
|
|
29
|
+
const value = (0, node_crypto_1.createHash)('sha1')
|
|
30
|
+
.update(password)
|
|
31
|
+
.digest('hex');
|
|
32
|
+
return this.value === value;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.default = SHA1Password;
|
|
@@ -15,7 +15,7 @@ export default class Authorization extends APIAuthValidator {
|
|
|
15
15
|
): Promise<AuthValidator>;
|
|
16
16
|
protected unauthorize(response: UnauthorizedResponse): void;
|
|
17
17
|
protected getRequestId(): string;
|
|
18
|
-
protected getAuthorizations(): Promise<string[]>;
|
|
19
18
|
protected handleAuthorized(): Promise<Response>;
|
|
19
|
+
protected getAuthorizationsHeader(): string;
|
|
20
20
|
}
|
|
21
21
|
export {};
|
|
@@ -14,15 +14,11 @@ class Authorization extends api_validator_1.APIAuthValidator {
|
|
|
14
14
|
getRequestId() {
|
|
15
15
|
return this.controller.request.headers['X-Amzn-Requestid'];
|
|
16
16
|
}
|
|
17
|
-
async getAuthorizations() {
|
|
18
|
-
const authorizations = this.controller.request.headers.authorizations;
|
|
19
|
-
if (typeof authorizations === 'string') {
|
|
20
|
-
return JSON.parse(authorizations);
|
|
21
|
-
}
|
|
22
|
-
return authorizations;
|
|
23
|
-
}
|
|
24
17
|
handleAuthorized() {
|
|
25
18
|
return this.controller.handle(this.controller.request);
|
|
26
19
|
}
|
|
20
|
+
getAuthorizationsHeader() {
|
|
21
|
+
return this.controller.request.headers.authorizations;
|
|
22
|
+
}
|
|
27
23
|
}
|
|
28
24
|
exports.default = Authorization;
|
|
@@ -10,11 +10,11 @@ export default class AuthorizationExpress extends APIAuthValidator {
|
|
|
10
10
|
query: AuthParams,
|
|
11
11
|
controllerMetaData: ExpressMidlewareProperties,
|
|
12
12
|
): Promise<AuthValidator>;
|
|
13
|
-
getAuthorizations(): Promise<string[]>;
|
|
14
13
|
getRequestId(): string;
|
|
15
14
|
handleAuthorized(): Promise<boolean | void>;
|
|
16
15
|
unauthorize(
|
|
17
16
|
response: UnauthorizedResponse,
|
|
18
17
|
): Promise<Response<any, Record<string, any>>>;
|
|
19
18
|
private setResponseHeaders;
|
|
19
|
+
protected getAuthorizationsHeader(): string;
|
|
20
20
|
}
|
|
@@ -6,10 +6,6 @@ class AuthorizationExpress extends api_validator_1.APIAuthValidator {
|
|
|
6
6
|
const instance = new AuthorizationExpress(query, controllerMetaData);
|
|
7
7
|
return await instance.setup();
|
|
8
8
|
}
|
|
9
|
-
async getAuthorizations() {
|
|
10
|
-
const authorizationsHeader = this.controller.request.headers.authorizations;
|
|
11
|
-
return authorizationsHeader?.split(',') || [];
|
|
12
|
-
}
|
|
13
9
|
getRequestId() {
|
|
14
10
|
return this.controller.request.headers['X-Amzn-Requestid'];
|
|
15
11
|
}
|
|
@@ -27,5 +23,8 @@ class AuthorizationExpress extends api_validator_1.APIAuthValidator {
|
|
|
27
23
|
}
|
|
28
24
|
return this.controller.response;
|
|
29
25
|
}
|
|
26
|
+
getAuthorizationsHeader() {
|
|
27
|
+
return this.controller.request.headers.authorizations;
|
|
28
|
+
}
|
|
30
29
|
}
|
|
31
30
|
exports.default = AuthorizationExpress;
|
|
@@ -10,6 +10,8 @@ export declare abstract class APIAuthValidator extends AuthValidator {
|
|
|
10
10
|
protected constructor(query: AuthParams, controller: any);
|
|
11
11
|
protected handleUnauthorized(): any;
|
|
12
12
|
setup(): Promise<AuthValidator>;
|
|
13
|
+
protected getAuthorizations(): Promise<string[]>;
|
|
13
14
|
protected abstract unauthorize(response: UnauthorizedResponse): any;
|
|
14
15
|
protected abstract getRequestId(): string;
|
|
16
|
+
protected abstract getAuthorizationsHeader(): string;
|
|
15
17
|
}
|
|
@@ -19,11 +19,16 @@ class APIAuthValidator extends auth_validator_1.default {
|
|
|
19
19
|
return this.unauthorize(response);
|
|
20
20
|
}
|
|
21
21
|
async setup() {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
request_id: this.getRequestId(),
|
|
25
|
-
};
|
|
22
|
+
await super.setup();
|
|
23
|
+
this.requestData.request_id = this.getRequestId();
|
|
26
24
|
return this;
|
|
27
25
|
}
|
|
26
|
+
async getAuthorizations() {
|
|
27
|
+
const authorizations = this.getAuthorizationsHeader();
|
|
28
|
+
if (typeof authorizations === 'string') {
|
|
29
|
+
return JSON.parse(authorizations);
|
|
30
|
+
}
|
|
31
|
+
return authorizations;
|
|
32
|
+
}
|
|
28
33
|
}
|
|
29
34
|
exports.APIAuthValidator = APIAuthValidator;
|
|
@@ -14,16 +14,16 @@ class MockMappingModel extends mapping_model_1.MappingModel {
|
|
|
14
14
|
getPathParams() {
|
|
15
15
|
throw new Error('Method not implemented.');
|
|
16
16
|
}
|
|
17
|
-
setHeader(
|
|
17
|
+
setHeader() {
|
|
18
18
|
throw new Error('Method not implemented.');
|
|
19
19
|
}
|
|
20
|
-
setQuery(
|
|
20
|
+
setQuery() {
|
|
21
21
|
throw new Error('Method not implemented.');
|
|
22
22
|
}
|
|
23
23
|
deleteParams() {
|
|
24
24
|
throw new Error('Method not implemented.');
|
|
25
25
|
}
|
|
26
|
-
setPathParams(
|
|
26
|
+
setPathParams() {
|
|
27
27
|
throw new Error('Method not implemented.');
|
|
28
28
|
}
|
|
29
29
|
next() {
|