mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-01-20 03:01:34 -06:00
35 lines
1011 B
Go
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)
|
|
}
|
|
}
|
|
}
|