Auth util func RequestToContext (#1386)
This commit is contained in:
parent
d2f153d795
commit
9501512219
@ -1,12 +1,15 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/micro/go-micro/v2/client/selector"
|
"github.com/micro/go-micro/v2/client/selector"
|
||||||
|
"github.com/micro/go-micro/v2/metadata"
|
||||||
"github.com/micro/go-micro/v2/registry"
|
"github.com/micro/go-micro/v2/registry"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -56,3 +59,14 @@ func NewRoundTripper(opts ...Option) http.RoundTripper {
|
|||||||
opts: options,
|
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)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user