瀏覽代碼

[FIX] token

Gogs 7 年之前
父節點
當前提交
f249ca6034
共有 3 個文件被更改,包括 17 次插入10 次删除
  1. 9 4
      api/resources/jwt_resource.py
  2. 8 3
      api/utils/jwt_authentication.py
  3. 0 3
      api/utils/jwt_token.py

+ 9 - 4
api/resources/jwt_resource.py

@@ -29,13 +29,15 @@ class JWTResource(Resource):
         # Check content type
         if request.content_type != 'application/json':
             return self.create_response(request, {
-                'error': 'request is not json'
+                'status': 401,
+                'error_message': 'request is not json'
             })
 
         # Check body
         if not request.body:
             return self.create_response(request, {
-                'error': 'request body is empty'
+                'status': 401,
+                'error_message': 'request body is empty'
             })
 
         body = json.loads(request.body)
@@ -43,7 +45,8 @@ class JWTResource(Resource):
         # Check required parameters
         if 'username' not in body or 'password' not in body:
             return self.create_response(request, {
-                'error': 'username or password not provided in request'
+                'status': 401,
+                'error_message': 'username or password not provided in request'
             })
 
         token = jwt_token.create_token(body['username'], body['password'])
@@ -51,10 +54,12 @@ class JWTResource(Resource):
         # Check user
         if not token:
             return self.create_response(request, {
-                'error': 'cannot authenticate user'
+                'status': 401,
+                'error_message': 'cannot authenticate user'
             })
 
         bundle = self.build_bundle(obj={
+            'status': 200,
             'token': token
         }, request=request)
 

+ 8 - 3
api/utils/jwt_authentication.py

@@ -3,10 +3,11 @@ from __future__ import unicode_literals
 from tastypie.authentication import Authentication
 from django.contrib.auth.models import User
 from django.conf import settings
-from .jwt_token import check_token, get_username
+from api.utils.jwt_token import check_token, get_username
 import simplejson as json
 
 class JWTAuthentication(Authentication):
+    
     '''
     '''
     def is_authenticated(self, request, **kwargs):
@@ -24,7 +25,9 @@ class JWTAuthentication(Authentication):
         if not authorization_header.startswith(settings.JWT_PREFIX_HEADER):
             return False
 
-        return check_token(authorization_header[4:])
+        prefix_length = len(settings.JWT_PREFIX_HEADER)
+
+        return check_token(authorization_header[prefix_length + 1:])
     '''
     '''
     def get_identifier(self, request):
@@ -43,4 +46,6 @@ class JWTAuthentication(Authentication):
         if not authorization_header.startswith(settings.JWT_PREFIX_HEADER):
             return False
 
-        return get_username(authorization_header[4:])
+        prefix_length = len(settings.JWT_PREFIX_HEADER)
+
+        return get_username(authorization_header[prefix_length + 1:])

+ 0 - 3
api/utils/jwt_token.py

@@ -45,7 +45,6 @@ def explode_token(token):
     try:
         payload = jwt.decode(token, settings.JWT_SECRET_KEY, algorithm='HS256')
     except DecodeError:
-        print('error')
         return False
 
     # Check payload parameters
@@ -82,8 +81,6 @@ def check_token(token):
     if not payload:
         return False
 
-    print(payload['uid'])
-
     user = User.objects.get(pk=payload['uid'])
 
     # Check if exists user