Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions apps/chat/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

from django.core import signing
from django.http import HttpResponse
from django.test import RequestFactory, SimpleTestCase
from django.test import RequestFactory, SimpleTestCase, TestCase
from rest_framework.test import APIRequestFactory, force_authenticate

from application.models import Application, ApplicationAccessToken, ApplicationFolder
from chat.views.v3.chat import BaseAuthView, FILE_AUTH_COOKIE, Logout
from common.auth.constants.operate_constants import Operate
from common.exception.app_exception import AppApiException
from system_manage.models import ChatUser, ResourceChatUserAuthorize, ResourceType, UserGroup


class BaseAuthViewTest(SimpleTestCase):
Expand Down Expand Up @@ -47,13 +49,14 @@ def test_access_token_login_validates_authorization_and_keeps_operate_token(self

token = self.view.create_token_and_cache(self.user, access_token, Operate.OIDC)

validate_authorization.assert_called_once_with(access_token, self.user)
validate_authorization.assert_called_once_with(access_token, self.user, Operate.OIDC)
self.assertEqual(
signing.loads(token, key="test-signing-key"),
{
"id": self.user.id,
"type": "CHAT_USER",
"login_type": "OIDC",
"source": "OIDC",
"kwargs": {"application_id": "application-id"},
},
)
Expand Down Expand Up @@ -114,6 +117,47 @@ def test_failed_authorization_records_failed_login_log(self):
)


class ChatUserAccessTokenSourceTest(TestCase):
def setUp(self):
self.folder = ApplicationFolder.objects.create(id="default", name="Default")
self.application = Application.objects.create(name="OIDC application", folder=self.folder)
ApplicationAccessToken.objects.create(
application=self.application,
access_token="application-access-token",
authentication=True,
authentication_value={"type": "login", "login_value": ["OIDC"]},
)
self.user = ChatUser.objects.create(
username="existing-oidc-user",
nick_name="Existing OIDC User",
password="encrypted-password",
source="LOCAL",
)
group = UserGroup.objects.create(name="OIDC users")
ResourceChatUserAuthorize.objects.create(
user_group=group,
user=self.user,
resource_id=self.application.id,
resource_type=ResourceType.APPLICATION.value,
is_auth=True,
)
self.view = BaseAuthView()
self.view.request = Mock()

def test_login_uses_current_operate_when_chat_user_source_is_stale(self):
with (
patch("chat.views.v3.chat.cache.set"),
patch("chat.views.v3.chat.record_log"),
patch("common.auth.common.get_key", return_value="test-signing-key"),
):
token = self.view.create_token_and_cache(self.user, "application-access-token", Operate.OIDC)

self.assertEqual(
signing.loads(token, key="test-signing-key")["login_type"],
Operate.OIDC.value,
)


class ChatLogoutTest(SimpleTestCase):
def test_https_logout_clears_cross_site_file_auth_cookie(self):
request = APIRequestFactory().post(
Expand Down
2 changes: 1 addition & 1 deletion apps/chat/views/v3/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def create_token_and_cache(self, user, access_token, operate):
status = 500
try:
if access_token:
ChatUserAccessTokenSerializer.create_token_and_cache(access_token, user)
ChatUserAccessTokenSerializer.create_token_and_cache(access_token, user, operate)
application_access_token = (
ApplicationAccessToken.objects.filter(access_token=access_token, is_active=True)
.select_related("application")
Expand Down
4 changes: 2 additions & 2 deletions apps/system_manage/serializers/chat_user_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

class ChatUserAccessTokenSerializer(serializers.Serializer):
@staticmethod
def create_token_and_cache(access_token, user):
def create_token_and_cache(access_token, user, login_type=None):
application_access_token = ApplicationAccessToken.objects.filter(access_token=access_token).first()

if not application_access_token:
Expand All @@ -31,7 +31,7 @@ def create_token_and_cache(access_token, user):
# 如果不需要认证就直接
if not application_access_token.authentication:
return
ensure_application_chat_user_authorized(application_id, user.id)
ensure_application_chat_user_authorized(application_id, user.id, login_type=login_type)

@staticmethod
def get_auth_setting(access_token):
Expand Down
11 changes: 7 additions & 4 deletions apps/system_manage/services/chat_user_authorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def ensure_application_chat_user_authorized(
application_id,
chat_user_id,
*,
login_type=None,
chat_user_type=ChatUserType.CHAT_USER.value,
debug=False,
):
Expand All @@ -21,11 +22,13 @@ def ensure_application_chat_user_authorized(
if not ApplicationAccessToken.objects.filter(application_id=application_id, authentication=True).exists():
return

chat_user = ChatUser.objects.filter(id=chat_user_id).only("source").first()
if chat_user is None:
raise AppApiException(1005, _("The user does not have permission to access the application"))
if login_type is None:
chat_user = ChatUser.objects.filter(id=chat_user_id).only("source").first()
if chat_user is None:
raise AppApiException(1005, _("The user does not have permission to access the application"))
login_type = chat_user.source

login_type = str(chat_user.source or "").upper()
login_type = str(login_type or "").upper()
auth = get_auth(login_type, chat_user_id, application_id)
if not ChatPermissionConstants.get_aggregate_permissions().hasPermission(
SimpleNamespace(auth=auth),
Expand Down
Loading