-
Notifications
You must be signed in to change notification settings - Fork 694
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add cors check Signed-off-by: Shovan Maity <[email protected]> * add cors check custom middleware Signed-off-by: Shovan Maity <[email protected]> * change default url Signed-off-by: Shovan Maity <[email protected]> --------- Signed-off-by: Shovan Maity <[email protected]> Signed-off-by: Shovan Maity <[email protected]>
- Loading branch information
1 parent
902cfde
commit 17085b4
Showing
9 changed files
with
179 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
const ( | ||
AllowedOrigin string = "Access-Control-Allow-Origin" | ||
AllowedMethods string = "Access-Control-Allow-Methods" | ||
AllowedHeaders string = "Access-Control-Allow-Headers" | ||
AllowedCredentials string = "Access-Control-Allow-Credentials" | ||
) | ||
|
||
func ValidateCors(allowedOrigins []string) gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
origin := c.GetHeader("Origin") | ||
if origin == "" { | ||
origin = c.Request.Host | ||
} | ||
|
||
validOrigin := false | ||
for _, allowedOrigin := range allowedOrigins { | ||
match, err := regexp.MatchString(allowedOrigin, origin) | ||
if err == nil && match { | ||
validOrigin = true | ||
c.Writer.Header().Set(AllowedOrigin, origin) | ||
break | ||
} | ||
} | ||
|
||
if !validOrigin { | ||
c.JSON(http.StatusForbidden, gin.H{ | ||
"error": "Invalid origin", | ||
}) | ||
c.Abort() | ||
return | ||
} | ||
|
||
c.Writer.Header().Set(AllowedMethods, strings.Join([]string{ | ||
"GET", | ||
"POST", | ||
"PUT", | ||
"DELETE", | ||
"OPTIONS", | ||
}, ",")) | ||
c.Writer.Header().Set(AllowedHeaders, "*") | ||
c.Writer.Header().Set(AllowedCredentials, "true") | ||
|
||
c.Next() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils" | ||
) | ||
|
||
const ( | ||
AllowedOrigin string = "Access-Control-Allow-Origin" | ||
AllowedMethods string = "Access-Control-Allow-Methods" | ||
AllowedHeaders string = "Access-Control-Allow-Headers" | ||
AllowedCredentials string = "Access-Control-Allow-Credentials" | ||
) | ||
|
||
func ValidateCors() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
allowedOrigins := utils.Config.AllowedOrigins | ||
origin := c.GetHeader("Origin") | ||
if origin == "" { | ||
origin = c.Request.Host | ||
} | ||
|
||
validOrigin := false | ||
for _, allowedOrigin := range allowedOrigins { | ||
match, err := regexp.MatchString(allowedOrigin, origin) | ||
if err == nil && match { | ||
validOrigin = true | ||
c.Writer.Header().Set(AllowedOrigin, origin) | ||
break | ||
} | ||
} | ||
|
||
if !validOrigin { | ||
c.JSON(http.StatusForbidden, gin.H{ | ||
"error": "Invalid origin", | ||
}) | ||
c.Abort() | ||
return | ||
} | ||
|
||
c.Writer.Header().Set(AllowedMethods, strings.Join([]string{ | ||
"GET", | ||
"POST", | ||
"PUT", | ||
"DELETE", | ||
"OPTIONS", | ||
}, ",")) | ||
c.Writer.Header().Set(AllowedHeaders, "*") | ||
c.Writer.Header().Set(AllowedCredentials, "true") | ||
|
||
c.Next() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters