Auth util func RequestToContext (#1386)

This commit is contained in:
Janos Dobronszki 2020-03-20 16:23:12 +01:00 committed by GitHub
parent d2f153d795
commit 9501512219
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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)
}