livekit / livekit-server-sdk-python

LiveKit Server SDK for Python
Apache License 2.0
26 stars 7 forks source link

Does this sdk have WebhookReceiver support? #2

Closed cristianrat closed 1 year ago

cristianrat commented 1 year ago

Hi, it's me again :) Still a user of this package, and thanks again for making it. I find myself in need of using the Webhook and unsure (can't see it anyway) if this package has support for webhooks

EDIT: I imagine I can simply look at the token, decode it to validate it and do what ever I need to do, even if not directly supported in this library

AlexJMohr commented 1 year ago

Hi again!

Handling the webhook is pretty framework specific. Here's an example with Flask:

@app.post('/livekit/webhook')
def livekit_webhook():
    token = request.headers.get('Authorization')
    if not token:
        abort(400)
    try:
        payload = jwt.decode(token, algorithms=['HS256'], key='<KEY>')
    except jwt.PyJWTError:
        abort(400)
    expected_hash = payload['sha256']
    calculated_hash = base64.b64encode(hashlib.sha256(request.data).digest()).decode()
    if calculated_hash != expected_hash:
        abort(400)
    event = request.json['event']
    # ...do something with event...
    return '', 204

We COULD add a utility function to this library to verify webhook signatures, however it isn't too difficult to write one yourself.

cristianrat commented 1 year ago

thanks for the quick reply @AlexJMohr Yeah, I thought about that after I sent the message, and kinda guessed the same :) I use django-rest-framework, but yeah, shouldn't be too tricky Thanks for the example, I shall borrow some of it ;)