MCPcopy Index your code
hub / github.com/coder/coder / normalizeAudienceURI

Function normalizeAudienceURI

coderd/httpmw/apikey.go:735–776  ·  view source on GitHub ↗

normalizeAudienceURI implements RFC 3986 URI normalization for OAuth2 audience comparison. This ensures consistent audience matching between authorization and token validation.

(audienceURI string)

Source from the content-addressed store, hash-verified

733// normalizeAudienceURI implements RFC 3986 URI normalization for OAuth2 audience comparison.
734// This ensures consistent audience matching between authorization and token validation.
735func normalizeAudienceURI(audienceURI string) string {
736 if audienceURI == "" {
737 return ""
738 }
739
740 u, err := url.Parse(audienceURI)
741 if err != nil {
742 // If parsing fails, return as-is to avoid breaking existing functionality
743 return audienceURI
744 }
745
746 // Apply RFC 3986 syntax-based normalization:
747
748 // 1. Scheme normalization - case-insensitive
749 u.Scheme = strings.ToLower(u.Scheme)
750
751 // 2. Host normalization - case-insensitive and IDN (punnycode) normalization
752 u.Host = normalizeHost(u.Host)
753
754 // 3. Remove default ports for HTTP/HTTPS
755 if (u.Scheme == "http" && strings.HasSuffix(u.Host, ":80")) ||
756 (u.Scheme == "https" && strings.HasSuffix(u.Host, ":443")) {
757 // Extract host without default port
758 if idx := strings.LastIndex(u.Host, ":"); idx > 0 {
759 u.Host = u.Host[:idx]
760 }
761 }
762
763 // 4. Path normalization including dot-segment removal (RFC 3986 Section 6.2.2.3)
764 u.Path = normalizePathSegments(u.Path)
765
766 // 5. Remove fragment - should already be empty due to earlier validation,
767 // but clear it as a safety measure in case validation was bypassed
768 if u.Fragment != "" {
769 // This should not happen if validation is working correctly
770 u.Fragment = ""
771 }
772
773 // 6. Keep query parameters as-is (rarely used in audience URIs but preserved for compatibility)
774
775 return u.String()
776}
777
778// normalizeHost performs host normalization including case-insensitive conversion
779// and IDN (Internationalized Domain Name) punnycode normalization.

Callers 3

extractExpectedAudienceFunction · 0.85
TestNormalizeAudienceURIFunction · 0.85

Calls 4

normalizePathSegmentsFunction · 0.85
normalizeHostFunction · 0.70
ParseMethod · 0.65
StringMethod · 0.45

Tested by 1

TestNormalizeAudienceURIFunction · 0.68