feat(op): issuer from custom headers (#478)

This commit is contained in:
Tim Möhlmann 2023-11-10 14:18:08 +02:00 committed by GitHub
parent 0cfc32345a
commit 7475023a65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 82 additions and 34 deletions

View file

@ -54,7 +54,24 @@ type Configuration interface {
type IssuerFromRequest func(r *http.Request) string type IssuerFromRequest func(r *http.Request) string
func IssuerFromHost(path string) func(bool) (IssuerFromRequest, error) { func IssuerFromHost(path string) func(bool) (IssuerFromRequest, error) {
return issuerFromForwardedOrHost(path, false) return issuerFromForwardedOrHost(path, new(issuerConfig))
}
type IssuerFromOption func(c *issuerConfig)
// WithIssuerFromCustomHeaders can be used to customize the header names used.
// The same rules apply where the first successful host is returned.
func WithIssuerFromCustomHeaders(headers ...string) IssuerFromOption {
return func(c *issuerConfig) {
for i, h := range headers {
headers[i] = http.CanonicalHeaderKey(h)
}
c.headers = headers
}
}
type issuerConfig struct {
headers []string
} }
// IssuerFromForwardedOrHost tries to establish the Issuer based // IssuerFromForwardedOrHost tries to establish the Issuer based
@ -64,11 +81,18 @@ func IssuerFromHost(path string) func(bool) (IssuerFromRequest, error) {
// If the Forwarded header is not present, no host field is found, // 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. // 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 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded
func IssuerFromForwardedOrHost(path string) func(bool) (IssuerFromRequest, error) { func IssuerFromForwardedOrHost(path string, opts ...IssuerFromOption) func(bool) (IssuerFromRequest, error) {
return issuerFromForwardedOrHost(path, true) c := &issuerConfig{
headers: []string{http.CanonicalHeaderKey("forwarded")},
}
for _, opt := range opts {
opt(c)
}
return issuerFromForwardedOrHost(path, c)
} }
func issuerFromForwardedOrHost(path string, parseForwarded bool) func(bool) (IssuerFromRequest, error) { func issuerFromForwardedOrHost(path string, c *issuerConfig) func(bool) (IssuerFromRequest, error) {
return func(allowInsecure bool) (IssuerFromRequest, error) { return func(allowInsecure bool) (IssuerFromRequest, error) {
issuerPath, err := url.Parse(path) issuerPath, err := url.Parse(path)
if err != nil { if err != nil {
@ -78,26 +102,26 @@ func issuerFromForwardedOrHost(path string, parseForwarded bool) func(bool) (Iss
return nil, err return nil, err
} }
return func(r *http.Request) string { return func(r *http.Request) string {
if parseForwarded { if host, ok := hostFromForwarded(r, c.headers); ok {
if host, ok := hostFromForwarded(r); ok {
return dynamicIssuer(host, path, allowInsecure) return dynamicIssuer(host, path, allowInsecure)
} }
}
return dynamicIssuer(r.Host, path, allowInsecure) return dynamicIssuer(r.Host, path, allowInsecure)
}, nil }, nil
} }
} }
func hostFromForwarded(r *http.Request) (host string, ok bool) { func hostFromForwarded(r *http.Request, headers []string) (host string, ok bool) {
fwd, err := httpforwarded.ParseFromRequest(r) for _, header := range headers {
hosts, err := httpforwarded.ParseParameter("host", r.Header[header])
if err != nil { if err != nil {
log.Printf("Err: issuer from forwarded header: %v", err) // TODO change to slog on next branch log.Printf("Err: issuer from forwarded header: %v", err) // TODO change to slog on next branch
return "", false continue
} }
if fwd == nil || len(fwd["host"]) == 0 { if len(hosts) > 0 {
return "", false return hosts[0], true
} }
return fwd["host"][0], true }
return "", false
} }
func StaticIssuer(issuer string) func(bool) (IssuerFromRequest, error) { func StaticIssuer(issuer string) func(bool) (IssuerFromRequest, error) {

View file

@ -1,6 +1,7 @@
package op package op
import ( import (
"net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"testing" "testing"
@ -265,8 +266,9 @@ func TestIssuerFromHost(t *testing.T) {
func TestIssuerFromForwardedOrHost(t *testing.T) { func TestIssuerFromForwardedOrHost(t *testing.T) {
type args struct { type args struct {
path string path string
opts []IssuerFromOption
target string target string
forwarded []string header map[string][]string
} }
type res struct { type res struct {
issuer string issuer string
@ -281,7 +283,7 @@ func TestIssuerFromForwardedOrHost(t *testing.T) {
args{ args{
path: "/custom/", path: "/custom/",
target: "https://issuer.com", target: "https://issuer.com",
forwarded: []string{"~~~"}, header: map[string][]string{"Forwarded": {"~~~~"}},
}, },
res{ res{
issuer: "https://issuer.com/custom/", issuer: "https://issuer.com/custom/",
@ -303,9 +305,9 @@ func TestIssuerFromForwardedOrHost(t *testing.T) {
args{ args{
path: "/custom/", path: "/custom/",
target: "https://issuer.com", target: "https://issuer.com",
forwarded: []string{ header: map[string][]string{"Forwarded": {
`by=identifier;for=identifier;proto=https`, `by=identifier;for=identifier;proto=https`,
}, }},
}, },
res{ res{
issuer: "https://issuer.com/custom/", issuer: "https://issuer.com/custom/",
@ -316,9 +318,9 @@ func TestIssuerFromForwardedOrHost(t *testing.T) {
args{ args{
path: "/custom/", path: "/custom/",
target: "https://issuer.com", target: "https://issuer.com",
forwarded: []string{ header: map[string][]string{"Forwarded": {
`by=identifier;for=identifier;host=first.com;proto=https`, `by=identifier;for=identifier;host=first.com;proto=https`,
}, }},
}, },
res{ res{
issuer: "https://first.com/custom/", issuer: "https://first.com/custom/",
@ -329,9 +331,9 @@ func TestIssuerFromForwardedOrHost(t *testing.T) {
args{ args{
path: "/custom/", path: "/custom/",
target: "https://issuer.com", target: "https://issuer.com",
forwarded: []string{ header: map[string][]string{"Forwarded": {
`by=identifier;for=identifier;host=first.com;proto=https,host=second.com`, `by=identifier;for=identifier;host=first.com;proto=https,host=second.com`,
}, }},
}, },
res{ res{
issuer: "https://first.com/custom/", issuer: "https://first.com/custom/",
@ -342,23 +344,45 @@ func TestIssuerFromForwardedOrHost(t *testing.T) {
args{ args{
path: "/custom/", path: "/custom/",
target: "https://issuer.com", target: "https://issuer.com",
forwarded: []string{ header: map[string][]string{"Forwarded": {
`by=identifier;for=identifier;host=first.com;proto=https,host=second.com`, `by=identifier;for=identifier;host=first.com;proto=https,host=second.com`,
`by=identifier;for=identifier;host=third.com;proto=https`, `by=identifier;for=identifier;host=third.com;proto=https`,
}, }},
}, },
res{ res{
issuer: "https://first.com/custom/", issuer: "https://first.com/custom/",
}, },
}, },
{
"custom header first",
args{
path: "/custom/",
target: "https://issuer.com",
header: map[string][]string{
"Forwarded": {
`by=identifier;for=identifier;host=first.com;proto=https,host=second.com`,
`by=identifier;for=identifier;host=third.com;proto=https`,
},
"X-Custom-Forwarded": {
`by=identifier;for=identifier;host=custom.com;proto=https,host=custom2.com`,
},
},
opts: []IssuerFromOption{
WithIssuerFromCustomHeaders("x-custom-forwarded"),
},
},
res{
issuer: "https://custom.com/custom/",
},
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
issuer, err := IssuerFromForwardedOrHost(tt.args.path)(false) issuer, err := IssuerFromForwardedOrHost(tt.args.path, tt.args.opts...)(false)
require.NoError(t, err) require.NoError(t, err)
req := httptest.NewRequest("", tt.args.target, nil) req := httptest.NewRequest("", tt.args.target, nil)
if tt.args.forwarded != nil { for k, v := range tt.args.header {
req.Header["Forwarded"] = tt.args.forwarded req.Header[http.CanonicalHeaderKey(k)] = v
} }
assert.Equal(t, tt.res.issuer, issuer(req)) assert.Equal(t, tt.res.issuer, issuer(req))
}) })