예제 #1
0
    def test_encode(self):
        for content_encoding in ["aesgcm", "aes128gcm"]:
            recv_key = ec.generate_private_key(ec.SECP256R1, default_backend())
            subscription_info = self._gen_subscription_info(recv_key)
            data = "Mary had a little lamb, with some nice mint jelly"
            push = WebPusher(subscription_info)
            encoded = push.encode(data, content_encoding=content_encoding)
            """
            crypto_key = base64.urlsafe_b64encode(
                self._get_pubkey_str(recv_key)
            ).strip(b'=')
            """
            # Convert these b64 strings into their raw, binary form.
            raw_salt = None
            if 'salt' in encoded:
                raw_salt = base64.urlsafe_b64decode(
                    push._repad(encoded['salt']))
            raw_dh = base64.urlsafe_b64decode(
                push._repad(encoded['crypto_key']))
            raw_auth = base64.urlsafe_b64decode(
                push._repad(subscription_info['keys']['auth']))

            decoded = http_ece.decrypt(encoded['body'],
                                       salt=raw_salt,
                                       dh=raw_dh,
                                       private_key=recv_key,
                                       auth_secret=raw_auth,
                                       version=content_encoding)
            eq_(decoded.decode('utf8'), data)
예제 #2
0
    def test_encode(self):
        recv_key = pyelliptic.ECC(curve="prime256v1")
        subscription_info = self._gen_subscription_info(recv_key)
        data = "Mary had a little lamb, with some nice mint jelly"
        push = WebPusher(subscription_info)
        encoded = push.encode(data)

        keyid = base64.urlsafe_b64encode(recv_key.get_pubkey()[1:])

        http_ece.keys[keyid] = recv_key
        http_ece.labels[keyid] = 'P-256'

        # Convert these b64 strings into their raw, binary form.
        raw_salt = base64.urlsafe_b64decode(push._repad(encoded['salt']))
        raw_dh = base64.urlsafe_b64decode(push._repad(encoded['crypto_key']))
        raw_auth = base64.urlsafe_b64decode(
            push._repad(subscription_info['keys']['auth']))

        decoded = http_ece.decrypt(buffer=encoded['body'],
                                   salt=raw_salt,
                                   dh=raw_dh,
                                   keyid=keyid,
                                   authSecret=raw_auth)

        eq_(decoded.decode('utf8'), data)
예제 #3
0
    def test_encode(self):
        recv_key = pyelliptic.ECC(curve="prime256v1")
        subscription_info = self._gen_subscription_info(recv_key)
        data = "Mary had a little lamb, with some nice mint jelly"
        push = WebPusher(subscription_info)
        encoded = push.encode(data)

        keyid = base64.urlsafe_b64encode(recv_key.get_pubkey()[1:])

        http_ece.keys[keyid] = recv_key
        http_ece.labels[keyid] = 'P-256'

        # Convert these b64 strings into their raw, binary form.
        raw_salt = base64.urlsafe_b64decode(push._repad(encoded['salt']))
        raw_dh = base64.urlsafe_b64decode(push._repad(encoded['crypto_key']))
        raw_auth = base64.urlsafe_b64decode(
            push._repad(subscription_info['keys']['auth']))

        decoded = http_ece.decrypt(
            buffer=encoded['body'],
            salt=raw_salt,
            dh=raw_dh,
            keyid=keyid,
            authSecret=raw_auth
            )

        eq_(decoded, data)
예제 #4
0
def send_push_notification(payload, config, subscription):
    """
    Send push notification using subscription info (url and keys)
    :param payload: payload (usable) data to be sent
    :param config: configuration information from Tornado app
    :param subscription: subscription info dict (keys, endpoint)
    :return: POST request result status code
    """
    subscription_info = {
        "endpoint": subscription["endpoint"],
        "keys": {
            "auth": subscription["auth_secret"],
            "p256dh": subscription["key"]
        }
    }
    web_push = WebPusher(subscription_info)
    body = web_push.encode(payload, "aesgcm")
    headers = generate_vapid_headers(config.VAPID_PRIVATE_KEY,
                                     subscription["endpoint"])
    crypto_key = headers.get("Crypto-Key", "")
    if crypto_key:
        crypto_key += ";"
    crypto_key += "dh=" + body["crypto_key"].decode()
    headers.update({
        "Crypto-Key": crypto_key,
        "Content-Encoding": "aesgcm",
        "Encryption": "salt=" + body["salt"].decode(),
        "TTL": "86400"
    })
    client = AsyncHTTPClient()
    result = yield client.fetch(subscription["endpoint"],
                                method="POST",
                                body=body["body"],
                                headers=headers)
    return result.code
예제 #5
0
 def test_encode(self):
     for content_encoding in ["aesgcm", "aes128gcm"]:
         recv_key = ec.generate_private_key(
             ec.SECP256R1, default_backend())
         subscription_info = self._gen_subscription_info(recv_key)
         data = "Mary had a little lamb, with some nice mint jelly"
         push = WebPusher(subscription_info)
         encoded = push.encode(data, content_encoding=content_encoding)
         """
         crypto_key = base64.urlsafe_b64encode(
             self._get_pubkey_str(recv_key)
         ).strip(b'=')
         """
         # Convert these b64 strings into their raw, binary form.
         raw_salt = None
         if 'salt' in encoded:
             raw_salt = base64.urlsafe_b64decode(
                 push._repad(encoded['salt']))
         raw_dh = None
         if content_encoding != "aes128gcm":
             raw_dh = base64.urlsafe_b64decode(
                 push._repad(encoded['crypto_key']))
         raw_auth = base64.urlsafe_b64decode(
             push._repad(subscription_info['keys']['auth']))
         decoded = http_ece.decrypt(
             encoded['body'],
             salt=raw_salt,
             dh=raw_dh,
             private_key=recv_key,
             auth_secret=raw_auth,
             version=content_encoding
             )
         eq_(decoded.decode('utf8'), data)