<commit_before>import urlparse
import urllib
import string
import time

from openid.consumer.stores import ConsumerAssociation, DumbStore
from openid.consumer.parse import parseLinkAttrs
from openid import oidUtil
from openid.dh import DiffieHellman

class OpenIDConsumer(object):
    NONCE_LEN = 8
    NONCE_CHRS = string.letters + string.digits
    TOKEN_LIFETIME = 60 * 2 # two minutes
    AUTH_BLOB_LIFETIME = 60 * 60 * 1 # one hour

    def __init__(self, store, trust_root, fetcher, immediate, split):
        self.store = store
        self.trust_root = trust_root
        self.fetcher = fetcher

        if immediate:
            self.mode = 'checkid_immediate'
        else:
            self.mode = 'checkid_setup'

        self.immediate = immediate
        self.split = split


    def constructRedirect(self, proxy):
        user_url = proxy.getUserInput()
        if user_url is None:
            return None

        ret = self._findIdentityInfo(user_url)
        if ret is None:
            return None

        consumer_id, server_id, server_url = ret
        nonce = oidUtil.randomString(self.NONCE_LEN, self.NONCE_CHRS)
        self.store.storeNonce(nonce)

        token = self._genToken(nonce, consumer_id, server_url)
        return_to = proxy.getReturnTo(token)

        redir_args = {'openid.identity': server_id,
                      'openid.return_to': return_to,}

        if self.trust_root is not None:
            redir_args['openid.trust_root'] = self.trust_root

        redir_args['openid.mode'] = self.mode

        assoc = self._getAssociation(server_url)

        if assoc is not None:
            redir_args['openid.assoc_handle'] = assoc.handle

        return str(oidUtil.appendArgs(server_url, redir_args))


    def processServerResponse(self, proxy):
        mode = self._extract(proxy, 'mode')
        func = getattr(self, '_do_' + mode, None)
        if func is None:
            return proxy.loginError(None)

        return func(proxy)


    def checkAuth(self, proxy):
        blob = proxy.getCheckAuthParams()
        if blob is None:
            return proxy.loginError(None)

        ret = self._splitCheckAuthBlob(blob)
        if ret is None:
            return proxy.loginError(None)

        nonce, consumer_id, post_data, server_url = ret

        return self._checkAuth(
            proxy, nonce, consumer_id, post_data, server_url)

    def _checkAuth(self, proxy, nonce, consumer_id, post_data, server_url):
        ret = self.fetcher.post(server_url, post_data)
        if ret is None:
            return proxy.loginError(consumer_id)

        results = oidUtil.parsekv(ret[1])
        is_valid = results.get('is_valid', 'false')

        if is_valid == 'true':
            invalidate_handle = results.get('invalidate_handle')
            if invalidate_handle is not None:
                self.store.removeAssociation(server_url, invalidate_handle)

            if not self.store.useNonce(nonce):
                return proxy.loginError(consumer_id)

            return proxy.loginGood(consumer_id)

        error = results.get('error')
        if error is not None:
            return proxy.serverError(error)

        return proxy.loginError(consumer_id)

    def _do_id_res(self, proxy):
        token = proxy.getToken()
        if token is None:
            return proxy.loginError(None)

        ret = self._splitToken(token)
        if ret is None:
            return proxy.loginError(None)

        nonce, consumer_id, server_url = ret

        return_to = self._extract(proxy, 'return_to')
        server_id = self._extract(proxy, 'identity')
        assoc_handle = self._extract(proxy, 'assoc_handle')

        if return_to is None or server_id is None or assoc_handle is None:
            return proxy.loginError(consumer_id)

        if not proxy.verifyReturnTo(return_to):
            return proxy.loginError(consumer_id)

        user_setup_url = self._extract(proxy, 'user_setup_url')
        if user_setup_url is not None:
            return proxy.setupNeeded(user_setup_url)

        assoc = self.store.getAssociation(server_url)

        if (assoc is None or assoc.handle != assoc_handle or
            assoc.expiresIn <= 0):
            # It's not an association we know about.  Dumb mode is our
            # only possible path for recovery.
            check_args = {}
            for k, v in proxy.getParameters().iteritems():
                if k.startswith('openid.'):
                    check_args[k] = v
            check_args['openid.mode'] = 'check_authentication'
            import sys
            sys.stderr.write('handle from server: %r' % check_args['openid.assoc_handle'])
            post_data = urllib.urlencode(check_args)

            if self.split:
                blob = self._genCheckAuthBlob(
                    nonce, consumer_id, post_data, server_url)

                return proxy.checkAuthRequired(blob)

            else:
                return self._checkAuth(
                    proxy, nonce, consumer_id, post_data, server_url)

        # Check the signature<commit_msg>[project @ Remove debugging code.]<commit_after>import urlparse
import urllib
import string
import time

