1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-11-24 08:02:32 +02:00

Merge pull request #1493 from micro/auth-encode-endpoint

Encode Endpoint in API auth wrapper
This commit is contained in:
ben-toogood 2020-04-06 16:21:14 +01:00 committed by GitHub
commit 3a378eb7d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
package auth
import (
"context"
"fmt"
"net"
"net/http"
@ -84,8 +85,14 @@ 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
ctx := context.WithValue(req.Context(), resolver.Endpoint{}, endpoint)
*req = *req.WithContext(ctx)
}
// construct the resource name, e.g. home => go.micro.web.home
@ -132,6 +139,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 +149,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