Verifying HMAC from Microsoft Teams bot in Python Flask -
i trying build microsoft teams chat bot using flask, following instructions on how build custom bots. unable verify hmac auth want security.
based on guides , documentation i've found using following minimial testing app trying calculate hmac incoming request. (bot name , description devbot
, key/security_token below testing).
#!/usr/bin/python # coding=utf-8 flask import flask, request, jsonify import hmac, hashlib, base64, json app = flask(__name__) @app.route('/', methods=['get', 'post']) def webhook(): if request.method == 'post': # authenticate security_token = b"o5xhu8oszwx8w9yim0urkr/ij4tzzizuwz7swc+1hze=" request_data = request.get_data() digest = hmac.new(security_token, msg=request_data, digestmod=hashlib.sha256).digest() signature = base64.b64encode(digest).decode() # todo: verify signature = authorization header hmac here return jsonify({ 'type' : 'message', 'text' : "auth header: {0} <br>calculated hmac: {1}".format(request.headers.get('authorization'), signature), }) elif request.method == 'get': return "hello world" if __name__ == '__main__': app.run(debug=true)
upon sending message @devbot test
following hashes in reply bot, aren't matching expected:
auth header: hmac ludmz97y/z2kwliz1wzasz3hloetdcwk5/ll/fk8gqm= calculated hmac: eaxtdjsluu3z4l94bxfiwvsbhjng9spxwq/uher7kca=
any ideas or pointers? i've been trying sorts of stuff encoding have feeling flask might doing modifies request body or something?
edit 1: small clarification
edit 2: full flask app example
edit 3: sample bot details, input , output examples
another option rather interfacing directly microsoft teams may use microsoft bot connector api.
https://docs.botframework.com/en-us/restapi/connector/
i have bot working microsoft teams using https://github.com/grungnie/microsoftbotframework validating jwt sent microsoft.
Comments
Post a Comment