negroniJWT

package module
v0.0.0-...-4089f74 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2016 License: MIT Imports: 18 Imported by: 0

README

negroniJWT

JSON Web Token Auth middleware for Negroni.

GoDoc can be found here.

##About

This is some simple middleware for handling JSON Web Token authorization. Token encoding and decoding is done using dgrijalva's jwt-go. Claims from request are retrieved using negroniJWT.Get(r) and uses gorilla's context so it's safe for concurrent use.

Initialization


import(
    "github.com/codegangsta/negroni"
    "github.com/denkyl08/negroniJWT"
)

func main() {
    
    // false means request without a valid token always fails
    // true means that you must check _, ok := negroniJWT.Get(request)
    negroniJWT.Init(false)
    
    n := negroni.Classic()
    n.Use(negroni.HandlerFunc(negroniJWT.Middleware))
    
}

##In Login Controller

    
    err = user.Authenticate()
    if err != nil {
        http.Error(w, err.Error(), 401)
        return
    }
    claims := make(map[string]interface{})
    claims["Username"] = user.Username

    // generate JWT token with encrypted claims
    token, err := negroniJWT.GenerateToken(claims, time.Now().Add(30*time.Minute))
    err = utils.WriteJson(struct {
        Token string
    }{token}, w)

##Retrieve Token claims from request

func ProtectedHandler(w http.ResponseWriter, r *http.Request) {
    token, ok := negroniJWT.Get(r); if !ok {
        http.Error(w, "Error: auth failure", 401)
        return
    }
    ...
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bundle

func Bundle() (p []byte, err error)

func GenerateToken

func GenerateToken(claims map[string]interface{}, expiration time.Time) (s string, err error)

GenerateToken generates the base64 encoded JSON Web Token including the claims map provided and and expiration time.

func Get

func Get(r *http.Request) (claims map[string]interface{}, ok bool)

Get attempts to retrieve the claims map for request. If there was an error decoding the JSON Web Token.

func Init

func Init(alwaysFailRequest bool, privKeyPath, pubKeyPath string)

func Middleware

func Middleware(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)

Middleware the main middleware function. Use with negroni.HandlerFunc to apply middleware like so : n := negroni.Classic() n.Use(negroni.HandlerFunc(negroniJWT.Middleware))

Types

This section is empty.

Jump to

Keyboard shortcuts

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