This repository has been archived by the owner on Jan 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 485
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
receiver receiverid challenge and find corresponding key
- Loading branch information
Showing
3 changed files
with
85 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import re | ||
import logging | ||
from owrx.config import Config | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
keyRegex = re.compile("^([a-zA-Z]+)-([0-9a-f]{32})-([0-9a-f]{64})$") | ||
keyChallengeRegex = re.compile("^([a-zA-Z]+)-([0-9a-f]{32})-([0-9a-f]{32})$") | ||
headerRegex = re.compile("^ReceiverId (.*)$") | ||
|
||
|
||
class KeyException(Exception): | ||
pass | ||
|
||
|
||
class Key(object): | ||
def __init__(self, keyString): | ||
matches = keyRegex.match(keyString) | ||
if not matches: | ||
raise KeyException("invalid key format") | ||
self.source = matches.group(1) | ||
self.id = matches.group(2) | ||
self.secret = matches.group(3) | ||
|
||
|
||
class KeyChallenge(object): | ||
def __init__(self, challengeString): | ||
matches = keyChallengeRegex.match(challengeString) | ||
if not matches: | ||
raise KeyException("invalid key challenge format") | ||
self.source = matches.group(1) | ||
self.id = matches.group(2) | ||
self.challenge = matches.group(3) | ||
|
||
|
||
class KeyResponse(object): | ||
def __str__(self): | ||
return "TODO" | ||
|
||
|
||
class ReceiverId(object): | ||
@staticmethod | ||
def getResponseHeader(requestHeader): | ||
matches = headerRegex.match(requestHeader) | ||
if not matches: | ||
raise KeyException("invalid authorization header") | ||
challenge = KeyChallenge(matches.group(1)) | ||
key = ReceiverId.findKey(challenge) | ||
# TODO sign challenge and respond | ||
|
||
@staticmethod | ||
def findKey(challenge): | ||
def parseKey(keyString): | ||
try: | ||
return Key(keyString) | ||
except KeyError as e: | ||
logger.error(e) | ||
keys = [key for key in (parseKey(keyString) for keyString in Config.get()['receiver_keys']) if key is not None] | ||
matching_keys = [key for key in keys if key.source == challenge.source and key.id == challenge.id] | ||
if matching_keys: | ||
return matching_keys[0] | ||
return None |