feat: issuer from Forwarded header (#443)
This commit is contained in:
parent
607a76c154
commit
364a7591d6
5 changed files with 149 additions and 6 deletions
|
@ -2,10 +2,12 @@ package op
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/muhlemmer/httpforwarded"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
|
@ -52,6 +54,21 @@ type Configuration interface {
|
|||
type IssuerFromRequest func(r *http.Request) string
|
||||
|
||||
func IssuerFromHost(path string) func(bool) (IssuerFromRequest, error) {
|
||||
return issuerFromForwardedOrHost(path, false)
|
||||
}
|
||||
|
||||
// IssuerFromForwardedOrHost tries to establish the Issuer based
|
||||
// on the Forwarded header host field.
|
||||
// If multiple Forwarded headers are present, the first mention
|
||||
// of the host field will be used.
|
||||
// If the Forwarded header is not present, no host field is found,
|
||||
// or there is a parser error the Request Host will be used as a fallback.
|
||||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded
|
||||
func IssuerFromForwardedOrHost(path string) func(bool) (IssuerFromRequest, error) {
|
||||
return issuerFromForwardedOrHost(path, true)
|
||||
}
|
||||
|
||||
func issuerFromForwardedOrHost(path string, parseForwarded bool) func(bool) (IssuerFromRequest, error) {
|
||||
return func(allowInsecure bool) (IssuerFromRequest, error) {
|
||||
issuerPath, err := url.Parse(path)
|
||||
if err != nil {
|
||||
|
@ -61,11 +78,28 @@ func IssuerFromHost(path string) func(bool) (IssuerFromRequest, error) {
|
|||
return nil, err
|
||||
}
|
||||
return func(r *http.Request) string {
|
||||
if parseForwarded {
|
||||
if host, ok := hostFromForwarded(r); ok {
|
||||
return dynamicIssuer(host, path, allowInsecure)
|
||||
}
|
||||
}
|
||||
return dynamicIssuer(r.Host, path, allowInsecure)
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func hostFromForwarded(r *http.Request) (host string, ok bool) {
|
||||
fwd, err := httpforwarded.ParseFromRequest(r)
|
||||
if err != nil {
|
||||
log.Printf("Err: issuer from forwarded header: %v", err) // TODO change to slog on next branch
|
||||
return "", false
|
||||
}
|
||||
if fwd == nil || len(fwd["host"]) == 0 {
|
||||
return "", false
|
||||
}
|
||||
return fwd["host"][0], true
|
||||
}
|
||||
|
||||
func StaticIssuer(issuer string) func(bool) (IssuerFromRequest, error) {
|
||||
return func(allowInsecure bool) (IssuerFromRequest, error) {
|
||||
if err := ValidateIssuer(issuer, allowInsecure); err != nil {
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestValidateIssuer(t *testing.T) {
|
||||
|
@ -234,7 +235,7 @@ func TestIssuerFromHost(t *testing.T) {
|
|||
},
|
||||
},
|
||||
{
|
||||
"custom path unsecure",
|
||||
"custom path insecure",
|
||||
args{
|
||||
path: "/custom/",
|
||||
allowInsecure: true,
|
||||
|
@ -261,6 +262,109 @@ func TestIssuerFromHost(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestIssuerFromForwardedOrHost(t *testing.T) {
|
||||
type args struct {
|
||||
path string
|
||||
target string
|
||||
forwarded []string
|
||||
}
|
||||
type res struct {
|
||||
issuer string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
res res
|
||||
}{
|
||||
{
|
||||
"header parse error",
|
||||
args{
|
||||
path: "/custom/",
|
||||
target: "https://issuer.com",
|
||||
forwarded: []string{"~~~"},
|
||||
},
|
||||
res{
|
||||
issuer: "https://issuer.com/custom/",
|
||||
},
|
||||
},
|
||||
{
|
||||
"no forwarded header",
|
||||
args{
|
||||
path: "/custom/",
|
||||
target: "https://issuer.com",
|
||||
},
|
||||
res{
|
||||
issuer: "https://issuer.com/custom/",
|
||||
},
|
||||
},
|
||||
// by=<identifier>;for=<identifier>;host=<host>;proto=<http|https>
|
||||
{
|
||||
"forwarded header without host",
|
||||
args{
|
||||
path: "/custom/",
|
||||
target: "https://issuer.com",
|
||||
forwarded: []string{
|
||||
`by=identifier;for=identifier;proto=https`,
|
||||
},
|
||||
},
|
||||
res{
|
||||
issuer: "https://issuer.com/custom/",
|
||||
},
|
||||
},
|
||||
{
|
||||
"forwarded header with host",
|
||||
args{
|
||||
path: "/custom/",
|
||||
target: "https://issuer.com",
|
||||
forwarded: []string{
|
||||
`by=identifier;for=identifier;host=first.com;proto=https`,
|
||||
},
|
||||
},
|
||||
res{
|
||||
issuer: "https://first.com/custom/",
|
||||
},
|
||||
},
|
||||
{
|
||||
"forwarded header with multiple hosts",
|
||||
args{
|
||||
path: "/custom/",
|
||||
target: "https://issuer.com",
|
||||
forwarded: []string{
|
||||
`by=identifier;for=identifier;host=first.com;proto=https,host=second.com`,
|
||||
},
|
||||
},
|
||||
res{
|
||||
issuer: "https://first.com/custom/",
|
||||
},
|
||||
},
|
||||
{
|
||||
"multiple forwarded headers hosts",
|
||||
args{
|
||||
path: "/custom/",
|
||||
target: "https://issuer.com",
|
||||
forwarded: []string{
|
||||
`by=identifier;for=identifier;host=first.com;proto=https,host=second.com`,
|
||||
`by=identifier;for=identifier;host=third.com;proto=https`,
|
||||
},
|
||||
},
|
||||
res{
|
||||
issuer: "https://first.com/custom/",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
issuer, err := IssuerFromForwardedOrHost(tt.args.path)(false)
|
||||
require.NoError(t, err)
|
||||
req := httptest.NewRequest("", tt.args.target, nil)
|
||||
if tt.args.forwarded != nil {
|
||||
req.Header["Forwarded"] = tt.args.forwarded
|
||||
}
|
||||
assert.Equal(t, tt.res.issuer, issuer(req))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStaticIssuer(t *testing.T) {
|
||||
type args struct {
|
||||
issuer string
|
||||
|
|
|
@ -169,10 +169,17 @@ func NewOpenIDProvider(issuer string, config *Config, storage Storage, opOpts ..
|
|||
return newProvider(config, storage, StaticIssuer(issuer), opOpts...)
|
||||
}
|
||||
|
||||
// NewForwardedOpenIDProvider tries to establishes the issuer from the request Host.
|
||||
func NewDynamicOpenIDProvider(path string, config *Config, storage Storage, opOpts ...Option) (*Provider, error) {
|
||||
return newProvider(config, storage, IssuerFromHost(path), opOpts...)
|
||||
}
|
||||
|
||||
// NewForwardedOpenIDProvider tries to establish the Issuer from a Forwarded request header, if it is set.
|
||||
// See [IssuerFromForwardedOrHost] for details.
|
||||
func NewForwardedOpenIDProvider(path string, config *Config, storage Storage, opOpts ...Option) (*Provider, error) {
|
||||
return newProvider(config, storage, IssuerFromForwardedOrHost(path), opOpts...)
|
||||
}
|
||||
|
||||
func newProvider(config *Config, storage Storage, issuer func(bool) (IssuerFromRequest, error), opOpts ...Option) (_ *Provider, err error) {
|
||||
o := &Provider{
|
||||
config: config,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue