From 00256f86df2b6542dd7f5345589a58374bfa5833 Mon Sep 17 00:00:00 2001 From: fit2bot <68588906+fit2bot@users.noreply.github.com> Date: Thu, 25 Jan 2024 14:00:13 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20OAuth2=E5=8D=8F=E8=AE=AE=E8=8E=B7?= =?UTF-8?q?=E5=8F=96token=E6=94=AF=E6=8C=81=E9=85=8D=E7=BD=AEjson=E6=88=96?= =?UTF-8?q?=E8=80=85data=20(#12602)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * perf: OAuth2协议获取token支持配置json或者data * perf: 优化注释 --------- Co-authored-by: jiangweidong --- apps/authentication/backends/oauth2/backends.py | 11 +++++++---- apps/settings/serializers/auth/oauth2.py | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/apps/authentication/backends/oauth2/backends.py b/apps/authentication/backends/oauth2/backends.py index 0c40d09bd..16a617b16 100644 --- a/apps/authentication/backends/oauth2/backends.py +++ b/apps/authentication/backends/oauth2/backends.py @@ -98,16 +98,19 @@ class OAuth2Backend(JMSModelBackend): access_token_url = '{url}{separator}{query}'.format( url=settings.AUTH_OAUTH2_ACCESS_TOKEN_ENDPOINT, separator=separator, query=urlencode(query_dict) ) + # token_method -> get, post(post_data), post_json token_method = settings.AUTH_OAUTH2_ACCESS_TOKEN_METHOD.lower() - requests_func = getattr(requests, token_method, requests.get) logger.debug(log_prompt.format('Call the access token endpoint[method: %s]' % token_method)) headers = { 'Accept': 'application/json' } - if token_method == 'post': - access_token_response = requests_func(access_token_url, headers=headers, data=query_dict) + if token_method.startswith('post'): + body_key = 'json' if token_method.endswith('json') else 'data' + access_token_response = requests.post( + access_token_url, headers=headers, **{body_key: query_dict} + ) else: - access_token_response = requests_func(access_token_url, headers=headers) + access_token_response = requests.get(access_token_url, headers=headers) try: access_token_response.raise_for_status() access_token_response_data = access_token_response.json() diff --git a/apps/settings/serializers/auth/oauth2.py b/apps/settings/serializers/auth/oauth2.py index 56ddd6a66..b5a0dbb62 100644 --- a/apps/settings/serializers/auth/oauth2.py +++ b/apps/settings/serializers/auth/oauth2.py @@ -43,7 +43,7 @@ class OAuth2SettingSerializer(serializers.Serializer): ) AUTH_OAUTH2_ACCESS_TOKEN_METHOD = serializers.ChoiceField( default='GET', label=_('Client authentication method'), - choices=(('GET', 'GET'), ('POST', 'POST')) + choices=(('GET', 'GET'), ('POST', 'POST-DATA'), ('POST_JSON', 'POST-JSON')) ) AUTH_OAUTH2_PROVIDER_USERINFO_ENDPOINT = serializers.CharField( required=True, max_length=1024, label=_('Provider userinfo endpoint')