forked from TencentBlueKing/bk-user
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bk-login): support compatibility api and URL use
login
prefix
- Loading branch information
Showing
19 changed files
with
261 additions
and
83 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available. | ||
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at http://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available. | ||
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at http://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available. | ||
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at http://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
""" | ||
from blue_krill.data_types.enum import EnumField, StructuredEnum | ||
|
||
|
||
class CompatibilityApiErrorCodeEnum(str, StructuredEnum): | ||
"""兼容 API 错误码""" | ||
|
||
SUCCESS = EnumField("SUCCESS") | ||
PARAM_NOT_VALID = EnumField("PARAM_NOT_VALID") | ||
USER_NOT_EXISTS = EnumField("USER_NOT_EXISTS") | ||
USER_NOT_EXISTS2 = EnumField("USER_NOT_EXISTS2") | ||
USER_INFO_UPDATE_FAIL = EnumField("USER_INFO_UPDATE_FAIL") | ||
ACCESS_PERMISSION_DENIED = EnumField("ACCESS_PERMISSION_DENIED") | ||
|
||
|
||
CompatibilityApiErrorCodeMap = { | ||
"v1": { | ||
CompatibilityApiErrorCodeEnum.SUCCESS: "00", | ||
CompatibilityApiErrorCodeEnum.PARAM_NOT_VALID: "1200", | ||
CompatibilityApiErrorCodeEnum.USER_NOT_EXISTS: "1201", | ||
CompatibilityApiErrorCodeEnum.USER_NOT_EXISTS2: "1300", | ||
CompatibilityApiErrorCodeEnum.USER_INFO_UPDATE_FAIL: "1202", | ||
CompatibilityApiErrorCodeEnum.ACCESS_PERMISSION_DENIED: "1203", | ||
}, | ||
"v2": { | ||
CompatibilityApiErrorCodeEnum.SUCCESS: 0, | ||
CompatibilityApiErrorCodeEnum.PARAM_NOT_VALID: 1302100, | ||
CompatibilityApiErrorCodeEnum.USER_NOT_EXISTS: 1302101, | ||
CompatibilityApiErrorCodeEnum.USER_NOT_EXISTS2: 1302103, | ||
CompatibilityApiErrorCodeEnum.USER_INFO_UPDATE_FAIL: 1302102, | ||
CompatibilityApiErrorCodeEnum.ACCESS_PERMISSION_DENIED: 1302403, | ||
}, | ||
"v3": { | ||
CompatibilityApiErrorCodeEnum.SUCCESS: 0, | ||
CompatibilityApiErrorCodeEnum.PARAM_NOT_VALID: 1302100, | ||
CompatibilityApiErrorCodeEnum.USER_NOT_EXISTS: 1302101, | ||
CompatibilityApiErrorCodeEnum.USER_NOT_EXISTS2: 1302103, | ||
CompatibilityApiErrorCodeEnum.USER_INFO_UPDATE_FAIL: 1302102, | ||
CompatibilityApiErrorCodeEnum.ACCESS_PERMISSION_DENIED: 1302403, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available. | ||
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at http://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
""" | ||
from typing import Dict | ||
|
||
from django.conf import settings | ||
from django.http import JsonResponse | ||
|
||
from .constants import CompatibilityApiErrorCodeEnum, CompatibilityApiErrorCodeMap | ||
|
||
|
||
class CompatibilityApiMixin: | ||
"""兼容API Mixin""" | ||
|
||
api_version = "v1" | ||
|
||
@staticmethod | ||
def is_request_from_esb(request): | ||
""" | ||
请求是否来自ESB | ||
""" | ||
x_app_token = request.META.get("HTTP_X_APP_TOKEN") | ||
x_app_code = request.META.get("HTTP_X_APP_CODE") | ||
if x_app_code == "esb" and x_app_token == settings.BK_PAAS_APP_SECRET: | ||
return True | ||
|
||
return False | ||
|
||
def fail_response(self, error_code: CompatibilityApiErrorCodeEnum, message: str) -> JsonResponse: | ||
code = CompatibilityApiErrorCodeMap[self.api_version][error_code] # type: ignore | ||
if self.api_version == "v2": | ||
return JsonResponse({"result": False, "bk_error_code": code, "bk_error_msg": message, "data": {}}) | ||
|
||
return JsonResponse({"result": False, "code": code, "message": message, "data": {}}) | ||
|
||
def ok_response(self, data: Dict) -> JsonResponse: | ||
code = CompatibilityApiErrorCodeMap[self.api_version][CompatibilityApiErrorCodeEnum.SUCCESS] # type: ignore | ||
if self.api_version == "v2": | ||
return JsonResponse({"result": True, "bk_error_code": code, "bk_error_msg": "", "data": data}) | ||
|
||
return JsonResponse({"result": True, "code": code, "message": "", "data": data}) | ||
|
||
@property | ||
def username_key(self) -> str: | ||
return "bk_username" if self.api_version in ["v2", "v3"] else "username" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available. | ||
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at http://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
""" | ||
from typing import Dict | ||
|
||
from django.conf import settings | ||
from django.views.generic import View | ||
|
||
from bklogin.authentication.manager import BkTokenManager | ||
from bklogin.component.bk_user import api as bk_user_api | ||
|
||
from .constants import CompatibilityApiErrorCodeEnum | ||
from .mixins import CompatibilityApiMixin | ||
|
||
|
||
class TokenIntrospectCompatibilityApi(View, CompatibilityApiMixin): | ||
"""Token 解析""" | ||
|
||
def get(self, request, *args, **kwargs): | ||
bk_token = request.GET.get(settings.BK_TOKEN_COOKIE_NAME) | ||
|
||
ok, username, msg = BkTokenManager().is_bk_token_valid(bk_token) | ||
if not ok: | ||
return self.fail_response(error_code=CompatibilityApiErrorCodeEnum.PARAM_NOT_VALID, message=msg) | ||
|
||
return self.ok_response(data={self.username_key: username}) | ||
|
||
|
||
class UserRetrieveCompatibilityApi(View, CompatibilityApiMixin): | ||
"""通过 Token 获取用户""" | ||
|
||
def get(self, request, *args, **kwargs): | ||
bk_token = request.GET.get(settings.BK_TOKEN_COOKIE_NAME) | ||
|
||
ok, username, msg = BkTokenManager().is_bk_token_valid(bk_token) | ||
if not ok: | ||
# 对于来着 ESB 请求,如果 bk_token 验证不通过,还可以通过 username 参数指定查询的用户 | ||
username = request.GET.get(self.username_key) | ||
if not (self.is_request_from_esb(request) and username): | ||
return self.fail_response(error_code=CompatibilityApiErrorCodeEnum.PARAM_NOT_VALID, message=msg) | ||
|
||
# 通过用户管理查询用户信息 | ||
user = bk_user_api.get_tenant_user(username) | ||
|
||
# Note: 与 self.username_key 不一样,区别在于 v3 API, 其 is_login 放回 bk_username, get_user 返回 username | ||
username_key = "bk_username" if self.api_version == "v2" else "username" | ||
user_info: Dict[str, int | str] = { | ||
# bk_username / username | ||
username_key: user.id, | ||
# 基本信息 | ||
"language": user.language, | ||
"time_zone": user.time_zone, | ||
# 多租户版本新增 | ||
"tenant_id": user.tenant_id, | ||
"full_name": user.full_name, # 姓名 | ||
"display_name": user.display_name, # 统一展示名 | ||
# ----- 兼容 ------ | ||
# 兼容数据 | ||
"chname": user.full_name, | ||
# 【兼容】固定或空值返回 | ||
"qq": "", | ||
"phone": "", | ||
"email": "", | ||
"wx_userid": "", | ||
} | ||
# 角色已废弃,这里只是兼容处理 | ||
role_key = "bk_role" if self.api_version == "v2" else "role" | ||
user_info[role_key] = "0" if self.api_version == "v1" else 0 | ||
|
||
return self.ok_response(data=user_info) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available. | ||
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at http://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
""" | ||
from django.urls import path | ||
|
||
from .compatibility import views as compatibility_views | ||
|
||
urlpatterns = [ | ||
# 兼容API, 兼容原有通过 ESB 和直接调用的两种方式 | ||
path("accounts/is_login/", compatibility_views.TokenIntrospectCompatibilityApi.as_view(api_version="v1")), | ||
path("accounts/get_user/", compatibility_views.UserRetrieveCompatibilityApi.as_view(api_version="v1")), | ||
path("api/v2/is_login/", compatibility_views.TokenIntrospectCompatibilityApi.as_view(api_version="v2")), | ||
path("api/v2/get_user/", compatibility_views.UserRetrieveCompatibilityApi.as_view(api_version="v2")), | ||
path("api/v3/is_login/", compatibility_views.TokenIntrospectCompatibilityApi.as_view(api_version="v3")), | ||
path("api/v3/get_user/", compatibility_views.UserRetrieveCompatibilityApi.as_view(api_version="v3")), | ||
# TODO: 新的 OpenAPI 后面统一接入 APIGateway,不支持直接调用, | ||
# 同时只提供给 APIGateway 做用户认证的接口与通用 OpenAPI 区分开 | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.