Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
cake-bot
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Oleksandr Barabash
cake-bot
Commits
e16141bc
Commit
e16141bc
authored
Oct 25, 2022
by
Oleksandr Barabash
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
signature updated
parent
14a91291
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
15 deletions
+26
-15
azure_key_vault_client.py
utils/azure_key_vault_client.py
+14
-1
token_helper.py
utils/token_helper.py
+12
-14
No files found.
utils/azure_key_vault_client.py
View file @
e16141bc
...
@@ -8,7 +8,8 @@ from typing import Awaitable
...
@@ -8,7 +8,8 @@ from typing import Awaitable
# noinspection PyPackageRequirements
# noinspection PyPackageRequirements
from
azure.keyvault.keys
import
KeyClient
,
KeyVaultKey
from
azure.keyvault.keys
import
KeyClient
,
KeyVaultKey
# noinspection PyPackageRequirements
# noinspection PyPackageRequirements
from
azure.keyvault.keys.crypto
import
CryptographyClient
,
EncryptionAlgorithm
from
azure.keyvault.keys.crypto
import
CryptographyClient
,
EncryptionAlgorithm
,
\
SignatureAlgorithm
# noinspection PyPackageRequirements
# noinspection PyPackageRequirements
from
azure.keyvault.secrets
import
SecretClient
,
KeyVaultSecret
from
azure.keyvault.secrets
import
SecretClient
,
KeyVaultSecret
# noinspection PyPackageRequirements
# noinspection PyPackageRequirements
...
@@ -103,6 +104,18 @@ class AzureKeyVaultClient:
...
@@ -103,6 +104,18 @@ class AzureKeyVaultClient:
result
=
cipher
.
encrypt
(
EncryptionAlgorithm
.
rsa_oaep
,
data
)
result
=
cipher
.
encrypt
(
EncryptionAlgorithm
.
rsa_oaep
,
data
)
return
result
.
ciphertext
return
result
.
ciphertext
def
sign_bl
(
self
,
key
:
KeyVaultKey
,
algorithm
:
SignatureAlgorithm
,
data
:
bytes
)
->
bytes
:
""" Sign data, blocking """
cipher
=
CryptographyClient
(
key
,
self
.
credential
)
return
cipher
.
sign
(
algorithm
,
data
)
.
signature
def
verify_bl
(
self
,
key
:
KeyVaultKey
,
algorithm
:
SignatureAlgorithm
,
digest
:
bytes
,
signature
:
bytes
)
->
bool
:
""" Verify signature, blocking """
cipher
=
CryptographyClient
(
key
,
self
.
credential
)
return
cipher
.
verify
(
algorithm
,
digest
,
signature
)
.
is_valid
def
decrypt_bl
(
self
,
key
:
KeyVaultKey
,
data
:
bytes
)
->
bytes
:
def
decrypt_bl
(
self
,
key
:
KeyVaultKey
,
data
:
bytes
)
->
bytes
:
""" Decrypt data """
""" Decrypt data """
cipher
=
CryptographyClient
(
key
,
self
.
credential
)
cipher
=
CryptographyClient
(
key
,
self
.
credential
)
...
...
utils/token_helper.py
View file @
e16141bc
...
@@ -13,7 +13,7 @@ from azure.core.exceptions import ResourceNotFoundError, HttpResponseError
...
@@ -13,7 +13,7 @@ from azure.core.exceptions import ResourceNotFoundError, HttpResponseError
from
entities.json.admin_user
import
AdminUser
from
entities.json.admin_user
import
AdminUser
from
utils.azure_key_vault_client
import
AzureKeyVaultClient
from
utils.azure_key_vault_client
import
AzureKeyVaultClient
from
utils.functions
import
b64encode_str
,
b64encode_np
,
parse_auth_header
,
\
from
utils.functions
import
b64encode_str
,
b64encode_np
,
parse_auth_header
,
\
b64decode_str
b64decode_str
,
b64decode_np
from
utils.json_func
import
json_dumps
,
json_loads
from
utils.json_func
import
json_dumps
,
json_loads
from
utils.log
import
Log
from
utils.log
import
Log
...
@@ -39,9 +39,9 @@ class TokenHelper:
...
@@ -39,9 +39,9 @@ class TokenHelper:
token_unsigned
=
"{}.{}"
.
format
(
b64encode_str
(
json_dumps
(
header
)),
token_unsigned
=
"{}.{}"
.
format
(
b64encode_str
(
json_dumps
(
header
)),
b64encode_str
(
json_dumps
(
body
)))
b64encode_str
(
json_dumps
(
body
)))
signature
=
SHA256
.
new
(
token_unsigned
.
encode
(
"utf-8"
))
.
digest
()
signature
=
self
.
azure_kv
.
sign_bl
(
key
,
Auth
.
Algorithms
.
RS256
,
signature_encrypted
=
self
.
azure_kv
.
encrypt_bl
(
key
,
signature
)
token_unsigned
.
encode
(
"utf-8"
)
)
signature_b64
=
b64encode_np
(
signature
_encrypted
)
.
decode
(
"utf-8"
)
signature_b64
=
b64encode_np
(
signature
)
.
decode
(
"utf-8"
)
return
"{}.{}"
.
format
(
token_unsigned
,
signature_b64
)
return
"{}.{}"
.
format
(
token_unsigned
,
signature_b64
)
elif
alg
==
Auth
.
Algorithms
.
HS256
:
elif
alg
==
Auth
.
Algorithms
.
HS256
:
""" HMAC with SHA-256 (HS256) """
""" HMAC with SHA-256 (HS256) """
...
@@ -86,12 +86,12 @@ class TokenHelper:
...
@@ -86,12 +86,12 @@ class TokenHelper:
Log
.
d
(
__name__
,
"is_token_valid"
)
Log
.
d
(
__name__
,
"is_token_valid"
)
# split first
# split first
header_b64_str
,
body_b64_str
,
signature
=
token
.
split
(
"."
)
header_b64_str
,
body_b64_str
,
signature
_b64_str
=
token
.
split
(
"."
)
token_unsigned
=
"{}.{}"
.
format
(
header_b64_str
,
body_b64_str
)
token_unsigned
=
"{}.{}"
.
format
(
header_b64_str
,
body_b64_str
)
Log
.
d
(
__name__
,
"header_b64_str, body_b64_str, signature:"
Log
.
d
(
__name__
,
"header_b64_str, body_b64_str, signature:"
"{}, {}, {}"
.
format
(
header_b64_str
,
body_b64_str
,
"{}, {}, {}"
.
format
(
header_b64_str
,
body_b64_str
,
signature
))
signature
_b64_str
))
# parse
# parse
header
=
json_loads
(
b64decode_str
(
header_b64_str
))
header
=
json_loads
(
b64decode_str
(
header_b64_str
))
...
@@ -142,14 +142,12 @@ class TokenHelper:
...
@@ -142,14 +142,12 @@ class TokenHelper:
Log
.
e
(
__name__
,
"Key not found: '{}'"
.
format
(
token_kid
))
Log
.
e
(
__name__
,
"Key not found: '{}'"
.
format
(
token_kid
))
return
False
return
False
signature_gen
=
SHA256
.
new
(
token_unsigned
.
encode
(
"utf-8"
))
.
digest
()
signature
=
b64decode_np
(
signature_b64_str
.
encode
(
"utf-8"
))
signature_encrypted
=
self
.
azure_kv
.
encrypt_bl
(
key
,
signature_gen
)
is_valid
=
self
.
azure_kv
.
verify_bl
(
key
,
Auth
.
ALGORITHM
,
signature_gen
=
b64encode_np
(
signature_encrypted
)
.
decode
(
"utf-8"
)
token_unsigned
.
encode
(
"utf-8"
),
signature
)
Log
.
d
(
__name__
,
f
"is equal: '{signature == signature_gen}'"
)
Log
.
d
(
__name__
,
f
"is_valid: {is_valid}"
)
Log
.
d
(
__name__
,
f
"signature_gen: '{signature_gen}'"
)
if
is_valid
:
Log
.
d
(
__name__
,
f
"signature: '{signature}'"
)
if
signature
==
signature_gen
:
return
True
return
True
return
False
return
False
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment