appid and derive_key(purpose=) must be bytes, not unicode

This commit is contained in:
Brian Warner 2015-09-24 12:37:55 -07:00
parent e8626fcea2
commit 5d93dccb88
9 changed files with 22 additions and 19 deletions

View File

@ -53,7 +53,7 @@ The synchronous+blocking flow looks like this:
from wormhole.blocking.transcribe import Wormhole from wormhole.blocking.transcribe import Wormhole
from wormhole.public_relay import RENDEZVOUS_RELAY from wormhole.public_relay import RENDEZVOUS_RELAY
mydata = b"initiator's data" mydata = b"initiator's data"
i = Wormhole("appid", RENDEZVOUS_RELAY) i = Wormhole(b"appid", RENDEZVOUS_RELAY)
code = i.get_code() code = i.get_code()
print("Invitation Code: %s" % code) print("Invitation Code: %s" % code)
theirdata = i.get_data(mydata) theirdata = i.get_data(mydata)
@ -66,7 +66,7 @@ from wormhole.blocking.transcribe import Wormhole
from wormhole.public_relay import RENDEZVOUS_RELAY from wormhole.public_relay import RENDEZVOUS_RELAY
mydata = b"receiver's data" mydata = b"receiver's data"
code = sys.argv[1] code = sys.argv[1]
r = Wormhole("appid", RENDEZVOUS_RELAY) r = Wormhole(b"appid", RENDEZVOUS_RELAY)
r.set_code(code) r.set_code(code)
theirdata = r.get_data(mydata) theirdata = r.get_data(mydata)
print("Their data: %s" % theirdata.decode("ascii")) print("Their data: %s" % theirdata.decode("ascii"))
@ -81,7 +81,7 @@ from twisted.internet import reactor
from wormhole.public_relay import RENDEZVOUS_RELAY from wormhole.public_relay import RENDEZVOUS_RELAY
from wormhole.twisted.transcribe import Wormhole from wormhole.twisted.transcribe import Wormhole
outbound_message = b"outbound data" outbound_message = b"outbound data"
w1 = Wormhole("appid", RENDEZVOUS_RELAY) w1 = Wormhole(b"appid", RENDEZVOUS_RELAY)
d = w1.get_code() d = w1.get_code()
def _got_code(code): def _got_code(code):
print "Invitation Code:", code print "Invitation Code:", code
@ -97,7 +97,7 @@ reactor.run()
On the other side, you call `set_code()` instead of waiting for `get_code()`: On the other side, you call `set_code()` instead of waiting for `get_code()`:
```python ```python
w2 = Wormhole("appid", RENDEZVOUS_RELAY) w2 = Wormhole(b"appid", RENDEZVOUS_RELAY)
w2.set_code(code) w2.set_code(code)
d = w2.get_data(my_message) d = w2.get_data(my_message)
... ...
@ -140,7 +140,8 @@ simple bytestring that distinguishes one application from another. To ensure
uniqueness, use a domain name. To use multiple apps for a single domain, just uniqueness, use a domain name. To use multiple apps for a single domain, just
use a string like `example.com/app1`. This string must be the same on both use a string like `example.com/app1`. This string must be the same on both
clients, otherwise they will not see each other. The invitation codes are clients, otherwise they will not see each other. The invitation codes are
scoped to the app-id. scoped to the app-id. Note that the app-id must be a bytestring, not unicode,
so on python3 use `b"appid"`.
Distinct app-ids reduce the size of the connection-id numbers. If fewer than Distinct app-ids reduce the size of the connection-id numbers. If fewer than
ten initiators are active for a given app-id, the connection-id will only ten initiators are active for a given app-id, the connection-id will only

View File

@ -29,6 +29,7 @@ class Wormhole:
version_warning_displayed = False version_warning_displayed = False
def __init__(self, appid, relay): def __init__(self, appid, relay):
if not isinstance(appid, type(b"")): raise UsageError
self.appid = appid self.appid = appid
self.relay = relay self.relay = relay
if not self.relay.endswith("/"): raise UsageError if not self.relay.endswith("/"): raise UsageError

View File

@ -2,7 +2,7 @@ from __future__ import print_function
import sys, os, json, binascii import sys, os, json, binascii
from ..errors import handle_server_error from ..errors import handle_server_error
APPID = "lothar.com/wormhole/file-xfer" APPID = b"lothar.com/wormhole/file-xfer"
@handle_server_error @handle_server_error
def receive_file(args): def receive_file(args):
@ -50,7 +50,7 @@ def receive_file(args):
# now receive the rest of the owl # now receive the rest of the owl
tdata = data["transit"] tdata = data["transit"]
transit_key = w.derive_key(APPID+"/transit-key") transit_key = w.derive_key(APPID+b"/transit-key")
transit_receiver.set_transit_key(transit_key) transit_receiver.set_transit_key(transit_key)
transit_receiver.add_their_direct_hints(tdata["direct_connection_hints"]) transit_receiver.add_their_direct_hints(tdata["direct_connection_hints"])
transit_receiver.add_their_relay_hints(tdata["relay_connection_hints"]) transit_receiver.add_their_relay_hints(tdata["relay_connection_hints"])

View File

@ -2,7 +2,7 @@ from __future__ import print_function
import sys, json, binascii import sys, json, binascii
from ..errors import handle_server_error from ..errors import handle_server_error
APPID = "lothar.com/wormhole/text-xfer" APPID = b"lothar.com/wormhole/text-xfer"
@handle_server_error @handle_server_error
def receive_text(args): def receive_text(args):

View File

