Giter Site home page Giter Site logo

webpush-payload-encryption's Introduction

webpush-payload-encryption

Python package to handle the encryption of push notifications for Firefox and Chrome.

Intended to support Firefox v46 and later, Chrome v44 and later.

Installation

pip install webpush_encryption

This package requires the cryptography python package, which has some installation dependencies. See https://cryptography.io/en/latest/installation/

Usage

The subscription information for web push contains either one or two encryption keys:

  • the public key
  • the auth secret, which is not present in some older Firefox subscriptions

We generally assume these are stored as base64-encoded strings. They're returned from PushSubscription.getKey as ArrayBuffers. In Javascript, an ArrayBuffer can be base-64 encoded with:

/**
 * Converts an ArrayBuffer of bytes to a Base64 encoded string, ready
 * for inclusion in a JSON payload. Useful for working with encryption keys.
 */
/* istanbul ignore next */
var encodeByteArray = function(bytes) {
    return btoa(String.fromCharCode.apply(null, new Uint8Array(bytes)));
};

Given the public key, a payload dict and optionally an auth key, you can call encrypt_payload to get the encrypted data (the ciphertext) and the HTTP headers needed when POSTing to either Google Cloud Messaging or the endpoint URL of a Firefox web push subscription:

    import webpush_encryption
    
    ciphertext, headers = webpush_encryption.encrypt_payload(
        key=public_key_base64,
        auth=auth_key_base64,
        payload=the_payload_dict
    )

If you store the key and auth un-encoded (or your code already decodes them from base64), you can call encrypt_encoded_payload directly:

    import json
    import webpush_encryption
    
    ciphertext, headers = webpush_encryption.encrypt_encoded_payload(
        key=public_key,
        auth=auth_key,
        payload=json.dumps(the_payload)
    )

References

https://tools.ietf.org/html/draft-ietf-webpush-encryption, specifically https://tools.ietf.org/html/draft-ietf-webpush-encryption#section-3

Releases

  1. Setup the venv and pip install setuptools twine
  2. Run the tests: python setup.py test
  3. Update the version in setup.py

webpush-payload-encryption's People

Contributors

benlast avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.