+func (s *ServerService) isValidAPIKey(key string) bool {
+ return (key == s.cfg.FileConf.XDSAPIKey && key != "")
+}
+
+func (s *ServerService) middlewareCSRF() gin.HandlerFunc {
+ return func(c *gin.Context) {
+ // Allow requests carrying a valid API key
+ if s.isValidAPIKey(c.Request.Header.Get("X-API-Key")) {
+ // Set the access-control-allow-origin header for CORS requests
+ // since a valid API key has been provided
+ c.Header("Access-Control-Allow-Origin", "*")
+ c.Next()
+ return
+ }
+
+ // Allow io.socket request
+ if strings.HasPrefix(c.Request.URL.Path, "/socket.io") {
+ c.Next()
+ return
+ }
+
+ /* FIXME Add really CSRF support
+
+ // Allow requests for anything not under the protected path prefix,
+ // and set a CSRF cookie if there isn't already a valid one.
+ if !strings.HasPrefix(c.Request.URL.Path, prefix) {
+ cookie, err := c.Cookie("CSRF-Token-" + unique)
+ if err != nil || !validCsrfToken(cookie.Value) {
+ s.log.Debugln("new CSRF cookie in response to request for", c.Request.URL)
+ c.SetCookie("CSRF-Token-"+unique, newCsrfToken(), 600, "/", "", false, false)
+ }
+ c.Next()
+ return
+ }
+
+ // Verify the CSRF token
+ token := c.Request.Header.Get("X-CSRF-Token-" + unique)
+ if !validCsrfToken(token) {
+ c.AbortWithError(403, "CSRF Error")
+ return
+ }
+
+ c.Next()
+ */
+ c.AbortWithError(403, fmt.Errorf("Not valid API key"))
+ }
+}
+