maleo-foundation 0.3.46__py3-none-any.whl → 0.3.48__py3-none-any.whl
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.
- maleo_foundation/authentication.py +24 -13
- maleo_foundation/authorization.py +2 -1
- maleo_foundation/client/manager.py +22 -21
- maleo_foundation/client/services/__init__.py +16 -7
- maleo_foundation/client/services/encryption/__init__.py +13 -4
- maleo_foundation/client/services/encryption/aes.py +41 -36
- maleo_foundation/client/services/encryption/rsa.py +50 -50
- maleo_foundation/client/services/hash/__init__.py +19 -6
- maleo_foundation/client/services/hash/bcrypt.py +20 -18
- maleo_foundation/client/services/hash/hmac.py +20 -17
- maleo_foundation/client/services/hash/sha256.py +18 -15
- maleo_foundation/client/services/key.py +50 -42
- maleo_foundation/client/services/signature.py +46 -42
- maleo_foundation/client/services/token.py +49 -58
- maleo_foundation/constants.py +12 -19
- maleo_foundation/enums.py +14 -13
- maleo_foundation/expanded_types/__init__.py +2 -3
- maleo_foundation/expanded_types/client.py +30 -34
- maleo_foundation/expanded_types/encryption/__init__.py +2 -1
- maleo_foundation/expanded_types/encryption/aes.py +7 -5
- maleo_foundation/expanded_types/encryption/rsa.py +7 -5
- maleo_foundation/expanded_types/general.py +13 -11
- maleo_foundation/expanded_types/hash.py +7 -5
- maleo_foundation/expanded_types/key.py +8 -6
- maleo_foundation/expanded_types/service.py +30 -34
- maleo_foundation/expanded_types/signature.py +7 -5
- maleo_foundation/expanded_types/token.py +7 -5
- maleo_foundation/extended_types.py +4 -3
- maleo_foundation/managers/cache.py +2 -1
- maleo_foundation/managers/client/base.py +25 -12
- maleo_foundation/managers/client/google/base.py +11 -4
- maleo_foundation/managers/client/google/parameter.py +9 -11
- maleo_foundation/managers/client/google/secret.py +53 -35
- maleo_foundation/managers/client/google/storage.py +52 -22
- maleo_foundation/managers/client/google/subscription.py +37 -39
- maleo_foundation/managers/client/maleo.py +18 -23
- maleo_foundation/managers/configuration.py +5 -9
- maleo_foundation/managers/credential.py +14 -17
- maleo_foundation/managers/db.py +51 -40
- maleo_foundation/managers/middleware.py +9 -9
- maleo_foundation/managers/service.py +47 -54
- maleo_foundation/middlewares/authentication.py +29 -54
- maleo_foundation/middlewares/base.py +83 -72
- maleo_foundation/middlewares/cors.py +8 -7
- maleo_foundation/models/__init__.py +2 -1
- maleo_foundation/models/responses.py +57 -29
- maleo_foundation/models/schemas/__init__.py +2 -1
- maleo_foundation/models/schemas/encryption.py +5 -2
- maleo_foundation/models/schemas/general.py +38 -18
- maleo_foundation/models/schemas/hash.py +2 -1
- maleo_foundation/models/schemas/key.py +5 -2
- maleo_foundation/models/schemas/parameter.py +45 -15
- maleo_foundation/models/schemas/result.py +35 -20
- maleo_foundation/models/schemas/signature.py +5 -2
- maleo_foundation/models/schemas/token.py +5 -2
- maleo_foundation/models/table.py +33 -27
- maleo_foundation/models/transfers/__init__.py +2 -1
- maleo_foundation/models/transfers/general/__init__.py +2 -1
- maleo_foundation/models/transfers/general/configurations/__init__.py +10 -4
- maleo_foundation/models/transfers/general/configurations/cache/__init__.py +3 -2
- maleo_foundation/models/transfers/general/configurations/cache/redis.py +13 -5
- maleo_foundation/models/transfers/general/configurations/client/__init__.py +5 -1
- maleo_foundation/models/transfers/general/configurations/client/maleo.py +38 -12
- maleo_foundation/models/transfers/general/configurations/database.py +5 -2
- maleo_foundation/models/transfers/general/configurations/middleware.py +22 -15
- maleo_foundation/models/transfers/general/configurations/service.py +2 -1
- maleo_foundation/models/transfers/general/credentials.py +2 -1
- maleo_foundation/models/transfers/general/database.py +11 -4
- maleo_foundation/models/transfers/general/key.py +13 -4
- maleo_foundation/models/transfers/general/request.py +28 -9
- maleo_foundation/models/transfers/general/settings.py +12 -22
- maleo_foundation/models/transfers/general/signature.py +4 -2
- maleo_foundation/models/transfers/general/token.py +34 -27
- maleo_foundation/models/transfers/parameters/__init__.py +2 -1
- maleo_foundation/models/transfers/parameters/client.py +15 -19
- maleo_foundation/models/transfers/parameters/encryption/__init__.py +2 -1
- maleo_foundation/models/transfers/parameters/encryption/aes.py +7 -5
- maleo_foundation/models/transfers/parameters/encryption/rsa.py +7 -5
- maleo_foundation/models/transfers/parameters/general.py +15 -13
- maleo_foundation/models/transfers/parameters/hash/__init__.py +2 -1
- maleo_foundation/models/transfers/parameters/hash/bcrypt.py +5 -5
- maleo_foundation/models/transfers/parameters/hash/hmac.py +6 -6
- maleo_foundation/models/transfers/parameters/hash/sha256.py +5 -5
- maleo_foundation/models/transfers/parameters/key.py +9 -8
- maleo_foundation/models/transfers/parameters/service.py +42 -48
- maleo_foundation/models/transfers/parameters/signature.py +7 -4
- maleo_foundation/models/transfers/parameters/token.py +10 -10
- maleo_foundation/models/transfers/results/__init__.py +2 -1
- maleo_foundation/models/transfers/results/client/__init__.py +2 -1
- maleo_foundation/models/transfers/results/client/controllers/__init__.py +2 -1
- maleo_foundation/models/transfers/results/client/controllers/http.py +10 -7
- maleo_foundation/models/transfers/results/client/service.py +12 -6
- maleo_foundation/models/transfers/results/encryption/__init__.py +2 -1
- maleo_foundation/models/transfers/results/encryption/aes.py +13 -5
- maleo_foundation/models/transfers/results/encryption/rsa.py +12 -4
- maleo_foundation/models/transfers/results/hash.py +7 -3
- maleo_foundation/models/transfers/results/key.py +18 -6
- maleo_foundation/models/transfers/results/service/__init__.py +2 -3
- maleo_foundation/models/transfers/results/service/controllers/__init__.py +2 -1
- maleo_foundation/models/transfers/results/service/controllers/rest.py +14 -11
- maleo_foundation/models/transfers/results/service/general.py +16 -10
- maleo_foundation/models/transfers/results/signature.py +12 -4
- maleo_foundation/models/transfers/results/token.py +10 -4
- maleo_foundation/rest_controller_result.py +23 -21
- maleo_foundation/types.py +15 -14
- maleo_foundation/utils/__init__.py +2 -1
- maleo_foundation/utils/cache.py +10 -13
- maleo_foundation/utils/client.py +25 -12
- maleo_foundation/utils/controller.py +59 -37
- maleo_foundation/utils/dependencies/__init__.py +2 -1
- maleo_foundation/utils/dependencies/auth.py +5 -12
- maleo_foundation/utils/dependencies/context.py +3 -4
- maleo_foundation/utils/exceptions.py +50 -28
- maleo_foundation/utils/extractor.py +18 -6
- maleo_foundation/utils/formatter/__init__.py +2 -1
- maleo_foundation/utils/formatter/case.py +5 -4
- maleo_foundation/utils/loaders/__init__.py +2 -1
- maleo_foundation/utils/loaders/credential/__init__.py +2 -1
- maleo_foundation/utils/loaders/credential/google.py +29 -15
- maleo_foundation/utils/loaders/json.py +3 -2
- maleo_foundation/utils/loaders/key/__init__.py +2 -1
- maleo_foundation/utils/loaders/key/rsa.py +26 -13
- maleo_foundation/utils/loaders/yaml.py +2 -1
- maleo_foundation/utils/logging.py +70 -46
- maleo_foundation/utils/merger.py +7 -9
- maleo_foundation/utils/query.py +41 -34
- maleo_foundation/utils/repository.py +29 -16
- maleo_foundation/utils/searcher.py +4 -6
- {maleo_foundation-0.3.46.dist-info → maleo_foundation-0.3.48.dist-info}/METADATA +14 -1
- maleo_foundation-0.3.48.dist-info/RECORD +137 -0
- maleo_foundation/expanded_types/repository.py +0 -68
- maleo_foundation/models/transfers/results/service/repository.py +0 -39
- maleo_foundation-0.3.46.dist-info/RECORD +0 -139
- {maleo_foundation-0.3.46.dist-info → maleo_foundation-0.3.48.dist-info}/WHEEL +0 -0
- {maleo_foundation-0.3.46.dist-info → maleo_foundation-0.3.48.dist-info}/top_level.txt +0 -0
@@ -2,17 +2,21 @@ from pydantic import BaseModel, ConfigDict, Field, model_validator
|
|
2
2
|
from starlette.authentication import AuthCredentials, BaseUser
|
3
3
|
from typing import Optional, Self, Sequence
|
4
4
|
from maleo_foundation.enums import BaseEnums
|
5
|
-
from maleo_foundation.models.transfers.general.token import
|
5
|
+
from maleo_foundation.models.transfers.general.token import (
|
6
|
+
MaleoFoundationTokenGeneralTransfers,
|
7
|
+
)
|
8
|
+
|
6
9
|
|
7
10
|
class Token(BaseModel):
|
8
11
|
type: BaseEnums.TokenType = Field(..., description="Token's type")
|
9
|
-
payload: MaleoFoundationTokenGeneralTransfers.DecodePayload = Field(
|
12
|
+
payload: MaleoFoundationTokenGeneralTransfers.DecodePayload = Field(
|
13
|
+
..., description="Token's payload"
|
14
|
+
)
|
15
|
+
|
10
16
|
|
11
17
|
class Credentials(AuthCredentials):
|
12
18
|
def __init__(
|
13
|
-
self,
|
14
|
-
token: Optional[Token] = None,
|
15
|
-
scopes: Optional[Sequence[str]] = None
|
19
|
+
self, token: Optional[Token] = None, scopes: Optional[Sequence[str]] = None
|
16
20
|
) -> None:
|
17
21
|
self._token = token
|
18
22
|
super().__init__(scopes)
|
@@ -21,16 +25,15 @@ class Credentials(AuthCredentials):
|
|
21
25
|
def token(self) -> Optional[Token]:
|
22
26
|
return self._token
|
23
27
|
|
28
|
+
|
24
29
|
class CredentialsModel(BaseModel):
|
25
30
|
token: Optional[Token] = Field(None, description="Token")
|
26
31
|
scopes: Optional[Sequence[str]] = Field(None, description="Scopes")
|
27
32
|
|
33
|
+
|
28
34
|
class User(BaseUser):
|
29
35
|
def __init__(
|
30
|
-
self,
|
31
|
-
authenticated: bool = True,
|
32
|
-
username: str = "",
|
33
|
-
email: str = ""
|
36
|
+
self, authenticated: bool = True, username: str = "", email: str = ""
|
34
37
|
) -> None:
|
35
38
|
self._authenticated = authenticated
|
36
39
|
self._username = username
|
@@ -48,18 +51,26 @@ class User(BaseUser):
|
|
48
51
|
def identity(self) -> str:
|
49
52
|
return self._email
|
50
53
|
|
54
|
+
|
51
55
|
class UserModel(BaseModel):
|
52
56
|
is_authenticated: bool = Field(True, description="Authenticated")
|
53
57
|
display_name: str = Field("", description="Username")
|
54
58
|
identity: str = Field("", description="Email")
|
55
59
|
|
60
|
+
|
56
61
|
class Authentication(BaseModel):
|
57
62
|
model_config = ConfigDict(arbitrary_types_allowed=True)
|
58
63
|
|
59
|
-
credentials: Credentials = Field(
|
60
|
-
|
64
|
+
credentials: Credentials = Field(
|
65
|
+
..., description="Credentials's information", exclude=True
|
66
|
+
)
|
67
|
+
credentials_model: CredentialsModel = Field(
|
68
|
+
default_factory=CredentialsModel, # type: ignore
|
69
|
+
description="Credential's model",
|
70
|
+
serialization_alias="credentials",
|
71
|
+
)
|
61
72
|
user: User = Field(..., description="User's information", exclude=True)
|
62
|
-
user_model: UserModel = Field(default_factory=UserModel, description="User's model", serialization_alias="user")
|
73
|
+
user_model: UserModel = Field(default_factory=UserModel, description="User's model", serialization_alias="user") # type: ignore
|
63
74
|
|
64
75
|
@model_validator(mode="after")
|
65
76
|
def define_models(self) -> Self:
|
@@ -68,4 +79,4 @@ class Authentication(BaseModel):
|
|
68
79
|
self.user_model.is_authenticated = self.user.is_authenticated
|
69
80
|
self.user_model.display_name = self.user.display_name
|
70
81
|
self.user_model.identity = self.user.identity
|
71
|
-
return self
|
82
|
+
return self
|
@@ -3,6 +3,7 @@ from pydantic import BaseModel, Field
|
|
3
3
|
|
4
4
|
TOKEN_SCHEME = HTTPBearer()
|
5
5
|
|
6
|
+
|
6
7
|
class Authorization(BaseModel):
|
7
8
|
scheme: str = Field(..., description="Authorization's scheme")
|
8
|
-
credentials: str = Field(..., description="Authorization's credentials")
|
9
|
+
credentials: str = Field(..., description="Authorization's credentials")
|
@@ -4,12 +4,12 @@ from maleo_foundation.types import BaseTypes
|
|
4
4
|
from maleo_foundation.utils.logging import SimpleConfig
|
5
5
|
from maleo_foundation.client.services.encryption import (
|
6
6
|
MaleoFoundationAESEncryptionClientService,
|
7
|
-
MaleoFoundationRSAEncryptionClientService
|
7
|
+
MaleoFoundationRSAEncryptionClientService,
|
8
8
|
)
|
9
9
|
from maleo_foundation.client.services.hash import (
|
10
10
|
MaleoFoundationSHA256HashClientService,
|
11
11
|
MaleoFoundationHMACHashClientService,
|
12
|
-
MaleoFoundationBcryptHashClientService
|
12
|
+
MaleoFoundationBcryptHashClientService,
|
13
13
|
)
|
14
14
|
from maleo_foundation.client.services import (
|
15
15
|
MaleoFoundationEncryptionServices,
|
@@ -17,42 +17,43 @@ from maleo_foundation.client.services import (
|
|
17
17
|
MaleoFoundationKeyClientService,
|
18
18
|
MaleoFoundationSignatureClientService,
|
19
19
|
MaleoFoundationTokenClientService,
|
20
|
-
MaleoFoundationServices
|
20
|
+
MaleoFoundationServices,
|
21
21
|
)
|
22
22
|
|
23
|
+
|
23
24
|
class MaleoFoundationClientManager(ClientManager):
|
24
25
|
def __init__(
|
25
|
-
self,
|
26
|
-
log_config: SimpleConfig,
|
27
|
-
service_key: BaseTypes.OptionalString = None
|
26
|
+
self, log_config: SimpleConfig, service_key: BaseTypes.OptionalString = None
|
28
27
|
) -> None:
|
29
28
|
key = "maleo-foundation"
|
30
29
|
name = "MaleoFoundation"
|
31
|
-
super().__init__(
|
32
|
-
key,
|
33
|
-
name,
|
34
|
-
log_config,
|
35
|
-
service_key
|
36
|
-
)
|
30
|
+
super().__init__(key, name, log_config, service_key)
|
37
31
|
self._initialize_services()
|
38
32
|
self._logger.info("Client manager initialized successfully")
|
39
33
|
|
40
34
|
def _initialize_services(self):
|
41
35
|
super()._initialize_services()
|
42
|
-
aes_encryption_service = MaleoFoundationAESEncryptionClientService(
|
43
|
-
|
36
|
+
aes_encryption_service = MaleoFoundationAESEncryptionClientService(
|
37
|
+
logger=self._logger
|
38
|
+
)
|
39
|
+
rsa_encryption_service = MaleoFoundationRSAEncryptionClientService(
|
40
|
+
logger=self._logger
|
41
|
+
)
|
44
42
|
encryption_services = MaleoFoundationEncryptionServices(
|
45
|
-
aes=aes_encryption_service,
|
46
|
-
rsa=rsa_encryption_service
|
43
|
+
aes=aes_encryption_service, rsa=rsa_encryption_service
|
47
44
|
)
|
48
45
|
key_service = MaleoFoundationKeyClientService(logger=self._logger)
|
49
|
-
bcrypt_hash_service = MaleoFoundationBcryptHashClientService(
|
46
|
+
bcrypt_hash_service = MaleoFoundationBcryptHashClientService(
|
47
|
+
logger=self._logger
|
48
|
+
)
|
50
49
|
hmac_hash_service = MaleoFoundationHMACHashClientService(logger=self._logger)
|
51
|
-
sha256_hash_service = MaleoFoundationSHA256HashClientService(
|
50
|
+
sha256_hash_service = MaleoFoundationSHA256HashClientService(
|
51
|
+
logger=self._logger
|
52
|
+
)
|
52
53
|
hash_services = MaleoFoundationHashServices(
|
53
54
|
bcrypt=bcrypt_hash_service,
|
54
55
|
hmac=hmac_hash_service,
|
55
|
-
sha256=sha256_hash_service
|
56
|
+
sha256=sha256_hash_service,
|
56
57
|
)
|
57
58
|
signature_service = MaleoFoundationSignatureClientService(logger=self._logger)
|
58
59
|
token_service = MaleoFoundationTokenClientService(logger=self._logger)
|
@@ -61,9 +62,9 @@ class MaleoFoundationClientManager(ClientManager):
|
|
61
62
|
hash=hash_services,
|
62
63
|
key=key_service,
|
63
64
|
signature=signature_service,
|
64
|
-
token=token_service
|
65
|
+
token=token_service,
|
65
66
|
)
|
66
67
|
|
67
68
|
@property
|
68
69
|
def services(self) -> MaleoFoundationServices:
|
69
|
-
return self._services
|
70
|
+
return self._services
|
@@ -1,15 +1,24 @@
|
|
1
1
|
from __future__ import annotations
|
2
2
|
from pydantic import Field
|
3
3
|
from maleo_foundation.managers.client.base import ClientServices
|
4
|
-
from maleo_foundation.client.services.encryption import
|
4
|
+
from maleo_foundation.client.services.encryption import (
|
5
|
+
MaleoFoundationEncryptionServices,
|
6
|
+
)
|
5
7
|
from maleo_foundation.client.services.hash import MaleoFoundationHashServices
|
6
8
|
from maleo_foundation.client.services.key import MaleoFoundationKeyClientService
|
7
|
-
from maleo_foundation.client.services.signature import
|
9
|
+
from maleo_foundation.client.services.signature import (
|
10
|
+
MaleoFoundationSignatureClientService,
|
11
|
+
)
|
8
12
|
from maleo_foundation.client.services.token import MaleoFoundationTokenClientService
|
9
13
|
|
14
|
+
|
10
15
|
class MaleoFoundationServices(ClientServices):
|
11
|
-
encryption:MaleoFoundationEncryptionServices = Field(
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
+
encryption: MaleoFoundationEncryptionServices = Field(
|
17
|
+
..., description="Encryption's services"
|
18
|
+
)
|
19
|
+
hash: MaleoFoundationHashServices = Field(..., description="Hash's services")
|
20
|
+
key: MaleoFoundationKeyClientService = Field(..., description="Key's service")
|
21
|
+
signature: MaleoFoundationSignatureClientService = Field(
|
22
|
+
..., description="Signature's service"
|
23
|
+
)
|
24
|
+
token: MaleoFoundationTokenClientService = Field(..., description="Token's service")
|
@@ -1,9 +1,18 @@
|
|
1
1
|
from __future__ import annotations
|
2
2
|
from pydantic import Field
|
3
3
|
from maleo_foundation.managers.client.base import ClientServices
|
4
|
-
from maleo_foundation.client.services.encryption.aes import
|
5
|
-
|
4
|
+
from maleo_foundation.client.services.encryption.aes import (
|
5
|
+
MaleoFoundationAESEncryptionClientService,
|
6
|
+
)
|
7
|
+
from maleo_foundation.client.services.encryption.rsa import (
|
8
|
+
MaleoFoundationRSAEncryptionClientService,
|
9
|
+
)
|
10
|
+
|
6
11
|
|
7
12
|
class MaleoFoundationEncryptionServices(ClientServices):
|
8
|
-
aes:MaleoFoundationAESEncryptionClientService = Field(
|
9
|
-
|
13
|
+
aes: MaleoFoundationAESEncryptionClientService = Field(
|
14
|
+
..., description="AES encryption's service"
|
15
|
+
)
|
16
|
+
rsa: MaleoFoundationRSAEncryptionClientService = Field(
|
17
|
+
..., description="RSA encryption's service"
|
18
|
+
)
|
@@ -2,83 +2,88 @@ import os
|
|
2
2
|
from base64 import b64decode, b64encode
|
3
3
|
from cryptography.hazmat.backends import default_backend
|
4
4
|
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
5
|
-
from maleo_foundation.expanded_types.encryption.aes
|
6
|
-
|
5
|
+
from maleo_foundation.expanded_types.encryption.aes import (
|
6
|
+
MaleoFoundationAESEncryptionResultsTypes,
|
7
|
+
)
|
7
8
|
from maleo_foundation.managers.client.base import ClientService
|
8
|
-
from maleo_foundation.models.schemas.encryption
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
from maleo_foundation.models.transfers.results.encryption.aes
|
13
|
-
|
9
|
+
from maleo_foundation.models.schemas.encryption import MaleoFoundationEncryptionSchemas
|
10
|
+
from maleo_foundation.models.transfers.parameters.encryption.aes import (
|
11
|
+
MaleoFoundationAESEncryptionParametersTransfers,
|
12
|
+
)
|
13
|
+
from maleo_foundation.models.transfers.results.encryption.aes import (
|
14
|
+
EncryptData,
|
15
|
+
MaleoFoundationAESEncryptionResultsTransfers,
|
16
|
+
)
|
14
17
|
from maleo_foundation.utils.exceptions import BaseExceptions
|
15
18
|
|
19
|
+
|
16
20
|
class MaleoFoundationAESEncryptionClientService(ClientService):
|
17
21
|
def encrypt(
|
18
|
-
self,
|
19
|
-
parameters: MaleoFoundationAESEncryptionParametersTransfers.Encrypt
|
22
|
+
self, parameters: MaleoFoundationAESEncryptionParametersTransfers.Encrypt
|
20
23
|
) -> MaleoFoundationAESEncryptionResultsTypes.Encrypt:
|
21
24
|
"""Encrypt a plaintext using AES algorithm."""
|
25
|
+
|
22
26
|
@BaseExceptions.service_exception_handler(
|
23
27
|
operation="encrypting plaintext",
|
24
28
|
logger=self._logger,
|
25
|
-
fail_result_class=MaleoFoundationAESEncryptionResultsTransfers.Fail
|
29
|
+
fail_result_class=MaleoFoundationAESEncryptionResultsTransfers.Fail,
|
26
30
|
)
|
27
31
|
def _impl():
|
28
|
-
|
32
|
+
# * Define random key and initialization vector bytes
|
29
33
|
key_bytes = os.urandom(32)
|
30
34
|
initialization_vector_bytes = os.urandom(16)
|
31
|
-
|
35
|
+
# * Encrypt message with encryptor instance
|
32
36
|
cipher = Cipher(
|
33
37
|
algorithm=algorithms.AES(key_bytes),
|
34
38
|
mode=modes.CFB(initialization_vector_bytes),
|
35
|
-
backend=default_backend()
|
39
|
+
backend=default_backend(),
|
36
40
|
)
|
37
41
|
encryptor = cipher.encryptor()
|
38
|
-
ciphertext = (
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
initialization_vector = b64encode(initialization_vector_bytes).decode('utf-8')
|
42
|
+
ciphertext = b64encode(
|
43
|
+
encryptor.update(parameters.plaintext.encode()) + encryptor.finalize()
|
44
|
+
).decode("utf-8")
|
45
|
+
# * Encode the results to base64 strings
|
46
|
+
key = b64encode(key_bytes).decode("utf-8")
|
47
|
+
initialization_vector = b64encode(initialization_vector_bytes).decode(
|
48
|
+
"utf-8"
|
49
|
+
)
|
47
50
|
data = EncryptData(
|
48
51
|
key=key,
|
49
52
|
initialization_vector=initialization_vector,
|
50
|
-
ciphertext=ciphertext
|
53
|
+
ciphertext=ciphertext,
|
51
54
|
)
|
52
55
|
self._logger.info("Plaintext successfully encrypted")
|
53
|
-
return MaleoFoundationAESEncryptionResultsTransfers.Encrypt(data=data)
|
56
|
+
return MaleoFoundationAESEncryptionResultsTransfers.Encrypt(data=data) # type: ignore
|
57
|
+
|
54
58
|
return _impl()
|
55
59
|
|
56
60
|
def decrypt(
|
57
|
-
self,
|
58
|
-
parameters: MaleoFoundationAESEncryptionParametersTransfers.Decrypt
|
61
|
+
self, parameters: MaleoFoundationAESEncryptionParametersTransfers.Decrypt
|
59
62
|
) -> MaleoFoundationAESEncryptionResultsTypes.Decrypt:
|
60
63
|
"""Decrypt a ciphertext using AES algorithm."""
|
64
|
+
|
61
65
|
@BaseExceptions.service_exception_handler(
|
62
66
|
operation="verify single encryption",
|
63
67
|
logger=self._logger,
|
64
|
-
fail_result_class=MaleoFoundationAESEncryptionResultsTransfers.Fail
|
68
|
+
fail_result_class=MaleoFoundationAESEncryptionResultsTransfers.Fail,
|
65
69
|
)
|
66
70
|
def _impl():
|
67
|
-
|
71
|
+
# * Decode base64-encoded AES key, IV, and encrypted message
|
68
72
|
key_bytes = b64decode(parameters.key)
|
69
73
|
initialization_vector_bytes = b64decode(parameters.initialization_vector)
|
70
|
-
|
74
|
+
# * Decrypt message with decryptor instance
|
71
75
|
cipher = Cipher(
|
72
76
|
algorithm=algorithms.AES(key_bytes),
|
73
77
|
mode=modes.CFB(initialization_vector_bytes),
|
74
|
-
backend=default_backend()
|
78
|
+
backend=default_backend(),
|
75
79
|
)
|
76
80
|
decryptor = cipher.decryptor()
|
77
81
|
plaintext = (
|
78
|
-
decryptor
|
79
|
-
|
80
|
-
)
|
82
|
+
decryptor.update(b64decode(parameters.ciphertext))
|
83
|
+
+ decryptor.finalize()
|
84
|
+
).decode()
|
81
85
|
data = MaleoFoundationEncryptionSchemas.Plaintext(plaintext=plaintext)
|
82
86
|
self._logger.info("Ciphertext successfully decrypted")
|
83
|
-
return MaleoFoundationAESEncryptionResultsTransfers.Decrypt(data=data)
|
84
|
-
|
87
|
+
return MaleoFoundationAESEncryptionResultsTransfers.Decrypt(data=data) # type: ignore
|
88
|
+
|
89
|
+
return _impl()
|
@@ -2,110 +2,110 @@ from base64 import b64decode, b64encode
|
|
2
2
|
from Crypto.Cipher import PKCS1_OAEP
|
3
3
|
from Crypto.Hash import SHA256
|
4
4
|
from maleo_foundation.enums import BaseEnums
|
5
|
-
from maleo_foundation.expanded_types.encryption.rsa
|
6
|
-
|
5
|
+
from maleo_foundation.expanded_types.encryption.rsa import (
|
6
|
+
MaleoFoundationRSAEncryptionResultsTypes,
|
7
|
+
)
|
7
8
|
from maleo_foundation.managers.client.base import ClientService
|
8
|
-
from maleo_foundation.models.schemas.encryption
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
from maleo_foundation.models.transfers.results.encryption.rsa
|
13
|
-
|
9
|
+
from maleo_foundation.models.schemas.encryption import MaleoFoundationEncryptionSchemas
|
10
|
+
from maleo_foundation.models.transfers.parameters.encryption.rsa import (
|
11
|
+
MaleoFoundationRSAEncryptionParametersTransfers,
|
12
|
+
)
|
13
|
+
from maleo_foundation.models.transfers.results.encryption.rsa import (
|
14
|
+
MaleoFoundationRSAEncryptionResultsTransfers,
|
15
|
+
)
|
14
16
|
from maleo_foundation.utils.exceptions import BaseExceptions
|
15
17
|
from maleo_foundation.utils.loaders.key.rsa import RSAKeyLoader
|
16
18
|
|
19
|
+
|
17
20
|
class MaleoFoundationRSAEncryptionClientService(ClientService):
|
18
21
|
def encrypt(
|
19
|
-
self,
|
20
|
-
parameters: MaleoFoundationRSAEncryptionParametersTransfers.Encrypt
|
22
|
+
self, parameters: MaleoFoundationRSAEncryptionParametersTransfers.Encrypt
|
21
23
|
) -> MaleoFoundationRSAEncryptionResultsTypes.Encrypt:
|
22
24
|
"""Encrypt a plaintext using RSA algorithm."""
|
25
|
+
|
23
26
|
@BaseExceptions.service_exception_handler(
|
24
27
|
operation="encrypting plaintext",
|
25
28
|
logger=self._logger,
|
26
|
-
fail_result_class=MaleoFoundationRSAEncryptionResultsTransfers.Fail
|
29
|
+
fail_result_class=MaleoFoundationRSAEncryptionResultsTransfers.Fail,
|
27
30
|
)
|
28
31
|
def _impl():
|
29
32
|
try:
|
30
33
|
public_key = RSAKeyLoader.load_with_pycryptodome(
|
31
|
-
type=BaseEnums.KeyType.PUBLIC,
|
32
|
-
extern_key=parameters.key
|
34
|
+
type=BaseEnums.KeyType.PUBLIC, extern_key=parameters.key
|
33
35
|
)
|
34
36
|
except TypeError:
|
35
37
|
message = "Invalid key type"
|
36
38
|
description = "A public key must be used for encrypting a plaintext"
|
37
39
|
other = "Ensure the given key is of type public key"
|
38
40
|
return MaleoFoundationRSAEncryptionResultsTransfers.Fail(
|
39
|
-
message=message,
|
40
|
-
|
41
|
-
other=other
|
42
|
-
)
|
41
|
+
message=message, description=description, other=other
|
42
|
+
) # type: ignore
|
43
43
|
except Exception as e:
|
44
|
-
self._logger.error(
|
44
|
+
self._logger.error(
|
45
|
+
"Unexpected error occured while trying to import key:\n'%s'",
|
46
|
+
str(e),
|
47
|
+
exc_info=True,
|
48
|
+
)
|
45
49
|
message = "Invalid key"
|
46
50
|
description = "Unexpected error occured while trying to import key"
|
47
51
|
other = "Ensure given key is valid"
|
48
52
|
return MaleoFoundationRSAEncryptionResultsTransfers.Fail(
|
49
|
-
message=message,
|
50
|
-
|
51
|
-
|
52
|
-
)
|
53
|
-
#* Initialize cipher with OAEP padding and SHA-256
|
53
|
+
message=message, description=description, other=other
|
54
|
+
) # type: ignore
|
55
|
+
# * Initialize cipher with OAEP padding and SHA-256
|
54
56
|
cipher = PKCS1_OAEP.new(public_key, hashAlgo=SHA256)
|
55
|
-
|
56
|
-
ciphertext = (
|
57
|
-
|
58
|
-
|
59
|
-
.encrypt(parameters.plaintext.encode('utf-8'))
|
60
|
-
)
|
61
|
-
.decode('utf-8')
|
62
|
-
)
|
57
|
+
# * Encrypt the plaintext and return as base64-encoded string
|
58
|
+
ciphertext = b64encode(
|
59
|
+
cipher.encrypt(parameters.plaintext.encode("utf-8"))
|
60
|
+
).decode("utf-8")
|
63
61
|
data = MaleoFoundationEncryptionSchemas.Ciphertext(ciphertext=ciphertext)
|
64
62
|
self._logger.info("Plaintext successfully encrypted")
|
65
|
-
return MaleoFoundationRSAEncryptionResultsTransfers.Encrypt(data=data)
|
63
|
+
return MaleoFoundationRSAEncryptionResultsTransfers.Encrypt(data=data) # type: ignore
|
64
|
+
|
66
65
|
return _impl()
|
67
66
|
|
68
67
|
def decrypt(
|
69
|
-
self,
|
70
|
-
parameters: MaleoFoundationRSAEncryptionParametersTransfers.Decrypt
|
68
|
+
self, parameters: MaleoFoundationRSAEncryptionParametersTransfers.Decrypt
|
71
69
|
) -> MaleoFoundationRSAEncryptionResultsTypes.Decrypt:
|
72
70
|
"""Decrypt a ciphertext using RSA algorithm."""
|
71
|
+
|
73
72
|
@BaseExceptions.service_exception_handler(
|
74
73
|
operation="verify single encryption",
|
75
74
|
logger=self._logger,
|
76
|
-
fail_result_class=MaleoFoundationRSAEncryptionResultsTransfers.Fail
|
75
|
+
fail_result_class=MaleoFoundationRSAEncryptionResultsTransfers.Fail,
|
77
76
|
)
|
78
77
|
def _impl():
|
79
78
|
try:
|
80
79
|
private_key = RSAKeyLoader.load_with_pycryptodome(
|
81
80
|
type=BaseEnums.KeyType.PRIVATE,
|
82
81
|
extern_key=parameters.key,
|
83
|
-
passphrase=parameters.password
|
82
|
+
passphrase=parameters.password,
|
84
83
|
)
|
85
84
|
except TypeError:
|
86
85
|
message = "Invalid key type"
|
87
86
|
description = "A private key must be used for decrypting a ciphertext"
|
88
87
|
other = "Ensure the given key is of type private key"
|
89
88
|
return MaleoFoundationRSAEncryptionResultsTransfers.Fail(
|
90
|
-
message=message,
|
91
|
-
|
92
|
-
other=other
|
93
|
-
)
|
89
|
+
message=message, description=description, other=other
|
90
|
+
) # type: ignore
|
94
91
|
except Exception as e:
|
95
|
-
self._logger.error(
|
92
|
+
self._logger.error(
|
93
|
+
"Unexpected error occured while trying to import key:\n'%s'",
|
94
|
+
str(e),
|
95
|
+
exc_info=True,
|
96
|
+
)
|
96
97
|
message = "Invalid key"
|
97
98
|
description = "Unexpected error occured while trying to import key"
|
98
99
|
other = "Ensure given key is valid"
|
99
100
|
return MaleoFoundationRSAEncryptionResultsTransfers.Fail(
|
100
|
-
message=message,
|
101
|
-
|
102
|
-
|
103
|
-
)
|
104
|
-
#* Initialize cipher with OAEP padding and SHA-256
|
101
|
+
message=message, description=description, other=other
|
102
|
+
) # type: ignore
|
103
|
+
# * Initialize cipher with OAEP padding and SHA-256
|
105
104
|
cipher = PKCS1_OAEP.new(private_key, hashAlgo=SHA256)
|
106
|
-
|
107
|
-
plaintext = cipher.decrypt(b64decode(parameters.ciphertext))
|
105
|
+
# * Decode the base64-encoded ciphertext and then decrypt
|
106
|
+
plaintext = cipher.decrypt(b64decode(parameters.ciphertext)).decode()
|
108
107
|
data = MaleoFoundationEncryptionSchemas.Plaintext(plaintext=plaintext)
|
109
108
|
self._logger.info("Ciphertext successfully decrypted")
|
110
|
-
return MaleoFoundationRSAEncryptionResultsTransfers.Decrypt(data=data)
|
111
|
-
|
109
|
+
return MaleoFoundationRSAEncryptionResultsTransfers.Decrypt(data=data) # type: ignore
|
110
|
+
|
111
|
+
return _impl()
|
@@ -1,11 +1,24 @@
|
|
1
1
|
from __future__ import annotations
|
2
2
|
from pydantic import Field
|
3
3
|
from maleo_foundation.managers.client.base import ClientServices
|
4
|
-
from maleo_foundation.client.services.hash.bcrypt import
|
5
|
-
|
6
|
-
|
4
|
+
from maleo_foundation.client.services.hash.bcrypt import (
|
5
|
+
MaleoFoundationBcryptHashClientService,
|
6
|
+
)
|
7
|
+
from maleo_foundation.client.services.hash.hmac import (
|
8
|
+
MaleoFoundationHMACHashClientService,
|
9
|
+
)
|
10
|
+
from maleo_foundation.client.services.hash.sha256 import (
|
11
|
+
MaleoFoundationSHA256HashClientService,
|
12
|
+
)
|
13
|
+
|
7
14
|
|
8
15
|
class MaleoFoundationHashServices(ClientServices):
|
9
|
-
bcrypt:MaleoFoundationBcryptHashClientService = Field(
|
10
|
-
|
11
|
-
|
16
|
+
bcrypt: MaleoFoundationBcryptHashClientService = Field(
|
17
|
+
..., description="Bcrypt hash's service"
|
18
|
+
)
|
19
|
+
hmac: MaleoFoundationHMACHashClientService = Field(
|
20
|
+
..., description="HMAC hash's service"
|
21
|
+
)
|
22
|
+
sha256: MaleoFoundationSHA256HashClientService = Field(
|
23
|
+
..., description="SHA256 hash's service"
|
24
|
+
)
|
@@ -1,52 +1,54 @@
|
|
1
1
|
import bcrypt
|
2
|
-
from maleo_foundation.expanded_types.hash
|
3
|
-
import MaleoFoundationHashResultsTypes
|
2
|
+
from maleo_foundation.expanded_types.hash import MaleoFoundationHashResultsTypes
|
4
3
|
from maleo_foundation.managers.client.base import ClientService
|
5
|
-
from maleo_foundation.models.schemas.hash import
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
from maleo_foundation.models.transfers.results.hash
|
10
|
-
|
4
|
+
from maleo_foundation.models.schemas.hash import MaleoFoundationHashSchemas
|
5
|
+
from maleo_foundation.models.transfers.parameters.hash.bcrypt import (
|
6
|
+
MaleoFoundationBcryptHashParametersTransfers,
|
7
|
+
)
|
8
|
+
from maleo_foundation.models.transfers.results.hash import (
|
9
|
+
MaleoFoundationHashResultsTransfers,
|
10
|
+
)
|
11
11
|
from maleo_foundation.utils.exceptions import BaseExceptions
|
12
12
|
|
13
|
+
|
13
14
|
class MaleoFoundationBcryptHashClientService(ClientService):
|
14
15
|
def hash(
|
15
|
-
self,
|
16
|
-
parameters:MaleoFoundationBcryptHashParametersTransfers.Hash
|
16
|
+
self, parameters: MaleoFoundationBcryptHashParametersTransfers.Hash
|
17
17
|
) -> MaleoFoundationHashResultsTypes.Hash:
|
18
18
|
"""Generate a bcrypt hash for the given message."""
|
19
|
+
|
19
20
|
@BaseExceptions.service_exception_handler(
|
20
21
|
operation="hashing single message",
|
21
22
|
logger=self._logger,
|
22
|
-
fail_result_class=MaleoFoundationHashResultsTransfers.Fail
|
23
|
+
fail_result_class=MaleoFoundationHashResultsTransfers.Fail,
|
23
24
|
)
|
24
25
|
def _impl():
|
25
26
|
hash = bcrypt.hashpw(
|
26
|
-
password=parameters.message.encode(),
|
27
|
-
salt=bcrypt.gensalt()
|
27
|
+
password=parameters.message.encode(), salt=bcrypt.gensalt()
|
28
28
|
).decode()
|
29
29
|
data = MaleoFoundationHashSchemas.Hash(hash=hash)
|
30
30
|
self._logger.info("Message successfully hashed")
|
31
31
|
return MaleoFoundationHashResultsTransfers.Hash(data=data)
|
32
|
+
|
32
33
|
return _impl()
|
33
34
|
|
34
35
|
def verify(
|
35
|
-
self,
|
36
|
-
parameters:MaleoFoundationBcryptHashParametersTransfers.Verify
|
36
|
+
self, parameters: MaleoFoundationBcryptHashParametersTransfers.Verify
|
37
37
|
) -> MaleoFoundationHashResultsTypes.Verify:
|
38
38
|
"""Verify a message against the given message hash."""
|
39
|
+
|
39
40
|
@BaseExceptions.service_exception_handler(
|
40
41
|
operation="verify single hash",
|
41
42
|
logger=self._logger,
|
42
|
-
fail_result_class=MaleoFoundationHashResultsTransfers.Fail
|
43
|
+
fail_result_class=MaleoFoundationHashResultsTransfers.Fail,
|
43
44
|
)
|
44
45
|
def _impl():
|
45
46
|
is_valid = bcrypt.checkpw(
|
46
47
|
password=parameters.message.encode(),
|
47
|
-
hashed_password=parameters.hash.encode()
|
48
|
+
hashed_password=parameters.hash.encode(),
|
48
49
|
)
|
49
50
|
data = MaleoFoundationHashSchemas.IsValid(is_valid=is_valid)
|
50
51
|
self._logger.info("Hash successfully verified")
|
51
52
|
return MaleoFoundationHashResultsTransfers.Verify(data=data)
|
52
|
-
|
53
|
+
|
54
|
+
return _impl()
|