using_webhooks.rst 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. Using web-hooks
  2. ===============
  3. Web-hooks are a notification system that could be compared to a callback.
  4. Basically, pagure will make a HTTP POST request to one or more third party
  5. server/application with information about what is or just happened.
  6. To set-up a web-hook, simply go to the settings page of your project and
  7. enter the URL to the server/endpoint that will receive the notifications.
  8. There is, in the settings page, a web-hook key which is used by the
  9. server (here pagure) to sign the message sent and which you can use to
  10. ensure the notifications received are coming from the right source.
  11. Each POST request made contains some specific headers:
  12. ::
  13. X-Pagure
  14. X-Pagure-Project
  15. X-Pagure-Signature
  16. X-Pagure-Signature-256
  17. X-Pagure-Topic
  18. ``X-Pagure`` contains URL of the pagure instance sending this notification.
  19. ``X-Pagure-Project`` contains the name of the project on that pagure instance.
  20. ``X-Pagure-Signature`` contains the signature of the message allowing to
  21. check that the message comes from pagure.
  22. ``X-Pagure-Signature-256`` contains the SHA-256 signature of the message
  23. allowing to check that the message comes from pagure.
  24. .. note:: These headers are present to allow you to verify that the webhook
  25. was actually sent by the correct Pagure instance. These are not
  26. included in the signed data.
  27. ``X-Pagure-Topic`` is a global header giving a clue about the type of action
  28. that just occurred. For example ``issue.edit``.
  29. .. warning:: The headers ``X-Pagure``, ``X-Pagure-Project`` and ``X-Pagure-Topic``
  30. are present for convenience only, they are not signed and therefore
  31. should not be trusted. Rely on the payload after checking the
  32. signature to make any decision.
  33. Pagure relies on ``hmac`` to sign the content of its messages. If you want
  34. to validate the message, in python, you can do something like the following:
  35. ::
  36. import hmac
  37. import hashlib
  38. payload = # content you received in the POST request
  39. headers = # headers of the POST request
  40. project_web_hook_key = # private web-hook key of the project
  41. hashhex = hmac.new(
  42. str(project_web_hook_key), payload, hashlib.sha1).hexdigest()
  43. if hashhex != headers.get('X-Pagure-Signature'):
  44. raise Exception('Message received with an invalid signature')
  45. The notifications sent via web-hooks have the same payload as what is sent
  46. via `fedmsg <http://www.fedmsg.com/en/latest/>`_. Therefore, the list of
  47. pagure topics as well as example messages can be found in the
  48. `fedmsg documentation about pagure
  49. <https://fedora-fedmsg.readthedocs.org/en/latest/topics.html#id532>`_