Skip to content

Commit

Permalink
Add to_jwk to Ed25519Algorithm. (#642)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesWill committed Apr 6, 2021
1 parent fc7a708 commit bdb0765
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions jwt/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
rsa_recover_prime_factors,
)
from cryptography.hazmat.primitives.serialization import (
Encoding,
NoEncryption,
PrivateFormat,
PublicFormat,
load_pem_private_key,
load_pem_public_key,
load_ssh_public_key,
Expand Down Expand Up @@ -587,6 +591,47 @@ def verify(self, msg, key, sig):
except cryptography.exceptions.InvalidSignature:
return False

@staticmethod
def to_jwk(key):
if isinstance(key, Ed25519PublicKey):
x = key.public_bytes(
encoding=Encoding.Raw,
format=PublicFormat.Raw,
)

json.dumps(
{
"x": base64url_encode(force_bytes(x)).decode(),
"kty": "OKP",
"alg": "EdDSA",
"crv": "Ed25519",
}
)

if isinstance(key, Ed25519PrivateKey):
d = key.private_bytes(
encoding=Encoding.Raw,
format=PrivateFormat.Raw,
encryption_algorithm=NoEncryption(),
)

x = key.public_key().public_bytes(
encoding=Encoding.Raw,
format=PublicFormat.Raw,
)

return json.dumps(
{
"x": base64url_encode(force_bytes(x)).decode(),
"d": base64url_encode(force_bytes(d)).decode(),
"kty": "OKP",
"alg": "EdDSA",
"crv": "Ed25519",
}
)

raise InvalidKeyError("Not a public or private key")

@staticmethod
def from_jwk(jwk):
try:
Expand Down

0 comments on commit bdb0765

Please sign in to comment.