Giter Club home page Giter Club logo

vapor-x-hub-middleware's Introduction

Logo



πŸ”‘ X-Hub-Signature Vapor middleware πŸ”‘

Swift 3.1 Platforms Package Managers Vapor 2 Build Status codebeat badge MIT License



πŸ“š Table of Contents

πŸ“¦ Installation

You just need to add this package as a dependency in your Package.swift:

let package = Package(
    name: "MyProject",
    dependencies: [
        .Package(url: "https://github.com/ythecombinator/vapor-x-hub-middleware.git", majorVersion: 1),
        // ...
    ]
    // ...
)

πŸš€ Getting Started

First of all, add the x-hub-signature middleware into your droplet middlewares array (inside Config/droplet.json):

{
    ...
    "middleware": [
        "error",
        "x-hub-signature",
        ...
    ],
    ...
}

Note: Make sure you insert this middleware after the error one. Otherwise the proper headers and body might not be added to the response.

Next time you boot your application, you will be prompted to add a x-hub-signature.json file inside Config/secrets which is like:

{
    "secret": "my_key",
}

Note: Environment Variables might not be the safest solution for secret data–there's much stuff out there on this. However, following the twelve-factor app manifesto can be a wiser instead of storing this data on a simple .json, so you'd better do something like:

export MY_KEY=my_key

and then:

{
    "secret": "$MY_KEY"
}

Afterwards you can add the middleware using a throwing overload of the initialiser that accepts Vapor's Config:

import XHubMiddleware

let config = try Config()
try config.setup()

// ↓ ↓ ↓ important setup stuff ↓ ↓ ↓

do {
    try config.addConfigurable(middleware: XHubMiddleware(config: config), name: "x-hub-signature")
} catch {
    fatalError("Error creating XHubMiddleware, please check that you've setup x-hub-signature.json correctly.")
}

// ↑ ↑ ↑ important setup stuff ↑ ↑ ↑

let drop = try Droplet(config)
try drop.setup()
try drop.run()
// ...

πŸŽ‡ Now It Works

A working scenario would be:

Your secret: secret

The payload received from the server:

{
    "example": "payload"
}

When everything goes okay, this middleware doesn't take care of forming the response body –it just returns a 200 HTTP status–since the feedback given by the webhook to pushed events may vary depending on your domain.

200 status code

Now let's say that the event emitter sent a different X-Hub-Signature than the expected:

401 status code

Last but not least, we are also aware of malformed–without a valid X-Hub-Signature header–requests:

400 status code

Yay, for sugary error messages.

πŸ€“ How It Works

The client must generate an HMAC signature of the payload and include that signature in the request headers. The X-Hub-Signature header's value must be sha1=signature, where signature is a hexadecimal representation of a SHA1 signature. The signature must be computed using the HMAC algorithm with the request body as the data and the secret as the key.

This middleware recomputes the SHA1 signature with the shared secret using the same method as the client. If the signature does not match, the request cannot be verified and should probably be dropped.

Originally this technique has been described in the PubSubHubbub spec.

βš–οΈ License

X-Hub-Signature Vapor middleware is distributed under the MIT License, available in this repository.

All contributions are assumed to be also licensed under the same.

vapor-x-hub-middleware's People

Contributors

ythecombinator avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 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.