mirror of
https://github.com/nais/wonderwall.git
synced 2026-05-21 15:52:54 +00:00
refactor: move redirect URI creation to openid pkg
This commit is contained in:
23
pkg/openid/redirect_uri.go
Normal file
23
pkg/openid/redirect_uri.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package openid
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path"
|
||||
|
||||
"github.com/nais/wonderwall/pkg/router/paths"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
base.Path = path.Join(base.Path, paths.OAuth2, paths.Callback)
|
||||
return base.String(), nil
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package provider_test
|
||||
package openid_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -6,14 +6,14 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/nais/wonderwall/pkg/provider"
|
||||
"github.com/nais/wonderwall/pkg/openid"
|
||||
)
|
||||
|
||||
func TestRedirectURI(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
input string
|
||||
want string
|
||||
err error
|
||||
err error
|
||||
}{
|
||||
{
|
||||
input: "https://nav.no/dagpenger",
|
||||
@@ -33,16 +33,15 @@ func TestRedirectURI(t *testing.T) {
|
||||
},
|
||||
{
|
||||
input: "",
|
||||
err: fmt.Errorf("ingress cannot be empty"),
|
||||
err: fmt.Errorf("ingress cannot be empty"),
|
||||
},
|
||||
} {
|
||||
actual, err := provider.RedirectURI(test.input)
|
||||
actual, err := openid.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)
|
||||
}
|
||||
|
||||
assert.Equal(t, test.want, actual)
|
||||
}
|
||||
}
|
||||
@@ -3,14 +3,11 @@ package provider
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path"
|
||||
|
||||
"github.com/lestrrat-go/jwx/jwk"
|
||||
|
||||
"github.com/nais/wonderwall/pkg/config"
|
||||
"github.com/nais/wonderwall/pkg/openid"
|
||||
"github.com/nais/wonderwall/pkg/router/paths"
|
||||
)
|
||||
|
||||
type Provider interface {
|
||||
@@ -53,7 +50,7 @@ func NewProvider(cfg *config.Config) (Provider, error) {
|
||||
return nil, fmt.Errorf("missing required config %s", config.Ingress)
|
||||
}
|
||||
|
||||
redirectURI, err := RedirectURI(ingress)
|
||||
redirectURI, err := openid.RedirectURI(ingress)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("creating redirect URI from ingress: %w", err)
|
||||
}
|
||||
@@ -105,17 +102,3 @@ func NewProvider(cfg *config.Config) (Provider, error) {
|
||||
jwkSet: jwkSet,
|
||||
}, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
base.Path = path.Join(base.Path, paths.OAuth2, paths.Callback)
|
||||
return base.String(), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user