Fix basic and bearer auth

This commit is contained in:
Abhishek Shroff
2024-08-06 23:13:35 +05:30
parent ae1d98c09e
commit 9023a2bea4
2 changed files with 12 additions and 4 deletions

View File

@@ -18,10 +18,10 @@ func CreateBasicAuthHandler(app *app.App) func(c *gin.Context) {
}
if userID == 0 {
c.Header("WWW-Authenticate", "Basic realm=\"Phylum WebDAV\"")
c.Status(http.StatusUnauthorized)
c.AbortWithStatus(http.StatusUnauthorized)
} else if fs, err := app.OpenFileSystem(c.Request.Context(), userID); err != nil {
logrus.Warn(err)
c.Status(http.StatusInternalServerError)
c.AbortWithStatus(http.StatusInternalServerError)
} else {
c.Set(keyUserID, userID)
c.Set(keyFileSystem, fs)

View File

@@ -1,11 +1,13 @@
package auth
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/shroff/phylum/server/internal/api/errors"
"github.com/shroff/phylum/server/internal/app"
"github.com/sirupsen/logrus"
)
const errCodeAuthRequred = "auth_required"
@@ -25,13 +27,19 @@ func CreateBearerAuthHandler(a *app.App) func(c *gin.Context) {
panic(errors.Err{Status: 401, Code: errCodeAuthRequred})
}
username, err := a.VerifyAccessToken(authParts[1])
userID, err := a.VerifyAccessToken(authParts[1])
if err != nil {
if errors.Is(err, app.ErrTokenExpired) || errors.Is(err, app.ErrTokenInvalid) {
panic(errors.Err{Status: 401, Code: errCodeTokenInvalid})
}
panic(err)
}
c.Set(keyUserID, username)
if fs, err := a.OpenFileSystem(c.Request.Context(), userID); err != nil {
logrus.Warn(err)
c.AbortWithStatus(http.StatusInternalServerError)
} else {
c.Set(keyUserID, userID)
c.Set(keyFileSystem, fs)
}
}
}