lists.arthurdejong.org
RSS feed

python-pskc branch master updated. 0.1-42-g50b429d

[Date Prev][Date Next] [Thread Prev][Thread Next]

python-pskc branch master updated. 0.1-42-g50b429d



This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "python-pskc".

The branch, master has been updated
       via  50b429d5f8698373dd8c0b0dbe554d4b98f1487c (commit)
       via  9a16ce4d853a0c72e00aaf62d9ef8d1e36072574 (commit)
      from  1b9ee9f51496ccb10094f9d5844589948a887a08 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
http://arthurdejong.org/git/python-pskc/commit/?id=50b429d5f8698373dd8c0b0dbe554d4b98f1487c

commit 50b429d5f8698373dd8c0b0dbe554d4b98f1487c
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Sun Jun 15 14:53:12 2014 +0200

    Refactor out some functions to parse
    
    This introduces the getint() and getbool() functions in parse to avoid
    some code duplication.

diff --git a/pskc/key.py b/pskc/key.py
index 244cc50..1eabdb1 100644
--- a/pskc/key.py
+++ b/pskc/key.py
@@ -197,7 +197,7 @@ class Key(object):
 
     def parse(self, key_package):
         """Read key information from the provided <KeyPackage> tree."""
-        from pskc.parse import find, findtext, findtime
+        from pskc.parse import find, findtext, findtime, getint, getbool
         if key_package is None:
             return
 
@@ -248,27 +248,17 @@ class Key(object):
             'pskc:Key/pskc:AlgorithmParameters/pskc:ChallengeFormat')
         if challenge_format is not None:
             self.challenge_encoding = challenge_format.get('Encoding')
-            value = challenge_format.get('Min')
-            if value:
-                self.challenge_min_length = int(value)
-            value = challenge_format.get('Max')
-            if value:
-                self.challenge_max_length = int(value)
-            value = challenge_format.get('CheckDigits')
-            if value:
-                self.challenge_check = value.lower() == 'true'
+            self.challenge_min_length = getint(challenge_format, 'Min')
+            self.challenge_max_length = getint(challenge_format, 'Max')
+            self.challenge_check = getbool(challenge_format, 'CheckDigits')
 
         response_format = find(
             key_package,
             'pskc:Key/pskc:AlgorithmParameters/pskc:ResponseFormat')
         if response_format is not None:
             self.response_encoding = response_format.get('Encoding')
-            value = response_format.get('Length')
-            if value:
-                self.response_length = int(value)
-            value = response_format.get('CheckDigits')
-            if value:
-                self.response_check = value.lower() == 'true'
+            self.response_length = getint(response_format, 'Length')
+            self.response_check = getbool(response_format, 'CheckDigits')
 
         self.policy.parse(find(key_package, 'pskc:Key/pskc:Policy'))
 
diff --git a/pskc/parse.py b/pskc/parse.py
index b35b507..4f3a7ec 100644
--- a/pskc/parse.py
+++ b/pskc/parse.py
@@ -95,3 +95,17 @@ def findbin(tree, match):
     if value:
         import base64
         return base64.b64decode(value)
+
+
+def getint(tree, attribute):
+    """Return an attribute value as an integer (or None)."""
+    value = tree.get(attribute)
+    if value:
+        return int(value)
+
+
+def getbool(tree, attribute):
+    """Return an attribute value as a boolean (or None)."""
+    value = tree.get(attribute)
+    if value:
+        return value.lower() == 'true'
diff --git a/pskc/policy.py b/pskc/policy.py
index a93d94b..ee0095e 100644
--- a/pskc/policy.py
+++ b/pskc/policy.py
@@ -109,7 +109,8 @@ class Policy(object):
 
     def parse(self, policy):
         """Read key policy information from the provided <Policy> tree."""
-        from pskc.parse import find, findall, findtext, findint, findtime
+        from pskc.parse import (
+            find, findall, findtext, findint, findtime, getint)
         if policy is None:
             return
 
@@ -124,15 +125,10 @@ class Policy(object):
         if pin_policy is not None:
             self.pin_key_id = pin_policy.get('PINKeyId')
             self.pin_usage = pin_policy.get('PINUsageMode')
-            value = pin_policy.get('MaxFailedAttempts')
-            if value:
-                self.pin_max_failed_attemtps = int(value)
-            value = pin_policy.get('MinLength')
-            if value:
-                self.pin_min_length = int(value)
-            value = pin_policy.get('MaxLength')
-            if value:
-                self.pin_max_length = int(value)
+            self.pin_max_failed_attemtps = getint(
+                pin_policy, 'MaxFailedAttempts')
+            self.pin_min_length = getint(pin_policy, 'MinLength')
+            self.pin_max_length = getint(pin_policy, 'MaxLength')
             self.pin_encoding = pin_policy.get('PINEncoding')
             # TODO: check if there are any other attributes set for PINPolicy
             # of if there are any children and set unknown_policy_elementss

