@@ -44,11 +44,11 @@ def __init__(
4444 self ._auth_session_timer = auth_session_timer
4545 self ._cached_auth_session : AuthSession | None = None
4646
47- async def create_session (self , user_id : UserId ) -> None :
47+ async def issue_session (self , user_id : UserId ) -> None :
4848 """
4949 :raises AuthenticationError:
5050 """
51- log .debug ("Create auth session: started. User ID: '%s'." , user_id .value )
51+ log .debug ("Issue auth session: started. User ID: '%s'." , user_id .value )
5252
5353 auth_session_id : str = self ._auth_session_id_generator ()
5454 expiration : datetime = self ._auth_session_timer .auth_session_expiration
@@ -68,7 +68,7 @@ async def create_session(self, user_id: UserId) -> None:
6868 self ._auth_session_transport .deliver (auth_session )
6969
7070 log .debug (
71- "Create auth session: done. User ID: '%s', Auth session id : '%s'." ,
71+ "Issue auth session: done. User ID: '%s', Auth session ID : '%s'." ,
7272 user_id .value ,
7373 auth_session .id_ ,
7474 )
@@ -79,111 +79,113 @@ async def get_authenticated_user_id(self) -> UserId:
7979 """
8080 log .debug ("Get authenticated user ID: started." )
8181
82- raw_auth_session = await self ._load_current_session ()
82+ raw_auth_session = await self ._get_current_auth_session ()
8383 valid_auth_session = await self ._validate_and_extend_session (raw_auth_session )
84+ self ._cached_auth_session = valid_auth_session
8485
8586 log .debug (
86- "Get authenticated user ID: done. Auth session ID: %s . User ID: %s ." ,
87+ "Get authenticated user ID: done. Auth session ID: '%s' . User ID: '%s' ." ,
8788 valid_auth_session .id_ ,
8889 valid_auth_session .user_id .value ,
8990 )
9091 return valid_auth_session .user_id
9192
92- async def invalidate_current_session (self ) -> None :
93- log .debug ("Invalidate current session: started. Auth session ID: unknown." )
93+ async def terminate_current_session (self ) -> None :
94+ log .debug ("Terminate current session: started. Auth session ID: unknown." )
9495
95- auth_session_id : str | None = self ._auth_session_transport .extract_id ()
96- if auth_session_id is None :
97- log .warning (
98- "Invalidate current session failed: partially failed. "
99- "Session ID can't be extracted from transport. "
100- "Auth session can't be identified." ,
96+ auth_session_id : str | None
97+ if self ._cached_auth_session is not None :
98+ auth_session_id = self ._cached_auth_session .id_
99+ log .debug (
100+ "Terminate current session: using ID from cache. "
101+ "Auth session ID: '%s'." ,
102+ auth_session_id ,
103+ )
104+ else :
105+ auth_session_id = self ._auth_session_transport .extract_id ()
106+ if auth_session_id is None :
107+ log .warning (
108+ "Terminate current session failed: partially failed. "
109+ "Session ID can't be extracted from transport. "
110+ "Auth session can't be identified." ,
111+ )
112+ return
113+ log .debug (
114+ "Terminate current session: using ID from transport. "
115+ "Auth session ID: '%s'." ,
116+ auth_session_id ,
101117 )
102- return
103-
104- log .debug (
105- "Invalidate current session: in progress. Auth session id: %s." ,
106- auth_session_id ,
107- )
108118
109119 self ._auth_session_transport .remove_current ()
110120
111- auth_session : AuthSession | None = None
112- try :
113- auth_session = await self ._auth_session_gateway .read_by_id (auth_session_id )
114-
115- except DataMapperError as error :
116- log .error ("%s: '%s'" , AUTH_SESSION_EXTRACTION_FAILED , error )
117-
118- if auth_session is None :
119- log .warning (
120- "Invalidate current session failed: partially failed. "
121- "Session ID was removed from transport, "
122- "but auth session was not found in storage." ,
123- )
124- return
125-
126121 try :
127- await self ._auth_session_gateway .delete (auth_session . id_ )
122+ await self ._auth_session_gateway .delete (auth_session_id )
128123 await self ._auth_transaction_manager .commit ()
124+ log .debug (
125+ "Terminate current session: done (transport cleared, storage deleted). "
126+ "Auth session ID: '%s'." ,
127+ auth_session_id ,
128+ )
129129
130130 except DataMapperError :
131131 log .warning (
132- (
133- "Invalidate current session failed: partially failed. "
134- "Session ID was removed from transport, "
135- "but auth session was not deleted from storage. "
136- "Auth session ID: '%s'."
137- ),
138- auth_session .id_ ,
132+ "Terminate current session: partially failed "
133+ "(transport cleared, storage delete failed). "
134+ "Auth session ID: '%s'." ,
135+ auth_session_id ,
139136 )
140137
141- async def invalidate_all_sessions_for_user (self , user_id : UserId ) -> None :
138+ self ._cached_auth_session = None
139+
140+ async def terminate_all_sessions_for_user (self , user_id : UserId ) -> None :
142141 """
143142 :raises DataMapperError:
144143 """
145144 log .debug (
146- "Invalidate all sessions for user: started. User id : '%s'." ,
145+ "Terminate all sessions for user: started. User ID : '%s'." ,
147146 user_id .value ,
148147 )
149148
150149 await self ._auth_session_gateway .delete_all_for_user (user_id )
151150 await self ._auth_transaction_manager .commit ()
152151
152+ if self ._cached_auth_session and self ._cached_auth_session .user_id == user_id :
153+ self ._auth_session_transport .remove_current ()
154+ self ._cached_auth_session = None
155+
153156 log .debug (
154- "Invalidate all sessions for user: done. User id : '%s'." ,
157+ "Terminate all sessions for user: done. User ID : '%s'." ,
155158 user_id .value ,
156159 )
157160
158- async def _load_current_session (self ) -> AuthSession :
161+ async def _get_current_auth_session (self ) -> AuthSession :
159162 """
160163 :raises AuthenticationError:
161164 """
162- log .debug ("Load current auth session: started. Auth session id: unknown." )
165+ log .debug ("Get current auth session: started. Auth session ID: unknown." )
166+
163167 if self ._cached_auth_session is not None :
164- cached_auth_session = self ._cached_auth_session
165168 log .debug (
166- "Load current auth session: done (from cache). Auth session id: %s ." ,
167- cached_auth_session .id_ ,
169+ "Get current auth session: done (from cache). Auth session ID: '%s' ." ,
170+ self . _cached_auth_session .id_ ,
168171 )
169- return cached_auth_session
172+ return self . _cached_auth_session
170173
171174 auth_session_id : str | None = self ._auth_session_transport .extract_id ()
172175 if auth_session_id is None :
173176 log .debug (AUTH_SESSION_NOT_FOUND )
174177 raise AuthenticationError (AUTH_NOT_AUTHENTICATED )
175178
176179 log .debug (
177- "Load current auth session: in progress . Auth session id: %s ." ,
180+ "Get current auth session: reading from storage . Auth session ID: '%s' ." ,
178181 auth_session_id ,
179182 )
180183
181184 try :
182185 auth_session : (
183186 AuthSession | None
184- ) = await self ._auth_session_gateway .read_by_id (
185- auth_session_id ,
186- )
187+ ) = await self ._auth_session_gateway .read_by_id (auth_session_id )
188+
187189 except DataMapperError as error :
188190 log .error ("%s: '%s'" , AUTH_SESSION_EXTRACTION_FAILED , error )
189191 raise AuthenticationError (AUTH_NOT_AUTHENTICATED ) from error
@@ -192,11 +194,8 @@ async def _load_current_session(self) -> AuthSession:
192194 log .debug (AUTH_SESSION_NOT_FOUND )
193195 raise AuthenticationError (AUTH_NOT_AUTHENTICATED )
194196
195- self ._cached_auth_session = auth_session
196-
197197 log .debug (
198- "Load current auth session: done. Auth session id: %s." ,
199- auth_session .id_ ,
198+ "Get current auth session: done. Auth session ID: '%s'." , auth_session .id_
200199 )
201200 return auth_session
202201
@@ -208,7 +207,7 @@ async def _validate_and_extend_session(
208207 :raises AuthenticationError:
209208 """
210209 log .debug (
211- "Validate and extend auth session: started. Auth session id: %s ." ,
210+ "Validate and extend auth session: started. Auth session ID: '%s' ." ,
212211 auth_session .id_ ,
213212 )
214213
@@ -223,7 +222,7 @@ async def _validate_and_extend_session(
223222 ):
224223 log .debug (
225224 "Validate and extend auth session: validated without extension. "
226- "Auth session id: %s ." ,
225+ "Auth session ID: '%s' ." ,
227226 auth_session .id_ ,
228227 )
229228 return auth_session
@@ -242,10 +241,10 @@ async def _validate_and_extend_session(
242241
243242 self ._auth_session_transport .deliver (auth_session )
244243
245- self ._cached_auth_session = auth_session
246-
247244 log .debug (
248- "Validate and extend auth session: done. Auth session id: %s." ,
245+ "Validate and extend auth session: done. "
246+ "Auth session ID: '%s'. New expiration: '%s'." ,
249247 auth_session .id_ ,
248+ auth_session .expiration .isoformat (),
250249 )
251250 return auth_session
0 commit comments