From e1c43497dbda180fe7a4c1a6edf5a8a0c0139d59 Mon Sep 17 00:00:00 2001 From: Pavel Korotkov Date: Wed, 22 Jul 2020 12:12:22 +0300 Subject: [PATCH] Embed bearer token into context --- cmd/gate/app-new-auth.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/cmd/gate/app-new-auth.go b/cmd/gate/app-new-auth.go index 6410193e2..012c8a210 100644 --- a/cmd/gate/app-new-auth.go +++ b/cmd/gate/app-new-auth.go @@ -1,6 +1,7 @@ package main import ( + "context" "net/http" "github.com/gorilla/mux" @@ -8,15 +9,19 @@ import ( "go.uber.org/zap" ) +type ContextKey string + +const BearerTokenContextKey ContextKey = "bearer-token" + func attachNewUserAuth(router *mux.Router, center *auth.Center, log *zap.Logger) { uamw := func(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - _, err := center.AuthenticationPassed(r) + bearerToken, err := center.AuthenticationPassed(r) if err != nil { 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) + h.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), BearerTokenContextKey, bearerToken))) }) }