Compare commits

...

1 Commits

Author SHA1 Message Date
Yuhong Sun
b8a6fa2cbd k 2024-12-05 08:24:42 -08:00
6 changed files with 35 additions and 20 deletions

View File

@@ -56,6 +56,7 @@ from danswer.auth.invited_users import get_invited_users
from danswer.auth.schemas import UserCreate
from danswer.auth.schemas import UserRole
from danswer.auth.schemas import UserUpdate
from danswer.configs.app_configs import ALLOW_ANONYMOUS_ACCESS
from danswer.configs.app_configs import AUTH_TYPE
from danswer.configs.app_configs import DISABLE_AUTH
from danswer.configs.app_configs import DISABLE_VERIFICATION
@@ -633,32 +634,37 @@ async def optional_user(
async def double_check_user(
user: User | None,
optional: bool = DISABLE_AUTH,
include_expired: bool = False,
allow_anonymous: bool = False,
) -> User | None:
if optional:
if DISABLE_AUTH:
return None
if user is None:
raise BasicAuthenticationError(
detail="Access denied. User is not authenticated.",
)
if user is not None:
# If user attempted to authenticate, verify them, do not default
# to anonymous access if it fails.
if user_needs_to_be_verified() and not user.is_verified:
raise BasicAuthenticationError(
detail="Access denied. User is not verified.",
)
if user_needs_to_be_verified() and not user.is_verified:
raise BasicAuthenticationError(
detail="Access denied. User is not verified.",
)
if (
user.oidc_expiry
and user.oidc_expiry < datetime.now(timezone.utc)
and not include_expired
):
raise BasicAuthenticationError(
detail="Access denied. User's OIDC token has expired.",
)
if (
user.oidc_expiry
and user.oidc_expiry < datetime.now(timezone.utc)
and not include_expired
):
raise BasicAuthenticationError(
detail="Access denied. User's OIDC token has expired.",
)
return user
return user
if allow_anonymous and ALLOW_ANONYMOUS_ACCESS:
return None
raise BasicAuthenticationError(
detail="Access denied. User is not authenticated.",
)
async def current_user_with_expired_token(
@@ -670,7 +676,9 @@ async def current_user_with_expired_token(
async def current_limited_user(
user: User | None = Depends(optional_user),
) -> User | None:
return await double_check_user(user)
# Currently all of the endpoints that accept the limited API key
# are also those that allow anonymous access
return await double_check_user(user, allow_anonymous=True)
async def current_user(

View File

@@ -43,6 +43,9 @@ WEB_DOMAIN = os.environ.get("WEB_DOMAIN") or "http://localhost:3000"
AUTH_TYPE = AuthType((os.environ.get("AUTH_TYPE") or AuthType.DISABLED.value).lower())
DISABLE_AUTH = AUTH_TYPE == AuthType.DISABLED
# For select endpoints, allow users to access without being authenticated
ALLOW_ANONYMOUS_ACCESS = os.environ.get("ALLOW_ANONYMOUS_ACCESS", "").lower() == "true"
# Necessary for cloud integration tests
DISABLE_VERIFICATION = os.environ.get("DISABLE_VERIFICATION", "").lower() == "true"

View File

@@ -19,6 +19,7 @@ services:
environment:
# Auth Settings
- AUTH_TYPE=${AUTH_TYPE:-disabled}
- ALLOW_ANONYMOUS_ACCESS=${ALLOW_ANONYMOUS_ACCESS:-false}
- SESSION_EXPIRE_TIME_SECONDS=${SESSION_EXPIRE_TIME_SECONDS:-}
- ENCRYPTION_KEY_SECRET=${ENCRYPTION_KEY_SECRET:-}
- VALID_EMAIL_DOMAINS=${VALID_EMAIL_DOMAINS:-}

View File

@@ -19,6 +19,7 @@ services:
environment:
# Auth Settings
- AUTH_TYPE=${AUTH_TYPE:-disabled}
- ALLOW_ANONYMOUS_ACCESS=${ALLOW_ANONYMOUS_ACCESS:-false}
- SESSION_EXPIRE_TIME_SECONDS=${SESSION_EXPIRE_TIME_SECONDS:-}
- ENCRYPTION_KEY_SECRET=${ENCRYPTION_KEY_SECRET:-}
- VALID_EMAIL_DOMAINS=${VALID_EMAIL_DOMAINS:-}

View File

@@ -402,6 +402,7 @@ auth:
configMap:
AUTH_TYPE: "disabled" # Change this for production uses unless Danswer is only accessible behind VPN
ALLOW_ANONYMOUS_ACCESS: "false"
SESSION_EXPIRE_TIME_SECONDS: "86400" # 1 Day Default
VALID_EMAIL_DOMAINS: "" # Can be something like danswer.ai, as an extra double-check
SMTP_SERVER: "" # For sending verification emails, if unspecified then defaults to 'smtp.gmail.com'

View File

@@ -6,6 +6,7 @@ data:
# Auth Setting, also check the secrets file
AUTH_TYPE: "disabled" # Change this for production uses unless Danswer is only accessible behind VPN
ALLOW_ANONYMOUS_ACCESS: "false"
ENCRYPTION_KEY_SECRET: "" # This should not be specified directly in the yaml, this is just for reference
SESSION_EXPIRE_TIME_SECONDS: "86400" # 1 Day Default
VALID_EMAIL_DOMAINS: "" # Can be something like danswer.ai, as an extra double-check