-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
137 additions
and
117 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
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,34 @@ | ||
from cryptography.x509 import load_pem_x509_certificate | ||
from cryptography.hazmat.primitives.serialization import ( | ||
load_pem_private_key, load_pem_public_key, load_ssh_public_key, | ||
) | ||
from cryptography.hazmat.backends import default_backend | ||
from authlib.common.encoding import to_bytes | ||
|
||
|
||
def load_pem_key(raw, ssh_type=None, key_type=None, password=None): | ||
raw = to_bytes(raw) | ||
|
||
if ssh_type and raw.startswith(ssh_type): | ||
return load_ssh_public_key(raw, backend=default_backend()) | ||
|
||
if key_type == 'public': | ||
return load_pem_public_key(raw, backend=default_backend()) | ||
|
||
if key_type == 'private' or password is not None: | ||
return load_pem_private_key(raw, password=password, backend=default_backend()) | ||
|
||
if b'PUBLIC' in raw: | ||
return load_pem_public_key(raw, backend=default_backend()) | ||
|
||
if b'PRIVATE' in raw: | ||
return load_pem_private_key(raw, password=password, backend=default_backend()) | ||
|
||
if b'CERTIFICATE' in raw: | ||
cert = load_pem_x509_certificate(raw, default_backend()) | ||
return cert.public_key() | ||
|
||
try: | ||
return load_pem_private_key(raw, password=password, backend=default_backend()) | ||
except ValueError: | ||
return load_pem_public_key(raw, backend=default_backend()) |
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,63 @@ | ||
from authlib.common.encoding import text_types, json_loads | ||
from ._cryptography_key import load_pem_key | ||
from .models import KeySet | ||
|
||
|
||
class JsonWebKey(object): | ||
JWK_KEY_CLS = {} | ||
|
||
@classmethod | ||
def generate_key(cls, kty, crv_or_size, options=None, is_private=False): | ||
"""Generate a Key with the given key type, curve name or bit size. | ||
:param kty: string of ``oct``, ``RSA``, ``EC``, ``OKP`` | ||
:param crv_or_size: curve name or bit size | ||
:param options: a dict of other options for Key | ||
:param is_private: create a private key or public key | ||
:return: Key instance | ||
""" | ||
key_cls = cls.JWK_KEY_CLS[kty] | ||
return key_cls.generate_key(crv_or_size, options, is_private) | ||
|
||
@classmethod | ||
def import_key(cls, raw, options=None): | ||
"""Import a Key from bytes, string, PEM or dict. | ||
:return: Key instance | ||
""" | ||
kty = None | ||
if options is not None: | ||
kty = options.get('kty') | ||
|
||
if kty is None and isinstance(raw, dict): | ||
kty = raw.get('kty') | ||
|
||
if kty is None: | ||
raw_key = load_pem_key(raw) | ||
for _kty in cls.JWK_KEY_CLS: | ||
key_cls = cls.JWK_KEY_CLS[_kty] | ||
if isinstance(raw_key, key_cls.RAW_KEY_CLS): | ||
return key_cls.import_key(raw_key, options) | ||
|
||
key_cls = cls.JWK_KEY_CLS[kty] | ||
return key_cls.import_key(raw, options) | ||
|
||
@classmethod | ||
def import_key_set(cls, raw): | ||
"""Import KeySet from string, dict or a list of keys. | ||
:return: KeySet instance | ||
""" | ||
raw = _transform_raw_key(raw) | ||
if isinstance(raw, dict) and 'keys' in raw: | ||
keys = raw.get('keys') | ||
return KeySet([cls.import_key(k) for k in keys]) | ||
|
||
|
||
def _transform_raw_key(raw): | ||
if isinstance(raw, text_types) and \ | ||
raw.startswith('{') and raw.endswith('}'): | ||
return json_loads(raw) | ||
elif isinstance(raw, (tuple, list)): | ||
return {'keys': raw} | ||
return raw |
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