|
@@ -1,8 +1,10 @@
|
|
|
# -*- coding: utf-8 -*-
|
|
|
from __future__ import unicode_literals
|
|
|
from django.conf.urls import url
|
|
|
+from tastypie import http
|
|
|
from tastypie.resources import Resource
|
|
|
from tastypie.utils import trailing_slash
|
|
|
+from tastypie.exceptions import ImmediateHttpResponse
|
|
|
from api.utils import jwt_token
|
|
|
import simplejson as json
|
|
|
|
|
@@ -28,38 +30,23 @@ class JWTResource(Resource):
|
|
|
|
|
|
# Check content type
|
|
|
if request.content_type != 'application/json':
|
|
|
- return self.create_response(request, {
|
|
|
- 'status': 401,
|
|
|
- 'error_message': 'request is not json'
|
|
|
- })
|
|
|
+ raise ImmediateHttpResponse(response=http.HttpUnauthorized())
|
|
|
|
|
|
# Check body
|
|
|
if not request.body:
|
|
|
- return self.create_response(request, {
|
|
|
- 'status': 401,
|
|
|
- 'error_message': 'request body is empty'
|
|
|
- })
|
|
|
-
|
|
|
- body = json.loads(request.body)
|
|
|
+ raise ImmediateHttpResponse(response=http.HttpUnauthorized())
|
|
|
|
|
|
# Check required parameters
|
|
|
+ body = json.loads(request.body)
|
|
|
if 'username' not in body or 'password' not in body:
|
|
|
- return self.create_response(request, {
|
|
|
- 'status': 401,
|
|
|
- 'error_message': 'username or password not provided in request'
|
|
|
- })
|
|
|
-
|
|
|
- token = jwt_token.create_token(body['username'], body['password'])
|
|
|
+ raise ImmediateHttpResponse(response=http.HttpUnauthorized())
|
|
|
|
|
|
# Check user
|
|
|
+ token = jwt_token.create_token(body['username'], body['password'])
|
|
|
if not token:
|
|
|
- return self.create_response(request, {
|
|
|
- 'status': 401,
|
|
|
- 'error_message': 'cannot authenticate user'
|
|
|
- })
|
|
|
+ raise ImmediateHttpResponse(response=http.HttpUnauthorized())
|
|
|
|
|
|
bundle = self.build_bundle(obj={
|
|
|
- 'status': 200,
|
|
|
'token': token,
|
|
|
'username': body['username']
|
|
|
}, request=request)
|
|
@@ -73,33 +60,27 @@ class JWTResource(Resource):
|
|
|
|
|
|
# Check content type
|
|
|
if request.content_type != 'application/json':
|
|
|
- return self.create_response(request, {
|
|
|
- 'status': 401,
|
|
|
- 'error': 'request is not json'
|
|
|
- })
|
|
|
+ raise ImmediateHttpResponse(response=http.HttpUnauthorized())
|
|
|
|
|
|
# Check body
|
|
|
if not request.body:
|
|
|
- return self.create_response(request, {
|
|
|
- 'status': 401,
|
|
|
- 'error': 'request body is empty'
|
|
|
- })
|
|
|
-
|
|
|
- body = json.loads(request.body)
|
|
|
+ raise ImmediateHttpResponse(response=http.HttpUnauthorized())
|
|
|
|
|
|
# Check required parameters
|
|
|
+ body = json.loads(request.body)
|
|
|
if 'token' not in body:
|
|
|
- return self.create_response(request, {
|
|
|
- 'status': 401,
|
|
|
- 'error': 'token not provided in request'
|
|
|
- })
|
|
|
+ raise ImmediateHttpResponse(response=http.HttpUnauthorized())
|
|
|
|
|
|
(user, ok) = jwt_token.check_token(body['token'])
|
|
|
|
|
|
+ # Check status
|
|
|
+ response_status = (401, 200)[bool(ok)]
|
|
|
+ if response_status == 401:
|
|
|
+ raise ImmediateHttpResponse(response=http.HttpUnauthorized())
|
|
|
+
|
|
|
bundle = self.build_bundle(obj={
|
|
|
- 'status': (401, 200)[bool(ok)],
|
|
|
'token': body['token'],
|
|
|
- 'username': (None, user.username)[bool(user.username)]
|
|
|
+ 'username': user.username
|
|
|
}, request=request)
|
|
|
|
|
|
return self.create_response(request, bundle.obj)
|