|
1 | 1 | package oauth
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "github.com/centralmind/gateway/cors" |
4 | 5 | "net/http"
|
5 | 6 | )
|
6 | 7 |
|
7 | 8 | // CORSMiddleware applies standard CORS headers to the response
|
8 | 9 | func CORSMiddleware(handler http.Handler) http.Handler {
|
9 | 10 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
10 |
| - // Add CORS headers |
11 |
| - w.Header().Set("Access-Control-Allow-Origin", "*") |
12 |
| - w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS") |
13 |
| - w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization") |
14 |
| - |
15 |
| - // Handle preflight OPTIONS request |
16 |
| - if r.Method == "OPTIONS" { |
17 |
| - w.WriteHeader(http.StatusOK) |
| 11 | + cors.ApplyCORSHeaders(w, "GET, POST") |
| 12 | + if cors.HandlePreflight(w, r) { |
18 | 13 | return
|
19 | 14 | }
|
20 |
| - |
21 | 15 | // Call the original handler
|
22 | 16 | handler.ServeHTTP(w, r)
|
23 | 17 | })
|
24 | 18 | }
|
25 |
| - |
26 |
| -// ApplyCORSHeaders adds the standard CORS headers to a response |
27 |
| -// For handlers that are not wrapped in middleware |
28 |
| -func ApplyCORSHeaders(w http.ResponseWriter, allowedMethods string) { |
29 |
| - w.Header().Set("Access-Control-Allow-Origin", "*") |
30 |
| - w.Header().Set("Access-Control-Allow-Methods", allowedMethods+", OPTIONS") |
31 |
| - w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization") |
32 |
| -} |
33 |
| - |
34 |
| -// HandlePreflight checks if the request is a preflight OPTIONS request and handles it |
35 |
| -// Returns true if the request was handled (caller should return immediately) |
36 |
| -func HandlePreflight(w http.ResponseWriter, r *http.Request) bool { |
37 |
| - if r.Method == "OPTIONS" { |
38 |
| - w.WriteHeader(http.StatusOK) |
39 |
| - return true |
40 |
| - } |
41 |
| - return false |
42 |
| -} |
0 commit comments