You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

filter.go 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2023 The casbin Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package routers
  15. import (
  16. "fmt"
  17. "net/http"
  18. "strings"
  19. "github.com/astaxie/beego"
  20. "github.com/astaxie/beego/context"
  21. "github.com/casibase/casibase/conf"
  22. "github.com/casibase/casibase/controllers"
  23. "github.com/casibase/casibase/util"
  24. )
  25. const (
  26. headerAllowOrigin = "Access-Control-Allow-Origin"
  27. headerAllowMethods = "Access-Control-Allow-Methods"
  28. headerAllowHeaders = "Access-Control-Allow-Headers"
  29. )
  30. func TransparentStatic(ctx *context.Context) {
  31. urlPath := ctx.Request.URL.Path
  32. if strings.HasPrefix(urlPath, "/api/") {
  33. return
  34. }
  35. landingFolder := beego.AppConfig.String("landingFolder")
  36. if landingFolder != "" {
  37. if urlPath == "" || urlPath == "/" || urlPath == "/about" {
  38. http.ServeFile(ctx.ResponseWriter, ctx.Request, fmt.Sprintf("../%s/web/build/index.html", landingFolder))
  39. return
  40. }
  41. landingPath := fmt.Sprintf("../%s/web/build%s", landingFolder, urlPath)
  42. if util.FileExist(landingPath) {
  43. http.ServeFile(ctx.ResponseWriter, ctx.Request, landingPath)
  44. return
  45. }
  46. }
  47. if strings.HasPrefix(urlPath, "/storage") {
  48. ctx.Output.Header(headerAllowOrigin, "*")
  49. ctx.Output.Header(headerAllowMethods, "POST, GET, OPTIONS, DELETE")
  50. ctx.Output.Header(headerAllowHeaders, "Content-Type, Authorization")
  51. urlPath = strings.TrimPrefix(urlPath, "/storage/")
  52. urlPath = strings.Replace(urlPath, "|", ":", 1)
  53. http.ServeFile(ctx.ResponseWriter, ctx.Request, urlPath)
  54. return
  55. }
  56. path := "web/build"
  57. if urlPath == "/" {
  58. path += "/index.html"
  59. } else {
  60. path += urlPath
  61. }
  62. if util.FileExist(path) {
  63. http.ServeFile(ctx.ResponseWriter, ctx.Request, path)
  64. } else {
  65. http.ServeFile(ctx.ResponseWriter, ctx.Request, "web/build/index.html")
  66. }
  67. }
  68. func ApiFilter(ctx *context.Context) {
  69. method := ctx.Request.Method
  70. urlPath := getUrlPath(ctx.Request.URL.Path)
  71. if conf.IsDemoMode() {
  72. if !isAllowedInDemoMode(method, urlPath) {
  73. controllers.DenyRequest(ctx)
  74. }
  75. }
  76. }
  77. func getUrlPath(urlPath string) string {
  78. if strings.HasPrefix(urlPath, "/cas") && (strings.HasSuffix(urlPath, "/serviceValidate") || strings.HasSuffix(urlPath, "/proxy") || strings.HasSuffix(urlPath, "/proxyValidate") || strings.HasSuffix(urlPath, "/validate") || strings.HasSuffix(urlPath, "/p3/serviceValidate") || strings.HasSuffix(urlPath, "/p3/proxyValidate") || strings.HasSuffix(urlPath, "/samlValidate")) {
  79. return "/cas"
  80. }
  81. if strings.HasPrefix(urlPath, "/api/login/oauth") {
  82. return "/api/login/oauth"
  83. }
  84. if strings.HasPrefix(urlPath, "/api/webauthn") {
  85. return "/api/webauthn"
  86. }
  87. return urlPath
  88. }
  89. func isAllowedInDemoMode(method string, urlPath string) bool {
  90. if method == "POST" && !(strings.HasPrefix(urlPath, "/api/signin") || urlPath == "/api/signout") {
  91. return false
  92. }
  93. return true
  94. }