|
@@ -20,12 +20,11 @@ from Object.RedisObject import RedisObject
|
|
|
from Object.TokenObject import TokenObject
|
|
|
from Service.ModelService import ModelService
|
|
|
from Service.CommonService import CommonService
|
|
|
-from django.http import JsonResponse
|
|
|
+from django.http import JsonResponse, HttpResponseRedirect
|
|
|
from django.contrib import auth
|
|
|
import time,json
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
- # http://192.168.136.39:8000/login/oauth/authorize
|
|
|
-
|
|
|
+# http://192.168.136.39:8000/login/oauth/authorize
|
|
|
# http://192.168.136.39:8000/application/query
|
|
|
class AuthView(View):
|
|
|
def get(self, request, *args, **kwargs):
|
|
@@ -65,6 +64,7 @@ class AuthView(View):
|
|
|
client_id = request_dict.get("client_id", '')
|
|
|
response_type = request_dict.get("response_type", '')
|
|
|
scope = request_dict.get("scope", '')
|
|
|
+ app_id = request_dict.get("app_id", '')
|
|
|
redirect_uri = request_dict.get("redirect_uri", '')
|
|
|
client_secret = request_dict.get("client_secret", '')
|
|
|
token = request_dict.get('token', None)
|
|
@@ -80,11 +80,16 @@ class AuthView(View):
|
|
|
nowTime = int(time.time())
|
|
|
user_qs = GrantCodeModel.objects.filter(userID__userID=userID)
|
|
|
code = CommonService.encrypt_data(randomlength=32)
|
|
|
- Application = ApplicationModel.objects.filter(client_id=client_id)
|
|
|
- if Application.exists():
|
|
|
- print(Application.exists())
|
|
|
+ application = ApplicationModel.objects.filter(client_id=client_id)
|
|
|
+
|
|
|
+ if application.exists():
|
|
|
+ print(application.exists())
|
|
|
else:
|
|
|
- return JsonResponse({'error': 'config error,client_id This value is wrong'})
|
|
|
+ return response.json(10005)
|
|
|
+
|
|
|
+ if application[0].redirect_uri != redirect_uri:
|
|
|
+ return response.json(10006)
|
|
|
+
|
|
|
if not user_qs.exists():
|
|
|
print('在创建')
|
|
|
try:
|
|
@@ -92,6 +97,7 @@ class AuthView(View):
|
|
|
userID=Device_User.objects.get(userID=userID),
|
|
|
application=ApplicationModel.objects.get(client_id=client_id),
|
|
|
code=code,
|
|
|
+ app_id=app_id,
|
|
|
expire_time=nowTime+3600,
|
|
|
add_time=nowTime,
|
|
|
update_time=nowTime)
|
|
@@ -102,13 +108,16 @@ class AuthView(View):
|
|
|
return response.json(178)
|
|
|
else:
|
|
|
print('在修改')
|
|
|
- user_qs.update(code=code,update_time=nowTime,expire_time=nowTime+3600)
|
|
|
+ user_qs.update(code=code, app_id=app_id, update_time=nowTime, expire_time=nowTime+3600)
|
|
|
+
|
|
|
redirect_uri = redirect_uri + '?code=' + code + '&state=' + state
|
|
|
+ if application[0].skip_auth:
|
|
|
+ return HttpResponseRedirect(redirect_uri)
|
|
|
return response.json(0, {'url': redirect_uri})
|
|
|
else:
|
|
|
return response.json(tko.code)
|
|
|
|
|
|
- # 增加对code和client_id的校验代码,返回access_token和refresh_token
|
|
|
+ # 增加对code和client_id,client_secret的校验代码,返回access_token和refresh_token
|
|
|
def do_token(self,request_dict, response, content_range):
|
|
|
code = request_dict.get("code", None)
|
|
|
print('code:')
|
|
@@ -124,7 +133,7 @@ class AuthView(View):
|
|
|
str_all = str.split(":", 1)
|
|
|
client_id = str_all[0]
|
|
|
client_secret = str_all[1]
|
|
|
- eq = ApplicationModel.objects.filter(client_secret=client_secret)
|
|
|
+ eq = ApplicationModel.objects.filter(client_secret=client_secret, client_id=client_id)
|
|
|
if eq.exists():
|
|
|
access_token = code
|
|
|
refresh_token = CommonService.encrypt_data(randomlength=32)
|
|
@@ -138,9 +147,9 @@ class AuthView(View):
|
|
|
print(res_json)
|
|
|
return JsonResponse(res_json)
|
|
|
else:
|
|
|
- return JsonResponse({'error': 'client_secret This value is misconfigured.'})
|
|
|
+ return response.json(10001)
|
|
|
else:
|
|
|
- return JsonResponse({'error': 'Check your configuration:no client_id,client_secret'})
|
|
|
+ return response.json(10002)
|
|
|
|
|
|
def do_user(self, request_dict, response,content_range):
|
|
|
str = content_range
|
|
@@ -158,14 +167,9 @@ class AuthView(View):
|
|
|
return JsonResponse(res_json)
|
|
|
else:
|
|
|
print('没有找到数据')
|
|
|
- return JsonResponse({'error': 'token inexistence'})
|
|
|
+ return response.json(10003)
|
|
|
else:
|
|
|
- return JsonResponse({'error': 'The request method is not correct. Please contact the administrator.'})
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+ return response.json(10004)
|
|
|
|
|
|
|
|
|
class ApplicationView(View):
|
|
@@ -202,7 +206,6 @@ class ApplicationView(View):
|
|
|
if own_perm is not True:
|
|
|
return response.json(404)
|
|
|
# http://192.168.136.39:8000/application/add?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySUQiOiIxMzgwMDEzODAwMSIsImxhbmciOiJjbiIsInVzZXIiOiIxMzgwMDEzODAwMSIsIm1fY29kZSI6IjEyMzQxMzI0MzIxNCIsImV4cCI6MTU4NzYxNjQ0NX0.BIwq - eWDcTnqLBTxqpi7BgJoU9TeIHC5Ibc2LUUJPls&name=pzb&client_id=pzb12345&client_secret=pzb12345678&client_type=confidential&grant_type=authorization_code&redirect_uri=https://www.zositech.cn&skip_auth=1
|
|
|
-
|
|
|
nowTime = int(time.time())
|
|
|
name = request_dict.get('name', None)
|
|
|
client_id = request_dict.get('client_id', None)
|
|
@@ -218,8 +221,6 @@ class ApplicationView(View):
|
|
|
except Exception:
|
|
|
return response.json(178)
|
|
|
|
|
|
-
|
|
|
-
|
|
|
def query(self, request_dict, userID, response):
|
|
|
own_perm = ModelService.check_perm(userID, 20)
|
|
|
if own_perm is True:
|
|
@@ -278,3 +279,63 @@ class ApplicationView(View):
|
|
|
return response.json(173)
|
|
|
|
|
|
|
|
|
+class GrantCodeView(View):
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ operation = kwargs.get('operation', None)
|
|
|
+ return self.validation(request.GET, operation)
|
|
|
+
|
|
|
+ def post(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ operation = kwargs.get('operation', None)
|
|
|
+ return self.validation(request.POST, operation)
|
|
|
+
|
|
|
+ def validation(self, request_dict, operation):
|
|
|
+ response = ResponseObject()
|
|
|
+ token = request_dict.get('token', None)
|
|
|
+ tko = TokenObject(token)
|
|
|
+ if tko.code == 0:
|
|
|
+ userID = tko.userID
|
|
|
+ if operation == 'query':
|
|
|
+ return self.query(request_dict, userID, response)
|
|
|
+ elif operation == 'delete':
|
|
|
+ return self.delete(request_dict, userID, response)
|
|
|
+ else:
|
|
|
+ return response.json(414)
|
|
|
+ else:
|
|
|
+ return response.json(tko.code)
|
|
|
+
|
|
|
+
|
|
|
+ def query(self, request_dict, userID, response):
|
|
|
+ own_perm = ModelService.check_perm(userID, 20)
|
|
|
+ if own_perm is True:
|
|
|
+ page = int(request_dict.get('page', 0))
|
|
|
+ line = int(request_dict.get('line', 0))
|
|
|
+ if page == 0:
|
|
|
+ page=1
|
|
|
+ if line == 0:
|
|
|
+ line=10
|
|
|
+ gc = GrantCodeModel.objects.filter()
|
|
|
+ if gc.exists():
|
|
|
+ count = gc.count()
|
|
|
+ res = gc[(page - 1) * line:page * line].values('id', 'userID', 'userID__username', 'userID__userEmail', 'userID__NickName', 'userID__language', 'userID__phone', 'code', 'app_id', 'application', 'add_time', 'update_time')
|
|
|
+ send_json = CommonService.qs_to_list(res)
|
|
|
+ return response.json(0, {'datas': send_json, 'count': count})
|
|
|
+ else:
|
|
|
+ return response.json(0, {'datas': [], 'count': 0})
|
|
|
+ else:
|
|
|
+ return response.json(404)
|
|
|
+
|
|
|
+
|
|
|
+ def delete(self, request_dict, userID, response):
|
|
|
+ own_perm = ModelService.check_perm(userID=userID, permID=10)
|
|
|
+ if own_perm is not True:
|
|
|
+ return response.json(404)
|
|
|
+ id = request_dict.get('id', None)
|
|
|
+ uid_set = GrantCodeModel.objects.filter(id=id)
|
|
|
+ if uid_set.exists():
|
|
|
+ uid_set.delete()
|
|
|
+ return response.json(0)
|
|
|
+ else:
|
|
|
+ return response.json(173)
|
|
|
+
|