| 1 |
package op |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"log" |
| 6 |
"net/http" |
| 7 |
"net/url" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"github.com/muhlemmer/httpforwarded" |
| 11 |
"golang.org/x/text/language" |
| 12 |
) |
| 13 |
|
| 14 |
var ( |
| 15 |
ErrInvalidIssuerPath = errors.New("no fragments or query allowed for issuer") |
| 16 |
ErrInvalidIssuerNoIssuer = errors.New("missing issuer") |
| 17 |
ErrInvalidIssuerURL = errors.New("invalid url for issuer") |
| 18 |
ErrInvalidIssuerMissingHost = errors.New("host for issuer missing") |
| 19 |
ErrInvalidIssuerHTTPS = errors.New("scheme for issuer must be `https`") |
| 20 |
) |
| 21 |
|
| 22 |
type Configuration interface { |
| 23 |
IssuerFromRequest(r *http.Request) string |
| 24 |
Insecure() bool |
| 25 |
AuthorizationEndpoint() *Endpoint |
| 26 |
TokenEndpoint() *Endpoint |
| 27 |
IntrospectionEndpoint() *Endpoint |
| 28 |
UserinfoEndpoint() *Endpoint |
| 29 |
RevocationEndpoint() *Endpoint |
| 30 |
EndSessionEndpoint() *Endpoint |
| 31 |
KeysEndpoint() *Endpoint |
| 32 |
DeviceAuthorizationEndpoint() *Endpoint |
| 33 |
CheckSessionIframe() *Endpoint |
| 34 |
|
| 35 |
AuthMethodPostSupported() bool |
| 36 |
CodeMethodS256Supported() bool |
| 37 |
AuthMethodPrivateKeyJWTSupported() bool |
| 38 |
TokenEndpointSigningAlgorithmsSupported() []string |
| 39 |
GrantTypeRefreshTokenSupported() bool |
| 40 |
GrantTypeTokenExchangeSupported() bool |
| 41 |
GrantTypeJWTAuthorizationSupported() bool |
| 42 |
GrantTypeClientCredentialsSupported() bool |
| 43 |
GrantTypeDeviceCodeSupported() bool |
| 44 |
IntrospectionAuthMethodPrivateKeyJWTSupported() bool |
| 45 |
IntrospectionEndpointSigningAlgorithmsSupported() []string |
| 46 |
RevocationAuthMethodPrivateKeyJWTSupported() bool |
| 47 |
RevocationEndpointSigningAlgorithmsSupported() []string |
| 48 |
RequestObjectSupported() bool |
| 49 |
RequestObjectSigningAlgorithmsSupported() []string |
| 50 |
|
| 51 |
SupportedUILocales() []language.Tag |
| 52 |
DeviceAuthorization() DeviceAuthorizationConfig |
| 53 |
|
| 54 |
BackChannelLogoutSupported() bool |
| 55 |
BackChannelLogoutSessionSupported() bool |
| 56 |
} |
| 57 |
|
| 58 |
type IssuerFromRequest func(r *http.Request) string |
| 59 |
|
| 60 |
func IssuerFromHost(path string) func(bool) (IssuerFromRequest, error) { |
| 61 |
return issuerFromForwardedOrHost(path, new(issuerConfig)) |
| 62 |
} |
| 63 |
|
| 64 |
type IssuerFromOption func(c *issuerConfig) |
| 65 |
|
| 66 |
// WithIssuerFromCustomHeaders can be used to customize the header names used. |
| 67 |
// The same rules apply where the first successful host is returned. |
| 68 |
func WithIssuerFromCustomHeaders(headers ...string) IssuerFromOption { |
| 69 |
return func(c *issuerConfig) { |
| 70 |
for i, h := range headers { |
| 71 |
headers[i] = http.CanonicalHeaderKey(h) |
| 72 |
} |
| 73 |
c.headers = headers |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
type issuerConfig struct { |
| 78 |
headers []string |
| 79 |
} |
| 80 |
|
| 81 |
// IssuerFromForwardedOrHost tries to establish the Issuer based |
| 82 |
// on the Forwarded header host field. |
| 83 |
// If multiple Forwarded headers are present, the first mention |
| 84 |
// of the host field will be used. |
| 85 |
// If the Forwarded header is not present, no host field is found, |
| 86 |
// or there is a parser error the Request Host will be used as a fallback. |
| 87 |
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded |
| 88 |
func IssuerFromForwardedOrHost(path string, opts ...IssuerFromOption) func(bool) (IssuerFromRequest, error) { |
| 89 |
c := &issuerConfig{ |
| 90 |
headers: []string{http.CanonicalHeaderKey("forwarded")}, |
| 91 |
} |
| 92 |
for _, opt := range opts { |
| 93 |
opt(c) |
| 94 |
} |
| 95 |
|
| 96 |
return issuerFromForwardedOrHost(path, c) |
| 97 |
} |
| 98 |
|
| 99 |
func issuerFromForwardedOrHost(path string, c *issuerConfig) func(bool) (IssuerFromRequest, error) { |
| 100 |
return func(allowInsecure bool) (IssuerFromRequest, error) { |
| 101 |
issuerPath, err := url.Parse(path) |
| 102 |
if err != nil { |
| 103 |
return nil, ErrInvalidIssuerURL |
| 104 |
} |
| 105 |
if err := ValidateIssuerPath(issuerPath); err != nil { |
| 106 |
return nil, err |
| 107 |
} |
| 108 |
return func(r *http.Request) string { |
| 109 |
if host, ok := hostFromForwarded(r, c.headers); ok { |
| 110 |
return dynamicIssuer(host, path, allowInsecure) |
| 111 |
} |
| 112 |
return dynamicIssuer(r.Host, path, allowInsecure) |
| 113 |
}, nil |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
func hostFromForwarded(r *http.Request, headers []string) (host string, ok bool) { |
| 118 |
for _, header := range headers { |
| 119 |
hosts, err := httpforwarded.ParseParameter("host", r.Header[header]) |
| 120 |
if err != nil { |
| 121 |
log.Printf("Err: issuer from forwarded header: %v", err) // TODO change to slog on next branch |
| 122 |
continue |
| 123 |
} |
| 124 |
if len(hosts) > 0 { |
| 125 |
return hosts[0], true |
| 126 |
} |
| 127 |
} |
| 128 |
return "", false |
| 129 |
} |
| 130 |
|
| 131 |
func StaticIssuer(issuer string) func(bool) (IssuerFromRequest, error) { |
| 132 |
return func(allowInsecure bool) (IssuerFromRequest, error) { |
| 133 |
if err := ValidateIssuer(issuer, allowInsecure); err != nil { |
| 134 |
return nil, err |
| 135 |
} |
| 136 |
return func(_ *http.Request) string { |
| 137 |
return issuer |
| 138 |
}, nil |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
func ValidateIssuer(issuer string, allowInsecure bool) error { |
| 143 |
if issuer == "" { |
| 144 |
return ErrInvalidIssuerNoIssuer |
| 145 |
} |
| 146 |
u, err := url.Parse(issuer) |
| 147 |
if err != nil { |
| 148 |
return ErrInvalidIssuerURL |
| 149 |
} |
| 150 |
if u.Host == "" { |
| 151 |
return ErrInvalidIssuerMissingHost |
| 152 |
} |
| 153 |
if u.Scheme != "https" { |
| 154 |
if !devLocalAllowed(u, allowInsecure) { |
| 155 |
return ErrInvalidIssuerHTTPS |
| 156 |
} |
| 157 |
} |
| 158 |
return ValidateIssuerPath(u) |
| 159 |
} |
| 160 |
|
| 161 |
func ValidateIssuerPath(issuer *url.URL) error { |
| 162 |
if issuer.Fragment != "" || len(issuer.Query()) > 0 { |
| 163 |
return ErrInvalidIssuerPath |
| 164 |
} |
| 165 |
return nil |
| 166 |
} |
| 167 |
|
| 168 |
func devLocalAllowed(url *url.URL, allowInsecure bool) bool { |
| 169 |
if !allowInsecure { |
| 170 |
return false |
| 171 |
} |
| 172 |
return url.Scheme == "http" |
| 173 |
} |
| 174 |
|
| 175 |
func dynamicIssuer(issuer, path string, allowInsecure bool) string { |
| 176 |
schema := "https" |
| 177 |
if allowInsecure { |
| 178 |
schema = "http" |
| 179 |
} |
| 180 |
if len(path) > 0 && !strings.HasPrefix(path, "/") { |
| 181 |
path = "/" + path |
| 182 |
} |
| 183 |
return schema + "://" + issuer + path |
| 184 |
} |