fix: cors and content-type on error
This commit is contained in:
2
auth.go
2
auth.go
@@ -14,7 +14,6 @@ func handleAuth(c *gin.Context) error {
|
|||||||
authorization := c.Request.Header.Get("Authorization")
|
authorization := c.Request.Header.Get("Authorization")
|
||||||
if !strings.HasPrefix(authorization, "Bearer") {
|
if !strings.HasPrefix(authorization, "Bearer") {
|
||||||
err = errors.New("authorization header should start with 'Bearer'")
|
err = errors.New("authorization header should start with 'Bearer'")
|
||||||
c.AbortWithError(403, err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,7 +23,6 @@ func handleAuth(c *gin.Context) error {
|
|||||||
for _, auth := range strings.Split(config.Authorization, ",") {
|
for _, auth := range strings.Split(config.Authorization, ",") {
|
||||||
if authorization != strings.Trim(auth, " ") {
|
if authorization != strings.Trim(auth, " ") {
|
||||||
err = errors.New("wrong authorization header")
|
err = errors.New("wrong authorization header")
|
||||||
c.AbortWithError(403, err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
12
cors.go
12
cors.go
@@ -6,14 +6,8 @@ import (
|
|||||||
|
|
||||||
func corsMiddleware() gin.HandlerFunc {
|
func corsMiddleware() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
// delete existing headers
|
c.Header("Access-Control-Allow-Origin", "*")
|
||||||
c.Writer.Header().Del("Access-Control-Allow-Origin")
|
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, PATCH")
|
||||||
c.Writer.Header().Del("Access-Control-Allow-Methods")
|
c.Header("Access-Control-Allow-Headers", "Origin, Authorization, Content-Type")
|
||||||
c.Writer.Header().Del("Access-Control-Allow-Headers")
|
|
||||||
|
|
||||||
// set new headers
|
|
||||||
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
|
||||||
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, PATCH")
|
|
||||||
c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin, Authorization, Content-Type")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
11
main.go
11
main.go
@@ -73,6 +73,9 @@ func main() {
|
|||||||
m.SetMetricPath("/v1/metrics")
|
m.SetMetricPath("/v1/metrics")
|
||||||
m.Use(engine)
|
m.Use(engine)
|
||||||
|
|
||||||
|
// CORS middleware
|
||||||
|
engine.Use(corsMiddleware())
|
||||||
|
|
||||||
// error handle middleware
|
// error handle middleware
|
||||||
engine.Use(func(c *gin.Context) {
|
engine.Use(func(c *gin.Context) {
|
||||||
c.Next()
|
c.Next()
|
||||||
@@ -85,9 +88,6 @@ func main() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// CORS middleware
|
|
||||||
engine.Use(corsMiddleware())
|
|
||||||
|
|
||||||
// CORS handler
|
// CORS handler
|
||||||
engine.OPTIONS("/v1/*any", func(ctx *gin.Context) {
|
engine.OPTIONS("/v1/*any", func(ctx *gin.Context) {
|
||||||
ctx.AbortWithStatus(200)
|
ctx.AbortWithStatus(200)
|
||||||
@@ -114,7 +114,10 @@ func main() {
|
|||||||
|
|
||||||
// check authorization header
|
// check authorization header
|
||||||
if !*noauth {
|
if !*noauth {
|
||||||
if handleAuth(c) != nil {
|
err := handleAuth(c)
|
||||||
|
if err != nil {
|
||||||
|
c.Header("Content-Type", "application/json")
|
||||||
|
c.AbortWithError(403, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user