Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 0f2e5820ba
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 74 lines (63 sloc) 2.31 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
# Copied from moz_inapp_pay. Will remove when tests are included in bundle.
import calendar
import json
import time
import unittest

from django.conf import settings

import jwt


class JWTtester(unittest.TestCase):
    key = 'Application key granted by Mozilla Marketplace'
    secret = 'Application secret granted by Mozilla Marketplace'

    def setUp(self):
        self.verifier = None

    def payload(self, iss=None, aud=None, exp=None, iat=None,
                typ='mozilla/postback/pay/v1', extra_req=None, extra_res=None,
                include_response=True):
        iss = iss or self.key
        aud = aud or settings.DOMAIN
        if not iat:
            iat = calendar.timegm(time.gmtime())
        if not exp:
            exp = iat + 3600 # Expires in 1 hour.

        req = {
            'pricePoint': 1,
            'id': 'some-generated-unique-id',
            'name': 'My bands latest album',
            'description': '320kbps MP3 download, DRM free!',
            'productdata': 'my_product_id=1234'
        }
        if extra_req:
            req.update(extra_req)

        payload = {
            'iss': iss,
            'aud': aud,
            'typ': typ,
            'exp': exp,
            'iat': iat,
            'request': req,
        }
        if include_response:
            res = {'transactionID': '1234'}
            if extra_res:
                res.update(extra_res)
            payload['response'] = res
        return payload

    def request(self, app_secret=None, payload=None, **payload_kw):
        if not app_secret:
            app_secret = self.secret
        if not payload:
            payload = self.payload(**payload_kw)
        encoded = jwt.encode(payload, app_secret, algorithm='HS256')
        return unicode(encoded) # e.g. django always passes unicode.

    def verify(self, request=None, update=None, update_request=None,
               verifier=None):
        if not verifier:
            verifier = self.verifier
        if not request:
            payload = self.payload()
            if update_request:
                payload['request'].update(update_request)
            if update:
                payload.update(update)
            request = self.request(payload=json.dumps(payload))
        return verifier(request, self.key, self.secret)
Something went wrong with that request. Please try again.