Statistics
| Branch: | Tag: | Revision:

root / astakos / im / target / local.py @ 64cd4730

History | View | Annotate | Download (4.6 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

    
38
from astakos.im.target.util import prepare_response
39
from astakos.im.models import User
40

    
41
from urllib import unquote
42

    
43
from hashlib import new as newhasher
44

    
45
def login(request):
46
    username = request.POST.get('username')
47
    password = request.POST.get('password')
48
    
49
    if not username:
50
        return HttpResponseBadRequest('No user')
51
    
52
    if not password:
53
        return HttpResponseBadRequest('No password')
54
    
55
    try:
56
        user = User.objects.get(uniq=username)
57
    except User.DoesNotExist:
58
        return HttpResponseBadRequest('No such user')
59
    
60
    hasher = newhasher('sha256')
61
    hasher.update(password)
62
    password = hasher.hexdigest()
63
    
64
    if not password or user.password != password:
65
        return HttpResponseBadRequest('Wrong password')
66
    
67
    if user.state == 'UNVERIFIED':
68
        return HttpResponseBadRequest('Unverified account')
69
    
70
    next = request.POST.get('next')
71
    return prepare_response(request, user, next)
72
    
73
def activate(request):
74
    token = request.GET.get('auth')
75
    next = request.GET.get('next')
76
    try:
77
        user = User.objects.get(auth_token=token)
78
    except User.DoesNotExist:
79
        return HttpResponseBadRequest('No such user')
80
    
81
    user.state = 'ACTIVE'
82
    user.save()
83
    return prepare_response(request, user, next, renew=True)
84

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