Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for verifying webhook signatures #257

Merged

Conversation

mrashed-dev
Copy link
Collaborator

@mrashed-dev mrashed-dev commented Mar 30, 2023

Description

This PR adds support for verifying the webhook signature of inbound webhook notifications.

Usage

from flask import Flask, request, jsonify
from nylas import APIClient, Webhook
import os
import json

app = Flask(__name__)
port = 9000
NYLAS_CLIENT_SECRET = os.environ.get("NYLAS_CLIENT_SECRET")

@app.route("/", methods=["POST"])
def webhook_callback():
    signature = request.headers.get("X-Nylas-Signature")
    request_data = json.dumps(request.get_json())
    
    if not Webhook.verify_webhook_signature(signature, request_data, NYLAS_CLIENT_SECRET):
        return jsonify({"error": "Invalid signature"}), 403

    body = request.json
    print("Webhook event received: ", json.dumps(body))

    return jsonify({"success": True}), 200

if __name__ == "__main__":
    app.run(port=port)

License

I confirm that this contribution is made under the terms of the MIT license and that I have the authority necessary to make this contribution on behalf of its copyright owner.

@mrashed-dev mrashed-dev requested review from a team, tasdrake and dominicj-nylas and removed request for a team March 30, 2023 19:50
@codecov
Copy link

codecov bot commented Mar 30, 2023

Codecov Report

Merging #257 (cab6a49) into main (246d0b2) will increase coverage by 0.01%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #257      +/-   ##
==========================================
+ Coverage   95.96%   95.97%   +0.01%     
==========================================
  Files          17       17              
  Lines        1710     1716       +6     
==========================================
+ Hits         1641     1647       +6     
  Misses         69       69              
Impacted Files Coverage Δ
nylas/client/restful_models.py 97.17% <100.00%> (+0.03%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@mrashed-dev mrashed-dev merged commit 5ed1367 into main Apr 3, 2023
5 checks passed
@mrashed-dev mrashed-dev deleted the ACE-117-python-sdk-add-webhook-verification-function branch April 3, 2023 19:28
@mrashed-dev mrashed-dev mentioned this pull request Apr 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants