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")
示例#2
0
    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)
示例#3
0
 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
示例#4
0
 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
示例#5
0
 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
示例#6
0
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
示例#7
0
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
示例#8
0
文件: verify.py 项目: vdt/zamboni
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
示例#9
0
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)
示例#10
0
文件: verify.py 项目: sarasan/zamboni
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
示例#11
0
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
示例#12
0
def get_key():
    """Return a key for using with encode."""
    return jwt.rsa_load(settings.WEBAPPS_RECEIPT_KEY)
示例#13
0
def get_key():
    """Return a key for using with encode."""
    return jwt.rsa_load(settings.WEBAPPS_RECEIPT_KEY)