@@ -42,6 +42,7 @@ import (
42
42
"gitea.com/go-chi/session"
43
43
"github.com/NYTimes/gziphandler"
44
44
"github.com/go-chi/chi/middleware"
45
+ "github.com/go-chi/cors"
45
46
"github.com/prometheus/client_golang/prometheus"
46
47
"github.com/tstranex/u2f"
47
48
)
@@ -51,14 +52,32 @@ const (
51
52
GzipMinSize = 1400
52
53
)
53
54
55
+ // CorsHandler return a http handler who set CORS options if enabled by config
56
+ func CorsHandler () func (next http.Handler ) http.Handler {
57
+ if setting .CORSConfig .Enabled {
58
+ return cors .Handler (cors.Options {
59
+ //Scheme: setting.CORSConfig.Scheme, // FIXME: the cors middleware needs scheme option
60
+ AllowedOrigins : setting .CORSConfig .AllowDomain ,
61
+ //setting.CORSConfig.AllowSubdomain // FIXME: the cors middleware needs allowSubdomain option
62
+ AllowedMethods : setting .CORSConfig .Methods ,
63
+ AllowCredentials : setting .CORSConfig .AllowCredentials ,
64
+ MaxAge : int (setting .CORSConfig .MaxAge .Seconds ()),
65
+ })
66
+ }
67
+
68
+ return func (next http.Handler ) http.Handler {
69
+ return next
70
+ }
71
+ }
72
+
54
73
// Routes returns all web routes
55
74
func Routes () * web.Route {
56
75
routes := web .NewRoute ()
57
76
58
77
routes .Use (public .AssetsHandler (& public.Options {
59
78
Directory : path .Join (setting .StaticRootPath , "public" ),
60
79
Prefix : "/assets" ,
61
- CorsHandler : common . CorsHandler (),
80
+ CorsHandler : CorsHandler (),
62
81
}))
63
82
64
83
routes .Use (session .Sessioner (session.Options {
@@ -271,7 +290,7 @@ func RegisterRoutes(m *web.Route) {
271
290
m .Post ("/authorize" , bindIgnErr (forms.AuthorizationForm {}), user .AuthorizeOAuth )
272
291
}, ignSignInAndCsrf , reqSignIn )
273
292
m .Get ("/login/oauth/userinfo" , ignSignInAndCsrf , user .InfoOAuth )
274
- m .Post ("/login/oauth/access_token" , common . CorsHandler (), bindIgnErr (forms.AccessTokenForm {}), ignSignInAndCsrf , user .AccessTokenOAuth )
293
+ m .Post ("/login/oauth/access_token" , CorsHandler (), bindIgnErr (forms.AccessTokenForm {}), ignSignInAndCsrf , user .AccessTokenOAuth )
275
294
276
295
m .Group ("/user/settings" , func () {
277
296
m .Get ("" , userSetting .Profile )
0 commit comments