util.config.validators: fix torrent validation

This code was mistaken the info dict with the params passed in an
announce request. Rather, now we expose a function for creating a jwt
from infohashes directly.
This commit is contained in:
Jimmy Zelinskie 2017-06-06 15:11:42 -04:00
parent 6321d08024
commit 7d07c2ed07
2 changed files with 20 additions and 13 deletions

View file

@ -1,7 +1,7 @@
import hashlib
import time
import urllib
from binascii import hexlify
from cachetools import lru_cache
import bencode
@ -21,22 +21,27 @@ def _load_private_key(private_key_file_path):
with open(private_key_file_path) as private_key_file:
return private_key_file.read()
def torrent_jwt(info_dict):
""" Returns an encoded JWT for the given information dictionary, signed by the local instance's
def jwt_from_infodict(infodict):
""" Returns an encoded JWT for the given BitTorrent info dict, signed by the local instance's
private key.
"""
digest = hashlib.sha1()
digest.update(bencode.bencode(infodict))
return jwt_from_infohash(digest.digest())
def jwt_from_infohash(infohash_digest):
""" Returns an encoded JWT for the given BitTorrent infohash, signed by the local instance's
private key.
"""
token_data = {
'iss': instance_keys.service_name,
'aud': ANNOUNCE_URL,
'infohash': _infohash(info_dict),
'infohash': hexlify(infohash_digest),
}
return jwt.encode(token_data, instance_keys.local_private_key, 'RS256')
def _infohash(infodict):
digest = hashlib.sha1()
digest.update(bencode.bencode(infodict))
return urllib.quote(digest.digest())
def make_torrent(name, webseed, length, piece_length, pieces):
info_dict = {
@ -48,7 +53,7 @@ def make_torrent(name, webseed, length, piece_length, pieces):
}
return bencode.bencode({
'announce': ANNOUNCE_URL + "?jwt=" + torrent_jwt(info_dict),
'announce': ANNOUNCE_URL + "?jwt=" + jwt_from_infodict(info_dict),
'url-list': webseed,
'encoding': 'UTF-8',
'created by': REGISTRY_TITLE,
@ -56,9 +61,11 @@ def make_torrent(name, webseed, length, piece_length, pieces):
'info': info_dict,
})
def public_torrent_filename(blob_uuid):
return hashlib.sha256(blob_uuid).hexdigest()
def per_user_torrent_filename(user_uuid, blob_uuid):
return hashlib.sha256(FILENAME_PEPPER + "||" + blob_uuid + "||" + user_uuid).hexdigest()