Merge pull request #11 from caos/feat/customEndpoints

feat: custom absolute endpoints
This commit is contained in:
livio-a 2020-02-27 09:45:27 +01:00 committed by GitHub
commit 7f486a54c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 16 deletions

View file

@ -21,7 +21,7 @@ func main() {
Port: "9998", Port: "9998",
} }
storage := mock.NewAuthStorage() 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View file

@ -26,11 +26,11 @@ const (
var ( var (
DefaultEndpoints = &endpoints{ DefaultEndpoints = &endpoints{
Authorization: defaultAuthorizationEndpoint, Authorization: NewEndpoint(defaultAuthorizationEndpoint),
Token: defaulTokenEndpoint, Token: NewEndpoint(defaulTokenEndpoint),
IntrospectionEndpoint: defaultIntrospectEndpoint, IntrospectionEndpoint: NewEndpoint(defaultIntrospectEndpoint),
Userinfo: defaultUserinfoEndpoint, Userinfo: NewEndpoint(defaultUserinfoEndpoint),
JwksURI: defaultKeysEndpoint, JwksURI: NewEndpoint(defaultKeysEndpoint),
} }
) )

View file

@ -2,14 +2,28 @@ package op
import "strings" 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 { func (e Endpoint) Relative() string {
return relativeEndpoint(string(e)) return relativeEndpoint(e.path)
} }
func (e Endpoint) Absolute(host string) string { 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 { func (e Endpoint) Validate() error {

View file

@ -6,7 +6,7 @@ import (
"github.com/caos/oidc/pkg/op" "github.com/caos/oidc/pkg/op"
) )
func TestEndpoint_Relative(t *testing.T) { func TestEndpoint_Path(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
e op.Endpoint e op.Endpoint
@ -14,12 +14,17 @@ func TestEndpoint_Relative(t *testing.T) {
}{ }{
{ {
"without starting /", "without starting /",
op.Endpoint("test"), op.NewEndpoint("test"),
"/test", "/test",
}, },
{ {
"with starting /", "with starting /",
op.Endpoint("/test"), op.NewEndpoint("/test"),
"/test",
},
{
"with url",
op.NewEndpointWithURL("/test", "http://test.com/test"),
"/test", "/test",
}, },
} }
@ -44,28 +49,34 @@ func TestEndpoint_Absolute(t *testing.T) {
}{ }{
{ {
"no /", "no /",
op.Endpoint("test"), op.NewEndpoint("test"),
args{"https://host"}, args{"https://host"},
"https://host/test", "https://host/test",
}, },
{ {
"endpoint without /", "endpoint without /",
op.Endpoint("test"), op.NewEndpoint("test"),
args{"https://host/"}, args{"https://host/"},
"https://host/test", "https://host/test",
}, },
{ {
"host without /", "host without /",
op.Endpoint("/test"), op.NewEndpoint("/test"),
args{"https://host"}, args{"https://host"},
"https://host/test", "https://host/test",
}, },
{ {
"both /", "both /",
op.Endpoint("/test"), op.NewEndpoint("/test"),
args{"https://host/"}, args{"https://host/"},
"https://host/test", "https://host/test",
}, },
{
"with url",
op.NewEndpointWithURL("test", "https://test.com/test"),
args{"https://host"},
"https://test.com/test",
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {