From 95015122192c0b9df93484f61b9aa3fbb054f2b7 Mon Sep 17 00:00:00 2001 From: Janos Dobronszki Date: Fri, 20 Mar 2020 16:23:12 +0100 Subject: [PATCH] Auth util func RequestToContext (#1386) --- util/http/http.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/util/http/http.go b/util/http/http.go index 3b743c2a..2f4dc636 100644 --- a/util/http/http.go +++ b/util/http/http.go @@ -1,12 +1,15 @@ package http import ( + "context" "encoding/json" "fmt" "log" "net/http" + "strings" "github.com/micro/go-micro/v2/client/selector" + "github.com/micro/go-micro/v2/metadata" "github.com/micro/go-micro/v2/registry" ) @@ -56,3 +59,14 @@ func NewRoundTripper(opts ...Option) http.RoundTripper { opts: options, } } + +// RequestToContext puts the `Authorization` header bearer token into context +// so calls to services will be authorized. +func RequestToContext(r *http.Request) context.Context { + ctx := context.Background() + md := make(metadata.Metadata) + for k, v := range r.Header { + md[k] = strings.Join(v, ",") + } + return metadata.NewContext(ctx, md) +}