|
1 | 1 | import sha
|
| 2 | +import base64 |
2 | 3 | import datetime
|
3 | 4 | import pytz
|
4 | 5 | from random import random
|
5 |
| -from django.http import HttpResponse, HttpResponseRedirect |
| 6 | +from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound, HttpResponseBadRequest |
6 | 7 | from django.shortcuts import render_to_response, get_object_or_404
|
7 | 8 | from handball.forms import SignUpForm
|
8 | 9 | from handball.models import Person
|
| 10 | +from handball.api import PersonResource |
| 11 | +from django.contrib.auth import authenticate |
9 | 12 | from django.contrib.auth.models import User
|
10 | 13 | from django.utils.translation import ugettext as _
|
11 | 14 |
|
@@ -70,3 +73,47 @@ def activate(request, activation_key):
|
70 | 73 |
|
71 | 74 | def thanks(request):
|
72 | 75 | 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