168 lines
4.9 KiB
Python
168 lines
4.9 KiB
Python
from django.conf import settings
|
|
from django.contrib.auth import get_user_model
|
|
from django.http import Http404, JsonResponse
|
|
from django.shortcuts import render
|
|
from django.views.decorators.cache import never_cache
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
from django.views.decorators.debug import (
|
|
sensitive_post_parameters,
|
|
sensitive_variables,
|
|
)
|
|
from django.views.decorators.http import require_POST
|
|
|
|
from .access import customer_for_user
|
|
from .credential_views import MailboxCredentialView
|
|
from .installation_features import panel_feature_allowed
|
|
from .models import HostingMailbox, RoundcubeGrant, ServiceCredentialAccess
|
|
from .service_credentials import (
|
|
CredentialKind,
|
|
CredentialOutcome,
|
|
can_reveal_credentials,
|
|
enabled_credential_access,
|
|
fetch_service_credentials,
|
|
)
|
|
from .signon import (
|
|
bridge_ticket,
|
|
consume_signon_grant,
|
|
issue_signon_grant,
|
|
signon_actor,
|
|
)
|
|
|
|
|
|
def roundcube_available(request):
|
|
return bool(
|
|
panel_feature_allowed("ROUNDCUBE_URL")
|
|
and settings.ROUNDCUBE_BRIDGE_KEY
|
|
and enabled_credential_access(request, CredentialKind.MAILBOX)
|
|
)
|
|
|
|
|
|
def mailbox_can_open_webmail(mailbox):
|
|
account = mailbox.mail_domain.account
|
|
job = getattr(mailbox, "provisioning", None)
|
|
return (
|
|
not account.services_suspended
|
|
and account.retired_at is None
|
|
and (job is None or job.state == "configured")
|
|
)
|
|
|
|
|
|
def actor_can_open_webmail(actor, mailbox):
|
|
if not (
|
|
can_reveal_credentials(actor, CredentialKind.MAILBOX)
|
|
and mailbox_can_open_webmail(mailbox)
|
|
):
|
|
return False
|
|
customer = customer_for_user(actor)
|
|
return (
|
|
customer is None
|
|
or mailbox.mail_domain.account.customer_id == customer.pk
|
|
)
|
|
|
|
|
|
class RoundcubeLaunch(MailboxCredentialView):
|
|
http_method_names = ["post"]
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
response = super().dispatch(request, *args, **kwargs)
|
|
response["Referrer-Policy"] = "origin"
|
|
return response
|
|
|
|
@sensitive_variables()
|
|
def post(self, request, *args, **kwargs):
|
|
if not roundcube_available(request):
|
|
raise Http404
|
|
mailbox = self.resource
|
|
if not actor_can_open_webmail(request.user, mailbox):
|
|
raise Http404
|
|
token = issue_signon_grant(
|
|
RoundcubeGrant,
|
|
request.user,
|
|
mailbox=mailbox,
|
|
address=mailbox.spec.address,
|
|
)
|
|
return render(
|
|
request,
|
|
"hosting/service_signon.html",
|
|
{
|
|
"destination": settings.ROUNDCUBE_URL + "/",
|
|
"product": "Roundcube",
|
|
"resource": mailbox.spec.address,
|
|
"ticket_field": "_coulobre_ticket",
|
|
"ticket": token,
|
|
"account": self.account,
|
|
},
|
|
)
|
|
|
|
|
|
@csrf_exempt
|
|
@require_POST
|
|
@never_cache
|
|
@sensitive_post_parameters("ticket")
|
|
@sensitive_variables()
|
|
def redeem_roundcube(request):
|
|
if not roundcube_available(request):
|
|
raise Http404
|
|
|
|
token = bridge_ticket(request, settings.ROUNDCUBE_BRIDGE_KEY)
|
|
grant = consume_signon_grant(
|
|
RoundcubeGrant,
|
|
token,
|
|
"mailbox__mail_domain__account",
|
|
"mailbox__provisioning",
|
|
)
|
|
if grant is None:
|
|
raise Http404
|
|
actor = signon_actor(grant, grant.actor)
|
|
if actor is None or not actor_can_open_webmail(actor, grant.mailbox):
|
|
raise Http404
|
|
mailbox = grant.mailbox
|
|
spec = mailbox.spec
|
|
if grant.address != spec.address:
|
|
raise Http404
|
|
|
|
audit = ServiceCredentialAccess.objects.create(
|
|
actor=grant.actor,
|
|
actor_username=grant.actor.get_username(),
|
|
hosting_account_id=mailbox.mail_domain.account_id,
|
|
kind=CredentialKind.MAILBOX,
|
|
resource=spec.address,
|
|
method=ServiceCredentialAccess.Method.WEBMAIL,
|
|
outcome=CredentialOutcome.REQUESTED,
|
|
)
|
|
try:
|
|
credentials = fetch_service_credentials(spec)
|
|
except (OSError, ValueError):
|
|
record_outcome(audit, CredentialOutcome.UNAVAILABLE)
|
|
return JsonResponse(
|
|
{"error": "Mailbox login unavailable."}, status=503
|
|
)
|
|
|
|
current = (
|
|
HostingMailbox.objects.select_related(
|
|
"mail_domain__account", "provisioning"
|
|
)
|
|
.filter(pk=mailbox.pk)
|
|
.first()
|
|
)
|
|
actor = signon_actor(
|
|
grant, get_user_model().objects.filter(pk=grant.actor_id).first()
|
|
)
|
|
if (
|
|
current is None
|
|
or actor is None
|
|
or current.spec != spec
|
|
or not actor_can_open_webmail(actor, current)
|
|
):
|
|
record_outcome(audit, CredentialOutcome.UNAVAILABLE)
|
|
raise Http404
|
|
record_outcome(audit, CredentialOutcome.REVEALED)
|
|
response = JsonResponse(credentials)
|
|
response["Referrer-Policy"] = "no-referrer"
|
|
return response
|
|
|
|
|
|
def record_outcome(audit, outcome):
|
|
audit.outcome = outcome
|
|
audit.save(update_fields=["outcome"])
|