Files
phylum/server/internal/api/webdav/auth.go
2025-04-04 23:22:52 +05:30

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)
}
}
}