http://arthurdejong.org/git/python-pskc/commit/?id=9a16ce4d853a0c72e00aaf62d9ef8d1e36072574

commit 9a16ce4d853a0c72e00aaf62d9ef8d1e36072574
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Sun Jun 15 14:17:19 2014 +0200

    Add support for setting secret
    
    This supports setters for the secret, counter, time_offset,
    time_interval and time_drift properties. Setting these values stores the
    values unencrypted internally.

diff --git a/pskc/key.py b/pskc/key.py
index f1414a6..244cc50 100644
--- a/pskc/key.py
+++ b/pskc/key.py
@@ -58,6 +58,11 @@ class DataType(object):
         self.encrypted_value.parse(find(element, 'pskc:EncryptedValue'))
         self.value_mac.parse(find(element, 'pskc:ValueMAC'))
 
+    def set_value(self, value):
+        """Save the plain value."""
+        self.plain_value = self.to_plain(value)
+        self.encrypted_value.cipher_value = None
+
     def check(self):
         """Check whether the embedded MAC is correct."""
         # this checks the encrypted value
@@ -77,6 +82,11 @@ class BinaryDataType(DataType):
         # encrypted value is in correct format
         return self.encrypted_value.decrypt()
 
+    def to_plain(self, value):
+        """Convert the value to an unencrypted string representation."""
+        if value:
+            return base64.b64encode(value)
+
 
 class IntegerDataType(DataType):
     """Subclass of DataType for integer types (e.g. counters)."""
@@ -97,6 +107,11 @@ class IntegerDataType(DataType):
                 v = (v << 8) + ord(x)
             return v
 
+    def to_plain(self, value):
+        """Convert the value to an unencrypted string representation."""
+        if value not in (None, ''):
+            return str(value)
+
 
 class Key(object):
     """Representation of a single key from a PSKC file.
@@ -257,30 +272,30 @@ class Key(object):
 
         self.policy.parse(find(key_package, 'pskc:Key/pskc:Policy'))
 
-    @property
-    def secret(self):
-        """The secret key itself."""
-        return self._secret.get_value()
-
-    @property
-    def counter(self):
-        """An event counter for event-based OTP."""
-        return self._counter.get_value()
-
-    @property
-    def time_offset(self):
-        """A time offset for time-based OTP (number of intervals)."""
-        return self._time_offset.get_value()
-
-    @property
-    def time_interval(self):
-        """A time interval in seconds."""
-        return self._time_interval.get_value()
-
-    @property
-    def time_drift(self):
-        """Device clock drift value (number of time intervals)."""
-        return self._time_drift.get_value()
+    secret = property(
+        fget=lambda self: self._secret.get_value(),
+        fset=lambda self, x: self._secret.set_value(x),
+        doc="The secret key itself.")
+
+    counter = property(
+        fget=lambda self: self._counter.get_value(),
+        fset=lambda self, x: self._counter.set_value(x),
+        doc="An event counter for event-based OTP.")
+
+    time_offset = property(
+        fget=lambda self: self._time_offset.get_value(),
+        fset=lambda self, x: self._time_offset.set_value(x),
+        doc="A time offset for time-based OTP (number of intervals).")
+
+    time_interval = property(
+        fget=lambda self: self._time_interval.get_value(),
+        fset=lambda self, x: self._time_interval.set_value(x),
+        doc="A time interval in seconds.")
+
+    time_drift = property(
+        fget=lambda self: self._time_drift.get_value(),
+        fset=lambda self, x: self._time_drift.set_value(x),
+        doc="Device clock drift value (number of time intervals).")
 
     def check(self):
         """Check if all MACs in the message are valid."""
diff --git a/tests/test_misc.doctest b/tests/test_misc.doctest
index f038f2f..0b9604c 100644
--- a/tests/test_misc.doctest
+++ b/tests/test_misc.doctest
@@ -44,13 +44,25 @@ Check creation of empty PSKC structure and adding an empty 
key to the list.
 >>> key.secret is None
 True
 
+
 Adding a key with unknown attributes raises an error.
+
 >>> key = pskc.add_key(foo='bar')
 Traceback (most recent call last):
     ...
 AttributeError
 
 
+Setting secret, counter, etc. also works
+
+>>> key = pskc.add_key(secret='VERYSECRET')
+>>> key.counter = 10
+>>> key.secret
+'VERYSECRET'
+>>> key.counter
+10
+
+
 Load an PSKC file with an odd namespace.
 
 >>> pskc = PSKC('tests/odd-namespace.pskcxml')

-----------------------------------------------------------------------

Summary of changes:
 pskc/key.py             |   85 +++++++++++++++++++++++++----------------------
 pskc/parse.py           |   14 ++++++++
 pskc/policy.py          |   16 ++++-----
 tests/test_misc.doctest |   12 +++++++
 4 files changed, 77 insertions(+), 50 deletions(-)


hooks/post-receive
-- 
python-pskc
-- 
To unsubscribe send an email to
python-pskc-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/python-pskc-commits/