Skip to content

Commit 23838c2

Browse files
GiteaBotwxiaoguang
andauthored
Make CORS work for oauth2 handlers (#28184) (#28185)
Backport #28184 Fix #25473 Although there was `m.Post("/login/oauth/access_token", CorsHandler()...`, it never really worked, because it still lacks the "OPTIONS" handler. Co-authored-by: wxiaoguang <[email protected]>
1 parent f9763f1 commit 23838c2

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
@@ -532,8 +532,10 @@ func registerRoutes(m *web.Route) {
532532
m.Post("/authorize", web.Bind(forms.AuthorizationForm{}), auth.AuthorizeOAuth)
533533
}, ignSignInAndCsrf, reqSignIn)
534534
m.Get("/login/oauth/userinfo", ignSignInAndCsrf, auth.InfoOAuth)
535+
m.Options("/login/oauth/access_token", CorsHandler(), misc.DummyBadRequest)
535536
m.Post("/login/oauth/access_token", CorsHandler(), web.Bind(forms.AccessTokenForm{}), ignSignInAndCsrf, auth.AccessTokenOAuth)
536537
m.Get("/login/oauth/keys", ignSignInAndCsrf, auth.OIDCKeys)
538+
m.Options("/login/oauth/introspect", CorsHandler(), misc.DummyBadRequest)
537539
m.Post("/login/oauth/introspect", CorsHandler(), web.Bind(forms.IntrospectTokenForm{}), ignSignInAndCsrf, auth.IntrospectOAuth)
538540

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

0 commit comments

Comments
 (0)