Skip to content

Commit de37fff

Browse files
committed
user validation and api_key retrieval via api
1 parent 37392cd commit de37fff

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

urls.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
urlpatterns = patterns('handball.views',
55
(r'^$', 'index'),
6-
(r'^signup/$', 'sign_up'),
7-
(r'^activate/([abcdef0123456789]+)$', 'activate'),
8-
(r'^thanks/$', 'thanks')
6+
(r'^auth/signup/$', 'sign_up'),
7+
(r'^auth/activate/([abcdef0123456789]+)$', 'activate'),
8+
(r'^thanks/$', 'thanks'),
9+
(r'^auth/validate/$', 'validate_user')
910
)

views.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import sha
2+
import base64
23
import datetime
34
import pytz
45
from random import random
5-
from django.http import HttpResponse, HttpResponseRedirect
6+
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound, HttpResponseBadRequest
67
from django.shortcuts import render_to_response, get_object_or_404
78
from handball.forms import SignUpForm
89
from handball.models import Person
10+
from handball.api import PersonResource
11+
from django.contrib.auth import authenticate
912
from django.contrib.auth.models import User
1013
from django.utils.translation import ugettext as _
1114

@@ -70,3 +73,47 @@ def activate(request, activation_key):
7073

7174
def thanks(request):
7275
return render_to_response('thanks.html')
76+
77+
78+
def validate_user(request):
79+
"""
80+
Checks a user's basic auth credentials and, if valid, returns the users data
81+
"""
82+
83+
# if not request.META.get('HTTP_AUTHORIZATION'):
84+
# return HttpResponseBadRequest('No HTTP_AUTHORIZATION header found')
85+
86+
# try:
87+
# (auth_type, data) = request.META['HTTP_AUTHORIZATION'].split()
88+
# if auth_type.lower() != 'basic':
89+
# return HttpResponseBadRequest('Wrong auth type. Use basic auth!')
90+
# user_pass = base64.b64decode(data)
91+
# except:
92+
# return HttpResponseBadRequest('Could not decode auth credentials.')
93+
94+
# bits = user_pass.split(':', 1)
95+
96+
# if len(bits) != 2:
97+
# return HttpResponseBadRequest('Could not decode auth credentials.')
98+
99+
# user = authenticate(username=bits[0], password=bits[1])
100+
101+
username = request.POST['username']
102+
password = request.POST['password']
103+
104+
if not username or not password:
105+
return HttpResponseBadRequest()
106+
107+
user = authenticate(username=username, password=password)
108+
109+
if user is None or not user.is_active:
110+
return HttpResponseNotFound('User does not exist or password incorrect.')
111+
112+
person = user.get_profile()
113+
114+
person_resource = PersonResource()
115+
bundle = person_resource.build_bundle(obj=person, request=request)
116+
person_resource.full_dehydrate(bundle)
117+
bundle.data['api_key'] = user.api_key.key
118+
119+
return HttpResponse(person_resource.serialize(None, bundle, 'application/json'))

0 commit comments

Comments
 (0)