Statistics
| Branch: | Tag: | Revision:

root / astakos / im / target / local.py @ 0905ccd2

History | View | Annotate | Download (5 kB)

1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseBadRequest
35
from django.conf import settings
36
from django.template.loader import render_to_string
37
from django.shortcuts import render_to_response
38
from django.template import RequestContext
39
from django.contrib.auth import authenticate
40
from django.utils.translation import ugettext as _
41

    
42
from astakos.im.target.util import prepare_response
43
from astakos.im.models import AstakosUser
44
from astakos.im.forms import LoginForm
45

    
46
from urllib import unquote
47

    
48
from hashlib import new as newhasher
49

    
50
def login(request, on_failure='index.html'):
51
    """
52
    on_failure: whatever redirect accepts as to
53
    """
54
    form = LoginForm(request.POST)
55
    
56
    if not form.is_valid():
57
        return render_to_response(on_failure,
58
                                  {'form':form},
59
                                  context_instance=RequestContext(request))
60
    
61
    user = authenticate(**form.cleaned_data)
62
    status = 'success'
63
    if not user:
64
        status = 'error'
65
        message = _('Cannot authenticate account')
66
    elif not user.is_active:
67
        status = 'error'
68
        message = _('Inactive account')
69
    
70
    if status == 'error':
71
        return render_to_response(on_failure,
72
                                  {'form':form,
73
                                   'message': _('Unverified account')},
74
                                  context_instance=RequestContext(request))
75
    
76
    next = request.POST.get('next')
77
    return prepare_response(request, user, next)
78
    
79
def activate(request):
80
    token = request.GET.get('auth')
81
    next = request.GET.get('next')
82
    try:
83
        user = AstakosUser.objects.get(auth_token=token)
84
    except AstakosUser.DoesNotExist:
85
        return HttpResponseBadRequest('No such user')
86
    
87
    user.is_active = True
88
    user.save()
89
    return prepare_response(request, user, next, renew=True)
90

    
91
def reset_password(request):
92
    if request.method == 'GET':
93
        cookie_value = unquote(request.COOKIES.get('_pithos2_a', ''))
94
        if cookie_value and '|' in cookie_value:
95
            token = cookie_value.split('|', 1)[1]
96
        else:
97
            token = request.GET.get('auth')
98
        next = request.GET.get('next')
99
        username = request.GET.get('username')
100
        kwargs = {'auth': token,
101
                  'next': next,
102
                  'username' : username}
103
        if not token:
104
            kwargs.update({'status': 'error',
105
                           'message': 'Missing token'})
106
        html = render_to_string('reset.html', kwargs)
107
        return HttpResponse(html)
108
    elif request.method == 'POST':
109
        token = request.POST.get('auth')
110
        username = request.POST.get('username')
111
        password = request.POST.get('password')
112
        next = request.POST.get('next')
113
        if not token:
114
            status = 'error'
115
            message = 'Bad Request: missing token'
116
        try:
117
            user = AstakosUser.objects.get(auth_token=token)
118
            if username != user.username:
119
                status = 'error'
120
                message = 'Bad Request: username mismatch'
121
            else:
122
                user.password = password
123
                user.status = 'NORMAL'
124
                user.save()
125
                return prepare_response(request, user, next, renew=True)
126
        except AstakosUser.DoesNotExist:
127
            status = 'error'
128
            message = 'Bad Request: invalid token'
129
            
130
        html = render_to_string('reset.html', {
131
                'status': status,
132
                'message': message})
133
        return HttpResponse(html)