async def incoming_event(request): slack = request.app.plugins["slack"] payload = await request.json() LOG.log(5, "Incoming event payload: %s", payload) if payload.get("type") == "url_verification": if slack.signing_secret: try: raw_payload = await request.read() validate_request_signature(raw_payload.decode("utf-8"), request.headers, slack.signing_secret) return Response(body=payload["challenge"]) except (InvalidSlackSignature, InvalidTimestamp): return Response(status=500) elif payload["token"] == slack.verify: return Response(body=payload["challenge"]) else: return Response(status=500) try: verification_token = await _validate_request(request, slack) event = Event.from_http(payload, verification_token=verification_token) except (FailedVerification, InvalidSlackSignature, InvalidTimestamp): return Response(status=401) if event["type"] == "message": return await _incoming_message(event, request) else: futures = list(_dispatch(slack.routers["event"], event, request.app)) if futures: return await _wait_and_check_result(futures) return Response(status=200)
async def _validate_request(request, slack): if slack.signing_secret: raw_payload = await request.read() validate_request_signature(raw_payload.decode("utf-8"), request.headers, slack.signing_secret) return None else: return slack.verify
def test_validate_request_signature_ok(self): headers = { "X-Slack-Request-Timestamp": "1534688291", "X-Slack-Signature": "v0=ac720e09cb1ecb0baa17bea5638fa3d11fc177576dd364e05475d6dbc620c696", } body = """{"token":"abcdefghijkl","team_id":"T000000","api_app_id":"A000000","event":{},"type":"event_callback","authed_teams":["T000000"],"event_id":"AAAAAAA","event_time":1111111111}""" sansio.validate_request_signature(body=body, headers=headers, signing_secret="mysupersecret")