normalizeAudienceURI implements RFC 3986 URI normalization for OAuth2 audience comparison. This ensures consistent audience matching between authorization and token validation.
(audienceURI string)
| 733 | // normalizeAudienceURI implements RFC 3986 URI normalization for OAuth2 audience comparison. |
| 734 | // This ensures consistent audience matching between authorization and token validation. |
| 735 | func 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. |