mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-01-21 19:49:31 -06:00
31 lines
749 B
Go
31 lines
749 B
Go
package webdav
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/shroff/phylum/server/internal/core/user"
|
|
)
|
|
|
|
const keyFileSystem = "filesystem"
|
|
|
|
func createBasicAuthHandler() func(c *gin.Context) {
|
|
return func(c *gin.Context) {
|
|
authSuccess := false
|
|
if username, pass, ok := c.Request.BasicAuth(); ok {
|
|
ctx := c.Request.Context()
|
|
if u, err := user.ManagerFromContext(ctx).VerifyUserPassword(username, pass); err == nil {
|
|
c.Set(keyFileSystem, u.OpenFileSystem(ctx))
|
|
authSuccess = true
|
|
} else if !errors.Is(err, user.ErrCredentialsInvalid) {
|
|
panic(err)
|
|
}
|
|
}
|
|
if !authSuccess {
|
|
c.Header("WWW-Authenticate", "Basic realm=\"Phylum WebDAV\"")
|
|
c.AbortWithStatus(http.StatusUnauthorized)
|
|
}
|
|
}
|
|
}
|