hookshot: github.com/ejholmes/hookshot Index | Examples | Files | Directories

package hookshot

import "github.com/ejholmes/hookshot"

Package hookshot is a router that de-multiplexes and authorizes github webhooks.

Code:

package main

import (
    "net/http"

    "github.com/ejholmes/hookshot"
)

func HandlePing(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(200)
    w.Write([]byte(`Pong`))
}

func main() {
    r := hookshot.NewRouter()
    r.HandleFunc("ping", HandlePing)

    http.ListenAndServe(":8080", r)
}

Index

Examples

Package Files

hookshot.go

Constants

const (
    // HeaderEvent is the name of the header that contains the type of event.
    HeaderEvent = "X-GitHub-Event"

    // HeaderSignature is the name of the header that contains the signature.
    HeaderSignature = "X-Hub-Signature"
)

Variables

var (
    // DefaultNotFoundHandler is the default NotFoundHandler for a Router instance.
    DefaultNotFoundHandler = http.HandlerFunc(http.NotFound)

    // DefaultUnauthorizedHandler is the default UnauthorizedHandler for a Router
    // instance, which responds with a 403 status and a plain text body.
    DefaultUnauthorizedHandler = http.HandlerFunc(unauthorized)
)

func IsAuthorized Uses

func IsAuthorized(r *http.Request, secret string) (string, bool)

IsAuthorized checks that the calculated signature for the request matches the provided signature in the request headers. Returns the calculated signature, and a boolean value indicating whether or not the calculated signature matches the X-Hub-Signature value.

Code:

h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    if _, ok := IsAuthorized(r, "secret"); !ok {
        http.Error(w, "The provided signature in the "+HeaderSignature+" header does not match.", 403)
        return
    }

    w.Write([]byte(`Ok`))
})

res := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "", bytes.NewBufferString(`{"data":"foo"}`))
req.Header.Set("X-Hub-Signature", "sha1=b3dc4e9a2d727ee1e60bb6828c2dcef88b5ec970")

h.ServeHTTP(res, req)

fmt.Print(res.Body)

Output:

Ok

func Signature Uses

func Signature(body []byte, secret string) string

Signature calculates the SHA1 HMAC signature of body, signed by the secret.

When github-services makes a POST request, it includes a SHA1 HMAC signature of the request body, signed with the secret provided in the webhook configuration. See http://goo.gl/Oe4WwR.

type Router Uses

type Router struct {
    // NotFoundHandler is called when a handler is not found for a given GitHub event.
    // The nil value for NotFoundHandler
    NotFoundHandler http.Handler
    // contains filtered or unexported fields
}

Router demultiplexes github hooks.

func NewRouter Uses

func NewRouter() *Router

NewRouter returns a new Router.

func (*Router) Handle Uses

func (r *Router) Handle(event string, h http.Handler)

Handle maps a github event to an http.Handler.

func (*Router) HandleFunc Uses

func (r *Router) HandleFunc(event string, fn func(http.ResponseWriter, *http.Request))

HandleFunc maps a github event to an http.HandlerFunc.

func (*Router) Handler Uses

func (r *Router) Handler(req *http.Request) http.Handler

Handler returns the http.Handler to use for the given request. It will always return a non nill Handler.

func (*Router) ServeHTTP Uses

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP implements the http.Handler interface to route a request to an appropriate http.Handler, based on the value of the X-GitHub-Event header.

type SecretHandler Uses

type SecretHandler struct {
    // The secret to use to verify the request.
    Secret string

    // SetHeader controls what happens when the X-Hub-Signature header value does
    // not match the calculated signature. Setting this value to true will set
    // the X-Calculated-Signature header in the response.
    //
    // It's recommended that you only enable this for debugging purposes.
    SetHeader bool

    // Handler is the http.Handler that will be called if the request is
    // authorized.
    Handler http.Handler

    // Unauthorized is the http.Handler that will be called if the request
    // is not authorized.
    Unauthorized http.Handler
}

SecretHandler is an http.Handler that will verify the authenticity of the request.

func Authorize Uses

func Authorize(h http.Handler, secret string) *SecretHandler

Authorize wraps an http.Handler to verify the authenticity of the request using the provided secret.

func (*SecretHandler) ServeHTTP Uses

func (h *SecretHandler) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP implements the http.Handler interface.

Directories

PathSynopsis
eventsPackage events containers types representing GitHub webhook payloads.
hookerPackage hooker can generate github webhooks.

Package hookshot imports 7 packages (graph) and is imported by 14 packages. Updated 2017-07-30. Refresh now. Tools for package owners.