Encode Endpoint in API auth wrapper

This commit is contained in:
Ben Toogood 2020-04-06 16:01:42 +01:00
parent 0f570d98e1
commit 774c0d30a7

View File

@ -1,12 +1,15 @@
package auth
import (
"encoding/json"
"fmt"
"net"
"net/http"
"net/url"
"strings"
"github.com/micro/go-micro/v2/metadata"
"github.com/micro/go-micro/v2/api/resolver"
"github.com/micro/go-micro/v2/api/resolver/path"
"github.com/micro/go-micro/v2/auth"
@ -84,8 +87,16 @@ func (h authHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// a file not served by the resolver has been requested (e.g. favicon.ico)
endpoint = &resolver.Endpoint{Path: req.URL.Path}
} else if err != nil {
logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
} else if err == nil {
// set the endpoint in the context so it can be used to resolve
// the request later
if bytes, err := json.Marshal(endpoint); err == nil {
ctx := metadata.Set(req.Context(), "endpoint", string(bytes))
*req = *req.WithContext(ctx)
}
}
// construct the resource name, e.g. home => go.micro.web.home
@ -132,6 +143,9 @@ func (h authHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}
func namespaceFromRequest(req *http.Request) (string, error) {
// needed to tmp debug host in prod. will be removed.
logger.Infof("Host is '%v'; URL Host is '%v'; URL Hostname is '%v'", req.Host, req.URL.Host, req.URL.Hostname())
// determine the host, e.g. dev.micro.mu:8080
host := req.URL.Hostname()
if len(host) == 0 {
@ -139,8 +153,6 @@ func namespaceFromRequest(req *http.Request) (string, error) {
host, _, _ = net.SplitHostPort(req.Host)
}
logger.Infof("Host is %v", host)
// check for an ip address
if net.ParseIP(host) != nil {
return auth.DefaultNamespace, nil