(
WsState {
frontegg,
oidc_rx,
authenticator_kind,
adapter_client_rx,
active_connection_counter,
helm_chart_version,
allowed_roles,
}: W
| 1262 | } |
| 1263 | |
| 1264 | async fn init_ws( |
| 1265 | WsState { |
| 1266 | frontegg, |
| 1267 | oidc_rx, |
| 1268 | authenticator_kind, |
| 1269 | adapter_client_rx, |
| 1270 | active_connection_counter, |
| 1271 | helm_chart_version, |
| 1272 | allowed_roles, |
| 1273 | }: WsState, |
| 1274 | existing_user: Option<ExistingUser>, |
| 1275 | peer_addr: IpAddr, |
| 1276 | ws: &mut WebSocket, |
| 1277 | ) -> Result<AuthedClient, anyhow::Error> { |
| 1278 | // TODO: Add a timeout here to prevent resource leaks by clients that |
| 1279 | // connect then never send a message. |
| 1280 | let ws_auth: WebSocketAuth = loop { |
| 1281 | let init_msg = ws.recv().await.ok_or_else(|| anyhow::anyhow!("closed"))??; |
| 1282 | match init_msg { |
| 1283 | Message::Text(data) => break serde_json::from_str(&data)?, |
| 1284 | Message::Binary(data) => break serde_json::from_slice(&data)?, |
| 1285 | // Handled automatically by the server. |
| 1286 | Message::Ping(_) => { |
| 1287 | continue; |
| 1288 | } |
| 1289 | Message::Pong(_) => { |
| 1290 | continue; |
| 1291 | } |
| 1292 | Message::Close(_) => { |
| 1293 | anyhow::bail!("closed"); |
| 1294 | } |
| 1295 | } |
| 1296 | }; |
| 1297 | |
| 1298 | // If credentials are provided, we perform a new authentication, |
| 1299 | // separate from the existing session. |
| 1300 | let (creds, options) = match ws_auth { |
| 1301 | WebSocketAuth::Basic { |
| 1302 | user, |
| 1303 | password, |
| 1304 | options, |
| 1305 | } => { |
| 1306 | let creds = Credentials::Password { |
| 1307 | username: user, |
| 1308 | password, |
| 1309 | }; |
| 1310 | (Some(creds), options) |
| 1311 | } |
| 1312 | WebSocketAuth::Bearer { token, options } => { |
| 1313 | let creds = Credentials::Token { token }; |
| 1314 | (Some(creds), options) |
| 1315 | } |
| 1316 | WebSocketAuth::OptionsOnly { options } => (None, options), |
| 1317 | }; |
| 1318 | |
| 1319 | let user = match (existing_user, creds) { |
| 1320 | (Some(ExistingUser::XMaterializeUserHeader(_)), Some(_creds)) => { |
| 1321 | warn!("Unexpected bearer or basic auth provided when using user header"); |
no test coverage detected