Files
phylum/server/internal/webdav/auth_basic.go
Abhishek Shroff 7abf2e4ba5 Fixes
2024-08-08 00:03:45 +05:30

35 lines
1011 B
Go

package webdav
import (
"errors"
"net/http"
"github.com/gin-gonic/gin"
"github.com/shroff/phylum/server/internal/core"
"github.com/sirupsen/logrus"
)
const keyFileSystem = "filesystem"
func createBasicAuthHandler(a *core.App) func(c *gin.Context) {
return func(c *gin.Context) {
if username, pass, ok := c.Request.BasicAuth(); !ok {
c.Header("WWW-Authenticate", "Basic realm=\"Phylum WebDAV\"")
c.AbortWithStatus(http.StatusUnauthorized)
} else if user, err := a.VerifyUserPassword(c.Request.Context(), username, pass); err != nil {
if errors.Is(err, core.ErrCredentialsInvalid) {
c.Header("WWW-Authenticate", "Basic realm=\"Phylum WebDAV\"")
c.AbortWithStatus(http.StatusUnauthorized)
} else {
logrus.Warn(err)
c.AbortWithStatus(http.StatusInternalServerError)
}
} else if fs, err := a.OpenFileSystem(c.Request.Context(), user); err != nil {
logrus.Warn(err)
c.AbortWithStatus(http.StatusInternalServerError)
} else {
c.Set(keyFileSystem, fs)
}
}
}