fix: correct join of paths for redirect URI

This commit is contained in:
Trong Huu Nguyen
2021-10-18 14:22:41 +02:00
parent 1b4ce5cab7
commit 62e9e91c73
2 changed files with 57 additions and 8 deletions
+9 -8
View File
@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/url"
"path"
"github.com/lestrrat-go/jwx/jwk"
@@ -52,7 +53,7 @@ func NewProvider(cfg *config.Config) (Provider, error) {
return nil, fmt.Errorf("missing required config %s", config.Ingress)
}
redirectURI, err := redirectURI(ingress)
redirectURI, err := RedirectURI(ingress)
if err != nil {
return nil, fmt.Errorf("creating redirect URI from ingress: %w", err)
}
@@ -105,16 +106,16 @@ func NewProvider(cfg *config.Config) (Provider, error) {
}, nil
}
func redirectURI(ingress string) (string, error) {
func RedirectURI(ingress string) (string, error) {
if len(ingress) == 0 {
return "", fmt.Errorf("ingress cannot be empty")
}
base, err := url.Parse(ingress)
if err != nil {
return "", err
}
callbackPath, err := url.Parse(paths.OAuth2 + paths.Callback)
if err != nil {
return "", err
}
return base.ResolveReference(callbackPath).String(), nil
base.Path = path.Join(base.Path, paths.OAuth2, paths.Callback)
return base.String(), nil
}
+48
View File
@@ -0,0 +1,48 @@
package provider_test
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/nais/wonderwall/pkg/provider"
)
func TestRedirectURI(t *testing.T) {
for _, test := range []struct {
input string
want string
err error
}{
{
input: "https://nav.no/dagpenger",
want: "https://nav.no/dagpenger/oauth2/callback",
},
{
input: "https://nav.no/dagpenger/soknad",
want: "https://nav.no/dagpenger/soknad/oauth2/callback",
},
{
input: "https://nav.no",
want: "https://nav.no/oauth2/callback",
},
{
input: "https://nav.no/",
want: "https://nav.no/oauth2/callback",
},
{
input: "",
err: fmt.Errorf("ingress cannot be empty"),
},
} {
actual, err := provider.RedirectURI(test.input)
if test.err != nil {
assert.EqualError(t, err, test.err.Error())
} else {
assert.NoError(t, err)
}
assert.Equal(t, test.want, actual)
}
}