authorize: build full URL from gRPC request

This commit is contained in:
Caleb Doxsey 2020-04-16 14:53:39 -06:00 committed by Caleb Doxsey
parent cd6d686822
commit 5ad0e0ebdc
2 changed files with 32 additions and 1 deletions

View file

@ -4,6 +4,7 @@ package authorize
import (
"context"
"net/url"
"github.com/pomerium/pomerium/authorize/evaluator"
"github.com/pomerium/pomerium/internal/grpc/authorize"
@ -22,7 +23,7 @@ func (a *Authorize) IsAuthorized(ctx context.Context, in *authorize.IsAuthorized
Method: in.GetRequestMethod(),
RequestURI: in.GetRequestRequestUri(),
RemoteAddr: in.GetRequestRemoteAddr(),
URL: in.GetRequestUrl(),
URL: getFullURL(in.GetRequestUrl(), in.GetRequestHost()),
}
return a.pe.IsAuthorized(ctx, req)
}
@ -38,3 +39,17 @@ func cloneHeaders(in protoHeader) map[string][]string {
}
return out
}
func getFullURL(rawurl, host string) string {
u, err := url.Parse(rawurl)
if err != nil {
u = &url.URL{Path: rawurl}
}
if u.Host == "" {
u.Host = host
}
if u.Scheme == "" {
u.Scheme = "http"
}
return u.String()
}

View file

@ -47,3 +47,19 @@ func TestAuthorize_IsAuthorized(t *testing.T) {
})
}
}
func Test_getFullURL(t *testing.T) {
tests := []struct {
rawurl, host, expect string
}{
{"https://www.example.com/admin", "", "https://www.example.com/admin"},
{"https://www.example.com/admin", "example.com", "https://www.example.com/admin"},
{"/admin", "example.com", "http://example.com/admin"},
}
for _, tt := range tests {
actual := getFullURL(tt.rawurl, tt.host)
if actual != tt.expect {
t.Errorf("expected getFullURL(%s, %s) to be %s, but got %s", tt.rawurl, tt.host, tt.expect, actual)
}
}
}