maleo-foundation 0.3.46__py3-none-any.whl → 0.3.47__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 +28 -13
- maleo_foundation/utils/searcher.py +4 -6
- {maleo_foundation-0.3.46.dist-info → maleo_foundation-0.3.47.dist-info}/METADATA +14 -1
- maleo_foundation-0.3.47.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.47.dist-info}/WHEEL +0 -0
- {maleo_foundation-0.3.46.dist-info → maleo_foundation-0.3.47.dist-info}/top_level.txt +0 -0
@@ -1,55 +1,58 @@
|
|
1
1
|
from Crypto.Hash import HMAC, SHA256
|
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.hmac import (
|
6
|
+
MaleoFoundationHMACHashParametersTransfers,
|
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 MaleoFoundationHMACHashClientService(ClientService):
|
14
15
|
def hash(
|
15
|
-
self,
|
16
|
-
parameters:MaleoFoundationHMACHashParametersTransfers.Hash
|
16
|
+
self, parameters: MaleoFoundationHMACHashParametersTransfers.Hash
|
17
17
|
) -> MaleoFoundationHashResultsTypes.Hash:
|
18
18
|
"""Generate a hmac 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 = HMAC.new(
|
26
27
|
key=parameters.key.encode(),
|
27
28
|
msg=parameters.message.encode(),
|
28
|
-
digestmod=SHA256
|
29
|
+
digestmod=SHA256,
|
29
30
|
).hexdigest()
|
30
31
|
data = MaleoFoundationHashSchemas.Hash(hash=hash)
|
31
32
|
self._logger.info("Message successfully hashed")
|
32
33
|
return MaleoFoundationHashResultsTransfers.Hash(data=data)
|
34
|
+
|
33
35
|
return _impl()
|
34
36
|
|
35
37
|
def verify(
|
36
|
-
self,
|
37
|
-
parameters:MaleoFoundationHMACHashParametersTransfers.Verify
|
38
|
+
self, parameters: MaleoFoundationHMACHashParametersTransfers.Verify
|
38
39
|
) -> MaleoFoundationHashResultsTypes.Verify:
|
39
40
|
"""Verify a message against the given message hash."""
|
41
|
+
|
40
42
|
@BaseExceptions.service_exception_handler(
|
41
43
|
operation="verify single hash",
|
42
44
|
logger=self._logger,
|
43
|
-
fail_result_class=MaleoFoundationHashResultsTransfers.Fail
|
45
|
+
fail_result_class=MaleoFoundationHashResultsTransfers.Fail,
|
44
46
|
)
|
45
47
|
def _impl():
|
46
48
|
computed_hash = HMAC.new(
|
47
49
|
key=parameters.key.encode(),
|
48
50
|
msg=parameters.message.encode(),
|
49
|
-
digestmod=SHA256
|
51
|
+
digestmod=SHA256,
|
50
52
|
).hexdigest()
|
51
53
|
is_valid = computed_hash == parameters.hash
|
52
54
|
data = MaleoFoundationHashSchemas.IsValid(is_valid=is_valid)
|
53
55
|
self._logger.info("Hash successfully verified")
|
54
56
|
return MaleoFoundationHashResultsTransfers.Verify(data=data)
|
55
|
-
|
57
|
+
|
58
|
+
return _impl()
|
@@ -1,42 +1,44 @@
|
|
1
1
|
from Crypto.Hash import SHA256
|
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
|
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.sha256 import (
|
6
|
+
MaleoFoundationSHA256HashParametersTransfers,
|
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 MaleoFoundationSHA256HashClientService(ClientService):
|
14
15
|
def hash(
|
15
|
-
self,
|
16
|
-
parameters:MaleoFoundationSHA256HashParametersTransfers.Hash
|
16
|
+
self, parameters: MaleoFoundationSHA256HashParametersTransfers.Hash
|
17
17
|
) -> MaleoFoundationHashResultsTypes.Hash:
|
18
18
|
"""Generate a sha256 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 = SHA256.new(parameters.message.encode()).hexdigest()
|
26
27
|
data = MaleoFoundationHashSchemas.Hash(hash=hash)
|
27
28
|
self._logger.info("Message successfully hashed")
|
28
29
|
return MaleoFoundationHashResultsTransfers.Hash(data=data)
|
30
|
+
|
29
31
|
return _impl()
|
30
32
|
|
31
33
|
def verify(
|
32
|
-
self,
|
33
|
-
parameters:MaleoFoundationSHA256HashParametersTransfers.Verify
|
34
|
+
self, parameters: MaleoFoundationSHA256HashParametersTransfers.Verify
|
34
35
|
) -> MaleoFoundationHashResultsTypes.Verify:
|
35
36
|
"""Verify a message against the given message hash."""
|
37
|
+
|
36
38
|
@BaseExceptions.service_exception_handler(
|
37
39
|
operation="verify single hash",
|
38
40
|
logger=self._logger,
|
39
|
-
fail_result_class=MaleoFoundationHashResultsTransfers.Fail
|
41
|
+
fail_result_class=MaleoFoundationHashResultsTransfers.Fail,
|
40
42
|
)
|
41
43
|
def _impl():
|
42
44
|
computed_hash = SHA256.new(parameters.message.encode()).hexdigest()
|
@@ -44,4 +46,5 @@ class MaleoFoundationSHA256HashClientService(ClientService):
|
|
44
46
|
data = MaleoFoundationHashSchemas.IsValid(is_valid=is_valid)
|
45
47
|
self._logger.info("Hash successfully verified")
|
46
48
|
return MaleoFoundationHashResultsTransfers.Verify(data=data)
|
47
|
-
|
49
|
+
|
50
|
+
return _impl()
|
@@ -1,34 +1,37 @@
|
|
1
1
|
from cryptography.hazmat.backends import default_backend
|
2
2
|
from cryptography.hazmat.primitives.asymmetric import rsa
|
3
3
|
from cryptography.hazmat.primitives import serialization
|
4
|
-
from maleo_foundation.expanded_types.key
|
5
|
-
import MaleoFoundationKeyResultsTypes
|
4
|
+
from maleo_foundation.expanded_types.key import MaleoFoundationKeyResultsTypes
|
6
5
|
from maleo_foundation.managers.client.base import ClientService
|
7
|
-
from maleo_foundation.models.transfers.general.key
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
6
|
+
from maleo_foundation.models.transfers.general.key import (
|
7
|
+
MaleoFoundationKeyGeneralTransfers,
|
8
|
+
)
|
9
|
+
from maleo_foundation.models.transfers.parameters.key import (
|
10
|
+
MaleoFoundationKeyParametersTransfers,
|
11
|
+
)
|
12
|
+
from maleo_foundation.models.transfers.results.key import (
|
13
|
+
MaleoFoundationKeyResultsTransfers,
|
14
|
+
)
|
13
15
|
from maleo_foundation.utils.exceptions import BaseExceptions
|
14
16
|
|
17
|
+
|
15
18
|
class MaleoFoundationKeyClientService(ClientService):
|
16
19
|
def create_private(
|
17
|
-
self,
|
18
|
-
parameters: MaleoFoundationKeyParametersTransfers.CreatePrivateOrPair
|
20
|
+
self, parameters: MaleoFoundationKeyParametersTransfers.CreatePrivateOrPair
|
19
21
|
) -> MaleoFoundationKeyResultsTypes.CreatePrivate:
|
20
22
|
"""Create an RSA private key with X.509 encoding in .pem format."""
|
23
|
+
|
21
24
|
@BaseExceptions.service_exception_handler(
|
22
25
|
operation="creating private key",
|
23
26
|
logger=self._logger,
|
24
|
-
fail_result_class=MaleoFoundationKeyResultsTransfers.Fail
|
27
|
+
fail_result_class=MaleoFoundationKeyResultsTransfers.Fail,
|
25
28
|
)
|
26
29
|
def _impl():
|
27
|
-
|
30
|
+
# * Create private key
|
28
31
|
private_key = rsa.generate_private_key(
|
29
32
|
public_exponent=65537,
|
30
33
|
key_size=parameters.key_size,
|
31
|
-
backend=default_backend()
|
34
|
+
backend=default_backend(),
|
32
35
|
)
|
33
36
|
|
34
37
|
if parameters.password is None:
|
@@ -38,70 +41,72 @@ class MaleoFoundationKeyClientService(ClientService):
|
|
38
41
|
parameters.password.encode()
|
39
42
|
)
|
40
43
|
|
41
|
-
|
44
|
+
# * Serialize private key to PEM format
|
42
45
|
private_key_bytes = private_key.private_bytes(
|
43
46
|
encoding=serialization.Encoding.PEM,
|
44
47
|
format=serialization.PrivateFormat.PKCS8,
|
45
|
-
encryption_algorithm=encryption_algorithm
|
48
|
+
encryption_algorithm=encryption_algorithm,
|
46
49
|
)
|
47
50
|
|
48
51
|
self._logger.info("Successfully created private key")
|
49
52
|
data = MaleoFoundationKeyGeneralTransfers.PrivateKey(
|
50
53
|
value=private_key_bytes.decode()
|
51
|
-
)
|
52
|
-
return MaleoFoundationKeyResultsTransfers.CreatePrivate(data=data)
|
54
|
+
) # type: ignore
|
55
|
+
return MaleoFoundationKeyResultsTransfers.CreatePrivate(data=data) # type: ignore
|
56
|
+
|
53
57
|
return _impl()
|
54
58
|
|
55
59
|
def create_public(
|
56
|
-
self,
|
57
|
-
parameters: MaleoFoundationKeyParametersTransfers.CreatePublic
|
60
|
+
self, parameters: MaleoFoundationKeyParametersTransfers.CreatePublic
|
58
61
|
) -> MaleoFoundationKeyResultsTypes.CreatePublic:
|
59
62
|
"""Create an RSA public key with X.509 encoding in .pem format."""
|
63
|
+
|
60
64
|
@BaseExceptions.service_exception_handler(
|
61
65
|
operation="creating public key",
|
62
66
|
logger=self._logger,
|
63
|
-
fail_result_class=MaleoFoundationKeyResultsTransfers.Fail
|
67
|
+
fail_result_class=MaleoFoundationKeyResultsTransfers.Fail,
|
64
68
|
)
|
65
69
|
def _impl():
|
66
|
-
|
70
|
+
# * Serialize private key
|
67
71
|
private_key_bytes = parameters.value.encode()
|
68
72
|
private_key = serialization.load_pem_private_key(
|
69
73
|
private_key_bytes,
|
70
74
|
password=parameters.password.encode() if parameters.password else None,
|
71
|
-
backend=default_backend()
|
75
|
+
backend=default_backend(),
|
72
76
|
)
|
73
77
|
|
74
|
-
public_key = private_key.public_key()
|
78
|
+
public_key = private_key.public_key() # * Create public key
|
75
79
|
|
76
|
-
|
80
|
+
# * Serialize public key to PEM format
|
77
81
|
public_key_bytes = public_key.public_bytes(
|
78
82
|
encoding=serialization.Encoding.PEM,
|
79
|
-
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
83
|
+
format=serialization.PublicFormat.SubjectPublicKeyInfo,
|
80
84
|
)
|
81
85
|
|
82
86
|
self._logger.info("Successfully created public key")
|
83
87
|
data = MaleoFoundationKeyGeneralTransfers.PublicKey(
|
84
88
|
value=public_key_bytes.decode()
|
85
|
-
)
|
86
|
-
return MaleoFoundationKeyResultsTransfers.CreatePublic(data=data)
|
89
|
+
) # type: ignore
|
90
|
+
return MaleoFoundationKeyResultsTransfers.CreatePublic(data=data) # type: ignore
|
91
|
+
|
87
92
|
return _impl()
|
88
93
|
|
89
94
|
def create_pair(
|
90
|
-
self,
|
91
|
-
parameters: MaleoFoundationKeyParametersTransfers.CreatePrivateOrPair
|
95
|
+
self, parameters: MaleoFoundationKeyParametersTransfers.CreatePrivateOrPair
|
92
96
|
) -> MaleoFoundationKeyResultsTypes.CreatePair:
|
93
97
|
"""Create an RSA key pair with X.509 encoding in .pem format."""
|
98
|
+
|
94
99
|
@BaseExceptions.service_exception_handler(
|
95
100
|
operation="creating key pair",
|
96
101
|
logger=self._logger,
|
97
|
-
fail_result_class=MaleoFoundationKeyResultsTransfers.Fail
|
102
|
+
fail_result_class=MaleoFoundationKeyResultsTransfers.Fail,
|
98
103
|
)
|
99
104
|
def _impl():
|
100
|
-
|
105
|
+
# * Create private key
|
101
106
|
private_key = rsa.generate_private_key(
|
102
107
|
public_exponent=65537,
|
103
108
|
key_size=parameters.key_size,
|
104
|
-
backend=default_backend()
|
109
|
+
backend=default_backend(),
|
105
110
|
)
|
106
111
|
|
107
112
|
if parameters.password is None:
|
@@ -111,28 +116,31 @@ class MaleoFoundationKeyClientService(ClientService):
|
|
111
116
|
parameters.password.encode()
|
112
117
|
)
|
113
118
|
|
114
|
-
|
119
|
+
# * Serialize private key to PEM format
|
115
120
|
private_key_bytes = private_key.private_bytes(
|
116
121
|
encoding=serialization.Encoding.PEM,
|
117
122
|
format=serialization.PrivateFormat.PKCS8,
|
118
|
-
encryption_algorithm=encryption_algorithm
|
123
|
+
encryption_algorithm=encryption_algorithm,
|
119
124
|
)
|
120
125
|
private = MaleoFoundationKeyGeneralTransfers.PrivateKey(
|
121
126
|
value=private_key_bytes.decode()
|
122
|
-
)
|
127
|
+
) # type: ignore
|
123
128
|
|
124
|
-
public_key = private_key.public_key()
|
129
|
+
public_key = private_key.public_key() # * Create public key
|
125
130
|
|
126
|
-
|
131
|
+
# * Serialize public key to PEM format
|
127
132
|
public_key_bytes = public_key.public_bytes(
|
128
133
|
encoding=serialization.Encoding.PEM,
|
129
|
-
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
134
|
+
format=serialization.PublicFormat.SubjectPublicKeyInfo,
|
130
135
|
)
|
131
136
|
public = MaleoFoundationKeyGeneralTransfers.PublicKey(
|
132
137
|
value=public_key_bytes.decode()
|
133
|
-
)
|
138
|
+
) # type: ignore
|
134
139
|
|
135
140
|
self._logger.info("Successfully created key pair")
|
136
|
-
data = MaleoFoundationKeyGeneralTransfers.KeyPair(
|
137
|
-
|
138
|
-
|
141
|
+
data = MaleoFoundationKeyGeneralTransfers.KeyPair(
|
142
|
+
private=private, public=public
|
143
|
+
)
|
144
|
+
return MaleoFoundationKeyResultsTransfers.CreatePair(data=data) # type: ignore
|
145
|
+
|
146
|
+
return _impl()
|
@@ -2,97 +2,100 @@ from base64 import b64decode, b64encode
|
|
2
2
|
from Crypto.Hash import SHA256
|
3
3
|
from Crypto.Signature import pkcs1_15
|
4
4
|
from maleo_foundation.enums import BaseEnums
|
5
|
-
from maleo_foundation.expanded_types.signature
|
6
|
-
|
5
|
+
from maleo_foundation.expanded_types.signature import (
|
6
|
+
MaleoFoundationSignatureResultsTypes,
|
7
|
+
)
|
7
8
|
from maleo_foundation.managers.client.base import ClientService
|
8
|
-
from maleo_foundation.models.schemas.signature
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
from maleo_foundation.models.transfers.results.signature
|
13
|
-
|
9
|
+
from maleo_foundation.models.schemas.signature import MaleoFoundationSignatureSchemas
|
10
|
+
from maleo_foundation.models.transfers.parameters.signature import (
|
11
|
+
MaleoFoundationSignatureParametersTransfers,
|
12
|
+
)
|
13
|
+
from maleo_foundation.models.transfers.results.signature import (
|
14
|
+
MaleoFoundationSignatureResultsTransfers,
|
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 MaleoFoundationSignatureClientService(ClientService):
|
18
21
|
def sign(
|
19
|
-
self,
|
20
|
-
parameters: MaleoFoundationSignatureParametersTransfers.Sign
|
22
|
+
self, parameters: MaleoFoundationSignatureParametersTransfers.Sign
|
21
23
|
) -> MaleoFoundationSignatureResultsTypes.Sign:
|
22
24
|
@BaseExceptions.service_exception_handler(
|
23
25
|
operation="signing single message",
|
24
26
|
logger=self._logger,
|
25
|
-
fail_result_class=MaleoFoundationSignatureResultsTransfers.Fail
|
27
|
+
fail_result_class=MaleoFoundationSignatureResultsTransfers.Fail,
|
26
28
|
)
|
27
29
|
def _impl():
|
28
30
|
try:
|
29
31
|
private_key = RSAKeyLoader.load_with_pycryptodome(
|
30
32
|
type=BaseEnums.KeyType.PRIVATE,
|
31
33
|
extern_key=parameters.key,
|
32
|
-
passphrase=parameters.password
|
34
|
+
passphrase=parameters.password,
|
33
35
|
)
|
34
36
|
except TypeError:
|
35
37
|
message = "Invalid key type"
|
36
38
|
description = "A private key must be used for signing a message"
|
37
39
|
other = "Ensure the given key is of type private key"
|
38
40
|
return MaleoFoundationSignatureResultsTransfers.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 MaleoFoundationSignatureResultsTransfers.Fail(
|
49
|
-
message=message,
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
53
|
+
message=message, description=description, other=other
|
54
|
+
) # type: ignore
|
55
|
+
hash = SHA256.new(parameters.message.encode()) # * Generate message hash
|
56
|
+
signature = b64encode(
|
57
|
+
pkcs1_15.new(private_key).sign(hash)
|
58
|
+
).decode() # * Sign the hashed message
|
55
59
|
data = MaleoFoundationSignatureSchemas.Signature(signature=signature)
|
56
60
|
self._logger.info("Message successfully signed")
|
57
|
-
return MaleoFoundationSignatureResultsTransfers.Sign(data=data)
|
61
|
+
return MaleoFoundationSignatureResultsTransfers.Sign(data=data) # type: ignore
|
62
|
+
|
58
63
|
return _impl()
|
59
64
|
|
60
65
|
def verify(
|
61
|
-
self,
|
62
|
-
parameters: MaleoFoundationSignatureParametersTransfers.Verify
|
66
|
+
self, parameters: MaleoFoundationSignatureParametersTransfers.Verify
|
63
67
|
) -> MaleoFoundationSignatureResultsTypes.Verify:
|
64
68
|
@BaseExceptions.service_exception_handler(
|
65
69
|
operation="verify single signature",
|
66
70
|
logger=self._logger,
|
67
|
-
fail_result_class=MaleoFoundationSignatureResultsTransfers.Fail
|
71
|
+
fail_result_class=MaleoFoundationSignatureResultsTransfers.Fail,
|
68
72
|
)
|
69
73
|
def _impl():
|
70
74
|
try:
|
71
75
|
public_key = RSAKeyLoader.load_with_pycryptodome(
|
72
|
-
type=BaseEnums.KeyType.PUBLIC,
|
73
|
-
extern_key=parameters.key
|
76
|
+
type=BaseEnums.KeyType.PUBLIC, extern_key=parameters.key
|
74
77
|
)
|
75
78
|
except TypeError:
|
76
79
|
message = "Invalid key type"
|
77
80
|
description = "A public key must be used for verifying a signature"
|
78
81
|
other = "Ensure the given key is of type public key"
|
79
82
|
return MaleoFoundationSignatureResultsTransfers.Fail(
|
80
|
-
message=message,
|
81
|
-
|
82
|
-
other=other
|
83
|
-
)
|
83
|
+
message=message, description=description, other=other
|
84
|
+
) # type: ignore
|
84
85
|
except Exception as e:
|
85
|
-
self._logger.error(
|
86
|
+
self._logger.error(
|
87
|
+
"Unexpected error occured while trying to import key:\n'%s'",
|
88
|
+
str(e),
|
89
|
+
exc_info=True,
|
90
|
+
)
|
86
91
|
message = "Invalid key"
|
87
92
|
description = "Unexpected error occured while trying to import key"
|
88
93
|
other = "Ensure given key is valid"
|
89
94
|
return MaleoFoundationSignatureResultsTransfers.Fail(
|
90
|
-
message=message,
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
hash = SHA256.new(parameters.message.encode()) #* Generate message hash
|
95
|
-
#* Verify the hashed message and decoded signature
|
95
|
+
message=message, description=description, other=other
|
96
|
+
) # type: ignore
|
97
|
+
hash = SHA256.new(parameters.message.encode()) # * Generate message hash
|
98
|
+
# * Verify the hashed message and decoded signature
|
96
99
|
try:
|
97
100
|
pkcs1_15.new(public_key).verify(hash, b64decode(parameters.signature))
|
98
101
|
is_valid = True
|
@@ -100,5 +103,6 @@ class MaleoFoundationSignatureClientService(ClientService):
|
|
100
103
|
is_valid = False
|
101
104
|
data = MaleoFoundationSignatureSchemas.IsValid(is_valid=is_valid)
|
102
105
|
self._logger.info("Signature successfully verified")
|
103
|
-
return MaleoFoundationSignatureResultsTransfers.Verify(data=data)
|
104
|
-
|
106
|
+
return MaleoFoundationSignatureResultsTransfers.Verify(data=data) # type: ignore
|
107
|
+
|
108
|
+
return _impl()
|
@@ -1,114 +1,105 @@
|
|
1
1
|
import jwt
|
2
2
|
from maleo_foundation.enums import BaseEnums
|
3
|
-
from maleo_foundation.expanded_types.token
|
4
|
-
import MaleoFoundationTokenResultsTypes
|
3
|
+
from maleo_foundation.expanded_types.token import MaleoFoundationTokenResultsTypes
|
5
4
|
from maleo_foundation.managers.client.base import ClientService
|
6
|
-
from maleo_foundation.models.schemas.token
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
from maleo_foundation.models.transfers.parameters.token
|
11
|
-
|
12
|
-
|
13
|
-
|
5
|
+
from maleo_foundation.models.schemas.token import MaleoFoundationTokenSchemas
|
6
|
+
from maleo_foundation.models.transfers.general.token import (
|
7
|
+
MaleoFoundationTokenGeneralTransfers,
|
8
|
+
)
|
9
|
+
from maleo_foundation.models.transfers.parameters.token import (
|
10
|
+
MaleoFoundationTokenParametersTransfers,
|
11
|
+
)
|
12
|
+
from maleo_foundation.models.transfers.results.token import (
|
13
|
+
MaleoFoundationTokenResultsTransfers,
|
14
|
+
)
|
14
15
|
from maleo_foundation.utils.exceptions import BaseExceptions
|
15
16
|
from maleo_foundation.utils.loaders.key.rsa import RSAKeyLoader
|
16
17
|
|
18
|
+
|
17
19
|
class MaleoFoundationTokenClientService(ClientService):
|
18
20
|
def encode(
|
19
|
-
self,
|
20
|
-
parameters: MaleoFoundationTokenParametersTransfers.Encode
|
21
|
+
self, parameters: MaleoFoundationTokenParametersTransfers.Encode
|
21
22
|
) -> MaleoFoundationTokenResultsTypes.Encode:
|
22
23
|
@BaseExceptions.service_exception_handler(
|
23
24
|
operation="encoding a payload into a token",
|
24
25
|
logger=self._logger,
|
25
|
-
fail_result_class=MaleoFoundationTokenResultsTransfers.Fail
|
26
|
+
fail_result_class=MaleoFoundationTokenResultsTransfers.Fail,
|
26
27
|
)
|
27
28
|
def _impl():
|
28
29
|
try:
|
29
30
|
private_key = RSAKeyLoader.load_with_pycryptodome(
|
30
31
|
type=BaseEnums.KeyType.PRIVATE,
|
31
32
|
extern_key=parameters.key,
|
32
|
-
passphrase=parameters.password
|
33
|
+
passphrase=parameters.password,
|
33
34
|
)
|
34
35
|
except TypeError:
|
35
36
|
message = "Invalid key type"
|
36
37
|
description = "A private key must be used for payload encoding"
|
37
38
|
other = "Ensure the given key is of type private key"
|
38
39
|
return MaleoFoundationTokenResultsTransfers.Fail(
|
39
|
-
message=message,
|
40
|
-
|
41
|
-
other=other
|
42
|
-
)
|
40
|
+
message=message, description=description, other=other
|
41
|
+
) # type: ignore
|
43
42
|
except Exception as e:
|
44
|
-
self._logger.error(
|
43
|
+
self._logger.error(
|
44
|
+
"Unexpected error occured while trying to import key:\n'%s'",
|
45
|
+
str(e),
|
46
|
+
exc_info=True,
|
47
|
+
)
|
45
48
|
message = "Invalid key"
|
46
49
|
description = "Unexpected error occured while trying to import key"
|
47
50
|
other = "Ensure given key is valid"
|
48
51
|
return MaleoFoundationTokenResultsTransfers.Fail(
|
49
|
-
message=message,
|
50
|
-
|
51
|
-
|
52
|
-
)
|
53
|
-
|
54
|
-
MaleoFoundationTokenGeneralTransfers
|
55
|
-
.EncodePayload
|
56
|
-
.model_validate(
|
57
|
-
parameters.payload.model_dump()
|
58
|
-
)
|
59
|
-
.model_dump(
|
60
|
-
mode="json",
|
61
|
-
exclude_none=True
|
62
|
-
)
|
63
|
-
)
|
52
|
+
message=message, description=description, other=other
|
53
|
+
) # type: ignore
|
54
|
+
payload = MaleoFoundationTokenGeneralTransfers.EncodePayload.model_validate(
|
55
|
+
parameters.payload.model_dump()
|
56
|
+
).model_dump(mode="json", exclude_none=True)
|
64
57
|
token = jwt.encode(
|
65
|
-
payload=payload,
|
66
|
-
key=private_key.export_key(),
|
67
|
-
algorithm="RS256"
|
58
|
+
payload=payload, key=private_key.export_key(), algorithm="RS256"
|
68
59
|
)
|
69
60
|
data = MaleoFoundationTokenSchemas.Token(token=token)
|
70
|
-
return MaleoFoundationTokenResultsTransfers.Encode(data=data)
|
61
|
+
return MaleoFoundationTokenResultsTransfers.Encode(data=data) # type: ignore
|
62
|
+
|
71
63
|
return _impl()
|
72
64
|
|
73
65
|
def decode(
|
74
|
-
self,
|
75
|
-
parameters: MaleoFoundationTokenParametersTransfers.Decode
|
66
|
+
self, parameters: MaleoFoundationTokenParametersTransfers.Decode
|
76
67
|
) -> MaleoFoundationTokenResultsTypes.Decode:
|
77
68
|
@BaseExceptions.service_exception_handler(
|
78
69
|
operation="decoding a token into a payload",
|
79
70
|
logger=self._logger,
|
80
|
-
fail_result_class=MaleoFoundationTokenResultsTransfers.Fail
|
71
|
+
fail_result_class=MaleoFoundationTokenResultsTransfers.Fail,
|
81
72
|
)
|
82
73
|
def _impl():
|
83
74
|
try:
|
84
75
|
public_key = RSAKeyLoader.load_with_pycryptodome(
|
85
|
-
type=BaseEnums.KeyType.PUBLIC,
|
86
|
-
extern_key=parameters.key
|
76
|
+
type=BaseEnums.KeyType.PUBLIC, extern_key=parameters.key
|
87
77
|
)
|
88
78
|
except TypeError:
|
89
79
|
message = "Invalid key type"
|
90
80
|
description = "A public key must be used for token decoding"
|
91
81
|
other = "Ensure the given key is of type public key"
|
92
82
|
return MaleoFoundationTokenResultsTransfers.Fail(
|
93
|
-
message=message,
|
94
|
-
|
95
|
-
other=other
|
96
|
-
)
|
83
|
+
message=message, description=description, other=other
|
84
|
+
) # type: ignore
|
97
85
|
except Exception as e:
|
98
|
-
self._logger.error(
|
86
|
+
self._logger.error(
|
87
|
+
"Unexpected error occured while trying to import key:\n'%s'",
|
88
|
+
str(e),
|
89
|
+
exc_info=True,
|
90
|
+
)
|
99
91
|
message = "Invalid key"
|
100
92
|
description = "Unexpected error occured while trying to import key"
|
101
93
|
other = "Ensure given key is valid"
|
102
94
|
return MaleoFoundationTokenResultsTransfers.Fail(
|
103
|
-
message=message,
|
104
|
-
|
105
|
-
other=other
|
106
|
-
)
|
95
|
+
message=message, description=description, other=other
|
96
|
+
) # type: ignore
|
107
97
|
payload = jwt.decode(
|
108
|
-
jwt=parameters.token,
|
109
|
-
key=public_key.export_key(),
|
110
|
-
algorithms=["RS256"]
|
98
|
+
jwt=parameters.token, key=public_key.export_key(), algorithms=["RS256"]
|
111
99
|
)
|
112
|
-
data = MaleoFoundationTokenGeneralTransfers.DecodePayload.model_validate(
|
113
|
-
|
114
|
-
|
100
|
+
data = MaleoFoundationTokenGeneralTransfers.DecodePayload.model_validate(
|
101
|
+
payload
|
102
|
+
)
|
103
|
+
return MaleoFoundationTokenResultsTransfers.Decode(data=data) # type: ignore
|
104
|
+
|
105
|
+
return _impl()
|