-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
35 lines (30 loc) · 951 Bytes
/
token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package martiniframework
import (
"github.com/go-martini/martini"
"log"
"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, SessionModel)) martini.Handler {
return func(res http.ResponseWriter, req *http.Request, c martini.Context) {
log.Print("TokenFunc()")
auth := req.Header.Get("Authorization")
log.Print("TokenFunc(): Authorization: " + auth)
if len(auth) < 7 || auth[:7] != "Bearer " {
http.Error(res, "Not Authorized", http.StatusUnauthorized)
return
}
authed, ses := authfn(auth[7:])
if !authed {
http.Error(res, "Not Authorized", http.StatusUnauthorized)
return
}
// Inject Token
c.Map(Token(auth[7:]))
// Inject SessionModel
c.Map(ses)
}
}