-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix: anonymous authentication #3216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,21 +14,19 @@ | |
|
|
||
|
|
||
| class ChatAuthentication: | ||
| def __init__(self, auth_type: str | None, is_auth: bool, auth_passed: bool): | ||
| self.is_auth = is_auth | ||
| self.auth_passed = auth_passed | ||
| def __init__(self, auth_type: str | None): | ||
| self.auth_type = auth_type | ||
|
|
||
| def to_dict(self): | ||
| return {'is_auth': self.is_auth, 'auth_passed': self.auth_passed, 'auth_type': self.auth_type} | ||
| return {'auth_type': self.auth_type} | ||
|
|
||
| def to_string(self): | ||
| return encrypt(json.dumps(self.to_dict())) | ||
|
|
||
| @staticmethod | ||
| def new_instance(authentication: str): | ||
| auth = json.loads(decrypt(authentication)) | ||
| return ChatAuthentication(auth.get('auth_type'), auth.get('is_auth'), auth.get('auth_passed')) | ||
| return ChatAuthentication(auth.get('auth_type')) | ||
|
|
||
|
|
||
| class ChatUserToken: | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are several areas where the code can be improved:
Here’s a proposed improvement on how the class could look like after addressing these points. Note that actual encryption/decryption logic is omitted as it depends on external libraries such as AES-CBC, but this should give an idea of restructuring. @@ -14,7 +14,8 @@
class ChatAuthentication:
AUTH_TYPE_NOT_SET = "Not Set"
NOT_AUTHENTICATED = False
AUTHENTICATION_SUCCESSFUL = True
def __init__(self, auth_type: str):
"""Initialize the authentication type."""
self.auth_type = auth_type or self.AUTH_TYPE_NOT_SET
def to_dict(self):
# This method doesn't need to include all fields anymore due to simplification
return {'auth_type': self.auth_type}
def to_string(self):
encrypted_data = encrypt(json.dumps(self.to_dict()))
# Encrypted result will be stringified before returning
return encrypted_result
@staticmethod
def new_instance(authentication: str):
decrypted_data = decrypt(authentication)
auth_obj = {k.lower(): v for k,v in json.loads(decrypted_data).items()}
return ChatAuthentication(
auth_type=auth_obj.get('auth_type', ChatAuthentication.AUTH_TYPE_NOT_SET),
authorization_status=auth_obj.get('authorization_status', ChatAuthentication.NOT_AUTHENTICATED) # Assuming 'authorization_status' replaces 'is_auth'
)Note: Replace |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,7 +45,8 @@ def handle(self, request, token: str, get_token_details): | |
| if application_setting_model is not None: | ||
| application_setting = QuerySet(application_setting_model).filter(application_id=application_id).first() | ||
| if application_setting.authentication: | ||
| raise AppAuthenticationFailed(1002, _('Authentication information is incorrect')) | ||
| if 'password' != chat_user_token.authentication.auth_type: | ||
| raise AppAuthenticationFailed(1002, _('Authentication information is incorrect')) | ||
| return None, ChatAuth( | ||
| current_role_list=[RoleConstants.CHAT_ANONYMOUS_USER], | ||
| permission_list=[ | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's an inconsistency in checking the @@ -45,7 +45,8 @@ def handle(self, request, token: str, get_token_details):
if application_setting_model is not None:
application_setting = QuerySet(application_setting_model).filter(application_id=application_id).first()
if application_setting.authentication:
- raise AppAuthenticationFailed(1002, _('Authentication information is incorrect'))
+ auth_types_allowed = ['custom', 'oauth'] # Add the allowed types here
+ if chat_user_token.authentication.auth_type not in auth_types_allowed:
+ raise AppAuthenticationFailed(1002,
+ _("Unknown or unsupported authentication method"))This ensures that only known and supported authentication methods trigger the exception, instead of being overly generic (e.g., "Authentication information is incorrect"). |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There seem to be two minor issues in the provided code:
The use of
ChatAuthentication(None, False, False)creates an instance ofChatAuthenticationwithout setting the required attributes. This might cause unexpected behavior when converting the token.The
_typevariable is set toAuthenticationType.CHAT_ANONYMOUS_USER, but ifwith_valid=False, aNotFound404exception will be raised instead of returning a token immediately. It would be better to handle this case more gracefully by either ensuring that the condition is always met or modifying how exceptions are handled after authentication checks.