Skip to content

Commit d666ba7

Browse files
committed
refactor: simplify Plex account linking/unlinking by removing redundant username validation
1 parent b28ab0a commit d666ba7

File tree

3 files changed

+20
-28
lines changed

3 files changed

+20
-28
lines changed

src/primary/auth.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -858,12 +858,12 @@ def verify_plex_user(plex_token: str) -> Tuple[bool, Optional[Dict[str, Any]]]:
858858
else:
859859
return False, None
860860

861-
def unlink_plex_from_user(username: str) -> bool:
861+
def unlink_plex_from_user(username: str = None) -> bool:
862862
"""
863-
Unlink Plex account from a user by removing Plex-related data
863+
Unlink Plex account from the current authenticated user by removing Plex-related data
864864
865865
Args:
866-
username: The username to unlink Plex from
866+
username: Not used anymore - kept for compatibility
867867
868868
Returns:
869869
bool: True if successful, False otherwise
@@ -876,20 +876,20 @@ def unlink_plex_from_user(username: str) -> bool:
876876
with open(USER_FILE, 'r') as f:
877877
user_data = json.load(f)
878878

879-
username_hash = hash_username(username)
880-
881-
# Check if this user exists and has Plex data
882-
if user_data.get("username") != username_hash:
883-
logger.error("User not found")
884-
return False
879+
# No need to validate username since user is already authenticated via session
880+
# Just check if Plex data exists to unlink
881+
if not user_data.get('plex_linked', False):
882+
logger.warning("No Plex account linked to unlink")
883+
return True # Not an error, just nothing to do
885884

886885
# Remove all Plex-related fields
887886
plex_fields_to_remove = [
888887
'plex_token',
889888
'plex_username',
890889
'plex_email',
891890
'plex_linked_at',
892-
'plex_linked'
891+
'plex_linked',
892+
'plex_user_id'
893893
]
894894

895895
removed_any = False
@@ -919,19 +919,19 @@ def unlink_plex_from_user(username: str) -> bool:
919919
# Set appropriate file permissions
920920
os.chmod(USER_FILE, 0o600)
921921

922-
logger.info(f"Successfully unlinked Plex account for user {username}")
922+
logger.info(f"Successfully unlinked Plex account from authenticated user")
923923
return True
924924

925925
except Exception as e:
926-
logger.error(f"Error unlinking Plex account for user {username}: {str(e)}")
927-
return False
926+
logger.error(f"Error unlinking Plex account: {str(e)}")
927+
raise # Re-raise the exception so the route handler can catch it
928928

929929
def link_plex_account_session_auth(username: str, plex_token: str, plex_user_data: Dict[str, Any]) -> bool:
930930
"""
931931
Link a Plex account to an existing local user using session authentication
932932
933933
Args:
934-
username: Local username (already verified via session)
934+
username: Not used for validation - kept for compatibility
935935
plex_token: Plex access token
936936
plex_user_data: User data from Plex API
937937
@@ -941,11 +941,8 @@ def link_plex_account_session_auth(username: str, plex_token: str, plex_user_dat
941941
try:
942942
user_data = get_user_data()
943943

944-
# Verify this is the correct user by checking username hash
945-
username_hash = hash_username(username)
946-
if user_data.get("username") != username_hash:
947-
logger.warning(f"Failed to link Plex account: Username mismatch for {username}")
948-
return False
944+
# No need to validate username since user is already authenticated via session
945+
# Just proceed to add Plex information
949946

950947
# Add Plex information to existing user
951948
user_data["plex_linked"] = True
@@ -956,7 +953,7 @@ def link_plex_account_session_auth(username: str, plex_token: str, plex_user_dat
956953
user_data["plex_linked_at"] = time.time()
957954

958955
if save_user_data(user_data):
959-
logger.info(f"Plex account linked to local user: {username}")
956+
logger.info(f"Plex account linked to authenticated user - Plex username: {plex_user_data.get('username')}")
960957
return True
961958
else:
962959
logger.error("Failed to save linked Plex data")

src/primary/routes/plex_auth_routes.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -421,13 +421,8 @@ def unlink_plex_account():
421421
if not session_id or not verify_session(session_id):
422422
return jsonify({'success': False, 'error': 'User not authenticated'}), 401
423423

424-
# Get username from session
425-
username = get_username_from_session(session_id)
426-
if not username:
427-
return jsonify({'success': False, 'error': 'Unable to determine username from session'}), 400
428-
429-
# Remove Plex data from user credentials
430-
if unlink_plex_from_user(username):
424+
# Since user is authenticated, we can directly unlink without username validation
425+
if unlink_plex_from_user():
431426
return jsonify({'success': True, 'message': 'Plex account unlinked successfully'})
432427
else:
433428
return jsonify({'success': False, 'error': 'Failed to unlink Plex account'}), 500

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7.6.3
1+
7.6.4

0 commit comments

Comments
 (0)