Embed bearer token into context

This commit is contained in:
Pavel Korotkov 2020-07-22 12:12:22 +03:00
parent 1c6da41bee
commit e1c43497db

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"context"
"net/http" "net/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
@ -8,15 +9,19 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
type ContextKey string
const BearerTokenContextKey ContextKey = "bearer-token"
func attachNewUserAuth(router *mux.Router, center *auth.Center, log *zap.Logger) { func attachNewUserAuth(router *mux.Router, center *auth.Center, log *zap.Logger) {
uamw := func(h http.Handler) http.Handler { uamw := func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := center.AuthenticationPassed(r) bearerToken, err := center.AuthenticationPassed(r)
if err != nil { if err != nil {
log.Error("failed to pass authentication", zap.Error(err)) log.Error("failed to pass authentication", zap.Error(err))
// TODO: Handle any auth error by rejecting request.
} }
// TODO: Handle any auth error by rejecting request. h.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), BearerTokenContextKey, bearerToken)))
h.ServeHTTP(w, r)
}) })
} }