You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
489 B
Go
27 lines
489 B
Go
package http
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
auth "github.com/abbot/go-http-auth"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// BasicAuthMiddleware gin middleware
|
|
func BasicAuthMiddleware(a *auth.BasicAuth) gin.HandlerFunc {
|
|
realmHeader := "Basic realm=" + strconv.Quote(a.Realm)
|
|
|
|
return func(c *gin.Context) {
|
|
user := a.CheckAuth(c.Request)
|
|
|
|
if user == "" {
|
|
c.Header("WWW-Authenticate", realmHeader)
|
|
c.AbortWithStatus(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
c.Set("user", user)
|
|
}
|
|
}
|