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

@ -4,7 +4,7 @@ from hashlib import sha1
from app import app from app import app
from util.config.validators import BaseValidator, ConfigValidationException from util.config.validators import BaseValidator, ConfigValidationException
from util.registry.torrent import torrent_jwt from util.registry.torrent import jwt_from_infohash
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -22,7 +22,7 @@ class BittorrentValidator(BaseValidator):
client = app.config['HTTPCLIENT'] client = app.config['HTTPCLIENT']
params = { params = {
'info_hash': sha1('somedata').digest(), 'info_hash': sha1('test').digest(),
'peer_id': '-QUAY00-6wfG2wk6wWLc', 'peer_id': '-QUAY00-6wfG2wk6wWLc',
'uploaded': 0, 'uploaded': 0,
'downloaded': 0, 'downloaded': 0,
@ -31,7 +31,7 @@ class BittorrentValidator(BaseValidator):
'port': 80, 'port': 80,
} }
encoded_jwt = torrent_jwt(params) encoded_jwt = jwt_from_infohash(params['info_hash'])
params['jwt'] = encoded_jwt params['jwt'] = encoded_jwt
resp = client.get(announce_url, timeout=5, params=params) resp = client.get(announce_url, timeout=5, params=params)

View file

@ -1,7 +1,7 @@
import hashlib import hashlib
import time import time
import urllib
from binascii import hexlify
from cachetools import lru_cache from cachetools import lru_cache
import bencode import bencode
@ -21,22 +21,27 @@ def _load_private_key(private_key_file_path):
with open(private_key_file_path) as private_key_file: with open(private_key_file_path) as private_key_file:
return private_key_file.read() 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. private key.
""" """
token_data = { token_data = {
'iss': instance_keys.service_name, 'iss': instance_keys.service_name,
'aud': ANNOUNCE_URL, 'aud': ANNOUNCE_URL,
'infohash': _infohash(info_dict), 'infohash': hexlify(infohash_digest),
} }
return jwt.encode(token_data, instance_keys.local_private_key, 'RS256') 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): def make_torrent(name, webseed, length, piece_length, pieces):
info_dict = { info_dict = {
@ -48,7 +53,7 @@ def make_torrent(name, webseed, length, piece_length, pieces):
} }
return bencode.bencode({ return bencode.bencode({
'announce': ANNOUNCE_URL + "?jwt=" + torrent_jwt(info_dict), 'announce': ANNOUNCE_URL + "?jwt=" + jwt_from_infodict(info_dict),
'url-list': webseed, 'url-list': webseed,
'encoding': 'UTF-8', 'encoding': 'UTF-8',
'created by': REGISTRY_TITLE, 'created by': REGISTRY_TITLE,
@ -56,9 +61,11 @@ def make_torrent(name, webseed, length, piece_length, pieces):
'info': info_dict, 'info': info_dict,
}) })
def public_torrent_filename(blob_uuid): def public_torrent_filename(blob_uuid):
return hashlib.sha256(blob_uuid).hexdigest() return hashlib.sha256(blob_uuid).hexdigest()
def per_user_torrent_filename(user_uuid, blob_uuid): def per_user_torrent_filename(user_uuid, blob_uuid):
return hashlib.sha256(FILENAME_PEPPER + "||" + blob_uuid + "||" + user_uuid).hexdigest() return hashlib.sha256(FILENAME_PEPPER + "||" + blob_uuid + "||" + user_uuid).hexdigest()