From f9dff3370f7d15468b6c7be6d33625f6bb0e0cb0 Mon Sep 17 00:00:00 2001 From: Ant Zucaro Date: Sun, 11 Aug 2013 10:08:28 -0400 Subject: [PATCH] Move the blind_id verification function to util. Before the only view that was using this function was the submission view. That is no longer going to be the case, so the function should be moved to a general purpose area. Util is the best place for that! --- xonstat/util.py | 35 +++++++++++++++++++++++++++++++++++ xonstat/views/submission.py | 31 +------------------------------ 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/xonstat/util.py b/xonstat/util.py index 8af24ef..d859930 100644 --- a/xonstat/util.py +++ b/xonstat/util.py @@ -1,9 +1,16 @@ +import logging +import pyramid.httpexceptions import re from colorsys import rgb_to_hls, hls_to_rgb from cgi import escape as html_escape from datetime import datetime, timedelta from decimal import Decimal from collections import namedtuple +from xonstat.d0_blind_id import d0_blind_id_verify + + +log = logging.getLogger(__name__) + # Map of special chars to ascii from Darkplace's console.c. _qfont_table = [ @@ -237,3 +244,31 @@ def is_cake_day(create_dt, today_dt=None): cake_day = True return cake_day + + +def verify_request(request): + """Verify requests using the d0_blind_id library""" + + # first determine if we should be verifying or not + val_verify_requests = request.registry.settings.get('xonstat.verify_requests', 'true') + if val_verify_requests == "true": + flg_verify_requests = True + else: + flg_verify_requests = False + + try: + (idfp, status) = d0_blind_id_verify( + sig=request.headers['X-D0-Blind-Id-Detached-Signature'], + querystring='', + postdata=request.body) + + log.debug('\nidfp: {0}\nstatus: {1}'.format(idfp, status)) + except: + idfp = None + status = None + + if flg_verify_requests and not idfp: + log.debug("ERROR: Unverified request") + raise pyramid.httpexceptions.HTTPUnauthorized("Unverified request") + + return (idfp, status) diff --git a/xonstat/views/submission.py b/xonstat/views/submission.py index 6c035fe..7442c80 100644 --- a/xonstat/views/submission.py +++ b/xonstat/views/submission.py @@ -8,10 +8,9 @@ import sqlalchemy.sql.expression as expr from pyramid.response import Response from sqlalchemy import Sequence from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound -from xonstat.d0_blind_id import d0_blind_id_verify from xonstat.elo import process_elos from xonstat.models import * -from xonstat.util import strip_colors, qfont_decode +from xonstat.util import strip_colors, qfont_decode, verify_request log = logging.getLogger(__name__) @@ -167,34 +166,6 @@ def is_supported_gametype(gametype, version): return is_supported -def verify_request(request): - """Verify requests using the d0_blind_id library""" - - # first determine if we should be verifying or not - val_verify_requests = request.registry.settings.get('xonstat.verify_requests', 'true') - if val_verify_requests == "true": - flg_verify_requests = True - else: - flg_verify_requests = False - - try: - (idfp, status) = d0_blind_id_verify( - sig=request.headers['X-D0-Blind-Id-Detached-Signature'], - querystring='', - postdata=request.body) - - log.debug('\nidfp: {0}\nstatus: {1}'.format(idfp, status)) - except: - idfp = None - status = None - - if flg_verify_requests and not idfp: - log.debug("ERROR: Unverified request") - raise pyramid.httpexceptions.HTTPUnauthorized("Unverified request") - - return (idfp, status) - - def do_precondition_checks(request, game_meta, raw_players): """Precondition checks for ALL gametypes. These do not require a database connection.""" -- 2.39.2