-
Notifications
You must be signed in to change notification settings - Fork 2
/
authorizer.go
34 lines (27 loc) · 909 Bytes
/
authorizer.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
package middleware
import (
"net/http"
"strings"
"github.com/nownabe/golink/backend/golinkcontext"
)
const (
googAccountsHeaderPrefix = "accounts.google.com:"
headerUserEmail = "X-Appengine-User-Email"
headerUserID = "X-Appengine-User-Id"
)
func NewAuthorizer() Middleware {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
email := strings.TrimPrefix(r.Header.Get(headerUserEmail), googAccountsHeaderPrefix)
userID := strings.TrimPrefix(r.Header.Get(headerUserID), googAccountsHeaderPrefix)
if email == "" || userID == "" {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
ctx := r.Context()
ctx = golinkcontext.WithUserEmail(ctx, email)
ctx = golinkcontext.WithUserID(ctx, userID)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}