<commit_before># -*- coding: utf-8 -*-
# This code is distributed under the two-clause BSD license.
# Copyright (c) 2012-2013 <NAME>

from __future__ import absolute_import, unicode_literals

import hashlib
import urllib

from django import forms
from django import http

from .. import conf


class LoginForm(forms.Form):
    def __init__(self, fields=(), *args, **kwargs):
        super(LoginForm, self).__init__(*args, **kwargs)

        for name in fields:
            self.fields[name] = forms.CharField(label=name.capitalize(), required=False)

    def build_reply(self, challenge):
        config = conf.AuthGroupeXConf()

        auth_parts = ['1', challenge, config.KEY]
        reply = http.QueryDict('', mutable=True)

        for field in config.FIELDS:
            value = self.cleaned_data[field]
            reply[field] = value
            auth_parts.append(value)

        auth_parts.append('1')
        auth = ''.join(auth_parts).encode('utf8')

        reply['auth'] = hashlib.md5(auth).hexdigest()

        return reply.urlencode()


long_text = forms.TextInput(attrs={'size': 60})


class EndPointForm(forms.Form):
    url = forms.CharField(max_length=600, widget=long_text, help_text=u"The return URL")
    challenge = forms.CharField(max_length=64, widget=long_text,
            help_text=u"A random challenge, crafted specifically for this request")
    _pass = forms.CharField(max_length=64, widget=long_text,
            help_text=u"Signature : md5(challenge + private_key)")

    def __init__(self, *args, **kwargs):
        super(EndPointForm, self).__init__(*args, **kwargs)
        self.fields['pass'] = self.fields.pop('_pass')

    def clean_url(self):
        return urllib.unquote(self.cleaned_data['url'])

    def clean(self):
        config = conf.AuthGroupeXConf()
        challenge = self.cleaned_data['challenge']
        password = self.cleaned_data['pass']

        expected_pass = hashlib.md5(challenge + config.KEY).hexdigest()

        if expected_pass != password:
            raise forms.ValidationError(u"Expected signature %s, got %s" % (expected_pass, password))<commit_msg>fake: improve some form widgets.<commit_after># -*- coding: utf-8 -*-
# This code is distributed under the two-clause BSD license.
# Copyright (c) 2012-2013 <NAME>

from __future__ import absolute_import, unicode_literals

import hashlib
import urllib

from django import forms
from django import http

from .. import conf


PERM_CHOICES = (
    ('admin', "Admin"),
    ('user', "User"),
)

GROUP_AUTH_CHOICES = (
    ('grpmember', "Group member"),
    ('grpadmin', "Group admin"),
)


DEFAULT_FIELDS = {
    'username': forms.SlugField(label="Username", required=False),
    'firstname': forms.CharField(label="First name", required=False),
    'lastname': forms.CharField(label="Last name", required=False),
    'perms': forms.ChoiceField(choices=PERM_CHOICES, label="Permissions", required=False),
    'grpauth': forms.ChoiceField(choices=GROUP_AUTH_CHOICES, label="Group access level", required=False),
}


class LoginForm(forms.Form):
    def __init__(self, fields=(), *args, **kwargs):
        super(LoginForm, self).__init__(*args, **kwargs)

        for name in fields:
            try:
                form_field = DEFAULT_FIELDS[name]
            except KeyError:
                form_field = forms.CharField(label=name.capitalize(), required=False)
            self.fields[name] = form_field

    def build_reply(self, challenge):
        config = conf.AuthGroupeXConf()

        auth_parts = ['1', challenge, config.KEY]
        reply = http.QueryDict('', mutable=True)

        for field in config.FIELDS:
            value = self.cleaned_data[field]
            reply[field] = value
            auth_parts.append(value)

        auth_parts.append('1')
        auth = ''.join(auth_parts).encode('utf8')

        reply['auth'] = hashlib.md5(auth).hexdigest()

        return reply.urlencode()


long_text = forms.TextInput(attrs={'size': 60})


class EndPointForm(forms.Form):
    url = forms.CharField(max_length=600, widget=long_text, help_text="The return URL")
    challenge = forms.CharField(max_length=64, widget=long_text,
            help_text="A random challenge, crafted specifically for this request")
    _pass = forms.CharField(max_length=64, widget=long_text,
            help_text="Signature : md5(challenge + private_key)")

    def __init__(self, *args, **kwargs):
        super(EndPointForm, self).__init__(*args, **kwargs)
        self.fields['pass'] = self.fields.pop('_pass')

    def clean_url(self):
        return urllib.unquote(self.cleaned_data['url'])

    def clean(self):
        config = conf.AuthGroupeXConf()
        challenge = self.cleaned_data['challenge']
        password = self.cleaned_data['pass']

        expected_pass = hashlib.md5(challenge + config.KEY).hexdigest()

        if expected_pass != password:
            raise forms.ValidationError("Expected signature %s, got %s" % (expected_pass, password))
{'fraction_non_alphanumeric': 0.12938816449348045, 'fraction_numerical': 0.00962888665997994, 'mean_word_length': 3.320623916811092, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5606945', 'n_tokens_mistral': 1569, 'n_tokens_neox': 1485, 'n_words': 332}