# Webhook Signature authentication issues

**URL:** <https://community.sendbird.com/t/webhook-signature-authentication-issues/1311>\
**Category:** Sendbird Chat API/SDK\
**Tags:** webhook\
**Created:** [November 23, 2020, 8:36pm UTC](https://community.sendbird.com/t/webhook-signature-authentication-issues/1311 "2020-11-23T20:36:16Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![bredelman](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/bredelman/32/684_2.png) [@bredelman](https://community.sendbird.com/u/bredelman)\
**Post date:** [November 23, 2020, 8:36pm UTC](https://community.sendbird.com/t/webhook-signature-authentication-issues/1311/1 "2020-11-23T20:36:16Z")

</div>

I am trying to verify the x-sendbird-signature in a webhook request using python. I have followed the documentation as well as [this post](https://community.sendbird.com/t/help-with-authenticating-webhook-signature/989) but nothing I have tried is generating the correct signature.

Here is my code

```
    sig = request.headers.get("X-Sendbird-Signature")

    api_key = str(django_settings.SENDBIRD_API_TOKEN).encode("utf8")

    # NOTE: being verbose here so I can mess around with the data being passed to hmac
    data = r"{}".format(request.data) # NOTE: this is pulled from the referenced forum ticket link above
    new_data = str(data).encode("utf8")

    signature_to_compare = hmac.new(
        key=api_key, msg=new_data, digestmod=hashlib.sha256,
    ).hexdigest()

    return signature_to_compare == sig

```

data is being printed out as

`{'category': 'open_channel:message_send', 'silent': False, 'custom_type': '', 'mention_type': 'users', 'mentioned_users': [], 'app_id': '767EBC26-B17E-4E59-9CF3-2D6045F48C62', 'sender_ip_addr': '54.156.5.93', 'type': 'ADMM', 'payload': {'created_at': 1606160253790, 'message': '{"type": "ASSIGNMENT_UPDATED", "assignment": {"id": 392910, "assignedTicket": 884513, "agent": {"id": 488, "displayName": "Ammar Mian", "role": "AGENT", "status": "ACTIVE", "connection": "ONLINE", "email": "ammar.mian08@gmail.com", "photoThumbnailUrl": "", "tier": "INTERMEDIATE", "phoneNumber": null}, "assignedAt": "2020-11-19T14:35:49.686687Z", "responsedAt": null, "endedAt": null, "status": "NOT_RESPONSED", "unreadCount": 57, "responseTime": null}}', 'data': '', 'message_id': 6615908978, 'custom_type': ''}, 'channel': {'data': '', 'channel_url': 'ticket_info_58574dd9_ba06_4985_8b45_57c3977335ce', 'name': 'SENDBIRD_DESK_OPEN_CHANNEL', 'custom_type': 'SENDBIRD_DESK_CHANNEL_CUSTOM_TYPE'}, 'sdk': 'API'}`

and the new\_data variable returns

`b'{\'category\': \'open_channel:message_send\', \'silent\': False, \'custom_type\': \'\', \'mention_type\': \'users\', \'mentioned_users\': [], \'app_id\': \'767EBC26-B17E-4E59-9CF3-2D6045F48C62\', \'sender_ip_addr\': \'54.156.5.93\', \'type\': \'ADMM\', \'payload\': {\'created_at\': 1606160253790, \'message\': \'{"type": "ASSIGNMENT_UPDATED", "assignment": {"id": 392910, "assignedTicket": 884513, "agent": {"id": 488, "displayName": "Ammar Mian", "role": "AGENT", "status": "ACTIVE", "connection": "ONLINE", "email": "ammar.mian08@gmail.com", "photoThumbnailUrl": "", "tier": "INTERMEDIATE", "phoneNumber": null}, "assignedAt": "2020-11-19T14:35:49.686687Z", "responsedAt": null, "endedAt": null, "status": "NOT_RESPONSED", "unreadCount": 57, "responseTime": null}}\', \'data\': \'\', \'message_id\': 6615908978, \'custom_type\': \'\'}, \'channel\': {\'data\': \'\', \'channel_url\': \'ticket_info_58574dd9_ba06_4985_8b45_57c3977335ce\', \'name\': \'SENDBIRD_DESK_OPEN_CHANNEL\', \'custom_type\': \'SENDBIRD_DESK_CHANNEL_CUSTOM_TYPE\'}, \'sdk\': \'API\'}'`

This is my current way of trying it, though I have replicated the exact code recommended in the webhook documentation for python to no avail. I have tried every API key available to me, though I know that the master key is the one you are requiring.

Any help you can offer will be appreciated

---

<div class="post-metadata">

**Author:** ![sardorbek.numonov](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/sardorbek.numonov/32/697_2.png) [@sardorbek.numonov](https://community.sendbird.com/u/sardorbek.numonov)\
**Post date:** [November 24, 2020, 6:17am UTC](https://community.sendbird.com/t/webhook-signature-authentication-issues/1311/2 "2020-11-24T06:17:28Z")

</div>

Hi @bredelman, please try to bytes instead of string. It should work.  
For example: Instead of api\_key = str(django\_settings.SENDBIRD\_API\_TOKEN).encode(“utf8”) and new\_data = str(data).encode(“utf8”),  
api\_key = bytes(str(django\_settings.SENDBIRD\_API\_TOKEN).encode(“utf8”))  
new\_data = bytes(str(data).encode(“utf8”))

---

<div class="post-metadata">

**Author:** ![bredelman](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/bredelman/32/684_2.png) [@bredelman](https://community.sendbird.com/u/bredelman)\
**Post date:** [November 24, 2020, 2:49pm UTC](https://community.sendbird.com/t/webhook-signature-authentication-issues/1311/3 "2020-11-24T14:49:45Z")

</div>

Sorry I had already tried that and just tried again but it doesn’t work.

```
     api_key = bytes(str(django_settings.SENDBIRD_API_TOKEN).encode("utf8"))
    data = bytes(str(request.data).encode("utf8"))
    new_data = str(data).encode("utf8")
    signature_to_compare = hmac.new(
        key=api_key, msg=new_data, digestmod=hashlib.sha256,
    ).hexdigest()
    sig = request.headers.get("X-Sendbird-Signature")
    return signature_to_compare == sig

```

If it’s helpful this is my raw request

`{'category': 'open_channel:message_send', 'silent': False, 'custom_type': '', 'mention_type': 'users', 'mentioned_users': [], 'app_id': '767EBC26-B17E-4E59-9CF3-2D6045F48C62', 'sender_ip_addr': '54.156.5.93', 'type': 'ADMM', 'payload': {'created_at': 1606229304667, 'message': '{"type": "ASSIGNMENT_UPDATED", "assignment": {"id": 392912, "assignedTicket": 884513, "agent": {"id": 491, "displayName": "Brian Edelman", "role": "AGENT", "status": "ACTIVE", "connection": "OFFLINE", "email": "bd.edelman@gmail.com", "photoThumbnailUrl": "", "tier": "INTERMEDIATE", "phoneNumber": null}, "assignedAt": "2020-11-23T20:58:29.040149Z", "responsedAt": "2020-11-23T20:58:51.838921Z", "endedAt": null, "status": "RESPONSED", "unreadCount": 0, "responseTime": 22}}', 'data': '', 'message_id': 6616349242, 'custom_type': ''}, 'channel': {'data': '', 'channel_url': 'ticket_info_58574dd9_ba06_4985_8b45_57c3977335ce', 'name': 'SENDBIRD_DESK_OPEN_CHANNEL', 'custom_type': 'SENDBIRD_DESK_CHANNEL_CUSTOM_TYPE'}, 'sdk': 'API'}`

I have a suspicion that maybe Django Rest Framework or django is doing something to the payload that makes this method not work correctly? Just a suspicion though, no evidence yet.

---

<div class="post-metadata">

**Author:** ![sardorbek.numonov](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/sardorbek.numonov/32/697_2.png) [@sardorbek.numonov](https://community.sendbird.com/u/sardorbek.numonov)\
**Post date:** [November 30, 2020, 6:10am UTC](https://community.sendbird.com/t/webhook-signature-authentication-issues/1311/4 "2020-11-30T06:10:30Z")

</div>

Sorry if I make you confuse. I missed the part `data = r"{}".format(request.data)` on your previous code.  
I think that you can use the `request.data` itself. Because it is bytes. No need to convert it to string and convert back to bytes and convert to a string again. Could you try the following, please?  
`data=request.data`  
signature\_to\_compare = hmac.new(  
key=api\_key, msg=data, digestmod=hashlib.sha256,  
).hexdigest()  
If it does not work again, could you please write direct message?

---

<div class="post-metadata">

**Author:** ![bredelman](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/bredelman/32/684_2.png) [@bredelman](https://community.sendbird.com/u/bredelman)\
**Post date:** [January 12, 2021, 10:43pm UTC](https://community.sendbird.com/t/webhook-signature-authentication-issues/1311/5 "2021-01-12T22:43:00Z")

</div>

Sorry I was away from this task for a while/my account was in limbo. I finally got this working, but now I have another issue, related.

Here is working code.

```
    api_key = bytes(str(django_settings.SENDBIRD_API_TOKEN).encode("utf8"))
    data = bytes(request.body)
    signature_to_compare = hmac.new(
        key=api_key, msg=data, digestmod=hashlib.sha256,
    ).hexdigest()
    if is_desk:
        sig = request.headers.get("x-hub-signature")
    else:
        sig = request.headers.get("X-Sendbird-Signature")
    return signature_to_compare == sig

```

The key was using request.body.

Anyway, you can see I am checking for the type of webhook we are validating against (desk or chat). The documentation for desk API says  
`assert signature_to_compare == 'x_hub_signature' # Check if the value of the 'x-hub-signature' request header is the same as the comparison value you've created.`  
which I believe is wrong entirely from the way it is written, but that aside there are no headers being sent from a status change to my webhook from the sendbird desk API.

Here are my headers for a successful non desk request:  
`{'Host': '18fab870a280.ngrok.io', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'SendBird', 'X-Signature': 'f1d9758302eb38bef05104a44998b4d7167edf1aa37eb30e2cdf399cddf39f38', 'Content-Type': 'application/json', 'Unicode-Escaped': 'true', 'X-Sendbird-Signature': 'f1d9758302eb38bef05104a44998b4d7167edf1aa37eb30e2cdf399cddf39f38', 'Content-Length': '551', 'X-Forwarded-Proto': 'https', 'X-Forwarded-For': '18.214.175.125'}`

and here are my headers for a desk request that is not successful:  
`{'Host': '18fab870a280.ngrok.io', 'User-Agent': 'python-requests/2.22.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Content-Type': 'application/json', 'Content-Length': '2624', 'X-Forwarded-Proto': 'https', 'X-Forwarded-For': '54.146.122.252'}`

So we’re almost there! But it looks like sendbird desk isn’t sending what it says it will.

---

<div class="post-metadata">

**Author:** ![sardorbek.numonov](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/sardorbek.numonov/32/697_2.png) [@sardorbek.numonov](https://community.sendbird.com/u/sardorbek.numonov)\
**Post date:** [January 13, 2021, 2:16am UTC](https://community.sendbird.com/t/webhook-signature-authentication-issues/1311/6 "2021-01-13T02:16:58Z")

</div>

Hi @bredelman, could you share with me your APP\_ID by message, please? I will check it.

---

<div class="post-metadata">

**Author:** ![Tyler](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/tyler/32/810_2.png) [@Tyler](https://community.sendbird.com/u/Tyler)\
**Post date:** [January 13, 2021, 2:34pm UTC](https://community.sendbird.com/t/webhook-signature-authentication-issues/1311/8 "2021-01-13T14:34:07Z")

</div>

@bredelman I would suggest removing your message and DMing him the APP\_ID.

---

<div class="post-metadata">

**Author:** ![bredelman](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/bredelman/32/684_2.png) [@bredelman](https://community.sendbird.com/u/bredelman)\
**Post date:** [January 13, 2021, 2:35pm UTC](https://community.sendbird.com/t/webhook-signature-authentication-issues/1311/9 "2021-01-13T14:35:08Z")

</div>

Yeah I couldn’t find a spot to actually do that. Any suggestions where to find that?

---

<div class="post-metadata">

**Author:** ![Tyler](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/tyler/32/810_2.png) [@Tyler](https://community.sendbird.com/u/Tyler)\
**Post date:** [January 13, 2021, 2:37pm UTC](https://community.sendbird.com/t/webhook-signature-authentication-issues/1311/10 "2021-01-13T14:37:12Z")

</div>

If you click on Sardorbek’s user, in the upper right hand corner is a message button.

 ![CleanShot 2021-01-13 at 09.36.22@2x](https://us1.discourse-cdn.com/flex020/uploads/sendbird/original/1X/6bc557ec899d7863d50d981df74ad83f51e12145.jpeg)

---

<div class="post-metadata">

**Author:** ![Tyler](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/tyler/32/810_2.png) [@Tyler](https://community.sendbird.com/u/Tyler)\
**Post date:** [January 13, 2021, 2:40pm UTC](https://community.sendbird.com/t/webhook-signature-authentication-issues/1311/13 "2021-01-13T14:40:58Z")

</div>

Gotcha. This is likely due to you being a newer member to the forums. Discourse requires you to be a certain level before it allows you to send messages. I went ahead and sent your APP\_ID to him internally. Sorry about the confusion.

> **[Understanding Discourse Trust Levels](https://blog.discourse.org/2018/06/understanding-discourse-trust-levels/)**
>
> The user trust system is a fundamental cornerstone of Discourse. Trust levels are a way of… Sandboxing new users in your community so that they cannot accidentally hurt themselves, or other users while they are learning what to do. Granting...

---

<div class="post-metadata">

**Author:** ![sardorbek.numonov](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/sardorbek.numonov/32/697_2.png) [@sardorbek.numonov](https://community.sendbird.com/u/sardorbek.numonov)\
**Post date:** [January 14, 2021, 3:08am UTC](https://community.sendbird.com/t/webhook-signature-authentication-issues/1311/14 "2021-01-14T03:08:06Z")

</div>

Hi @bredelman. We checked your app. Seems that you didn’t set secret key information for your desk webhooks. Desk webhook is a bit different from chat webhook. You need to set the secret key for the desk webhook. Could you change the secret key? (Settings \> Desk \> Webhooks \> secret key form). Then please use the secret key while [comparing signatures.](https://sendbird.com/docs/desk/v1/platform-api/guides/webhooks?_ga=2.150865265.676492217.1610351187-1324071004.1584496646#2-webhook-endpoint-requirements)

---

<div class="post-metadata">

**Author:** ![bredelman](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/bredelman/32/684_2.png) [@bredelman](https://community.sendbird.com/u/bredelman)\
**Post date:** [January 14, 2021, 3:08pm UTC](https://community.sendbird.com/t/webhook-signature-authentication-issues/1311/15 "2021-01-14T15:08:56Z")

</div>

Ah ok thanks for checking on that. That resolves the issue. For what it’s worth that field is not required though it seems as though it should be given the issues I had. Also it looks as though it is filled with a value with the black circles (like for a password field), even if it is not.
