Skip to content

Commit

Permalink
Merge pull request #11 from caos/feat/customEndpoints
Browse files Browse the repository at this point in the history
feat: custom absolute endpoints
  • Loading branch information
livio-a authored Feb 27, 2020
2 parents c3e583b + 660519a commit 7f486a5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
2 changes: 1 addition & 1 deletion example/server/default/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/op/default_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
)

Expand Down
20 changes: 17 additions & 3 deletions pkg/op/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 18 additions & 7 deletions pkg/op/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,25 @@ 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
want string
}{
{
"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",
},
}
Expand All @@ -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) {
Expand Down

0 comments on commit 7f486a5

Please sign in to comment.