@ -2,7 +2,7 @@ from __future__ import print_function
import os, sys, json, binascii import os, sys, json, binascii
from ..errors import handle_server_error from ..errors import handle_server_error
APPID = "lothar.com/wormhole/file-xfer" APPID = b"lothar.com/wormhole/file-xfer"
@handle_server_error @handle_server_error
def send_file(args): def send_file(args):
@ -70,7 +70,7 @@ def send_file(args):
tdata = them_d["transit"] tdata = them_d["transit"]
transit_key = w.derive_key(APPID+"/transit-key") transit_key = w.derive_key(APPID+b"/transit-key")
transit_sender.set_transit_key(transit_key) transit_sender.set_transit_key(transit_key)
transit_sender.add_their_direct_hints(tdata["direct_connection_hints"]) transit_sender.add_their_direct_hints(tdata["direct_connection_hints"])
transit_sender.add_their_relay_hints(tdata["relay_connection_hints"]) transit_sender.add_their_relay_hints(tdata["relay_connection_hints"])

View File

@ -2,7 +2,7 @@ from __future__ import print_function
import sys, json, binascii import sys, json, binascii
from ..errors import handle_server_error from ..errors import handle_server_error
APPID = "lothar.com/wormhole/text-xfer" APPID = b"lothar.com/wormhole/text-xfer"
@handle_server_error @handle_server_error
def send_text(args): def send_text(args):

View File

@ -10,7 +10,7 @@ class Blocking(ServerBase, unittest.TestCase):
# with deferToThread() # with deferToThread()
def test_basic(self): def test_basic(self):
appid = "appid" appid = b"appid"
w1 = BlockingWormhole(appid, self.relayurl) w1 = BlockingWormhole(appid, self.relayurl)
w2 = BlockingWormhole(appid, self.relayurl) w2 = BlockingWormhole(appid, self.relayurl)
d = deferToThread(w1.get_code) d = deferToThread(w1.get_code)
@ -31,7 +31,7 @@ class Blocking(ServerBase, unittest.TestCase):
return d return d
def test_fixed_code(self): def test_fixed_code(self):
appid = "appid" appid = b"appid"
w1 = BlockingWormhole(appid, self.relayurl) w1 = BlockingWormhole(appid, self.relayurl)
w2 = BlockingWormhole(appid, self.relayurl) w2 = BlockingWormhole(appid, self.relayurl)
w1.set_code("123-purple-elephant") w1.set_code("123-purple-elephant")
@ -50,7 +50,7 @@ class Blocking(ServerBase, unittest.TestCase):
return d return d
def test_errors(self): def test_errors(self):
appid = "appid" appid = b"appid"
w1 = BlockingWormhole(appid, self.relayurl) w1 = BlockingWormhole(appid, self.relayurl)
self.assertRaises(UsageError, w1.get_verifier) self.assertRaises(UsageError, w1.get_verifier)
self.assertRaises(UsageError, w1.get_data, "data") self.assertRaises(UsageError, w1.get_data, "data")
@ -65,7 +65,7 @@ class Blocking(ServerBase, unittest.TestCase):
return d return d
def test_serialize(self): def test_serialize(self):
appid = "appid" appid = b"appid"
w1 = BlockingWormhole(appid, self.relayurl) w1 = BlockingWormhole(appid, self.relayurl)
self.assertRaises(UsageError, w1.serialize) # too early self.assertRaises(UsageError, w1.serialize) # too early
w2 = BlockingWormhole(appid, self.relayurl) w2 = BlockingWormhole(appid, self.relayurl)

View File

@ -9,7 +9,7 @@ from .common import ServerBase
class Basic(ServerBase, unittest.TestCase): class Basic(ServerBase, unittest.TestCase):
def test_basic(self): def test_basic(self):
appid = "appid" appid = b"appid"
w1 = Wormhole(appid, self.relayurl) w1 = Wormhole(appid, self.relayurl)
w2 = Wormhole(appid, self.relayurl) w2 = Wormhole(appid, self.relayurl)
d = w1.get_code() d = w1.get_code()
@ -30,7 +30,7 @@ class Basic(ServerBase, unittest.TestCase):
return d return d
def test_fixed_code(self): def test_fixed_code(self):
appid = "appid" appid = b"appid"
w1 = Wormhole(appid, self.relayurl) w1 = Wormhole(appid, self.relayurl)
w2 = Wormhole(appid, self.relayurl) w2 = Wormhole(appid, self.relayurl)
w1.set_code("123-purple-elephant") w1.set_code("123-purple-elephant")
@ -49,7 +49,7 @@ class Basic(ServerBase, unittest.TestCase):
return d return d
def test_errors(self): def test_errors(self):
appid = "appid" appid = b"appid"
w1 = Wormhole(appid, self.relayurl) w1 = Wormhole(appid, self.relayurl)
self.assertRaises(UsageError, w1.get_verifier) self.assertRaises(UsageError, w1.get_verifier)
self.assertRaises(UsageError, w1.get_data, "data") self.assertRaises(UsageError, w1.get_data, "data")
@ -62,7 +62,7 @@ class Basic(ServerBase, unittest.TestCase):
return d return d
def test_serialize(self): def test_serialize(self):
appid = "appid" appid = b"appid"
w1 = Wormhole(appid, self.relayurl) w1 = Wormhole(appid, self.relayurl)
self.assertRaises(UsageError, w1.serialize) # too early self.assertRaises(UsageError, w1.serialize) # too early
w2 = Wormhole(appid, self.relayurl) w2 = Wormhole(appid, self.relayurl)

View File

@ -38,6 +38,7 @@ class Wormhole:
version_warning_displayed = False version_warning_displayed = False
def __init__(self, appid, relay): def __init__(self, appid, relay):
if not isinstance(appid, type(b"")): raise UsageError
self.appid = appid self.appid = appid
self.relay = relay self.relay = relay
self.agent = web_client.Agent(reactor) self.agent = web_client.Agent(reactor)