<commit_before>import cgi
import urlparse

from xml.sax.saxutils import escape, quoteattr
from openid import oidUtil

def buildRedirect(redirect_url):
    return """\
Location: %s
Content-type: text/plain

Redirecting to %s""" % (redirect_url, redirect_url)

def parseQuery(qs):
    query = {}
    for k, v in cgi.parse_qsl(qs):
        query[k] = v
    return query

class SimpleOpenIDProxy(object):
    def __init__(self, query, this_uri):
        self.this_uri = this_uri
        self.parsed_uri = urlparse.urlparse(this_uri)
        self.query = query

    def getUserInput(self):
        """Return the URL that the user entered to be verified.

        This just gets the URL from the form data. This step will fail
        if there was no input.
        """
        return self.query.get('identity_url')

    def getParameters(self):
        """Return all parameters that are part of the OpenID protocol,
        as a dictionary.

        That is, return all parameters that have 'openid.' as a prefix
        """
        return self.query

    def getReturnTo(self, token):
        """Generate a return_to URL.

        The return_to URL should trigger the processServerResult
        method of the openid_consumer object to be called.

        Our return_to URL is this script with the parameter
        step=process and the library's token attached. We could have
        used some other mechanism to get the token to the next step,
        such as setting a cookie, but adding it to the URL is easy.
        """
        return_to_query = [('step', 'process'), ('token', token)]
        return oidUtil.appendArgs(self.this_uri, return_to_query)

    def verifyReturnTo(self, return_to):
        """Check that the return_to URL from the server is what we
        expect.

        We make sure that it is something that could have been
        generated by our getReturnTo method and that the token in it
        matches the token that we got. The return_to URL should be a
        prefix of the current URL.

        We also extract the token from the URL to return back to the
        library.
        """
        parsed_uri = urlparse.urlparse(return_to)
        if parsed_uri[:4] != self.parsed_uri[:4]:
            return False

        # query is the query from the return_to url.
        #
        # self.query is the query this request received.
        #
        # Check that they match, on the parameters included in the
        # return_to url.
        query = parseQuery(parsed_uri[4])
        if query.get('step') != self.query.get('step'):
            return False

        if query.get('token') != self.query.get('token'):
            return False

        return True

    def getToken(self):
        return self.query.get('token')

    # ======================================================================
    # Callbacks from processServerResult

    def loginGood(self, normalized_id):
        fmt = "You have successfully verified %s as your identity."
        message = fmt % (escape(normalized_id),)
        return message, False, normalized_id

    def loginCancelled(self):
        return "Verification cancelled.", False, None

    ## Failure cases

    def loginError(self, normalized_id):
        if normalized_id is not None:<commit_msg>[project @ Work with comments in simpleproxy]<commit_after>import cgi
import urlparse

from xml.sax.saxutils import escape, quoteattr
from openid import oidUtil

def buildRedirect(redirect_url):
    return """\
Location: %s
Content-type: text/plain

Redirecting to %s""" % (redirect_url, redirect_url)

def parseQuery(qs):
    query = {}
    for k, v in cgi.parse_qsl(qs):
        query[k] = v
    return query

class SimpleOpenIDProxy(object):
    def __init__(self, query, this_uri):
        self.this_uri = this_uri
        self.parsed_uri = urlparse.urlparse(this_uri)
        self.query = query

    def getUserInput(self):
        """Return the URL that the user entered to be verified.

        This just gets the URL from the form data. This step will fail
        if there was no input.
        """
        return self.query.get('identity_url')

    def getParameters(self):
        """Return all parameters that are part of the OpenID protocol,
        as a dictionary.

        That is, return all parameters that have 'openid.' as a prefix
        """
        return self.query

    def getReturnTo(self, token):
        """Generate a return_to URL.

        The return_to URL should trigger the processServerResult
        method of the openid_consumer object to be called.

        Our return_to URL is this script with the parameter
        step=process and the library's token attached. We could have
        used some other mechanism to get the token to the next step,
        such as setting a cookie, but adding it to the URL is easy.
        """
        return_to_query = [('step', 'process'), ('token', token)]
        return oidUtil.appendArgs(self.this_uri, return_to_query)

    def verifyReturnTo(self, return_to):
        """Check that the return_to URL from the server is what we
        expect.

        We make sure that it is something that could have been
        generated by our getReturnTo method.
        """
        parsed_uri = urlparse.urlparse(return_to)
        if parsed_uri[:4] != self.parsed_uri[:4]:
            return False

        # query is the query from the return_to url.
        #
        # self.query is the query this request received.
        #
        # Check that they match, on the parameters included in the
        # return_to url.
        query = parseQuery(parsed_uri[4])
        if query.get('step') != self.query.get('step'):
            return False

        if query.get('token') != self.query.get('token'):
            return False

        return True

    def getToken(self):
        """Extract the token from the query for the library."""
        return self.query.get('token')

    # ======================================================================
    # Callbacks from processServerResult

    def loginGood(self, normalized_id):
        fmt = "You have successfully verified %s as your identity."
        message = fmt % (escape(normalized_id),)
        return message, False, normalized_id

    def loginCancelled(self):
        return "Verification cancelled.", False, None

    ## Failure cases

    def loginError(self, normalized_id):
        if normalized_id is not None:
{'fraction_non_alphanumeric': 0.09951795988182242, 'fraction_numerical': 0.0009329808738920852, 'mean_word_length': 2.482403898213319, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1841918', 'n_tokens_mistral': 1732, 'n_tokens_neox': 1686, 'n_words': 658}