1
1
"""JSON Web Key."""
2
2
import abc
3
- import base64
4
3
import json
5
4
import logging
6
5
import math
@@ -407,38 +406,45 @@ class JWKOKP(JWK):
407
406
)
408
407
required = ('crv' , JWK .type_field_name , 'x' )
409
408
410
- def __init__ (self , * args , ** kwargs ) -> None :
409
+ def __init__ (self , * args , ** kwargs ):
411
410
if 'key' in kwargs and not isinstance (kwargs ['key' ], util .ComparableOKPKey ):
412
411
kwargs ['key' ] = util .ComparableOKPKey (kwargs ['key' ])
413
412
super ().__init__ (* args , ** kwargs )
414
413
415
- def public_key (self ) -> Union [
416
- ed25519 .Ed25519PublicKey , ed448 .Ed448PublicKey ,
417
- x25519 .X25519PublicKey , x448 .X448PublicKey ,
418
- ]:
419
- return self ._wrapped .__class__ .public_key ()
414
+ def public_key (self ):
415
+ return self .key ._wrapped .__class__ .public_key ()
416
+
417
+ def _key_to_crv (self ):
418
+ if isinstance (self .key ._wrapped , (ed25519 .Ed25519PrivateKey , ed25519 .Ed25519PrivateKey )):
419
+ return "Ed25519"
420
+ elif isinstance (self .key ._wrapped , (ed448 .Ed448PrivateKey , ed448 .Ed448PrivateKey )):
421
+ return "Ed448"
422
+ elif isinstance (self .key ._wrapped , (x25519 .X25519PrivateKey , x25519 .X25519PrivateKey )):
423
+ return "X25519"
424
+ elif isinstance (self .key ._wrapped , (x448 .X448PrivateKey , x448 .X448PrivateKey )):
425
+ return "X448"
426
+ return NotImplemented
420
427
421
428
def fields_to_partial_json (self ) -> Dict :
422
- params = {} # type: Dict
429
+ params = {}
430
+ print (dir (self ))
423
431
if self .key .is_private ():
424
- params ['d' ] = base64 . b64encode (self .key .private_bytes (
432
+ params ['d' ] = json_util . encode_b64jose (self .key .private_bytes (
425
433
encoding = serialization .Encoding .PEM ,
426
434
format = serialization .PrivateFormat .PKCS8 ,
427
435
encryption_algorithm = serialization .NoEncryption ()
428
436
))
429
437
params ['x' ] = self .key .public_key ().public_bytes (
430
438
encoding = serialization .Encoding .PEM ,
431
- format = serialization .PublicFormat .PKCS8 ,
432
- encryption_algorithm = serialization .NoEncryption ()
439
+ format = serialization .PublicFormat .SubjectPublicKeyInfo ,
433
440
)
434
441
else :
435
- params ['x' ] = base64 . b64decode (self .key .public_bytes (
442
+ params ['x' ] = json_util . encode_b64jose (self .key .public_bytes (
436
443
serialization .Encoding .Raw ,
437
444
serialization .PublicFormat .Raw ,
438
445
serialization .NoEncryption (),
439
446
))
440
- # TODO find a better way to get the curve name
441
- params ['crv' ] = 'ed25519'
447
+ params ['crv' ] = self ._key_to_crv ()
442
448
return params
443
449
444
450
@classmethod
@@ -463,12 +469,12 @@ def fields_from_json(cls, jobj) -> ComparableOKPKey:
463
469
464
470
if "x" not in obj :
465
471
raise errors .DeserializationError ('OKP should have "x" parameter' )
466
- x = base64 . b64decode (jobj .get ("x" ))
472
+ x = json_util . decode_b64jose (jobj .get ("x" ))
467
473
468
474
try :
469
475
if "d" not in obj :
470
476
return jobj ["key" ]._wrapped .__class__ .from_public_bytes (x ) # noqa
471
- d = base64 . b64decode (obj .get ("d" ))
477
+ d = json_util . decode_b64jose (obj .get ("d" ))
472
478
return jobj ["key" ]._wrapped .__class__ .from_private_bytes (d ) # noqa
473
479
except ValueError as err :
474
480
raise errors .DeserializationError ("Invalid key parameter" ) from err
0 commit comments