Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(demo-mode): passwordless login #83090

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/sentry/utils/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from sentry.users.models.user import User
from sentry.users.services.user import RpcUser
from sentry.users.services.user.service import user_service
from sentry.utils import metrics
from sentry.utils import demo_mode, metrics
from sentry.utils.http import absolute_uri

logger = logging.getLogger("sentry.auth")
Expand Down Expand Up @@ -417,6 +417,8 @@ def authenticate(
if users:
for user in users:
try:
if demo_mode.is_readonly_user(user):
return user
if user.password:
# XXX(joshuarli): This is checked before (and therefore, regardless of outcome)
# password checking as a mechanism to drop old password hashers immediately and
Expand Down
36 changes: 36 additions & 0 deletions src/sentry/utils/demo_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from sentry import options
from sentry.models.organization import Organization
from sentry.users.models.user import User


def is_readonly_user(user: User | None) -> bool:
if not options.get("demo-mode.enabled"):
return False

if not user:
return False

email = getattr(user, "email", None)

if email:
return True

return email in options.get("demo-mode.users")
Comment on lines +15 to +18
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this check short circuits the check whether the email is in the "demo-mode.users" option. It will return with True whenever it is set to any non-empty string.



def is_demo_org(organization: Organization | None):
if not options.get("demo-mode.enabled"):
return False

if not organization:
return False

return organization.id in options.get("demo-mode.orgs")


def get_readonly_user():
if not options.get("demo-mode.enabled"):
return None

email = options.get("demo-mode.users")[0]
return User.objects.get(email=email)
6 changes: 6 additions & 0 deletions src/sentry/web/frontend/auth_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
is_valid_redirect,
login,
)
from sentry.utils.demo_mode import get_readonly_user, is_demo_org
from sentry.utils.http import absolute_uri
from sentry.utils.sdk import capture_exception
from sentry.utils.urls import add_params_to_url
Expand Down Expand Up @@ -562,6 +563,11 @@ def handle_basic_auth(self, request: Request, **kwargs) -> HttpResponseBase:
op = request.POST.get("op")
organization = kwargs.pop("organization", None)

if is_demo_org(organization):
user = get_readonly_user()
self._handle_login(request, user, organization)
return self.redirect(get_login_redirect(request))

if request.method == "GET" and request.subdomain and self.org_exists(request):
urls = [
reverse("sentry-auth-organization", args=[request.subdomain]),
Expand Down
Loading