Skip to content

Commit

Permalink
refactor: move authentication data to header X-Bkapi-Authorization (#141
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jamesgetx authored Dec 24, 2023
1 parent c5355c5 commit c131448
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
3 changes: 3 additions & 0 deletions sdks/bkpaas-auth/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# 版本历史

## 2.0.7
- 将认证信息标准化到请求头 X-Bkapi-Authorization 中

## 2.0.6
- TokenRequestBackend.request_username 支持国际化

Expand Down
12 changes: 9 additions & 3 deletions sdks/bkpaas-auth/bkpaas_auth/core/services.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import json
import logging
from typing import Dict

Expand All @@ -11,6 +12,7 @@
from bkpaas_auth.core.user_info import BkUserInfo, RtxUserInfo
from bkpaas_auth.utils import scrub_data


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -38,14 +40,18 @@ def _get_and_cache_user_info(cache_key, user_params, response_ok_checker):
if cached_result:
return cached_result

params = dict(user_params, **get_app_credentials())
is_success, result = http_get(conf.TOKEN_USER_INFO_ENDPOINT, params=params)
is_success, result = http_get(
conf.TOKEN_USER_INFO_ENDPOINT,
headers={
"X-Bkapi-Authorization": json.dumps(dict(user_params, **get_app_credentials())),
},
)
if not is_success:
raise ServiceError('Unable to get user info')

if not response_ok_checker(result):
logger.error(
f'Get user info fail, url: {conf.TOKEN_USER_INFO_ENDPOINT}, params: {scrub_data(params)}'
f'Get user info fail, url: {conf.TOKEN_USER_INFO_ENDPOINT}, params: {scrub_data(user_params)}'
f', response: {result}',
)
return
Expand Down
7 changes: 5 additions & 2 deletions sdks/bkpaas-auth/bkpaas_auth/core/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""Access token for blueking
"""
import datetime
import json
import logging

from django.utils.timezone import now
Expand Down Expand Up @@ -32,9 +33,11 @@ def request_username(self, **credentials):
"""Get username through credentials"""
is_success, resp = http_get(
bkauth_settings.USER_COOKIE_VERIFY_URL,
params=dict(credentials, **get_app_credentials()),
timeout=10,
headers={'blueking-language': get_language()},
headers={
'blueking-language': get_language(),
"X-Bkapi-Authorization": json.dumps(dict(credentials, **get_app_credentials())),
},
)
if not is_success:
raise ServiceError('unable to fetch token services')
Expand Down

0 comments on commit c131448

Please sign in to comment.