1414class AuthService :
1515 """Handle service request for epic.authorize with integrated cache management."""
1616
17+ AUTH_CACHE_VERSION_KEY = "auth_cache_version"
18+
19+ @classmethod
20+ def _get_auth_cache_version (cls ):
21+ """
22+ Get current auth cache version from shared cache.
23+
24+ This ensures all pods see the same version number.
25+ """
26+ version = cache .get (cls .AUTH_CACHE_VERSION_KEY )
27+ if version is None :
28+ version = 0
29+ cache .set (cls .AUTH_CACHE_VERSION_KEY , version )
30+ return version
31+
1732 @staticmethod
1833 def get_epic_user_by_guid (auth_user_guid : str ):
1934 """
@@ -24,9 +39,10 @@ def get_epic_user_by_guid(auth_user_guid: str):
2439 """
2540 from compliance_api .services .cached_staff_user import CachedStaffUserService
2641
27- # Include token hash in cache key for security
42+ # Include version AND token hash in cache key
43+ version = AuthService ._get_auth_cache_version ()
2844 token_hash = CachedStaffUserService ._get_token_hash ()
29- cache_key = f"auth_user:{ auth_user_guid } :{ token_hash } "
45+ cache_key = f"auth_user:{ auth_user_guid } :{ token_hash } :v { version } "
3046
3147 cached_result = cache .get (cache_key )
3248 if cached_result is not None :
@@ -56,9 +72,10 @@ def get_epic_users_by_app():
5672 from compliance_api .utils .constant import AUTH_APP
5773 from compliance_api .exceptions import BusinessError
5874
59- # Include token hash in cache key for security
75+ # Include version AND token hash in cache key
76+ version = AuthService ._get_auth_cache_version ()
6077 token_hash = CachedStaffUserService ._get_token_hash ()
61- cache_key = f"auth_users_app:{ AUTH_APP } :{ token_hash } "
78+ cache_key = f"auth_users_app:{ AUTH_APP } :{ token_hash } :v { version } "
6279
6380 cached_result = cache .get (cache_key )
6481 if cached_result is not None :
@@ -91,11 +108,8 @@ def update_user_group(auth_user_guid: str, payload: dict):
91108 f"Update group in the auth server failed for user : { auth_user_guid } "
92109 )
93110
94- # Invalidate auth caches for this specific user
95- AuthService ._invalidate_auth_user_cache (auth_user_guid )
96-
97- # Invalidate the "all users by app" cache since this user's groups changed
98- AuthService ._invalidate_auth_users_by_app_cache ()
111+ # Invalidate all auth caches by bumping version
112+ AuthService ._invalidate_all_auth_cache ()
99113
100114 # Invalidate ALL staff caches since permissions changed
101115 CachedStaffUserService .invalidate_staff_cache (auth_user_guid )
@@ -116,50 +130,30 @@ def delete_user_group(auth_user_guid: str, group: str, del_sub_group_mappings=Tr
116130 if delete_response .status_code != 204 :
117131 raise BusinessError ("Delete group mapping failed" )
118132
119- # Invalidate auth caches for this specific user
120- AuthService ._invalidate_auth_user_cache (auth_user_guid )
121-
122- # Invalidate the "all users by app" cache since this user's groups changed
123- AuthService ._invalidate_auth_users_by_app_cache ()
133+ # Invalidate all auth caches by bumping version
134+ AuthService ._invalidate_all_auth_cache ()
124135
125- # Invalidate ALL staff caches since permissions changed
136+ # Invalidate all staff caches since permissions changed
126137 CachedStaffUserService .invalidate_staff_cache (auth_user_guid )
127138
128139 return delete_response
129140
130141 @staticmethod
131- def _invalidate_auth_user_cache (auth_user_guid : str ):
132- """
133- Invalidate the individual auth user cache.
134-
135- Since cache keys include token hashes, we can't delete all variations.
136- Instead, we use a pattern-based approach or accept that cache will expire naturally.
137- """
138- from compliance_api .services .cached_staff_user import CachedStaffUserService
139-
140- # Get current token hash
141- token_hash = CachedStaffUserService ._get_token_hash ()
142- cache_key = f"auth_user:{ auth_user_guid } :{ token_hash } "
143-
144- cache .delete (cache_key )
145- current_app .logger .info (f"Invalidated auth user cache for { auth_user_guid } " )
146-
147- @staticmethod
148- def _invalidate_auth_users_by_app_cache ():
149- """
150- Invalidate the "all users by app" cache.
151-
152- Since cache keys include token hashes, we can't delete all variations.
153- Instead, we delete for the current token.
154- """
155- from compliance_api .services .cached_staff_user import CachedStaffUserService
156-
157- # Get current token hash
158- token_hash = CachedStaffUserService ._get_token_hash ()
159- cache_key = f"auth_users_app:{ AUTH_APP } :{ token_hash } "
160-
161- cache .delete (cache_key )
162- current_app .logger .info ("Invalidated auth users by app cache" )
142+ def _invalidate_all_auth_cache ():
143+ """Invalidate all auth cache by bumping version number."""
144+ try :
145+ current_version = cache .get (AuthService .AUTH_CACHE_VERSION_KEY )
146+ if current_version is None :
147+ current_version = 0
148+
149+ new_version = current_version + 1
150+ cache .set (AuthService .AUTH_CACHE_VERSION_KEY , new_version )
151+
152+ current_app .logger .debug (
153+ f"Bumped auth cache version from { current_version } to { new_version } "
154+ )
155+ except (AttributeError , RuntimeError ) as e :
156+ current_app .logger .error (f"Error invalidating auth cache: { e } " )
163157
164158
165159def _request_auth_service (
0 commit comments