From 9c868ee10ba26a90e7e2c30df06a257e32b4e29d Mon Sep 17 00:00:00 2001 From: sabaimran Date: Fri, 24 Nov 2023 20:41:19 -0800 Subject: [PATCH] Use the state.billing_enabled field to determine whether to use the subscribed scope --- src/khoj/configure.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/khoj/configure.py b/src/khoj/configure.py index 4e4e1008..2d21bd01 100644 --- a/src/khoj/configure.py +++ b/src/khoj/configure.py @@ -75,11 +75,14 @@ class UserAuthenticationBackend(AuthenticationBackend): .afirst() ) if user: - subscription_state = await aget_user_subscription_state(user) - subscribed = subscription_state == SubscriptionState.SUBSCRIBED.value - if subscribed: - return AuthCredentials(["authenticated", "subscribed"]), AuthenticatedKhojUser(user) - return AuthCredentials(["authenticated", "subscribed"]), AuthenticatedKhojUser(user) + if state.billing_enabled: + subscription_state = await aget_user_subscription_state(user) + subscribed = subscription_state == SubscriptionState.SUBSCRIBED.value + if subscribed: + return AuthCredentials(["authenticated", "subscribed"]), AuthenticatedKhojUser( + user_with_token.user + ) + return AuthCredentials(["authenticated"]), AuthenticatedKhojUser(user) if len(request.headers.get("Authorization", "").split("Bearer ")) == 2: # Get bearer token from header bearer_token = request.headers["Authorization"].split("Bearer ")[1] @@ -91,10 +94,13 @@ class UserAuthenticationBackend(AuthenticationBackend): .afirst() ) if user_with_token: - subscription_state = await aget_user_subscription_state(user_with_token.user) - subscribed = subscription_state == SubscriptionState.SUBSCRIBED.value - if subscribed: - return AuthCredentials(["authenticated", "subscribed"]), AuthenticatedKhojUser(user_with_token.user) + if state.billing_enabled: + subscription_state = await aget_user_subscription_state(user_with_token.user) + subscribed = subscription_state == SubscriptionState.SUBSCRIBED.value + if subscribed: + return AuthCredentials(["authenticated", "subscribed"]), AuthenticatedKhojUser( + user_with_token.user + ) return AuthCredentials(["authenticated"]), AuthenticatedKhojUser(user_with_token.user) if state.anonymous_mode: user = await self.khojuser_manager.filter(username="default").prefetch_related("subscription").afirst()