-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor req_ds_cnf to delegation_scope_key Move logic to allow token cache to work wip
- Loading branch information
Showing
6 changed files
with
220 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from base64 import urlsafe_b64encode | ||
|
||
from cryptography.hazmat.primitives.asymmetric import rsa | ||
|
||
|
||
def _urlsafe_b64encode(n:int, bit_size:int) -> str: | ||
return urlsafe_b64encode(n.to_bytes(length=int(bit_size/8))).decode("utf-8") | ||
|
||
|
||
def _to_jwk(public_key: rsa.RSAPublicKey) -> dict: | ||
numbers = public_key.public_numbers() | ||
return { | ||
"kty": "RSA", | ||
"n": _urlsafe_b64encode(numbers.n, public_key.key_size), | ||
"e": _urlsafe_b64encode(numbers.e, 24), # TODO: TBD. PyJWT/jwt/algorithms.py RSAAlgorithm.to_jwk() | ||
} | ||
|
||
def _convert_rsa_keys(private_key: rsa.RSAPrivateKey): | ||
return "pairs.private_bytes()", _to_jwk(private_key.public_key()) | ||
|
||
def _generate_rsa_key() -> rsa.RSAPrivateKey: | ||
return rsa.generate_private_key(public_exponent=65537, key_size=2048) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from unittest import TestCase | ||
|
||
from msal.crypto import _generate_rsa_key, _convert_rsa_keys | ||
|
||
|
||
class CryptoTestCase(TestCase): | ||
def test_key_generation(self): | ||
key = _generate_rsa_key() | ||
_, jwk = _convert_rsa_keys(key) | ||
self.assertEqual(jwk.get("kty"), "RSA") | ||
self.assertIsNotNone(jwk.get("n") and jwk.get("e")) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters