66//! lease + device identity, receive the successor pair, and classify
77//! rejections so the frontend can drop dead leases.
88//!
9- //! Rotations serialize through `SessionState::rotate_lock`: the lease is
10- //! single-use and the server treats presenting an already-rotated token as a
11- //! leak that revokes the device scope, so two concurrent rotations must
12- //! never race.
9+ //! Successful rotations are shared by callers presenting the same lease so
10+ //! concurrent entitlement and activation requests cannot replay a consumed token,
11+ //! including when the app sleeps between queued callers.
1312
1413use serde:: { Deserialize , Serialize } ;
1514use serde_json:: json;
15+ use std:: collections:: VecDeque ;
1616use tauri:: { AppHandle , Emitter , State } ;
1717
1818use crate :: common:: console;
@@ -22,10 +22,16 @@ pub const SESSION_REJECTED_ERROR_TYPE: &str = "SESSION_REJECTED";
2222
2323#[ derive( Default ) ]
2424pub struct SessionState {
25- /// See module docs — guards the present-then-replace window.
26- pub rotate_lock : tokio:: sync:: Mutex < ( ) > ,
25+ rotations : tokio:: sync:: Mutex < VecDeque < CachedRotation > > ,
2726}
2827
28+ struct CachedRotation {
29+ previous : String ,
30+ session : RefreshedSession ,
31+ }
32+
33+ const ROTATION_CACHE_LIMIT : usize = 64 ;
34+
2935pub fn session_rejected_error ( message : & str ) -> String {
3036 json ! ( {
3137 "error_type" : SESSION_REJECTED_ERROR_TYPE ,
@@ -40,6 +46,8 @@ pub struct RefreshedSession {
4046 pub access_token : String ,
4147 #[ serde( rename = "refresh_token" ) ]
4248 pub refresh_token : String ,
49+ #[ serde( default , skip_deserializing) ]
50+ pub previous_refresh_token : String ,
4351}
4452
4553/// Exchange a lease for the successor pair. The caller must surface the
@@ -60,7 +68,13 @@ pub async fn rotate_session(
6068 return Err ( "no stored refresh token" . to_string ( ) ) ;
6169 }
6270
63- let _guard = state. rotate_lock . lock ( ) . await ;
71+ let mut rotations = state. rotations . lock ( ) . await ;
72+ if let Some ( cached) = rotations
73+ . iter ( )
74+ . find ( |entry| entry. previous == refresh_token)
75+ {
76+ return Ok ( cached. session . clone ( ) ) ;
77+ }
6478 let response = console:: client ( )
6579 . post ( format ! ( "{}/api/v1/auth/refresh" , console:: api_base_url( ) ) )
6680 . json ( & json ! ( { "refresh_token" : refresh_token, "device" : device_payload } ) )
@@ -89,8 +103,19 @@ pub async fn rotate_session(
89103 }
90104
91105 let data = raw. get ( "data" ) . cloned ( ) . unwrap_or ( json ! ( { } ) ) ;
92- let session: RefreshedSession =
106+ let mut session: RefreshedSession =
93107 serde_json:: from_value ( data) . map_err ( |e| format ! ( "invalid refresh result: {e}" ) ) ?;
108+ if !status. is_success ( ) {
109+ return Err ( format ! ( "refresh endpoint returned HTTP {status}" ) ) ;
110+ }
111+ session. previous_refresh_token = refresh_token. to_string ( ) ;
112+ if rotations. len ( ) >= ROTATION_CACHE_LIMIT {
113+ rotations. pop_front ( ) ;
114+ }
115+ rotations. push_back ( CachedRotation {
116+ previous : refresh_token. to_string ( ) ,
117+ session : session. clone ( ) ,
118+ } ) ;
94119 Ok ( session)
95120}
96121
@@ -114,6 +139,7 @@ pub async fn rotate_session_now(
114139 json ! ( {
115140 "accessToken" : refreshed. access_token,
116141 "refreshToken" : refreshed. refresh_token,
142+ "previousRefreshToken" : refreshed. previous_refresh_token,
117143 } ) ,
118144 ) ;
119145 Ok ( refreshed)
@@ -134,7 +160,10 @@ pub async fn revoke_session(refresh_token: Option<String>) -> Result<(), String>
134160 . await
135161 . map_err ( |e| format ! ( "network error: {e}" ) ) ?;
136162 if !response. status ( ) . is_success ( ) {
137- return Err ( format ! ( "logout endpoint returned HTTP {}" , response. status( ) ) ) ;
163+ return Err ( format ! (
164+ "logout endpoint returned HTTP {}" ,
165+ response. status( )
166+ ) ) ;
138167 }
139168 Ok ( ( ) )
140169}
0 commit comments