2022-10-08 23:34:43 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2023-03-14 21:47:14 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2022-10-08 23:34:43 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
gc "github.com/patrickmn/go-cache"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/api"
|
2023-03-14 21:47:14 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2022-10-08 23:34:43 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2023-01-24 06:05:31 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2022-10-11 22:44:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/server/limiter"
|
2022-10-08 23:34:43 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2023-03-14 21:47:14 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2024-01-09 10:58:47 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/header"
|
2022-10-08 23:34:43 +02:00
|
|
|
)
|
|
|
|
|
2023-12-12 18:42:50 +01:00
|
|
|
// To improve performance, we use a basic auth cache
|
|
|
|
// with an expiration time of about 5 minutes.
|
2022-10-08 23:34:43 +02:00
|
|
|
var basicAuthExpiration = 5 * time.Minute
|
|
|
|
var basicAuthCache = gc.New(basicAuthExpiration, basicAuthExpiration)
|
|
|
|
var basicAuthMutex = sync.Mutex{}
|
|
|
|
var BasicAuthRealm = "Basic realm=\"WebDAV Authorization Required\""
|
|
|
|
|
2023-12-12 18:42:50 +01:00
|
|
|
// WebDAVAuth checks authentication and authentication
|
|
|
|
// before WebDAV requests are processed.
|
|
|
|
func WebDAVAuth(conf *config.Config) gin.HandlerFunc {
|
|
|
|
// Helper function that extracts the login information from the request headers.
|
|
|
|
var basicAuth = func(c *gin.Context) (username, password, cacheKey string, authorized bool) {
|
|
|
|
// Extract credentials from the HTTP request headers.
|
2024-01-09 10:58:47 +01:00
|
|
|
username, password, cacheKey = header.BasicAuth(c)
|
2023-12-12 18:42:50 +01:00
|
|
|
|
|
|
|
// Fail if the username or password is empty, as
|
|
|
|
// this is not allowed under any circumstances.
|
|
|
|
if username == "" || password == "" || cacheKey == "" {
|
|
|
|
return "", "", "", false
|
2022-10-08 23:34:43 +02:00
|
|
|
}
|
|
|
|
|
2023-12-12 18:42:50 +01:00
|
|
|
// To improve performance, check the cache for already authorized users.
|
|
|
|
if user, found := basicAuthCache.Get(cacheKey); found && user != nil {
|
|
|
|
// Add cached user information to the request context.
|
|
|
|
c.Set(gin.AuthUserKey, user.(*entity.User))
|
|
|
|
// Credentials have already been authorized within the configured
|
|
|
|
// expiration time of the basic auth cache (about 5 minutes).
|
|
|
|
return username, password, cacheKey, true
|
|
|
|
} else {
|
|
|
|
// Credentials found, but not pre-authorized. If successful, the
|
|
|
|
// authorization will be cached for the next request.
|
|
|
|
return username, password, cacheKey, false
|
2022-10-08 23:34:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-12 18:42:50 +01:00
|
|
|
// Authentication handler that is called before WebDAV requests are processed.
|
2022-10-08 23:34:43 +02:00
|
|
|
return func(c *gin.Context) {
|
2022-10-11 22:44:11 +02:00
|
|
|
if c == nil {
|
|
|
|
return
|
|
|
|
}
|
2022-10-08 23:34:43 +02:00
|
|
|
|
2023-12-12 18:42:50 +01:00
|
|
|
// Get basic authentication credentials.
|
|
|
|
username, password, cacheKey, authorized := basicAuth(c)
|
2022-10-11 22:44:11 +02:00
|
|
|
|
2023-12-12 18:42:50 +01:00
|
|
|
// Allow requests from already authorized users to be processed.
|
|
|
|
if authorized {
|
2022-10-08 23:34:43 +02:00
|
|
|
return
|
2023-12-12 18:42:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Re-request authentication if credentials are missing or incomplete.
|
|
|
|
if cacheKey == "" {
|
2022-10-08 23:34:43 +02:00
|
|
|
c.Header("WWW-Authenticate", BasicAuthRealm)
|
|
|
|
c.AbortWithStatus(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-12 18:42:50 +01:00
|
|
|
// Get the client IP address from the request headers
|
|
|
|
// for use in logs and to enforce request rate limits.
|
2022-10-11 22:44:11 +02:00
|
|
|
clientIp := api.ClientIP(c)
|
|
|
|
|
2023-12-12 18:42:50 +01:00
|
|
|
// Check the authentication request rate to block the client after
|
|
|
|
// too many failed attempts (10/req per minute by default).
|
2022-10-19 05:09:09 +02:00
|
|
|
if limiter.Login.Reject(clientIp) {
|
2022-10-11 22:44:11 +02:00
|
|
|
limiter.Abort(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-08 23:34:43 +02:00
|
|
|
basicAuthMutex.Lock()
|
|
|
|
defer basicAuthMutex.Unlock()
|
|
|
|
|
2023-01-24 06:05:31 +01:00
|
|
|
// User credentials.
|
|
|
|
f := form.Login{
|
2023-12-12 18:42:50 +01:00
|
|
|
UserName: username,
|
2023-01-24 06:05:31 +01:00
|
|
|
Password: password,
|
|
|
|
}
|
2022-10-08 23:34:43 +02:00
|
|
|
|
2023-01-24 06:05:31 +01:00
|
|
|
// Check credentials and authorization.
|
|
|
|
if user, _, err := entity.Auth(f, nil, c); err != nil {
|
|
|
|
message := err.Error()
|
2022-10-19 05:09:09 +02:00
|
|
|
limiter.Login.Reserve(clientIp)
|
2023-12-12 18:42:50 +01:00
|
|
|
event.AuditErr([]string{clientIp, "webdav login as %s", message}, clean.LogQuote(username))
|
|
|
|
event.LoginError(clientIp, "webdav", username, api.UserAgent(c), message)
|
2023-01-24 06:05:31 +01:00
|
|
|
} else if user == nil {
|
|
|
|
message := "account not found"
|
|
|
|
limiter.Login.Reserve(clientIp)
|
2023-12-12 18:42:50 +01:00
|
|
|
event.AuditErr([]string{clientIp, "webdav login as %s", message}, clean.LogQuote(username))
|
|
|
|
event.LoginError(clientIp, "webdav", username, api.UserAgent(c), message)
|
2022-10-13 22:11:02 +02:00
|
|
|
} else if !user.CanUseWebDAV() {
|
2022-10-08 23:34:43 +02:00
|
|
|
// Sync disabled for this account.
|
|
|
|
message := "sync disabled"
|
|
|
|
|
2023-12-12 18:42:50 +01:00
|
|
|
event.AuditWarn([]string{clientIp, "webdav login as %s", message}, clean.LogQuote(username))
|
|
|
|
event.LoginError(clientIp, "webdav", username, api.UserAgent(c), message)
|
2023-03-14 21:47:14 +01:00
|
|
|
} else if err = os.MkdirAll(filepath.Join(conf.OriginalsPath(), user.GetUploadPath()), fs.ModeDir); err != nil {
|
|
|
|
message := "failed to create user upload path"
|
|
|
|
|
2023-12-12 18:42:50 +01:00
|
|
|
event.AuditWarn([]string{clientIp, "webdav login as %s", message}, clean.LogQuote(username))
|
|
|
|
event.LoginError(clientIp, "webdav", username, api.UserAgent(c), message)
|
2022-10-08 23:34:43 +02:00
|
|
|
} else {
|
|
|
|
// Successfully authenticated.
|
2023-12-12 18:42:50 +01:00
|
|
|
event.AuditInfo([]string{clientIp, "webdav login as %s", "succeeded"}, clean.LogQuote(username))
|
|
|
|
event.LoginInfo(clientIp, "webdav", username, api.UserAgent(c))
|
2022-10-08 23:34:43 +02:00
|
|
|
|
|
|
|
// Cache successful authentication.
|
2023-12-12 18:42:50 +01:00
|
|
|
basicAuthCache.SetDefault(cacheKey, user)
|
2022-10-08 23:34:43 +02:00
|
|
|
c.Set(gin.AuthUserKey, user)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Abort request.
|
|
|
|
c.Header("WWW-Authenticate", BasicAuthRealm)
|
|
|
|
c.AbortWithStatus(http.StatusUnauthorized)
|
|
|
|
}
|
|
|
|
}
|