Skip to content

Commit

Permalink
Key modules (#1544)
Browse files Browse the repository at this point in the history
* add type hints and docs
* more key interfaces (wamp-cryptosign)
  • Loading branch information
oberstet committed Apr 11, 2022
1 parent 8d4d788 commit ceefbd8
Show file tree
Hide file tree
Showing 5 changed files with 451 additions and 47 deletions.
2 changes: 1 addition & 1 deletion autobahn/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
#
###############################################################################

__version__ = '22.4.1.dev3'
__version__ = '22.4.1.dev4'

__build__ = '00000000-0000000'
27 changes: 14 additions & 13 deletions autobahn/wamp/cryptosign.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@

import binascii
import struct
from typing import Callable
from typing import Callable, Optional, Union

import txaio

from autobahn import util
from autobahn.wamp.interfaces import ISigningKey
from autobahn.wamp.interfaces import IEd25519Key, ISession
from autobahn.wamp.types import Challenge

__all__ = [
Expand Down Expand Up @@ -400,7 +400,7 @@ def format_challenge(challenge: Challenge, channel_id_raw: bytes, channel_id_typ

return data

def sign_challenge(data: bytes, signer_func: Callable):
def sign_challenge(data: bytes, signer_func: Callable) -> bytes:
"""
Sign the provided data using the provided signer.
Expand Down Expand Up @@ -447,7 +447,7 @@ def can_sign(self):
return self._can_sign

@util.public
def sign_challenge(self, session, challenge, channel_id_type='tls-unique'):
def sign_challenge(self, session: ISession, challenge: Challenge, channel_id_type: str = 'tls-unique') -> bytes:
"""
Sign WAMP-cryptosign challenge.
Expand All @@ -461,13 +461,13 @@ def sign_challenge(self, session, challenge, channel_id_type='tls-unique'):
:rtype: str
"""
# get the TLS channel ID of the underlying TLS connection. Could be None.
channel_id_raw = session._transport.get_channel_id()
channel_id_raw = session._transport.get_channel_id(channel_id_type)
data = format_challenge(challenge, channel_id_raw, channel_id_type)

return sign_challenge(data, self.sign)

@util.public
def sign(self, data):
def sign(self, data: bytes) -> bytes:
"""
Sign some data.
Expand All @@ -487,7 +487,7 @@ def sign(self, data):
sig = self._key.sign(data)

# we only return the actual signature! if we return "sig",
# it get coerced into the concatenation of message + signature
# it gets coerced into the concatenation of message + signature
# not sure which order, but we don't want that. we only want
# the signature
return txaio.create_future_success(sig.signature)
Expand Down Expand Up @@ -532,7 +532,7 @@ def comment(self):
return self._comment

@util.public
def public_key(self, binary=False):
def public_key(self, binary: bool = False) -> Union[str, bytes]:
"""
Returns the public key part of a signing key or the (public) verification key.
Expand All @@ -551,7 +551,7 @@ def public_key(self, binary=False):

@util.public
@classmethod
def from_key_bytes(cls, keydata, comment=None):
def from_key_bytes(cls, keydata: str, comment: Optional[str] = None) -> 'SigningKey':
if not (comment is None or type(comment) == str):
raise ValueError("invalid type {} for comment".format(type(comment)))

Expand All @@ -564,8 +564,9 @@ def from_key_bytes(cls, keydata, comment=None):
key = signing.SigningKey(keydata)
return cls(key, comment)

@util.public
@classmethod
def from_raw_key(cls, filename, comment=None):
def from_raw_key(cls, filename: str, comment: Optional[str] = None) -> 'SigningKey':
"""
Load an Ed25519 (private) signing key (actually, the seed for the key) from a raw file of 32 bytes length.
This can be any random byte sequence, such as generated from Python code like
Expand Down Expand Up @@ -594,7 +595,7 @@ def from_raw_key(cls, filename, comment=None):

@util.public
@classmethod
def from_ssh_key(cls, filename):
def from_ssh_key(cls, filename: str) -> 'SigningKey':
"""
Load an Ed25519 key from a SSH key file. The key file can be a (private) signing
key (from a SSH private key file) or a (public) verification key (from a SSH
Expand All @@ -607,7 +608,7 @@ def from_ssh_key(cls, filename):

@util.public
@classmethod
def from_ssh_data(cls, keydata):
def from_ssh_data(cls, keydata: str) -> 'SigningKey':
"""
Load an Ed25519 key from SSH key file. The key file can be a (private) signing
key (from a SSH private key file) or a (public) verification key (from a SSH
Expand All @@ -625,7 +626,7 @@ def from_ssh_data(cls, keydata):

return cls(key, comment)

ISigningKey.register(SigningKey)
IEd25519Key.register(SigningKey)


if __name__ == '__main__':
Expand Down

0 comments on commit ceefbd8

Please sign in to comment.