Skip to content

Commit

Permalink
fix: fix CORS error after sucessful OPTION (beego#838)
Browse files Browse the repository at this point in the history
  • Loading branch information
aecra authored Jun 30, 2022
1 parent cd902a2 commit 919eaf1
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions routers/cors_filter.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package routers

import (
"net/http"

"github.com/astaxie/beego/context"
"github.com/casdoor/casdoor/conf"
"github.com/casdoor/casdoor/object"
)

Expand All @@ -15,17 +30,20 @@ const (
)

func CorsFilter(ctx *context.Context) {
if ctx.Input.Method() == "OPTIONS" {
origin := ctx.Input.Header(headerOrigin)

origin := ctx.Input.Header(headerOrigin)
if origin != "" && origin != conf.GetConfigString("origin") {
if object.IsAllowOrigin(origin) {
ctx.Output.Header(headerAllowOrigin, origin)
ctx.Output.Header(headerAllowMethods, "POST, GET, OPTIONS")
ctx.Output.Header(headerAllowHeaders, "Content-Type, Authorization")
ctx.ResponseWriter.WriteHeader(http.StatusOK)
} else {
ctx.ResponseWriter.WriteHeader(http.StatusForbidden)
return
}

if ctx.Input.Method() == "OPTIONS" {
ctx.ResponseWriter.WriteHeader(http.StatusOK)
return
}
return
}
}

0 comments on commit 919eaf1

Please sign in to comment.