Skip to content

Commit

Permalink
Fix HTTP Basic (#152)
Browse files Browse the repository at this point in the history
Co-authored-by: Geoffrey Ragot <[email protected]>
  • Loading branch information
altitude and gfyrag authored Feb 7, 2022
1 parent 151a47a commit 730e5f5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
15 changes: 11 additions & 4 deletions pkg/api/middlewares/auth_middleware.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package middlewares

import (
"net/http"
"strings"

"github.com/gin-gonic/gin"
Expand All @@ -19,13 +20,19 @@ func NewAuthMiddleware(httpBasic string) AuthMiddleware {
}

// AuthMiddleware
func (m AuthMiddleware) AuthMiddleware(engine *gin.Engine) gin.HandlerFunc {
func (m AuthMiddleware) AuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
if auth := m.HTTPBasic; auth != "" {
segment := strings.Split(auth, ":")
engine.Use(gin.BasicAuth(gin.Accounts{
segment[0]: segment[1],
}))
username, password, ok := c.Request.BasicAuth()
if !ok {
c.AbortWithStatus(http.StatusForbidden)
return
}
if segment[0] != username || segment[1] != password {
c.AbortWithStatus(http.StatusForbidden)
return
}
}
}
}
1 change: 1 addition & 0 deletions pkg/api/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func (r *Routes) Engine(cc cors.Config) *gin.Engine {
"user_agent": c.Request.UserAgent(),
}).Info(c.Request.Context(), "Request")
},
r.authMiddleware.AuthMiddleware(),
}, r.globalMiddlewares...)

// Default Middlewares
Expand Down

0 comments on commit 730e5f5

Please sign in to comment.