Example #1
0
def interactive_args_validation(request: Request) -> Dict:
    """
    Validates the arguments passed to the /webapp endpoints

    Returns a dictionary, either containing an 'error' response
    object or the transaction and asset objects specified by the
    incoming request.
    """
    transaction_id = request.GET.get("transaction_id")
    asset_code = request.GET.get("asset_code")
    callback = request.GET.get("callback")
    amount_str = request.GET.get("amount")
    asset = Asset.objects.filter(code=asset_code, sep24_enabled=True).first()
    if not transaction_id:
        return dict(error=render_error_response(
            _("no 'transaction_id' provided"), content_type="text/html"))
    elif not (asset_code and asset):
        return dict(error=render_error_response(_("invalid 'asset_code'"),
                                                content_type="text/html"))
    try:
        transaction = Transaction.objects.get(id=transaction_id, asset=asset)
    except (Transaction.DoesNotExist, ValidationError):
        return dict(error=render_error_response(
            _("Transaction with ID and asset_code not found"),
            content_type="text/html",
            status_code=status.HTTP_404_NOT_FOUND,
        ))

    # Verify that amount is provided, and that can be parsed into a decimal:
    amount = None
    if amount_str:
        try:
            amount = Decimal(amount_str)
        except (DecimalException, TypeError):
            return dict(error=render_error_response("invalid 'amount'"))

        err_resp = verify_valid_asset_operation(asset,
                                                amount,
                                                transaction.kind,
                                                content_type="text/html")
        if err_resp:
            return dict(error=err_resp)

    return dict(transaction=transaction,
                asset=asset,
                callback=callback,
                amount=amount)
Example #2
0
def interactive_args_validation(request: Request, kind: str) -> Dict:
    """
    Validates the arguments passed to the /webapp endpoints

    Returns a dictionary, either containing an 'error' response
    object or the transaction and asset objects specified by the
    incoming request.
    """
    transaction_id = request.GET.get("transaction_id")
    asset_code = request.GET.get("asset_code")
    callback = request.GET.get("callback")
    on_change_callback = request.GET.get("on_change_callback")
    amount_str = request.GET.get("amount")
    asset = Asset.objects.filter(code=asset_code, sep24_enabled=True).first()
    if not transaction_id:
        return dict(error=render_error_response(
            _("no 'transaction_id' provided"), content_type="text/html"))
    elif not (asset_code and asset):
        return dict(error=render_error_response(_("invalid 'asset_code'"),
                                                content_type="text/html"))
    elif on_change_callback and any(
            domain in on_change_callback
            for domain in settings.CALLBACK_REQUEST_DOMAIN_DENYLIST):
        on_change_callback = None
    try:
        transaction = Transaction.objects.get(id=transaction_id,
                                              asset=asset,
                                              kind=kind)
    except (Transaction.DoesNotExist, ValidationError):
        return dict(error=render_error_response(
            _("Transaction with ID and asset_code not found"),
            content_type="text/html",
            status_code=status.HTTP_404_NOT_FOUND,
        ))

    # Verify that amount is provided, and that can be parsed into a decimal:
    amount = None
    if amount_str:
        try:
            amount = Decimal(amount_str)
        except (DecimalException, TypeError):
            return dict(error=render_error_response(_("invalid 'amount'")))

        err_resp = verify_valid_asset_operation(asset,
                                                amount,
                                                transaction.kind,
                                                content_type="text/html")
        if err_resp:
            return dict(error=err_resp)

    for url in [callback, on_change_callback]:
        if url and url.lower() != "postmessage":
            error_response = validate_url(url)
            if error_response:
                return error_response

    return dict(
        transaction=transaction,
        asset=asset,
        callback=callback,
        on_change_callback=on_change_callback,
        amount=amount,
    )