2023-11-07 23:12:39 +01:00
|
|
|
import re
|
2023-10-05 13:53:09 +02:00
|
|
|
from typing import Optional
|
|
|
|
|
2023-11-15 22:44:24 +01:00
|
|
|
from django.conf import settings
|
|
|
|
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
|
|
|
|
from django.shortcuts import render
|
2024-02-28 20:03:34 +01:00
|
|
|
from django.urls import reverse
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2016-07-26 23:43:38 +02:00
|
|
|
|
2023-11-15 22:44:24 +01:00
|
|
|
from zerver.decorator import human_users_only, zulip_login_required
|
|
|
|
from zerver.lib.exceptions import (
|
|
|
|
JsonableError,
|
|
|
|
MissingRemoteRealmError,
|
|
|
|
OrganizationOwnerRequiredError,
|
2023-12-10 11:59:28 +01:00
|
|
|
RemoteRealmServerMismatchError,
|
2024-01-06 20:31:41 +01:00
|
|
|
ResourceNotFoundError,
|
2023-11-15 22:44:24 +01:00
|
|
|
)
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.push_notifications import (
|
2023-10-08 00:43:41 +02:00
|
|
|
InvalidPushDeviceTokenError,
|
2020-06-11 00:54:34 +02:00
|
|
|
add_push_device_token,
|
|
|
|
b64_to_hex,
|
|
|
|
remove_push_device_token,
|
2023-10-05 13:53:09 +02:00
|
|
|
send_test_push_notification,
|
2023-11-15 22:44:24 +01:00
|
|
|
uses_notification_bouncer,
|
|
|
|
)
|
|
|
|
from zerver.lib.remote_server import (
|
|
|
|
UserDataForRemoteBilling,
|
|
|
|
get_realms_info_for_push_bouncer,
|
2023-12-11 14:24:13 +01:00
|
|
|
send_server_data_to_push_bouncer,
|
2023-11-15 22:44:24 +01:00
|
|
|
send_to_push_bouncer,
|
2020-06-11 00:54:34 +02:00
|
|
|
)
|
2021-07-16 22:11:10 +02:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2019-02-02 23:53:22 +01:00
|
|
|
from zerver.lib.response import json_success
|
2023-11-30 07:03:25 +01:00
|
|
|
from zerver.lib.typed_endpoint import typed_endpoint
|
2023-11-07 23:12:39 +01:00
|
|
|
from zerver.lib.validator import check_string
|
2016-07-26 23:43:38 +02:00
|
|
|
from zerver.models import PushDeviceToken, UserProfile
|
2024-02-28 20:03:34 +01:00
|
|
|
from zerver.views.errors import config_error
|
2016-07-26 23:43:38 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2023-11-07 23:12:39 +01:00
|
|
|
def check_app_id(var_name: str, val: object) -> str:
|
|
|
|
# Garbage values should be harmless, but we can be picky
|
|
|
|
# as insurance against bugs somewhere.
|
|
|
|
s = check_string(var_name, val)
|
|
|
|
if not re.fullmatch("[.a-zA-Z0-9-]+", s):
|
|
|
|
raise JsonableError(_("Invalid app ID"))
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
2019-11-13 06:54:30 +01:00
|
|
|
def validate_token(token_str: str, kind: int) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
if token_str == "" or len(token_str) > 4096:
|
|
|
|
raise JsonableError(_("Empty or invalid length token"))
|
2017-07-07 18:18:37 +02:00
|
|
|
if kind == PushDeviceToken.APNS:
|
|
|
|
# Validate that we can actually decode the token.
|
|
|
|
try:
|
|
|
|
b64_to_hex(token_str)
|
|
|
|
except Exception:
|
2021-02-12 08:20:45 +01:00
|
|
|
raise JsonableError(_("Invalid APNS token"))
|
2017-03-06 04:04:23 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-06-19 22:09:35 +02:00
|
|
|
@human_users_only
|
2016-07-26 23:43:38 +02:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def add_apns_device_token(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
token: str = REQ(),
|
2023-11-07 23:12:39 +01:00
|
|
|
appid: str = REQ(str_validator=check_app_id),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2017-07-07 18:18:37 +02:00
|
|
|
validate_token(token, PushDeviceToken.APNS)
|
2017-03-06 03:57:31 +01:00
|
|
|
add_push_device_token(user_profile, token, PushDeviceToken.APNS, ios_app_id=appid)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2016-07-26 23:43:38 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-06-19 22:09:35 +02:00
|
|
|
@human_users_only
|
2016-07-26 23:43:38 +02:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def add_android_reg_id(
|
|
|
|
request: HttpRequest, user_profile: UserProfile, token: str = REQ()
|
|
|
|
) -> HttpResponse:
|
2017-07-07 18:18:37 +02:00
|
|
|
validate_token(token, PushDeviceToken.GCM)
|
2017-03-06 04:04:23 +01:00
|
|
|
add_push_device_token(user_profile, token, PushDeviceToken.GCM)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2016-07-26 23:43:38 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-06-19 22:09:35 +02:00
|
|
|
@human_users_only
|
2016-07-26 23:43:38 +02:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def remove_apns_device_token(
|
|
|
|
request: HttpRequest, user_profile: UserProfile, token: str = REQ()
|
|
|
|
) -> HttpResponse:
|
2017-07-07 18:18:37 +02:00
|
|
|
validate_token(token, PushDeviceToken.APNS)
|
2017-03-06 07:01:28 +01:00
|
|
|
remove_push_device_token(user_profile, token, PushDeviceToken.APNS)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2016-07-26 23:43:38 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-06-19 22:09:35 +02:00
|
|
|
@human_users_only
|
2016-07-26 23:43:38 +02:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def remove_android_reg_id(
|
|
|
|
request: HttpRequest, user_profile: UserProfile, token: str = REQ()
|
|
|
|
) -> HttpResponse:
|
2017-07-07 18:18:37 +02:00
|
|
|
validate_token(token, PushDeviceToken.GCM)
|
2017-03-06 07:01:28 +01:00
|
|
|
remove_push_device_token(user_profile, token, PushDeviceToken.GCM)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2023-10-05 13:53:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
@human_users_only
|
|
|
|
@has_request_variables
|
|
|
|
def send_test_push_notification_api(
|
|
|
|
request: HttpRequest, user_profile: UserProfile, token: Optional[str] = REQ(default=None)
|
|
|
|
) -> HttpResponse:
|
|
|
|
# If a token is specified in the request, the test notification is supposed to be sent
|
|
|
|
# to that device. If no token is provided, the test notification should be sent to
|
|
|
|
# all devices registered for the user.
|
|
|
|
if token is not None:
|
|
|
|
try:
|
|
|
|
devices = [PushDeviceToken.objects.get(token=token, user=user_profile)]
|
|
|
|
except PushDeviceToken.DoesNotExist:
|
2023-10-08 00:43:41 +02:00
|
|
|
raise InvalidPushDeviceTokenError
|
2023-10-05 13:53:09 +02:00
|
|
|
else:
|
|
|
|
devices = list(PushDeviceToken.objects.filter(user=user_profile))
|
|
|
|
|
|
|
|
send_test_push_notification(user_profile, devices)
|
|
|
|
|
|
|
|
return json_success(request)
|
2023-11-15 22:44:24 +01:00
|
|
|
|
|
|
|
|
2024-01-06 20:31:41 +01:00
|
|
|
def self_hosting_auth_view_common(
|
|
|
|
request: HttpRequest, user_profile: UserProfile, next_page: Optional[str] = None
|
|
|
|
) -> str:
|
2024-03-03 23:20:49 +01:00
|
|
|
if not user_profile.has_billing_access:
|
|
|
|
# We may want to replace this with an html error page at some point,
|
|
|
|
# but this endpoint shouldn't be accessible via the UI to an unauthorized
|
|
|
|
# user_profile - and they need to directly enter the URL in their browser. So a json
|
|
|
|
# error may be sufficient.
|
|
|
|
raise OrganizationOwnerRequiredError
|
|
|
|
|
2024-01-06 20:31:41 +01:00
|
|
|
if not uses_notification_bouncer():
|
2024-02-28 20:03:34 +01:00
|
|
|
if settings.CORPORATE_ENABLED:
|
|
|
|
# This endpoint makes no sense on zulipchat.com, so just 404.
|
|
|
|
raise ResourceNotFoundError(_("Server doesn't use the push notification service"))
|
|
|
|
else:
|
|
|
|
return reverse(self_hosting_auth_not_configured)
|
2023-11-15 22:44:24 +01:00
|
|
|
|
2024-01-06 20:31:41 +01:00
|
|
|
realm_info = get_realms_info_for_push_bouncer(user_profile.realm_id)[0]
|
2023-11-15 22:44:24 +01:00
|
|
|
|
|
|
|
user_info = UserDataForRemoteBilling(
|
2024-01-06 20:31:41 +01:00
|
|
|
uuid=user_profile.uuid,
|
|
|
|
email=user_profile.delivery_email,
|
|
|
|
full_name=user_profile.full_name,
|
2023-11-15 22:44:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
post_data = {
|
|
|
|
"user": user_info.model_dump_json(),
|
|
|
|
"realm": realm_info.model_dump_json(),
|
2023-12-02 22:37:54 +01:00
|
|
|
# The uri_scheme is necessary for the bouncer to know the correct URL
|
|
|
|
# to redirect the user to for re-authing in case the session expires.
|
|
|
|
# Otherwise, the bouncer would know only the realm.host but be missing
|
|
|
|
# the knowledge of whether to use http or https.
|
|
|
|
"uri_scheme": settings.EXTERNAL_URI_SCHEME,
|
2023-11-15 22:44:24 +01:00
|
|
|
}
|
2023-11-30 07:03:25 +01:00
|
|
|
if next_page is not None:
|
|
|
|
post_data["next_page"] = next_page
|
|
|
|
|
2023-11-15 22:44:24 +01:00
|
|
|
try:
|
|
|
|
result = send_to_push_bouncer("POST", "server/billing", post_data)
|
|
|
|
except MissingRemoteRealmError:
|
|
|
|
# Upload realm info and re-try. It should work now.
|
2023-12-11 14:24:13 +01:00
|
|
|
send_server_data_to_push_bouncer(consider_usage_statistics=False)
|
2023-11-15 22:44:24 +01:00
|
|
|
result = send_to_push_bouncer("POST", "server/billing", post_data)
|
|
|
|
|
2024-01-05 17:25:53 +01:00
|
|
|
if result["result"] != "success": # nocoverage
|
2023-11-15 22:44:24 +01:00
|
|
|
raise JsonableError(_("Error returned by the bouncer: {result}").format(result=result))
|
|
|
|
|
|
|
|
redirect_url = result["billing_access_url"]
|
|
|
|
assert isinstance(redirect_url, str)
|
2024-01-06 20:31:41 +01:00
|
|
|
return redirect_url
|
|
|
|
|
|
|
|
|
|
|
|
@zulip_login_required
|
|
|
|
@typed_endpoint
|
|
|
|
def self_hosting_auth_redirect_endpoint(
|
|
|
|
request: HttpRequest,
|
|
|
|
*,
|
|
|
|
next_page: Optional[str] = None,
|
|
|
|
) -> HttpResponse:
|
|
|
|
"""
|
|
|
|
This endpoint is used by the web app running in the browser. We serve HTML
|
|
|
|
error pages, and in case of success a simple redirect to the remote billing
|
|
|
|
access link received from the bouncer.
|
|
|
|
"""
|
|
|
|
|
|
|
|
user = request.user
|
|
|
|
assert user.is_authenticated
|
|
|
|
assert isinstance(user, UserProfile)
|
|
|
|
|
|
|
|
try:
|
|
|
|
redirect_url = self_hosting_auth_view_common(request, user, next_page)
|
|
|
|
except ResourceNotFoundError:
|
|
|
|
return render(request, "404.html", status=404)
|
|
|
|
except RemoteRealmServerMismatchError:
|
2024-01-10 15:50:30 +01:00
|
|
|
return render(request, "zerver/remote_realm_server_mismatch_error.html", status=403)
|
2024-01-06 20:31:41 +01:00
|
|
|
|
2023-11-15 22:44:24 +01:00
|
|
|
return HttpResponseRedirect(redirect_url)
|
2024-01-06 20:31:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
@typed_endpoint
|
|
|
|
def self_hosting_auth_json_endpoint(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
*,
|
|
|
|
next_page: Optional[str] = None,
|
|
|
|
) -> HttpResponse:
|
|
|
|
"""
|
|
|
|
This endpoint is used by the desktop application. It makes an API request here,
|
|
|
|
expecting a JSON response with either the billing access link, or appropriate
|
|
|
|
error information.
|
|
|
|
"""
|
|
|
|
|
|
|
|
redirect_url = self_hosting_auth_view_common(request, user_profile, next_page)
|
|
|
|
|
|
|
|
return json_success(request, data={"billing_access_url": redirect_url})
|
2024-02-28 20:03:34 +01:00
|
|
|
|
|
|
|
|
2024-03-03 23:20:49 +01:00
|
|
|
@zulip_login_required
|
2024-02-28 20:03:34 +01:00
|
|
|
def self_hosting_auth_not_configured(request: HttpRequest) -> HttpResponse:
|
2024-03-03 23:20:49 +01:00
|
|
|
# Use the same access model as the main endpoints for consistency
|
|
|
|
# and to not have to worry about this endpoint leaking some kind of
|
|
|
|
# sensitive configuration information in the future.
|
|
|
|
user = request.user
|
|
|
|
assert user.is_authenticated
|
|
|
|
assert isinstance(user, UserProfile)
|
|
|
|
if not user.has_billing_access:
|
|
|
|
raise OrganizationOwnerRequiredError
|
|
|
|
|
2024-02-28 20:03:34 +01:00
|
|
|
if settings.CORPORATE_ENABLED or uses_notification_bouncer():
|
|
|
|
# This error page should only be available if the config error
|
|
|
|
# is actually real.
|
|
|
|
return render(request, "404.html", status=404)
|
|
|
|
|
2024-03-05 15:42:09 +01:00
|
|
|
return config_error(
|
|
|
|
request,
|
|
|
|
"remote_billing_bouncer_not_configured",
|
|
|
|
go_back_to_url="/",
|
|
|
|
go_back_to_url_name="the app",
|
|
|
|
)
|