Validates the signature associated with the given request.

def validate_signature(request, secret_key):
    """
    Validates the signature associated with the given request.
    """

    # Extract the request parameters according to the HTTP method
    data = request.GET.copy()
    if request.method != 'GET':
        message_body = getattr(request, request.method, {})
        data.update(message_body)

    # Make sure the request contains a signature
    if data.get('sig', False):
        sig = data['sig']
        del data['sig']
    else:
        return False

    # Make sure the request contains a timestamp
    if data.get('t', False):
        timestamp = int(data.get('t', False))
        del data['t']
    else:
        return False

    # Make sure the signature has not expired
    local_time = datetime.utcnow()
    remote_time = datetime.utcfromtimestamp(timestamp)
    
    
    # this stops a bug if the client clock is ever a little ahead of 
    # the server clock.  Makes the window of acceptable time current +/- 5 mins
    if local_time > remote_time:
        delta = local_time - remote_time
    else:   
        delta = remote_time - local_time
    
    if delta.seconds > 5 * 60:  # If the signature is older than 5 minutes, it's invalid
        return False

    # Make sure the signature is valid
    return sig == calculate_signature(secret_key, data, timestamp)