<commit_before>import BaseHTTPServer
import time
import sys
import cgitb
import random
import hmac, sha

from urlparse import urlparse

from openid.consumer import OpenIDConsumer, SimpleHTTPClient
from openid.interface import Request
from openid.association import (BaseAssociationManager,
                                DiffieHelmanAssociator,
                                DumbAssociationManager)
from openid.errors import (ProtocolError, UserCancelled,
                           ValueMismatchError)
from openid.util import random_string, append_args, hmacsha1, to_b64

import exutil

http_client = SimpleHTTPClient()

class DictionaryAssociationManager(BaseAssociationManager):
    def __init__(self):
        associator = DiffieHelmanAssociator(http_client)
        BaseAssociationManager.__init__(self, associator)
        self.associations = [] # inefficient, but ok for a toy example

    def update(self, new_assoc, expired):
        # This is horribly inefficient.  Don't use this code outside
        # of toy examples.
        if new_assoc is not None:
            self.associations.append(new_assoc)

        if expired is not None:
            for assoc1 in expired:
                for i, assoc2 in enumerate(self.associations):
                    if assoc1 == assoc2:
                        del self.associations[i]
                        break

    def get_all(self, server_url):
        results = []
        for assoc in self.associations:
            if assoc.server_url == server_url:
                results.append(assoc)

        return results

    def invalidate(self, server_url, assoc_handle):
        for i, assoc in enumerate(self.associations):
            if assoc.server_url == server_url and assoc.handle == assoc_handle:
                del self.associations[i]
                break

assoc_mngr = None

class SampleConsumer(OpenIDConsumer):
    def __init__(self, *args, **kwargs):
        OpenIDConsumer.__init__(self, *args, **kwargs)
        self.secret = random_string(20, random.SystemRandom())

    def get_assoc_mngr(self):
        return assoc_mngr

    def get_http_client(self):
        return http_client

    def verify_return_to(self, req):
        # parse the input url
        proto, host, selector, params, qs, frag = urlparse(req.return_to)
        if host != 'localhost:8081':
            return False

        query = exutil.parseQuery(qs)
        v = to_b64(hmacsha1(self.secret, query['id'] + query['time']))

        if v != query['v']:
            return False

        # reject really old return_to urls
        if int(query['time']) + 60 * 60 * 6 > int(time.time()):
            return False

        return True

    def create_return_to(self, base, identity):
        args = {
            'id': identity,
            'time': str(int(time.time())),
            }

        args['v'] = to_b64(hmacsha1(self.secret, args['id'] + args['time']))
        return append_args(base, args)<commit_msg>[project @ Fix time-related bug in sample consumer]<commit_after>import BaseHTTPServer
import time
import sys
import cgitb
import random
import hmac, sha

from urlparse import urlparse

from openid.consumer import OpenIDConsumer, SimpleHTTPClient
from openid.interface import Request
from openid.association import (BaseAssociationManager,
                                DiffieHelmanAssociator,
                                DumbAssociationManager)
from openid.errors import (ProtocolError, UserCancelled,
                           ValueMismatchError)
from openid.util import random_string, append_args, hmacsha1, to_b64

import exutil

http_client = SimpleHTTPClient()

class DictionaryAssociationManager(BaseAssociationManager):
    def __init__(self):
        associator = DiffieHelmanAssociator(http_client)
        BaseAssociationManager.__init__(self, associator)
        self.associations = [] # inefficient, but ok for a toy example

    def update(self, new_assoc, expired):
        # This is horribly inefficient.  Don't use this code outside
        # of toy examples.
        if new_assoc is not None:
            self.associations.append(new_assoc)

        if expired is not None:
            for assoc1 in expired:
                for i, assoc2 in enumerate(self.associations):
                    if assoc1 == assoc2:
                        del self.associations[i]
                        break

    def get_all(self, server_url):
        results = []
        for assoc in self.associations:
            if assoc.server_url == server_url:
                results.append(assoc)

        return results

    def invalidate(self, server_url, assoc_handle):
        for i, assoc in enumerate(self.associations):
            if assoc.server_url == server_url and assoc.handle == assoc_handle:
                del self.associations[i]
                break

assoc_mngr = None

class SampleConsumer(OpenIDConsumer):
    def __init__(self, *args, **kwargs):
        OpenIDConsumer.__init__(self, *args, **kwargs)
        self.secret = random_string(20, random.SystemRandom())

    def get_assoc_mngr(self):
        return assoc_mngr

    def get_http_client(self):
        return http_client

    def verify_return_to(self, req):
        # parse the input url
        proto, host, selector, params, qs, frag = urlparse(req.return_to)
        if host != 'localhost:8081':
            return False

        query = exutil.parseQuery(qs)
        v = to_b64(hmacsha1(self.secret, query['id'] + query['time']))

        if v != query['v']:
            return False

        # reject really old return_to urls
        if int(query['time']) + 60 * 60 * 6 < int(time.time()):
            return False

        return True

    def create_return_to(self, base, identity):
        args = {
            'id': identity,
            'time': str(int(time.time())),
            }

        args['v'] = to_b64(hmacsha1(self.secret, args['id'] + args['time']))
        return append_args(base, args)
{'fraction_non_alphanumeric': 0.08501344086021505, 'fraction_numerical': 0.008064516129032258, 'mean_word_length': 2.2944106253458774, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '5075854', 'n_tokens_mistral': 1733, 'n_tokens_neox': 1630, 'n_words': 449}