Ejemplo n.º 1
0
 def _wrapped_func_arguments(request: HttpRequest,
                             email: Text = REQ(),
                             api_key: Optional[Text] = REQ(
                                 default=None),
                             api_key_legacy: Optional[Text] = REQ(
                                 'api-key', default=None),
                             *args: Any,
                             **kwargs: Any) -> HttpResponse:
     if api_key is None:
         api_key = api_key_legacy
     if api_key is None:
         raise RequestVariableMissingError("api_key")
     user_profile = validate_api_key(request, email, api_key,
                                     is_webhook)
     # Apply rate limiting
     limited_func = rate_limit()(view_func)
     try:
         return limited_func(request, user_profile, *args, **kwargs)
     except Exception as err:
         if is_webhook:
             # In this case, request_body is passed explicitly because the body
             # of the request has already been read in has_request_variables and
             # can't be read/accessed more than once, so we just access it from
             # the request.POST QueryDict.
             log_exception_to_webhook_logger(
                 request,
                 user_profile,
                 request_body=request.POST.get('payload'))
         raise err
Ejemplo n.º 2
0
def api_slack_incoming_webhook(
    request: HttpRequest,
    user_profile: UserProfile,
    user_specified_topic: Optional[str] = REQ("topic", default=None),
) -> HttpResponse:

    # Slack accepts webhook payloads as payload="encoded json" as
    # application/x-www-form-urlencoded, as well as in the body as
    # application/json.
    if request.content_type == "application/json":
        try:
            val = request.body.decode(request.encoding or "utf-8")
        except UnicodeDecodeError:  # nocoverage
            raise JsonableError(_("Malformed payload"))
    else:
        req_var = "payload"
        if req_var in request.POST:
            val = request.POST[req_var]
        elif req_var in request.GET:  # nocoverage
            val = request.GET[req_var]
        else:
            raise RequestVariableMissingError(req_var)

    payload = to_wild_value("payload", val)

    if user_specified_topic is None and "channel" in payload:
        channel = payload["channel"].tame(check_string)
        user_specified_topic = re.sub("^[@#]", "", channel)

    if user_specified_topic is None:
        user_specified_topic = "(no topic)"

    pieces = []
    if "blocks" in payload and payload["blocks"]:
        for block in payload["blocks"]:
            pieces.append(render_block(block))

    if "attachments" in payload and payload["attachments"]:
        for attachment in payload["attachments"]:
            pieces.append(render_attachment(attachment))

    body = "\n\n".join(piece.strip() for piece in pieces
                       if piece.strip() != "")

    if body == "" and "text" in payload and payload["text"]:
        if "icon_emoji" in payload and payload["icon_emoji"]:
            body = payload["icon_emoji"].tame(check_string) + " "
        body += payload["text"].tame(check_string)
        body = body.strip()

    if body != "":
        body = replace_formatting(replace_links(body).strip())
        check_send_webhook_message(request, user_profile, user_specified_topic,
                                   body)
    return json_success(request)
Ejemplo n.º 3
0
 def _wrapped_func_arguments(request, email=REQ(), api_key=REQ(default=None),
                             api_key_legacy=REQ('api-key', default=None),
                             *args, **kwargs):
     # type: (HttpRequest, Text, Optional[Text], Optional[Text], *Any, **Any) -> HttpResponse
     if api_key is None:
         api_key = api_key_legacy
     if api_key is None:
         raise RequestVariableMissingError("api_key")
     user_profile = validate_api_key(request, email, api_key, is_webhook)
     # Apply rate limiting
     limited_func = rate_limit()(view_func)
     return limited_func(request, user_profile, *args, **kwargs)
Ejemplo n.º 4
0
def api_slack_incoming_webhook(
    request: HttpRequest,
    user_profile: UserProfile,
    user_specified_topic: Optional[str] = REQ("topic", default=None),
) -> HttpResponse:

    # Slack accepts webhook payloads as payload="encoded json" as
    # application/x-www-form-urlencoded, as well as in the body as
    # application/json.
    if request.content_type == "application/json":
        try:
            val = request.body.decode(request.encoding or "utf-8")
        except UnicodeDecodeError:  # nocoverage
            raise JsonableError(_("Malformed payload"))
    else:
        req_var = "payload"
        if req_var in request.POST:
            val = request.POST[req_var]
        elif req_var in request.GET:  # nocoverage
            val = request.GET[req_var]
        else:
            raise RequestVariableMissingError(req_var)
    try:
        payload = orjson.loads(val)
    except orjson.JSONDecodeError:  # nocoverage
        raise InvalidJSONError(_("Malformed JSON"))

    if user_specified_topic is None and "channel" in payload:
        user_specified_topic = re.sub("^[@#]", "", payload["channel"])

    if user_specified_topic is None:
        user_specified_topic = "(no topic)"

    body = ""

    if "blocks" in payload:
        for block in payload["blocks"]:
            body = add_block(block, body)

    if "attachments" in payload:
        for attachment in payload["attachments"]:
            body = add_attachment(attachment, body)

    if body == "" and "text" in payload and payload["text"] is not None:
        body += payload["text"]
        if "icon_emoji" in payload and payload["icon_emoji"] is not None:
            body = "{} {}".format(payload["icon_emoji"], body)

    if body != "":
        body = replace_formatting(replace_links(body).strip())
        check_send_webhook_message(request, user_profile, user_specified_topic,
                                   body)
    return json_success(request)
Ejemplo n.º 5
0
 def _wrapped_view_func(request, email=REQ(), api_key=REQ('api_key', default=None),
                        api_key_legacy=REQ('api-key', default=None),
                        *args, **kwargs):
     if not api_key and not api_key_legacy:
         raise RequestVariableMissingError("api_key")
     elif not api_key:
         api_key = api_key_legacy
     user_profile = validate_api_key(email, api_key)
     request.user = user_profile
     request._email = user_profile.email
     process_client(request, user_profile)
     # Apply rate limiting
     limited_func = rate_limit()(view_func)
     return limited_func(request, user_profile, *args, **kwargs)
Ejemplo n.º 6
0
 def _wrapped_func_arguments(request, email=REQ(), api_key=REQ(default=None),
                             api_key_legacy=REQ('api-key', default=None),
                             *args, **kwargs):
     # type: (HttpRequest, text_type, Optional[text_type], Optional[text_type], *Any, **Any) -> HttpResponse
     if not api_key and not api_key_legacy:
         raise RequestVariableMissingError("api_key")
     elif not api_key:
         api_key = api_key_legacy
     user_profile = validate_api_key(request, email, api_key, is_webhook)
     request.user = user_profile
     request._email = user_profile.email
     process_client(request, user_profile)
     # Apply rate limiting
     limited_func = rate_limit()(view_func)
     return limited_func(request, user_profile, *args, **kwargs)
Ejemplo n.º 7
0
def api_dropbox_webhook(
        request: HttpRequest,
        user_profile: UserProfile,
        challenge: Optional[str] = REQ(default=None),
) -> HttpResponse:
    if request.method == "POST":
        topic = "Dropbox"
        check_send_webhook_message(request, user_profile, topic,
                                   "File has been updated on Dropbox!")
        return json_success(request)
    else:
        if challenge is None:
            raise RequestVariableMissingError("challenge")
        return HttpResponse(challenge,
                            content_type="text/plain; charset=UTF-8")