Skip to content

Commit 7c0ab8b

Browse files
authored
Make CORS work for oauth2 handlers (#28184)
Fix #25473 Although there was `m.Post("/login/oauth/access_token", CorsHandler()...`, it never really worked, because it still lacks the "OPTIONS" handler.
1 parent 778d604 commit 7c0ab8b

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

modules/web/route.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ func (r *Route) Get(pattern string, h ...any) {
136136
r.Methods("GET", pattern, h...)
137137
}
138138

139+
func (r *Route) Options(pattern string, h ...any) {
140+
r.Methods("OPTIONS", pattern, h...)
141+
}
142+
139143
// GetOptions delegate get and options method
140144
func (r *Route) GetOptions(pattern string, h ...any) {
141145
r.Methods("GET,OPTIONS", pattern, h...)

routers/web/misc/misc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ func DummyOK(w http.ResponseWriter, req *http.Request) {
3333
w.WriteHeader(http.StatusOK)
3434
}
3535

36+
func DummyBadRequest(w http.ResponseWriter, req *http.Request) {
37+
w.WriteHeader(http.StatusBadRequest)
38+
}
39+
3640
func RobotsTxt(w http.ResponseWriter, req *http.Request) {
3741
robotsTxt := util.FilePathJoinAbs(setting.CustomPath, "public/robots.txt")
3842
if ok, _ := util.IsExist(robotsTxt); !ok {

routers/web/web.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,10 @@ func registerRoutes(m *web.Route) {
533533
m.Post("/authorize", web.Bind(forms.AuthorizationForm{}), auth.AuthorizeOAuth)
534534
}, ignSignInAndCsrf, reqSignIn)
535535
m.Get("/login/oauth/userinfo", ignSignInAndCsrf, auth.InfoOAuth)
536+
m.Options("/login/oauth/access_token", CorsHandler(), misc.DummyBadRequest)
536537
m.Post("/login/oauth/access_token", CorsHandler(), web.Bind(forms.AccessTokenForm{}), ignSignInAndCsrf, auth.AccessTokenOAuth)
537538
m.Get("/login/oauth/keys", ignSignInAndCsrf, auth.OIDCKeys)
539+
m.Options("/login/oauth/introspect", CorsHandler(), misc.DummyBadRequest)
538540
m.Post("/login/oauth/introspect", CorsHandler(), web.Bind(forms.IntrospectTokenForm{}), ignSignInAndCsrf, auth.IntrospectOAuth)
539541

540542
m.Group("/user/settings", func() {

0 commit comments

Comments
 (0)