ghwebhookauth

package module
v0.0.0-...-a441866 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 2, 2016 License: MIT Imports: 7 Imported by: 0

README

ghwebhookauth

Build Status Report Card Coverage

A middleware that will check that a valid X-Hub-Signature header is sent for POST requests.

This module lets you secure webhook HTTP requests from GitHub in your Go Programming Language applications.

Installing

go get github.com/StefanKjartansson/ghwebhookauth

Using it

You can use ghwebhookauth with default net/http as follows.

// main.go
package main

import (
  "net/http"
  "os"
  "github.com/StefanKjartansson/ghwebhookauth"
)

var webhookHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  // normal handler code
})

func main() {
  secretKey := os.Getenv("GITHUB_SECRET_KEY")
  gh := ghwebhookauth.New(secretKey)
  app := gh.Handler(http.HandlerFunc(webhookHandler))
  http.ListenAndServe("0.0.0.0:3000", app)
}

You can also use it with Negroni as follows:

// main.go
package main

import (
  "net/http"
  "os"
  "github.com/StefanKjartansson/ghwebhookauth"
  "github.com/codegangsta/negroni"
  "github.com/gorilla/mux"
)

var webhookHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  // normal handler code
})

func main() {
  secretKey := os.Getenv("GITHUB_SECRET_KEY")
  gh := ghwebhookauth.New(secretKey)
  r := mux.NewRouter()
  r.Handle("/myhook", negroni.New(
    negroni.HandlerFunc(gh.HandlerWithNext),
    negroni.Wrap(webhookHandler),
  ))
  http.Handle("/", r)
  http.ListenAndServe(":3001", nil)
}

Documentation

Index

Constants

View Source
const HeaderName = "X-Hub-Signature"

HeaderName is the name of the header containing the signature GitHub sends

Variables

View Source
var (
	// Error condition when a request is missing a header
	ErrMissingHeader = errors.New("Missing Header")
	// Error condition when a request has a header but the contents are invalid
	ErrInvalidSignature = errors.New("Invalid Signature")
	// Error condition when a request has no body
	ErrMissingBody = errors.New("Missing Body")
	// Error condition when a request has an invalid method
	ErrMethodNotAllowed = errors.New("Method not allowed")
)

Functions

This section is empty.

Types

type GitHubWebhookAuth

type GitHubWebhookAuth struct {
	SecretKey []byte
}

func New

func New(secretKey string) *GitHubWebhookAuth

New returns an instance of GitHubWebhookAuth middleware

func (*GitHubWebhookAuth) Handler

func (g *GitHubWebhookAuth) Handler(h http.Handler) http.Handler

Handler returns net/http compatible middleware handler

func (*GitHubWebhookAuth) HandlerWithNext

func (g *GitHubWebhookAuth) HandlerWithNext(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

HandlerWithNext is a Negroni compatible middleware function

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL