Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package auth

import (
"github.com/go-martini/martini"
"net/http"
)

// Token is the authorization token extracted from the request.
type Token string

// TokenFunc returns a Handler that authenticates via an Authentication: Bearer header using the provided function.
// The function should return true for a valid token.
func TokenFunc(authfn func(string) bool) martini.Handler {
return func(res http.ResponseWriter, req *http.Request, c martini.Context) {
auth := req.Header.Get("Authorization")
if len(auth) < 7 || auth[:7] != "Bearer " || !authfn(auth[7:]) {
http.Error(res, "Not Authorized", http.StatusUnauthorized)
return
}
c.Map(Token(auth[7:]))
}
}