|
@@ -1,14 +1,45 @@
|
|
|
# -*- coding: utf-8 -*-
|
|
|
from __future__ import unicode_literals
|
|
|
from tastypie.authentication import Authentication
|
|
|
+from django.contrib.auth.models import User
|
|
|
+from .jwt_token import check_token, get_username
|
|
|
+import simplejson as json
|
|
|
|
|
|
class JWTAuthentication(Authentication):
|
|
|
'''
|
|
|
'''
|
|
|
def is_authenticated(self, request, **kwargs):
|
|
|
- return True
|
|
|
+ # Check content type
|
|
|
+ if request.content_type != 'application/json':
|
|
|
+ return False
|
|
|
+
|
|
|
+ # Check body
|
|
|
+ if not request.body:
|
|
|
+ return False
|
|
|
+
|
|
|
+ body = json.loads(request.body)
|
|
|
+
|
|
|
+ # Check required parameters
|
|
|
+ if 'token' not in body:
|
|
|
+ return False
|
|
|
+
|
|
|
+ return check_token(body['token'])
|
|
|
|
|
|
'''
|
|
|
'''
|
|
|
def get_identifier(self, request):
|
|
|
- return request.user.username
|
|
|
+ # Check content type
|
|
|
+ if request.content_type != 'application/json':
|
|
|
+ return None
|
|
|
+
|
|
|
+ # Check body
|
|
|
+ if not request.body:
|
|
|
+ return None
|
|
|
+
|
|
|
+ body = json.loads(request.body)
|
|
|
+
|
|
|
+ # Check required parameters
|
|
|
+ if 'token' not in body:
|
|
|
+ return None
|
|
|
+
|
|
|
+ return get_username(body['token'])
|