From 660519a19f9218eba5933392178c3d23b71710b8 Mon Sep 17 00:00:00 2001 From: Livio Amstutz Date: Fri, 21 Feb 2020 10:04:50 +0100 Subject: [PATCH] fix: custom absolute endpoints --- example/server/default/default.go | 2 +- pkg/op/default_op.go | 10 +++++----- pkg/op/endpoint.go | 20 +++++++++++++++++--- pkg/op/endpoint_test.go | 25 ++++++++++++++++++------- 4 files changed, 41 insertions(+), 16 deletions(-) diff --git a/example/server/default/default.go b/example/server/default/default.go index 0b0bb8e..e5b9d0f 100644 --- a/example/server/default/default.go +++ b/example/server/default/default.go @@ -21,7 +21,7 @@ func main() { Port: "9998", } storage := mock.NewAuthStorage() - handler, err := op.NewDefaultOP(ctx, config, storage, op.WithCustomTokenEndpoint("test")) + handler, err := op.NewDefaultOP(ctx, config, storage, op.WithCustomTokenEndpoint(op.NewEndpoint("test"))) if err != nil { log.Fatal(err) } diff --git a/pkg/op/default_op.go b/pkg/op/default_op.go index 63d9cd3..f39bffd 100644 --- a/pkg/op/default_op.go +++ b/pkg/op/default_op.go @@ -26,11 +26,11 @@ const ( var ( DefaultEndpoints = &endpoints{ - Authorization: defaultAuthorizationEndpoint, - Token: defaulTokenEndpoint, - IntrospectionEndpoint: defaultIntrospectEndpoint, - Userinfo: defaultUserinfoEndpoint, - JwksURI: defaultKeysEndpoint, + Authorization: NewEndpoint(defaultAuthorizationEndpoint), + Token: NewEndpoint(defaulTokenEndpoint), + IntrospectionEndpoint: NewEndpoint(defaultIntrospectEndpoint), + Userinfo: NewEndpoint(defaultUserinfoEndpoint), + JwksURI: NewEndpoint(defaultKeysEndpoint), } ) diff --git a/pkg/op/endpoint.go b/pkg/op/endpoint.go index cc0419c..21907f4 100644 --- a/pkg/op/endpoint.go +++ b/pkg/op/endpoint.go @@ -2,14 +2,28 @@ package op import "strings" -type Endpoint string +type Endpoint struct { + path string + url string +} + +func NewEndpoint(path string) Endpoint { + return Endpoint{path: path} +} + +func NewEndpointWithURL(path, url string) Endpoint { + return Endpoint{path: path, url: url} +} func (e Endpoint) Relative() string { - return relativeEndpoint(string(e)) + return relativeEndpoint(e.path) } func (e Endpoint) Absolute(host string) string { - return absoluteEndpoint(host, string(e)) + if e.url != "" { + return e.url + } + return absoluteEndpoint(host, e.path) } func (e Endpoint) Validate() error { diff --git a/pkg/op/endpoint_test.go b/pkg/op/endpoint_test.go index 227bf9d..fe00326 100644 --- a/pkg/op/endpoint_test.go +++ b/pkg/op/endpoint_test.go @@ -6,7 +6,7 @@ import ( "github.com/caos/oidc/pkg/op" ) -func TestEndpoint_Relative(t *testing.T) { +func TestEndpoint_Path(t *testing.T) { tests := []struct { name string e op.Endpoint @@ -14,12 +14,17 @@ func TestEndpoint_Relative(t *testing.T) { }{ { "without starting /", - op.Endpoint("test"), + op.NewEndpoint("test"), "/test", }, { "with starting /", - op.Endpoint("/test"), + op.NewEndpoint("/test"), + "/test", + }, + { + "with url", + op.NewEndpointWithURL("/test", "http://test.com/test"), "/test", }, } @@ -44,28 +49,34 @@ func TestEndpoint_Absolute(t *testing.T) { }{ { "no /", - op.Endpoint("test"), + op.NewEndpoint("test"), args{"https://host"}, "https://host/test", }, { "endpoint without /", - op.Endpoint("test"), + op.NewEndpoint("test"), args{"https://host/"}, "https://host/test", }, { "host without /", - op.Endpoint("/test"), + op.NewEndpoint("/test"), args{"https://host"}, "https://host/test", }, { "both /", - op.Endpoint("/test"), + op.NewEndpoint("/test"), args{"https://host/"}, "https://host/test", }, + { + "with url", + op.NewEndpointWithURL("test", "https://test.com/test"), + args{"https://host"}, + "https://test.com/test", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {