|
5 | 5 | from tastypie.authorization import DjangoAuthorization, Authorization
|
6 | 6 | from tastypie.authentication import BasicAuthentication, Authentication, ApiKeyAuthentication
|
7 | 7 | from handball.authorization import ManagerAuthorization
|
| 8 | +from django.contrib.auth import authenticate |
| 9 | +from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound, HttpResponseBadRequest |
| 10 | +from tastypie.serializers import Serializer |
| 11 | +from tastypie.utils.mime import determine_format |
8 | 12 |
|
9 | 13 |
|
10 | 14 | class UnionResource(ModelResource):
|
@@ -142,5 +146,86 @@ class EventResource(ModelResource):
|
142 | 146 | class Meta:
|
143 | 147 | queryset = Event.objects.all()
|
144 | 148 | authorization = Authorization()
|
145 |
| - authentication = ApiKeyAuthentication() |
| 149 | + authentication = Authentication() |
146 | 150 | include_resource_uri = False
|
| 151 | + |
| 152 | + |
| 153 | +""" |
| 154 | +Non-resource api endpoints |
| 155 | +""" |
| 156 | + |
| 157 | + |
| 158 | +def validate_user(request): |
| 159 | + """ |
| 160 | + Checks a user's basic auth credentials and, if valid, returns the users data |
| 161 | + """ |
| 162 | + |
| 163 | + # if not request.META.get('HTTP_AUTHORIZATION'): |
| 164 | + # return HttpResponseBadRequest('No HTTP_AUTHORIZATION header found') |
| 165 | + |
| 166 | + # try: |
| 167 | + # (auth_type, data) = request.META['HTTP_AUTHORIZATION'].split() |
| 168 | + # if auth_type.lower() != 'basic': |
| 169 | + # return HttpResponseBadRequest('Wrong auth type. Use basic auth!') |
| 170 | + # user_pass = base64.b64decode(data) |
| 171 | + # except: |
| 172 | + # return HttpResponseBadRequest('Could not decode auth credentials.') |
| 173 | + |
| 174 | + # bits = user_pass.split(':', 1) |
| 175 | + |
| 176 | + # if len(bits) != 2: |
| 177 | + # return HttpResponseBadRequest('Could not decode auth credentials.') |
| 178 | + |
| 179 | + # user = authenticate(username=bits[0], password=bits[1]) |
| 180 | + |
| 181 | + username = request.POST['username'] |
| 182 | + password = request.POST['password'] |
| 183 | + |
| 184 | + if not username or not password: |
| 185 | + return HttpResponseBadRequest() |
| 186 | + |
| 187 | + user = authenticate(username=username, password=password) |
| 188 | + |
| 189 | + if user is None or not user.is_active: |
| 190 | + return HttpResponseNotFound('User does not exist or password incorrect.') |
| 191 | + |
| 192 | + person = user.get_profile() |
| 193 | + |
| 194 | + person_resource = PersonResource() |
| 195 | + bundle = person_resource.build_bundle(obj=person, request=request) |
| 196 | + person_resource.full_dehydrate(bundle) |
| 197 | + bundle.data['api_key'] = user.api_key.key |
| 198 | + |
| 199 | + return HttpResponse(person_resource.serialize(None, bundle, 'application/json')) |
| 200 | + |
| 201 | + |
| 202 | +def user_exists(request): |
| 203 | + username = request.GET['username'] |
| 204 | + email = request.GET['email'] |
| 205 | + username_exists = True |
| 206 | + email_exists = True |
| 207 | + |
| 208 | + try: |
| 209 | + User.objects.get(username=username) |
| 210 | + except User.DoesNotExist: |
| 211 | + username_exists = False |
| 212 | + except User.MultipleObjectsReturned: |
| 213 | + email_exists = True |
| 214 | + |
| 215 | + try: |
| 216 | + User.objects.get(email=email) |
| 217 | + except User.DoesNotExist: |
| 218 | + email_exists = False |
| 219 | + except User.MultipleObjectsReturned: |
| 220 | + email_exists = True |
| 221 | + |
| 222 | + data = { |
| 223 | + 'username': username_exists, |
| 224 | + 'email': email_exists |
| 225 | + } |
| 226 | + |
| 227 | + serializer = Serializer() |
| 228 | + |
| 229 | + format = determine_format(request, serializer, default_format='application/json') |
| 230 | + |
| 231 | + return HttpResponse(serializer.serialize(data, format, {})) |
0 commit comments