fix: custom absolute endpoints

This commit is contained in:
Livio Amstutz 2020-02-21 10:04:50 +01:00
parent 30d8dec409
commit 660519a19f
4 changed files with 41 additions and 16 deletions

View file

@ -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)
}

View file

@ -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),
}
)

View file

@ -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 {

View file

@ -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) {