Permalink
Browse files

make PyBrowserID optional, show issues and expires dates

  • Loading branch information...
1 parent 7528dd1 commit 69f8f0f6c5db3df6cea327a361cada2066377e51 @andymckay committed May 7, 2012
Showing with 57 additions and 7 deletions.
  1. +17 −1 docs/index.rst
  2. +6 −1 docs/lib.rst
  3. +7 −1 receipts/fx.py
  4. +25 −2 receipts/receipts.py
  5. +2 −2 setup.py
View
@@ -19,9 +19,25 @@ Requires Python 2.7 and pip::
Dependencies that will be installed:
* requests
-* PyBrowserID
* pyjwt
+Optional dependency:
+
+* PyBrowserID
+
+This is required for the crypto verficition check of the receipt. The
+verification server will do this for you, but you can optionally do it
+yourself. To install this::
+
+ pip install PyBrowserID
+
+Changes:
+
+* 0.2.2
+
+ Make PyBrowserID and hence M2Crypto optional. Add in issue and expiry dates
+ into the list command nicely formatted.
+
License
----------------------------------
View
@@ -18,4 +18,9 @@ because it's really long::
>>> receipt.verify_crypto()
True
-More to come.
+* `verify_server`: will send the receipt to the URL specified in the receipt
+ for verification and return its result.
+
+* `verify_crypto`: this requires PyBrowserID to be installed. This is optional
+ and is not installed in the setup due to its dependencies. If you don't have
+ this installed: `MissingPyBrowserId` is raised.
View
@@ -1,13 +1,14 @@
import argparse
import ConfigParser
+from datetime import date
from gettext import ngettext
import json
from pprint import pprint
import os
import sys
import warnings
-from receipts import Install, VerificationError
+from receipts import Install, MissingPyBrowserId, VerificationError
directory = os.path.expanduser('~/Library/Application Support/Firefox')
@@ -54,6 +55,9 @@ def list(self, *args):
rcs = len(i.receipts)
receipt_text = ngettext('1 receipt', '%s receipts' % rcs, rcs)
print u'%s: %s' % (self._good(i.origin), receipt_text)
+ for r in i.receipts:
+ f = lambda x: date.fromtimestamp(x).strftime('%d %B %y')
+ print u' Issued: %s, expires %s' % (f(r.issue), f(r.expiry))
def check(self, domain):
for i in self.installs:
@@ -71,6 +75,8 @@ def check(self, domain):
res = r.verify_crypto()
except VerificationError, error:
print 'Validity error: %s' % self._bad(error)
+ except MissingPyBrowserId, error:
+ continue
else:
states = {True: self._good('good'),
False: self._bad('bad')}
View
@@ -11,19 +11,28 @@
import requests
from requests.exceptions import RequestException
-import certs
+try:
+ import certs
+ CERTS = True
+except ImportError:
+ CERTS = False
class VerificationError(Exception):
pass
+class MissingPyBrowserId(Exception):
+ pass
+
+
class Receipt(object):
def __init__(self, data):
self.receipt = ''
self.cert = ''
self.full = data
+ self.decoded = {}
if '~' in data:
self.cert, self.receipt = data.split('~')
else:
@@ -33,12 +42,23 @@ def cert_decoded(self):
return jwt.decode(self.cert.encode('ascii'), verify=False)
def receipt_decoded(self):
- return jwt.decode(self.receipt.encode('ascii'), verify=False)
+ if not self.decoded:
+ self.decoded = jwt.decode(self.receipt.encode('ascii'),
+ verify=False)
+ return self.decoded
@property
def verifier(self):
return self.receipt_decoded()['verify']
+ @property
+ def issue(self):
+ return self.receipt_decoded()['iat']
+
+ @property
+ def expiry(self):
+ return self.receipt_decoded()['exp']
+
def verify_server(self):
try:
response = requests.post(self.verifier, self.full)
@@ -47,6 +67,9 @@ def verify_server(self):
return json.loads(response.text)
def verify_crypto(self):
+ if not CERTS:
+ raise MissingPyBrowserId('Requires optional dependency: '
+ 'pip install PyBrowserID')
try:
return certs.ReceiptVerifier().verify(self.full)
except Exception, error:
View
@@ -2,13 +2,13 @@
setup(
name='receipts',
- version='0.2.1',
+ version='0.2.2',
description='Verify web app receipts',
long_description=open('readme.rst').read(),
author='Andy McKay',
author_email='andym@mozilla.com',
license='BSD',
- install_requires=['pyjwt', 'requests', 'PyBrowserID'],
+ install_requires=['pyjwt', 'requests'],
py_modules=['receipts'],
entry_points={
'console_scripts': [

0 comments on commit 69f8f0f

Please sign in to comment.