from openid.consumer.stores import ConsumerAssociation, DumbStore
from openid.consumer.parse import parseLinkAttrs
from openid import oidUtil
from openid.dh import DiffieHellman

class OpenIDConsumer(object):
    NONCE_LEN = 8
    NONCE_CHRS = string.letters + string.digits
    TOKEN_LIFETIME = 60 * 2 # two minutes
    AUTH_BLOB_LIFETIME = 60 * 60 * 1 # one hour

    def __init__(self, store, trust_root, fetcher, immediate, split):
        self.store = store
        self.trust_root = trust_root
        self.fetcher = fetcher

        if immediate:
            self.mode = 'checkid_immediate'
        else:
            self.mode = 'checkid_setup'

        self.immediate = immediate
        self.split = split


    def constructRedirect(self, proxy):
        user_url = proxy.getUserInput()
        if user_url is None:
            return None

        ret = self._findIdentityInfo(user_url)
        if ret is None:
            return None

        consumer_id, server_id, server_url = ret
        nonce = oidUtil.randomString(self.NONCE_LEN, self.NONCE_CHRS)
        self.store.storeNonce(nonce)

        token = self._genToken(nonce, consumer_id, server_url)
        return_to = proxy.getReturnTo(token)

        redir_args = {'openid.identity': server_id,
                      'openid.return_to': return_to,}

        if self.trust_root is not None:
            redir_args['openid.trust_root'] = self.trust_root

        redir_args['openid.mode'] = self.mode

        assoc = self._getAssociation(server_url)

        if assoc is not None:
            redir_args['openid.assoc_handle'] = assoc.handle

        return str(oidUtil.appendArgs(server_url, redir_args))


    def processServerResponse(self, proxy):
        mode = self._extract(proxy, 'mode')
        func = getattr(self, '_do_' + mode, None)
        if func is None:
            return proxy.loginError(None)

        return func(proxy)


    def checkAuth(self, proxy):
        blob = proxy.getCheckAuthParams()
        if blob is None:
            return proxy.loginError(None)

        ret = self._splitCheckAuthBlob(blob)
        if ret is None:
            return proxy.loginError(None)

        nonce, consumer_id, post_data, server_url = ret

        return self._checkAuth(
            proxy, nonce, consumer_id, post_data, server_url)

    def _checkAuth(self, proxy, nonce, consumer_id, post_data, server_url):
        ret = self.fetcher.post(server_url, post_data)
        if ret is None:
            return proxy.loginError(consumer_id)

        results = oidUtil.parsekv(ret[1])
        is_valid = results.get('is_valid', 'false')

        if is_valid == 'true':
            invalidate_handle = results.get('invalidate_handle')
            if invalidate_handle is not None:
                self.store.removeAssociation(server_url, invalidate_handle)

            if not self.store.useNonce(nonce):
                return proxy.loginError(consumer_id)

            return proxy.loginGood(consumer_id)

        error = results.get('error')
        if error is not None:
            return proxy.serverError(error)

        return proxy.loginError(consumer_id)

    def _do_id_res(self, proxy):
        token = proxy.getToken()
        if token is None:
            return proxy.loginError(None)

        ret = self._splitToken(token)
        if ret is None:
            return proxy.loginError(None)

        nonce, consumer_id, server_url = ret

        return_to = self._extract(proxy, 'return_to')
        server_id = self._extract(proxy, 'identity')
        assoc_handle = self._extract(proxy, 'assoc_handle')

        if return_to is None or server_id is None or assoc_handle is None:
            return proxy.loginError(consumer_id)

        if not proxy.verifyReturnTo(return_to):
            return proxy.loginError(consumer_id)

        user_setup_url = self._extract(proxy, 'user_setup_url')
        if user_setup_url is not None:
            return proxy.setupNeeded(user_setup_url)

        assoc = self.store.getAssociation(server_url)

        if (assoc is None or assoc.handle != assoc_handle or
            assoc.expiresIn <= 0):
            # It's not an association we know about.  Dumb mode is our
            # only possible path for recovery.
            check_args = {}
            for k, v in proxy.getParameters().iteritems():
                if k.startswith('openid.'):
                    check_args[k] = v
            check_args['openid.mode'] = 'check_authentication'
            post_data = urllib.urlencode(check_args)

            if self.split:
                blob = self._genCheckAuthBlob(
                    nonce, consumer_id, post_data, server_url)

                return proxy.checkAuthRequired(blob)

            else:
                return self._checkAuth(
                    proxy, nonce, consumer_id, post_data, server_url)

        # Check the signature
{'fraction_non_alphanumeric': 0.082414854532817, 'fraction_numerical': 0.0021844901201469565, 'mean_word_length': 2.19441801458928, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '6238828', 'n_tokens_mistral': 3040, 'n_tokens_neox': 2865, 'n_words': 711}