Ejemplo n.º 1
0
 def clean_password1(self):
     username = self.cleaned_data.get('login')
     password = self.cleaned_data.get('password1')
     user = dj_authenticate(username=username, password=password)
     if not user:
         raise forms.ValidationError("Sorry, that login was invalid. Please try again.")
     return self.cleaned_data
Ejemplo n.º 2
0
def login(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect('http://localhost')
    best_members = member_manager(5)
    best_tags = tag_manager(9)
    tmp = request.GET.get("next", "/")
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            my_username = request.POST.get('login', '')
            my_password = request.POST.get('password1', '')
            user = dj_authenticate(username=my_username, password=my_password)
            if user:
                auth_login(request, user)
                return HttpResponseRedirect('http://localhost' + tmp)
    else:
        form = LoginForm(request.POST)
    return render(
        request, 'login.html', {
            'members': best_members,
            'best_tags': best_tags,
            'user': request.user,
            'temp': tmp,
            'form': form
        })
Ejemplo n.º 3
0
 def clean_password1(self):
     username = self.cleaned_data.get('login')
     password = self.cleaned_data.get('password1')
     user = dj_authenticate(username=username, password=password)
     if not user:
         raise forms.ValidationError(
             "Sorry, that login was invalid. Please try again.")
     return self.cleaned_data
Ejemplo n.º 4
0
def authenticate(request):
    """Authenticate a user, but do *not* log them in.

    If the correct username and password are given, credentials suitable for
    use with MAAS's Web API are returned. This can be used by client libraries
    to exchange a username+password for an API token.

    Accepts HTTP POST requests with the following parameters:

      username: The user's username.
      password: The user's password.
      consumer: The name to use for the token, which can be used to later
          understand which consumer requested and is using a token. Optional.

    If `consumer` is provided, existing credentials belonging to the user with
    a matching consumer will be returned, if any exist, else new credentials
    will be created and labelled with `consumer` before being returned.

    If `consumer` is not provided, the earliest created credentials belonging
    to the user will be returned. If no preexisting credentials exist, new
    credentials will be created and returned.
    """
    if request.method != "POST":
        return HttpResponseNotAllowed(["POST"])

    username = request.POST.get("username")
    password = request.POST.get("password")
    consumer = request.POST.get("consumer")
    user = dj_authenticate(username=username, password=password)

    if user is None or not user.is_active:
        # This is_active check mimics confirm_login_allowed from Django's
        # django.contrib.auth.forms.AuthenticationForm.
        return HttpResponseForbidden()

    # Find an existing token. There might be more than one so take the first.
    tokens = get_auth_tokens(user)
    if consumer is not None:
        tokens = tokens.filter(consumer__name=consumer)
    token = tokens.first()

    # When no existing token is found, create a new one.
    if token is None:
        token = create_auth_token(user, consumer)

    # Return something with the same shape as that rendered by
    # AccountHandler.create_authorisation_token.
    return JsonResponse({
        "consumer_key": token.consumer.key,
        "name": token.consumer.name,
        "token_key": token.key,
        "token_secret": token.secret,
    })
Ejemplo n.º 5
0
 def clean_password(self):
     email = self.cleaned_data.get('email')
     try:
         username = Profile.objects.get(email=email).user.username
     except ObjectDoesNotExist:
         raise forms.ValidationError(
             "Sorry, wrong couple of login/password. Please try again.")
     password = self.cleaned_data.get('password')
     user = dj_authenticate(username=username, password=password)
     if not user:
         raise forms.ValidationError(
             "Sorry, wrong couple of login/password. Please try again.")
     return self.cleaned_data
Ejemplo n.º 6
0
def login(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect('http://localhost')  
    best_members = member_manager(5)
    best_tags = tag_manager(9)
    tmp = request.GET.get("next","/")
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            my_username = request.POST.get('login', '')
            my_password = request.POST.get('password1', '')
            user = dj_authenticate(username = my_username, password = my_password)
            if user:
                auth_login(request, user)
                return HttpResponseRedirect('http://localhost'+tmp)
    else: 
        form = LoginForm(request.POST)
    return render(request, 'login.html', { 'members': best_members, 'best_tags': best_tags,'user':request.user, 'temp': tmp, 'form': form })