pub struct SessionService { /* private fields */ }connect-server only.Expand description
Runs the session-initiation half of an endpoint.
Almost everything it needs is in the Store and in the bearer the client presented,
including the replacement access token it issued and has not yet seen confirmed — a
server that held that in memory would retire the wrong token after a restart.
What it keeps in memory is what must not survive a restart: the communication tokens handed out and not yet used (each valid for thirty seconds and one connection), and the WebSocket sessions currently open, so that unpairing can end them.
Implementations§
Source§impl SessionService
impl SessionService
Sourcepub fn new(store: Arc<dyn Store>, websocket_url: impl Into<String>) -> Self
pub fn new(store: Arc<dyn Store>, websocket_url: impl Into<String>) -> Self
A service answering on websocket_url, which must be wss://
(S2C §Encrypted connection: “S2 MUST NOT be sent over unencrypted channels”).
Sourcepub fn with_s2_message_versions(self, versions: Vec<ProtocolVersion>) -> Self
pub fn with_s2_message_versions(self, versions: Vec<ProtocolVersion>) -> Self
Offer a different set of S2 JSON message versions, newest first.
Sourcepub fn initiate_session(
&self,
bearer: &AccessToken,
request: &SessionRequest,
access_entropy: &[u8],
now: Timestamp,
) -> Result<SessionGrant, ConnectError>
pub fn initiate_session( &self, bearer: &AccessToken, request: &SessionRequest, access_entropy: &[u8], now: Timestamp, ) -> Result<SessionGrant, ConnectError>
POST /v1/initiateSession.
The replacement token is stored alongside the presented one, never instead of it: until the client confirms, either must work, or a lost reply destroys the pairing.
A pairing this endpoint revoked answers NoLongerPaired — once, to a client that
proves it held it — and is then forgotten.
Sourcepub fn confirm_access_token(
&self,
bearer: &AccessToken,
communication_entropy: &[u8],
now: Timestamp,
) -> Result<(PairingKey, CommunicationDetails), ConnectError>
pub fn confirm_access_token( &self, bearer: &AccessToken, communication_entropy: &[u8], now: Timestamp, ) -> Result<(PairingKey, CommunicationDetails), ConnectError>
POST /v1/confirmAccessToken.
The bearer is the confirmation. Every older token is retired here, and not one request earlier.
Whether a bearer presented on a WebSocket upgrade is one this endpoint issued, and which pairing it belongs to.
The token is consumed: s2-connect-common.yml/CommunicationToken is “valid for a
single connection … for maximum 30 seconds”, so a replay is refused rather than
granted a second session in parallel with the first. Every token is compared in
constant time and the list is swept of expired entries on the way through.
This is what a host mounts its /s2 route on —
websocket_router does it for you.
Sourcepub fn pending_connections(&self) -> usize
pub fn pending_connections(&self) -> usize
How many issued communication tokens are still waiting to be used.
For a health endpoint, and for a test that wants to see one consumed.
Sourcepub fn register_session(self: &Arc<Self>, key: PairingKey) -> LiveSession
pub fn register_session(self: &Arc<Self>, key: PairingKey) -> LiveSession
Start tracking an open WebSocket session, so unpairing can end it.
websocket_router does this for every socket it
accepts; a host terminating the WebSocket itself calls it after
authorize_connection.
Sourcepub fn live_sessions(&self, key: PairingKey) -> usize
pub fn live_sessions(&self, key: PairingKey) -> usize
How many WebSocket sessions are open for one pairing.
Sourcepub fn end_sessions(&self, key: PairingKey)
pub fn end_sessions(&self, key: PairingKey)
End every open session of a pairing, and void its unused communication tokens.
Sourcepub fn unpair(
&self,
bearer: &AccessToken,
request: &UnpairRequest,
) -> Result<(), ConnectError>
pub fn unpair( &self, bearer: &AccessToken, request: &UnpairRequest, ) -> Result<(), ConnectError>
POST /v1/unpair — the client renounces the pairing.
Sourcepub fn unpair_remote(&self, key: PairingKey)
pub fn unpair_remote(&self, key: PairingKey)
Unpair from this side: end the pairing’s sessions now, and answer its next
initiateSession with NoLongerPaired.
S2C §Unpairing by the communication server also says the server “should
send an S2 SessionRequest … RECONNECT” over the open session first; that is a
message of the S2 session the host runs, so send it before calling this.
Sourcepub fn tokens(&self, key: PairingKey) -> Option<TokenStore>
pub fn tokens(&self, key: PairingKey) -> Option<TokenStore>
The tokens of one pairing, for a route that must check a bearer.