Skip to content

Commit a6416a1

Browse files
authored
bkpaas-auth: bump to 3.1.3 & fix linter issues & upgrade to poetry v2 (#245)
1 parent 61625d6 commit a6416a1

26 files changed

Lines changed: 275 additions & 426 deletions

.github/workflows/bkpaas-auth.yml

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,25 @@ jobs:
2020
steps:
2121
- uses: actions/checkout@v2
2222
- name: Set up Python
23-
uses: actions/setup-python@v2
23+
uses: actions/setup-python@v5
2424
with:
2525
python-version: 3.11
26-
- name: Format with isort
27-
working-directory: sdks/bkpaas-auth
28-
run: |
29-
pip install isort==5.12.0
30-
isort . --settings-path=pyproject.toml
31-
- name: Format with black
32-
working-directory: sdks/bkpaas-auth
33-
run: |
34-
pip install black==23.7.0 click==8.1.6
35-
black . --config=pyproject.toml
36-
- name: Lint with flake8
37-
working-directory: sdks/bkpaas-auth
38-
run: |
39-
pip install flake8==4.0.1 pyproject-flake8==0.0.1a5
40-
pflake8 . --config=pyproject.toml
26+
- name: Setup uv
27+
uses: astral-sh/setup-uv@v5
28+
- name: Install Poetry system-wide
29+
# Use uv install to install poetry directly instead github action for simplicity
30+
run: uv pip install --system poetry==2.1.3
31+
- name: Ruff check
32+
run: poetry install && poetry run ruff check ./sdks/bkpaas-auth --config ./pyproject.toml
4133
- name: Lint with mypy
4234
working-directory: sdks/bkpaas-auth
43-
run: |
44-
pip install mypy==1.12.0 types-mock==4.0.15.2 types-requests==2.32.0.20241016 types-six==1.16.21.20241009
45-
mypy . --config-file=pyproject.toml
35+
run: poetry install && poetry run mypy . --config-file=./pyproject.toml
4636

4737
test:
4838
strategy:
4939
fail-fast: false
5040
matrix:
51-
python-version: ["3.8", "3.9", "3.10", "3.11"]
41+
python-version: ["3.9", "3.10", "3.11"]
5242
os: [ubuntu-latest, macos-latest]
5343
runs-on: ${{ matrix.os }}
5444
steps:
@@ -57,40 +47,36 @@ jobs:
5747
uses: actions/setup-python@v2
5848
with:
5949
python-version: ${{ matrix.python-version }}
60-
- name: Set up Poetry
61-
uses: abatilo/actions-poetry@v2.3.0
62-
with:
63-
poetry-version: 1.5.1
64-
- name: Install dependencies
50+
- name: Setup uv
51+
uses: astral-sh/setup-uv@v5
52+
- name: Install Nox
53+
run: uv pip install --system nox==2025.11.12
54+
- name: Show nox version
6555
working-directory: sdks/bkpaas-auth
66-
run: |
67-
poetry export --without-hashes --dev -o requirements-dev.txt
68-
python -m pip install --upgrade pip
69-
python -m pip install -r requirements-dev.txt
70-
python -m pip install tox-gh-actions==2.8.1
71-
- name: Run test with tox targets for ${{ matrix.python-version }}
56+
run: nox --version
57+
- name: Run tests on ${{ matrix.os }}
7258
working-directory: sdks/bkpaas-auth
73-
run: tox
59+
run: nox --non-interactive --error-on-missing-interpreters --session "tests(python='${{ matrix.python-version }}')" -- --full-trace
7460

7561
build:
7662
runs-on: macos-latest
7763
if: github.event.release && contains(github.event.release.tag_name, 'bkpaas-auth')
7864
steps:
7965
- uses: actions/checkout@v2
8066
- name: Set up Python
81-
uses: actions/setup-python@v2
67+
uses: actions/setup-python@v5
8268
with:
83-
python-version: 3.8
69+
python-version: 3.11
8470
- name: Set up Poetry
8571
uses: abatilo/actions-poetry@v2.3.0
8672
with:
87-
poetry-version: 1.5.1
73+
poetry-version: 2.1.1
8874
- name: Build bkpaas-auth
8975
run: |
9076
cd sdks/bkpaas-auth
9177
poetry install
9278
poetry build
93-
echo "${{ github.event.relesae.tag_name }} ${{ github.sha }}" > Release.txt
79+
echo "${{ github.event.release.tag_name }} ${{ github.sha }}" > Release.txt
9480
cat Release.txt
9581
- name: Release
9682
uses: softprops/action-gh-release@v1

.github/workflows/blue-krill.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ jobs:
5757
- name: Setup uv
5858
uses: astral-sh/setup-uv@v5
5959
- name: Install Nox
60-
run: uv pip install --system nox
60+
run: uv pip install --system nox==2025.11.12
6161
- name: Run tests on ${{ matrix.os }}
6262
working-directory: sdks/blue-krill
63-
run: nox --non-interactive --error-on-missing-interpreter --session "tests(python='${{ matrix.python-version }}')" -- --full-trace
63+
run: nox --non-interactive --error-on-missing-interpreters --session "tests(python='${{ matrix.python-version }}')" -- --full-trace
6464
build:
6565
runs-on: macos-latest
6666
if: github.event.release && contains(github.event.release.tag_name, 'blue-krill')

sdks/bkpaas-auth/.flake8

Lines changed: 0 additions & 8 deletions
This file was deleted.

sdks/bkpaas-auth/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# 版本历史
22

3+
## 3.1.3
4+
- feat: 用户模型增加时区支持,新增 time_zone 属性,由认证服务返回时区
5+
36
## 3.1.2
47
- fix: 修复通过 APIGatewayAuthBackend 认证无法获取 tenant_id 的问题
58

sdks/bkpaas-auth/README.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,10 @@
1111
### 发布包
1212

1313
-`bkpaas_auth/__init__.py` 文件中更新 `__version__`
14-
-`setup.py` 文件中更新 `version`
1514
-`pyproject.toml` 中更新 `version`
1615
-`CHANGES.md` 中添加对应的版本日志
1716
- 执行 `poetry build` 命令在 dist 目录下生成当前版本的包。然后执行 `twine upload dist/* --repository-url {pypi_address} --username {your_name} --password {your_token}` 将其上传到 pypi 服务器上。
1817

19-
### 关于 setup.py
20-
21-
虽然在 [PEP 517](https://python-poetry.org/docs/pyproject/#poetry-and-pep-517) 规范里,Python 包不再需要 `setup.py` 文件。但真正少了 `setup.py` 文件后,会发现有些功能就没法正常使用,比如 pip 的可编辑安装模式、tox 等([相关文档](https://github.com/python-poetry/poetry/issues/761))。所以我们仍然需要它。
22-
23-
为了避免维护重复的 `pyproject.toml``setup.py` 文件,我们使用了 [dephell](https://github.com/dephell/dephell) 工具来自动生成 `setup.py` 文件。
24-
25-
- 安装 dephell
26-
- 在根目录执行 `dephell deps convert --from pyproject.toml --to setup.py`
27-
2818
## 使用指南
2919
1. 更新 settings:
3020
```python

sdks/bkpaas-auth/bkpaas_auth/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
__version__ = "3.1.2"
2+
__version__ = "3.1.3"
33

44

55
def get_user_by_user_id(user_id: str, username_only: bool = True):

sdks/bkpaas-auth/bkpaas_auth/backends.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ class APIGatewayAuthBackend:
210210
_TOKEN_EXPIRE_TIME = 86400 # 24 hours in seconds
211211

212212
def _create_authenticated_user(
213-
self, username: str, provider_type: ProviderType, tenant_id: str | None = None
213+
self, username: str, provider_type: ProviderType, tenant_id: Optional[str] = None
214214
) -> User:
215215
"""Create a user object for authenticated requests."""
216216
return User(
@@ -221,7 +221,7 @@ def _create_authenticated_user(
221221
)
222222

223223
def _authenticate_common(
224-
self, verified: bool, username: str | None = None, tenant_id: str | None = None
224+
self, verified: bool, username: Optional[str] = None, tenant_id: Optional[str] = None
225225
) -> Union[User, AnonymousUser]:
226226
"""Common authentication logic for all versions."""
227227
if not verified or not username:
@@ -236,7 +236,7 @@ def authenticate_with_signature_v3(
236236
request: HttpRequest,
237237
gateway_name: str,
238238
bk_username: str,
239-
tenant_id: str | None = None,
239+
tenant_id: Optional[str] = None,
240240
verified: bool = False,
241241
**credentials: Dict,
242242
) -> Union[User, AnonymousUser]:

sdks/bkpaas-auth/bkpaas_auth/core/plugins.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,18 @@ def validate_login_token(self, login_token):
2020

2121
if login_token.expired():
2222
return False
23-
24-
if (now() - login_token.issued_at).total_seconds() > bkauth_settings.SESSION_TIMEOUT:
25-
return False
26-
27-
return True
23+
return (now() - login_token.issued_at).total_seconds() <= bkauth_settings.SESSION_TIMEOUT
2824

2925

3026
class BkTicketPlugin(BasePlugin):
3127
"""Auth backend for bk_ticket"""
3228

3329
def get_credentials(self, request):
34-
bk_ticket = request.COOKIES.get('bk_ticket')
30+
bk_ticket = request.COOKIES.get("bk_ticket")
3531

3632
if bk_ticket:
3733
return {
38-
'bk_ticket': bk_ticket,
34+
"bk_ticket": bk_ticket,
3935
}
4036

4137
return None
@@ -45,9 +41,9 @@ class BkTokenPlugin(BasePlugin):
4541
"""Auth backend for bk_token"""
4642

4743
def get_credentials(self, request):
48-
bk_token = request.COOKIES.get('bk_token')
44+
bk_token = request.COOKIES.get("bk_token")
4945

5046
if bk_token:
51-
return {'bk_token': bk_token}
47+
return {"bk_token": bk_token}
5248

5349
return None

sdks/bkpaas-auth/bkpaas_auth/core/services.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
def get_app_credentials() -> Dict[str, str]:
1919
"""Get app credentials to verify app, which is required for requesting user info API"""
2020
if conf.TOKEN_APP_CODE and conf.TOKEN_SECRET_KEY:
21-
return {'bk_app_code': conf.TOKEN_APP_CODE, 'bk_app_secret': conf.TOKEN_SECRET_KEY}
21+
return {"bk_app_code": conf.TOKEN_APP_CODE, "bk_app_secret": conf.TOKEN_SECRET_KEY}
2222
raise ImproperlyConfigured("BKAUTH_TOKEN_APP_CODE and BKAUTH_TOKEN_SECRET_KEY not set")
2323

2424

@@ -48,19 +48,19 @@ def _get_and_cache_user_info(cache_key, user_params, response_ok_checker):
4848
params=user_params,
4949
)
5050
except HttpRequestError:
51-
raise ServiceError('Unable to get user info')
51+
raise ServiceError("Unable to get user info")
5252

5353
result = resp_to_json(resp)
5454

5555
if not isinstance(result, dict):
56-
raise ValueError(f'response type expect dict, got: {result}')
56+
raise ValueError(f"response type expect dict, got: {result}") # noqa: TRY004
5757

5858
if not response_ok_checker(result):
5959
logger.error(
60-
f'Get user info fail, url: {conf.TOKEN_USER_INFO_ENDPOINT}, params: {scrub_data(user_params)}'
61-
f', response: {result}',
60+
f"Get user info fail, url: {conf.TOKEN_USER_INFO_ENDPOINT}, params: {scrub_data(user_params)}"
61+
f", response: {result}",
6262
)
63-
return
63+
return None
6464

6565
# 获取用户信息成后才缓存数据
6666
cache.set(cache_key, result, timeout=86400)
@@ -76,13 +76,13 @@ def get_rtx_user_info(username):
7676
"""
7777

7878
def response_ok_checker(result):
79-
return result['result']
79+
return result["result"]
8080

81-
cache_key = f'bkauth::rests::get_rtx_user_info::{username}'
82-
result = _get_and_cache_user_info(cache_key, {'login_name': username}, response_ok_checker)
81+
cache_key = f"bkauth::rests::get_rtx_user_info::{username}"
82+
result = _get_and_cache_user_info(cache_key, {"login_name": username}, response_ok_checker)
8383

8484
if result and response_ok_checker(result):
85-
return RtxUserInfo(**result['data'])
85+
return RtxUserInfo(**result["data"])
8686

8787
return None
8888

@@ -95,12 +95,12 @@ def get_bk_user_info(username):
9595
"""
9696

9797
def response_ok_checker(result):
98-
return result['code'] == 0
98+
return result["code"] == 0
9999

100-
cache_key = f'bkauth::rests::get_bk_user_info::{username}'
101-
result = _get_and_cache_user_info(cache_key, {'bk_username': username}, response_ok_checker)
100+
cache_key = f"bkauth::rests::get_bk_user_info::{username}"
101+
result = _get_and_cache_user_info(cache_key, {"bk_username": username}, response_ok_checker)
102102

103103
if result and response_ok_checker(result):
104-
return BkUserInfo(**result['data'])
104+
return BkUserInfo(**result["data"])
105105

106106
return None

0 commit comments

Comments
 (0)