Skip to content

armgit / ente

Public Imported from GitHub
0

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

Enforce paid locker access

Neeraj Guptaf61b07d181f2
diff --git a/server/ente/errors.go b/server/ente/errors.go
index 0e96de0a63..25579873b2 100644
--- a/server/ente/errors.go
+++ b/server/ente/errors.go
@@ -30,7 +30,11 @@ var ErrCollectionDeleted = &ApiError{
HttpStatusCode: http.StatusNotFound,
}
-var ErrFileLimitReached = errors.New("file limit reached")
+var ErrFileLimitReached = ApiError{
+ Code: FileLimitReached,
+ Message: "Maximum file limit reached",
+ HttpStatusCode: http.StatusForbidden,
+}
// ErrBadRequest is returned when a bad request is encountered
var ErrBadRequest = errors.New("bad request")
@@ -197,6 +201,12 @@ var ErrCastIPMismatch = ApiError{
HttpStatusCode: http.StatusForbidden,
}
+var ErrLockerRegistrationDisabled = &ApiError{
+ Code: LockerRegistrationDisabled,
+ Message: "Locker is restricted to paid users currently",
+ HttpStatusCode: http.StatusForbidden,
+}
+
type ErrorCode string
const (
@@ -228,6 +238,12 @@ const (
// MaxPasskeysReached is thrown when user attempts to create more than max allowed passkeys
MaxPasskeysReached ErrorCode = "MAX_PASSKEYS_REACHED"
+ // LockerRegistrationDisabled indicates that locker registration is currently disabled
+ LockerRegistrationDisabled ErrorCode = "LOCKER_REGISTRATION_DISABLED"
+
+ // FileLimitReached indicates the user hit the maximum number of files allowed
+ FileLimitReached ErrorCode = "FILE_LIMIT_REACHED"
+
SessionExpired ErrorCode = "SESSION_EXPIRED"
)
diff --git a/server/pkg/controller/billing.go b/server/pkg/controller/billing.go
index ff57c94775..f91eabf3c1 100644
--- a/server/pkg/controller/billing.go
+++ b/server/pkg/controller/billing.go
@@ -5,9 +5,10 @@ import (
"database/sql"
"errors"
"fmt"
- "github.com/ente-io/museum/pkg/controller/commonbilling"
"strconv"
+ "github.com/ente-io/museum/pkg/controller/commonbilling"
+
"github.com/ente-io/museum/pkg/repo/storagebonus"
"github.com/ente-io/museum/pkg/controller/discord"
diff --git a/server/pkg/controller/user/userauth.go b/server/pkg/controller/user/userauth.go
index a93db6536b..619320262f 100644
--- a/server/pkg/controller/user/userauth.go
+++ b/server/pkg/controller/user/userauth.go
@@ -113,7 +113,7 @@ func (c *UserController) SendEmailOTT(context *gin.Context, email string, purpos
return stacktrace.Propagate(ente.ErrTooManyBadRequest, msg)
}
- err = c.UserAuthRepo.AddOTT(emailHash, auth.GetApp(context), ott, time.Microseconds()+OTTValidityDurationInMicroSeconds)
+ err = c.UserAuthRepo.AddOTT(emailHash, app, ott, time.Microseconds()+OTTValidityDurationInMicroSeconds)
if !hasHardcodedOTT {
// ignore error for AddOTT for hardcode OTT. This is to avoid error when unique OTT check fails at db layer
if err != nil {
@@ -148,6 +148,10 @@ func (c *UserController) validateSendOTT(ctx *gin.Context, email string, purpose
return err
}
}
+ app := auth.GetApp(ctx)
+ if app == ente.Locker && purpose == ente.SignUpOTTPurpose {
+ return stacktrace.Propagate(ente.ErrLockerRegistrationDisabled, "signup ott restricted for locker")
+ }
isSignUpComplete, err := c.isSignUpComplete(email)
if err != nil {
return stacktrace.Propagate(err, "")
@@ -353,7 +357,28 @@ func (c *UserController) GetActiveSessions(context *gin.Context, userID int64) (
return tokens, nil
}
+func (c *UserController) ensureLockerPaidAccess(userID int64, app ente.App) error {
+ if app != ente.Locker {
+ return nil
+ }
+ const restrictionMsg = "locker is restricted to paid users currently"
+ if c.BillingController == nil {
+ log.Error("locker access: billing controller is not configured")
+ return stacktrace.Propagate(ente.ErrLockerRegistrationDisabled, restrictionMsg)
+ }
+ if err := c.BillingController.IsActivePayingSubscriber(userID); err != nil {
+ if !errors.Is(err, ente.ErrNoActiveSubscription) && !errors.Is(err, ente.ErrSharingDisabledForFreeAccounts) {
+ log.WithError(err).Error("locker access: failed to verify active subscription status")
+ }
+ return stacktrace.Propagate(ente.ErrLockerRegistrationDisabled, restrictionMsg)
+ }
+ return nil
+}
+
func (c *UserController) AddTokenAndNotify(userID int64, app ente.App, token string, ip string, userAgent string) error {
+ if err := c.ensureLockerPaidAccess(userID, app); err != nil {
+ return err
+ }
err := c.UserAuthRepo.AddToken(userID, app, token, ip, userAgent)
if err != nil {
return stacktrace.Propagate(err, "failed to insert token")
@@ -408,10 +433,14 @@ func emailOTT(app ente.App, to string, ott string, purpose string, mobile bool)
// source indicates where the user came from. It can be nil.
func (c *UserController) onVerificationSuccess(context *gin.Context, email string, source *string) (ente.EmailAuthorizationResponse, error) {
isTwoFactorEnabled := false
+ app := auth.GetApp(context)
userID, err := c.UserRepo.GetUserIDWithEmail(email)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
+ if app == ente.Locker {
+ return ente.EmailAuthorizationResponse{}, stacktrace.Propagate(ente.ErrLockerRegistrationDisabled, "locker signup is restricted to paid users")
+ }
if viper.GetBool("internal.disable-registration") {
return ente.EmailAuthorizationResponse{}, stacktrace.Propagate(ente.ErrPermissionDenied, "")
} else {
@@ -473,7 +502,10 @@ func (c *UserController) onVerificationSuccess(context *gin.Context, email strin
if errors.Is(err, sql.ErrNoRows) {
// user creation is pending on key attributes set based on the password.
// No need to send login notification
- err = c.UserAuthRepo.AddToken(userID, auth.GetApp(context), token,
+ if lockerErr := c.ensureLockerPaidAccess(userID, app); lockerErr != nil {
+ return ente.EmailAuthorizationResponse{}, lockerErr
+ }
+ err = c.UserAuthRepo.AddToken(userID, app, token,
network.GetClientIP(context), context.Request.UserAgent())
if err != nil {
return ente.EmailAuthorizationResponse{}, stacktrace.Propagate(err, "")
@@ -488,7 +520,7 @@ func (c *UserController) onVerificationSuccess(context *gin.Context, email strin
if err != nil {
return ente.EmailAuthorizationResponse{}, stacktrace.Propagate(err, "")
}
- err = c.AddTokenAndNotify(userID, auth.GetApp(context), token,
+ err = c.AddTokenAndNotify(userID, app, token,
network.GetClientIP(context), context.Request.UserAgent())
if err != nil {
return ente.EmailAuthorizationResponse{}, stacktrace.Propagate(err, "")