Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Validate cast tokens synchronously

Neeraj Gupta666070896b05
diff --git a/server/cmd/museum/main.go b/server/cmd/museum/main.go
index f8b34cd43f..17df36df66 100644
--- a/server/cmd/museum/main.go
+++ b/server/cmd/museum/main.go
@@ -838,7 +838,7 @@ func main() {
castAPI := server.Group("/cast")
castCtrl := cast.NewController(&castDb, accessCtrl)
- castMiddleware := middleware.CastMiddleware{CastCtrl: castCtrl, Cache: authCache}
+ castMiddleware := middleware.CastMiddleware{CastCtrl: castCtrl}
castAPI.Use(rateLimiter.GlobalRateLimiter(), castMiddleware.CastAuthMiddleware())
castHandler := &api.CastHandler{
diff --git a/server/pkg/middleware/cast_auth.go b/server/pkg/middleware/cast_auth.go
index ee23d9a6f5..2d9fce2713 100644
--- a/server/pkg/middleware/cast_auth.go
+++ b/server/pkg/middleware/cast_auth.go
@@ -1,16 +1,14 @@
package middleware
import (
- "fmt"
+ "net/http"
+
castCtrl "github.com/ente/museum/pkg/controller/cast"
"github.com/ente/museum/pkg/utils/auth"
"github.com/gin-gonic/gin"
- "github.com/patrickmn/go-cache"
- "net/http"
)
type CastMiddleware struct {
- Cache *cache.Cache
CastCtrl *castCtrl.Controller
}
@@ -21,27 +19,12 @@ func (m *CastMiddleware) CastAuthMiddleware() gin.HandlerFunc {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "cast access token missing"})
return
}
- app := auth.GetApp(c)
- cacheKey := fmt.Sprintf("%s:%s:%s", app, token, "cast")
- cachedCastCtx, found := m.Cache.Get(cacheKey)
- if !found {
- castCtx, err := m.CastCtrl.GetCollectionAndCasterIDForToken(c, token)
- if err != nil {
- c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid token"})
- return
- }
- c.Set(auth.CastContext, castCtx)
- m.Cache.Set(cacheKey, *castCtx, cache.DefaultExpiration)
- c.Set(auth.CastContext, *castCtx)
- } else {
- c.Set(auth.CastContext, cachedCastCtx)
- go func() {
- _, err := m.CastCtrl.GetCollectionAndCasterIDForToken(c, token)
- if err != nil {
- m.Cache.Delete(cacheKey)
- }
- }()
+ castCtx, err := m.CastCtrl.GetCollectionAndCasterIDForToken(c, token)
+ if err != nil {
+ c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid token"})
+ return
}
+ c.Set(auth.CastContext, *castCtx)
c.Next()
}
}
diff --git a/server/pkg/middleware/cast_auth_test.go b/server/pkg/middleware/cast_auth_test.go
new file mode 100644
index 0000000000..07ae45adc0
--- /dev/null
+++ b/server/pkg/middleware/cast_auth_test.go
@@ -0,0 +1,72 @@
+package middleware
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "testing"
+
+ "github.com/ente/museum/ente/cast"
+ "github.com/ente/museum/internal/testutil"
+ "github.com/ente/museum/pkg/controller/access"
+ castController "github.com/ente/museum/pkg/controller/cast"
+ castRepo "github.com/ente/museum/pkg/repo/cast"
+ "github.com/ente/museum/pkg/utils/auth"
+ "github.com/gin-gonic/gin"
+ "github.com/google/uuid"
+)
+
+type allowCastCollectionAccess struct {
+ access.Controller
+}
+
+func (allowCastCollectionAccess) GetCollection(*gin.Context, *access.GetCollectionParams) (*access.GetCollectionResponse, error) {
+ return &access.GetCollectionResponse{}, nil
+}
+
+func TestCastAuthRejectsRevokedToken(t *testing.T) {
+ testutil.WithServerRoot(t)
+ db := testutil.RequireTestDB(t)
+ testutil.ResetTables(t, db)
+ t.Cleanup(func() { testutil.ResetTables(t, db) })
+
+ const userID = int64(92001)
+ const collectionID = int64(92002)
+ deviceID := uuid.New()
+ const token = "cast-auth-test-token"
+ _, err := db.Exec(
+ `INSERT INTO casting(id, code, public_key, collection_id, cast_user, token, ip)
+ VALUES($1, $2, $3, $4, $5, $6, $7)`,
+ deviceID, "CAST01", "key", collectionID, userID, token, "127.0.0.1",
+ )
+ if err != nil {
+ t.Fatalf("insert casting row: %v", err)
+ }
+
+ repository := &castRepo.Repository{DB: db}
+ controller := castController.NewController(repository, allowCastCollectionAccess{})
+ router := gin.New()
+ router.Use((&CastMiddleware{CastCtrl: controller}).CastAuthMiddleware())
+ router.GET("/", func(c *gin.Context) {
+ if got := auth.GetCastCtx(c); got != (cast.AuthContext{UserID: userID, CollectionID: collectionID}) {
+ t.Fatalf("cast context = %+v", got)
+ }
+ c.Status(http.StatusNoContent)
+ })
+
+ request := func() *httptest.ResponseRecorder {
+ recorder := httptest.NewRecorder()
+ req := httptest.NewRequest(http.MethodGet, "/", nil)
+ req.Header.Set("X-Cast-Access-Token", token)
+ router.ServeHTTP(recorder, req)
+ return recorder
+ }
+ if got := request().Code; got != http.StatusNoContent {
+ t.Fatalf("active token status = %d", got)
+ }
+ if err := repository.RevokeForGivenUserAndDevice(t.Context(), userID, deviceID); err != nil {
+ t.Fatalf("revoke token: %v", err)
+ }
+ if got := request().Code; got != http.StatusUnauthorized {
+ t.Fatalf("revoked token status = %d", got)
+ }
+}