ProtectedResourceMetadataHandler returns an http.Handler that serves OAuth 2.0 protected resource metadata (RFC 9728) with CORS support. This handler allows cross-origin requests from any origin (Access-Control-Allow-Origin: *) because OAuth metadata is public information intended for client discov
(metadata *oauthex.ProtectedResourceMetadata)
| 152 | // For more sophisticated CORS policies or to restrict origins, wrap this handler with a |
| 153 | // CORS middleware like github.com/rs/cors or github.com/jub0bs/cors. |
| 154 | func ProtectedResourceMetadataHandler(metadata *oauthex.ProtectedResourceMetadata) http.Handler { |
| 155 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 156 | // Set CORS headers for cross-origin client discovery. |
| 157 | // OAuth metadata is public information, so allowing any origin is safe. |
| 158 | w.Header().Set("Access-Control-Allow-Origin", "*") |
| 159 | w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS") |
| 160 | w.Header().Set("Access-Control-Allow-Headers", "Content-Type") |
| 161 | |
| 162 | // Handle CORS preflight requests |
| 163 | if r.Method == http.MethodOptions { |
| 164 | w.WriteHeader(http.StatusNoContent) |
| 165 | return |
| 166 | } |
| 167 | |
| 168 | // Only GET allowed for metadata retrieval |
| 169 | if r.Method != http.MethodGet { |
| 170 | http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
| 171 | return |
| 172 | } |
| 173 | |
| 174 | w.Header().Set("Content-Type", "application/json") |
| 175 | if err := json.NewEncoder(w).Encode(metadata); err != nil { |
| 176 | http.Error(w, "Failed to encode metadata", http.StatusInternalServerError) |
| 177 | return |
| 178 | } |
| 179 | }) |
| 180 | } |
searching dependent graphs…