def get_token(): """ Retrieve access token with a JWT token """ f = open("token", "r") token = f.readline() f.close() token_splitted = token.split(",") if time.time() < float(token_splitted[1]) : return token_splitted[0] key = jwt.rsa_load(config.get("api", "private_key")) token = jwt.encode({ "iss" : config.get("api", "iss"), "scope" : config.get("api", "scope"), "aud":"https://accounts.google.com/o/oauth2/token", "exp":int(time.time()) + 3600, "iat":int(time.time()) },key, "RS256") payload = {'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer', 'assertion': token} headers = {"Content-Type": "application/x-www-form-urlencoded"} url = "https://accounts.google.com/o/oauth2/token" rep = requests.post(url, headers = headers, data=payload) rep_as_json = rep.json() f = open("token", "w") f.write(rep_as_json.get("access_token")+","+str(time.time()+3600)) f.flush() f.close() return rep_as_json.get("access_token")
def test_rsa_encode(self): root = os.path.dirname(__file__) pubkey = jwt.rsa_load_pub(os.path.join(root, "rsapubkey.pem")) jwt.decode( b"eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ.cC4hiUPoj9Eetdgtv3hF80EGrhuB__dzERat0XF9g2VtQgr9PJbu3XOiZj5RZmh7AAuHIm4Bh-0Qc_lF5YKt_O8W2Fp5jujGbds9uJdbF9CUAr7t1dnZcAcQjbKBYNX4BAynRFdiuB--f_nZLgrnbyTyWzO75vRK5h6xBArLIARNPvkSjtQBMHlb1L07Qe7K0GarZRmB_eSN9383LcOLn6_dO--xi12jzDwusC-eOkHWEsqtFZESc6BfI7noOPqvhJ1phCnvWh6IeYI2w9QOYEUipUTI8np6LbgGY9Fs98rqVt5AXLIhWkWywlVmtVrBp0igcN_IoypGlUPQGe77Rw", pubkey, ) key = jwt.rsa_load(os.path.join(root, "rsakey.pem")) # Example from the JWS spec assert jwt.check( b"eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ.cC4hiUPoj9Eetdgtv3hF80EGrhuB__dzERat0XF9g2VtQgr9PJbu3XOiZj5RZmh7AAuHIm4Bh-0Qc_lF5YKt_O8W2Fp5jujGbds9uJdbF9CUAr7t1dnZcAcQjbKBYNX4BAynRFdiuB--f_nZLgrnbyTyWzO75vRK5h6xBArLIARNPvkSjtQBMHlb1L07Qe7K0GarZRmB_eSN9383LcOLn6_dO--xi12jzDwusC-eOkHWEsqtFZESc6BfI7noOPqvhJ1phCnvWh6IeYI2w9QOYEUipUTI8np6LbgGY9Fs98rqVt5AXLIhWkWywlVmtVrBp0igcN_IoypGlUPQGe77Rw", key, ) assert not jwt.check( b"eyJhbGciOiJSUzI1NiJ9.AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.cC4hiUPoj9Eetdgtv3hF80EGrhuB__dzERat0XF9g2VtQgr9PJbu3XOiZj5RZmh7AAuHIm4Bh-0Qc_lF5YKt_O8W2Fp5jujGbds9uJdbF9CUAr7t1dnZcAcQjbKBYNX4BAynRFdiuB--f_nZLgrnbyTyWzO75vRK5h6xBArLIARNPvkSjtQBMHlb1L07Qe7K0GarZRmB_eSN9383LcOLn6_dO--xi12jzDwusC-eOkHWEsqtFZESc6BfI7noOPqvhJ1phCnvWh6IeYI2w9QOYEUipUTI8np6LbgGY9Fs98rqVt5AXLIhWkWywlVmtVrBp0igcN_IoypGlUPQGe77Rw", key, ) # XXX Should test the Signer classes directly. The check(encode()) dance # doesn't really verify that the correct algorithm was used, or that the # algorithm was implemented properly. assert jwt.check(jwt.encode(u"test", key, u"RS256"), key) assert jwt.check(jwt.encode(u"test", key, u"RS384"), key) assert jwt.check(jwt.encode(u"test", key, u"RS512"), key)
def create(self, data, request=None): stuff = {'user': {'type': 'directed-identifier'}} stuff.update(data) key = jwt.rsa_load(settings.WEBAPPS_RECEIPT_KEY) receipt = jwt.encode(stuff, key, u'RS512') v = verify.Verify(receipt, request) v.decoded = v.decode() return v
def create(self, data, request=None): stuff = {"user": {"type": "directed-identifier"}} stuff.update(data) key = jwt.rsa_load(settings.WEBAPPS_RECEIPT_KEY) receipt = jwt.encode(stuff, key, u"RS512") v = verify.Verify(receipt, request) v.decoded = v.decode() return v
def decode_receipt(receipt): """ Cracks the receipt using the private key. This will probably change to using the cert at some point, especially when we get the HSM. """ with statsd.timer('services.decode'): key = jwt.rsa_load(settings.WEBAPPS_RECEIPT_KEY) raw = jwt.decode(receipt, key) return raw
def decode_receipt(receipt): """ Cracks the receipt using the private key. This will probably change to using the cert at some point, especially when we get the HSM. """ with statsd.timer('services.decode'): if settings.SIGNING_SERVER_ACTIVE: verifier = certs.ReceiptVerifier() if not verifier.verify(receipt): raise VerificationError() return jwt.decode(receipt.split('~')[1], verify=False) else: key = jwt.rsa_load(settings.WEBAPPS_RECEIPT_KEY) raw = jwt.decode(receipt, key) return raw
def test_check_rsa(): key = jwt.rsa_load("rsakey.pem") # Example from the JWS spec assert jwt.check( b"eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ.cC4hiUPoj9Eetdgtv3hF80EGrhuB__dzERat0XF9g2VtQgr9PJbu3XOiZj5RZmh7AAuHIm4Bh-0Qc_lF5YKt_O8W2Fp5jujGbds9uJdbF9CUAr7t1dnZcAcQjbKBYNX4BAynRFdiuB--f_nZLgrnbyTyWzO75vRK5h6xBArLIARNPvkSjtQBMHlb1L07Qe7K0GarZRmB_eSN9383LcOLn6_dO--xi12jzDwusC-eOkHWEsqtFZESc6BfI7noOPqvhJ1phCnvWh6IeYI2w9QOYEUipUTI8np6LbgGY9Fs98rqVt5AXLIhWkWywlVmtVrBp0igcN_IoypGlUPQGe77Rw", key) assert not jwt.check( b"eyJhbGciOiJSUzI1NiJ9.AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.cC4hiUPoj9Eetdgtv3hF80EGrhuB__dzERat0XF9g2VtQgr9PJbu3XOiZj5RZmh7AAuHIm4Bh-0Qc_lF5YKt_O8W2Fp5jujGbds9uJdbF9CUAr7t1dnZcAcQjbKBYNX4BAynRFdiuB--f_nZLgrnbyTyWzO75vRK5h6xBArLIARNPvkSjtQBMHlb1L07Qe7K0GarZRmB_eSN9383LcOLn6_dO--xi12jzDwusC-eOkHWEsqtFZESc6BfI7noOPqvhJ1phCnvWh6IeYI2w9QOYEUipUTI8np6LbgGY9Fs98rqVt5AXLIhWkWywlVmtVrBp0igcN_IoypGlUPQGe77Rw", key) # XXX Should test the Signer classes directly. The check(sign()) dance # doesn't really verify that the correct algorithm was used, or that the # algorithm was implemented properly. assert jwt.check(jwt.sign(u'RS256', u"test", key), key) assert jwt.check(jwt.sign(u'RS384', u"test", key), key) assert jwt.check(jwt.sign(u'RS512', u"test", key), key)
def decode_receipt(receipt): """ Cracks the receipt using the private key. This will probably change to using the cert at some point, especially when we get the HSM. """ with statsd.timer('services.decode'): if settings.SIGNING_SERVER_ACTIVE: verifier = certs.ReceiptVerifier() try: result = verifier.verify(receipt) except ExpiredSignatureError: # Until we can do something meaningful with this, just ignore. return jwt.decode(receipt.split('~')[1], verify=False) if not result: raise VerificationError() return jwt.decode(receipt.split('~')[1], verify=False) else: key = jwt.rsa_load(settings.WEBAPPS_RECEIPT_KEY) raw = jwt.decode(receipt, key) return raw
def get_key(): """Return a key for using with encode.""" return jwt.rsa_load(settings.WEBAPPS_RECEIPT_KEY)