Files
summercms/modules/wristband/oidc_redirect.go
Jakub Zych 659b42aaa8 feat(19.4-01): OIDC flavour of wristband with an end-to-end tracer
- NewOIDCServer/SetOIDCBackend build a second Server on the same engine;
  Authorize and Token dispatch by flavour, Metadata/Register 404 on OIDC
- OIDCBackend/OIDCTx, KeyStore and OIDCHost seams; MCP Backend untouched
- Discovery (PHP field order), JWKS (stdlib JWK encoding), authorize with
  resume and one-shot consent token, token with an RS256 id_token signed
  through golang-jwt map claims, userinfo
- TestOIDCTracerSmoke verifies the id_token against the served JWKS
2026-10-19 18:18:05 +02:00

80 lines
2.0 KiB
Go

package wristband
import (
"fmt"
"strings"
)
// phpURLEncode ports PHP urlencode (the encoder behind http_build_query's
// default PHP_QUERY_RFC1738): A-Z a-z 0-9 . _ - pass, a space becomes +,
// every other byte becomes an uppercase %XX triplet, so a tilde is %7E.
// The MCP flavour's rfc3986Escape must not be used for OIDC redirects
// (D-14, Pitfall 2).
func phpURLEncode(s string) string {
var b strings.Builder
b.Grow(len(s))
for i := 0; i < len(s); i++ {
c := s[i]
switch {
case (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '.' || c == '_' || c == '-':
b.WriteByte(c)
case c == ' ':
b.WriteByte('+')
default:
fmt.Fprintf(&b, "%%%02X", c)
}
}
return b.String()
}
// appendPHPQuery appends pairs to uri in the given order with
// http_build_query semantics, using & when uri already carries a query and
// ? otherwise (PHP str_contains($uri, '?') ? '&' : '?').
func appendPHPQuery(uri string, pairs [][2]string) string {
parts := make([]string, len(pairs))
for i, p := range pairs {
parts[i] = phpURLEncode(p[0]) + "=" + phpURLEncode(p[1])
}
sep := "?"
if strings.Contains(uri, "?") {
sep = "&"
}
return uri + sep + strings.Join(parts, "&")
}
// phpURLDecode ports PHP urldecode: + becomes a space, a % followed by two
// hex digits becomes that byte, anything else (a lone or malformed %) is
// kept as it is.
func phpURLDecode(s string) string {
var b strings.Builder
b.Grow(len(s))
for i := 0; i < len(s); i++ {
c := s[i]
switch {
case c == '+':
b.WriteByte(' ')
case c == '%' && i+2 < len(s) && isHex(s[i+1]) && isHex(s[i+2]):
b.WriteByte(unhex(s[i+1])<<4 | unhex(s[i+2]))
i += 2
default:
b.WriteByte(c)
}
}
return b.String()
}
func isHex(c byte) bool {
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')
}
func unhex(c byte) byte {
switch {
case c >= '0' && c <= '9':
return c - '0'
case c >= 'a' && c <= 'f':
return c - 'a' + 10
default:
return c - 'A' + 10
}
}