Repository: matrix-org/synapse

Issue:
VOIP crashes under Python 3
```
2018-09-07 12:33:00,847 - synapse.http.server - 101 - ERROR - b'GET'-83- Failed handle request via <function JsonResource._async_render at 0x7f3dc8904158>: <XForwardedForRequest at 0x7f3dc633c198 method=b'GET' uri=b'/_matrix/client/r0/voi
p/turnServer?' clientproto=b'HTTP/1.1' site=8008>: Traceback (most recent call last):
  File "/home/krombel/synapse/venv35/lib/python3.5/site-packages/Twisted-18.7.0-py3.5-linux-x86_64.egg/twisted/internet/defer.py", line 1418, in _inlineCallbacks
    result = g.send(result)
  File "/home/krombel/synapse/synapse/http/server.py", line 295, in _async_render
    callback_return = yield callback(request, **kwargs)
  File "/home/krombel/synapse/venv35/lib/python3.5/site-packages/Twisted-18.7.0-py3.5-linux-x86_64.egg/twisted/internet/defer.py", line 1613, in unwindGenerator
    return _cancellableInlineCallbacks(gen)
  File "/home/krombel/synapse/venv35/lib/python3.5/site-packages/Twisted-18.7.0-py3.5-linux-x86_64.egg/twisted/internet/defer.py", line 1529, in _cancellableInlineCallbacks
    _inlineCallbacks(None, g, status) 
--- <exception caught here> ---
  File "/home/krombel/synapse/synapse/http/server.py", line 81, in wrapped_request_handler
    yield h(self, request)
  File "/home/krombel/synapse/synapse/http/server.py", line 295, in _async_render
    callback_return = yield callback(request, **kwargs)
  File "/home/krombel/synapse/venv35/lib/python3.5/site-packages/Twisted-18.7.0-py3.5-linux-x86_64.egg/twisted/internet/defer.py", line 1418, in _inlineCallbacks                                                                                 result = g.send(result)
  File "/home/krombel/synapse/synapse/rest/client/v1/voip.py", line 45, in on_GET
    mac = hmac.new(turnSecret, msg=username, digestmod=hashlib.sha1) 
  File "/home/krombel/synapse/venv35/lib/python3.5/hmac.py", line 144, in new                                                                                                                                                                     return HMAC(key, msg, digestmod)
  File "/home/krombel/synapse/venv35/lib/python3.5/hmac.py", line 42, in __init__
    raise TypeError("key: expected bytes or bytearray, but got %r" % type(key).__name__)
builtins.TypeError: key: expected bytes or bytearray, but got 'str'
```


Discussion:

Resolving patch:
diff --git a/synapse/rest/client/v1/voip.py b/synapse/rest/client/v1/voip.py
--- a/synapse/rest/client/v1/voip.py
+++ b/synapse/rest/client/v1/voip.py
@@ -42,7 +42,11 @@ def on_GET(self, request):
             expiry = (self.hs.get_clock().time_msec() + userLifetime) / 1000
             username = "%d:%s" % (expiry, requester.user.to_string())
 
-            mac = hmac.new(turnSecret, msg=username, digestmod=hashlib.sha1)
+            mac = hmac.new(
+                turnSecret.encode(),
+                msg=username.encode(),
+                digestmod=hashlib.sha1
+            )
             # We need to use standard padded base64 encoding here
             # encode_base64 because we need to add the standard padding to get the
             # same result as the TURN server.


Test patch: