Commit 5bfbcfd2c6
Unregistered key
Layout: unified · split
src/config/mod.rs +55 −10
| @@ -317,9 +317,38 @@ impl From<Session> for AuthSession { | ||
| 317 | 317 | /// (#775). Saving the ID lets the next login ask the homeserver for the |
| 318 | 318 | /// same device and keep using that store. |
| 319 | 319 | #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] |
| 320 | pub struct SavedDevice { | |
| 321 | user_id: OwnedUserId, | |
| 322 | device_id: OwnedDeviceId, | |
| 320 | #[serde(untagged)] | |
| 321 | pub enum SavedDevice { | |
| 322 | OAuth { | |
| 323 | user_id: OwnedUserId, | |
| 324 | device_id: OwnedDeviceId, | |
| 325 | client_id: ClientId, | |
| 326 | }, | |
| 327 | MatrixAuth { | |
| 328 | user_id: OwnedUserId, | |
| 329 | device_id: OwnedDeviceId, | |
| 330 | }, | |
| 331 | } | |
| 332 | ||
| 333 | impl SavedDevice { | |
| 334 | pub fn device_id(&self) -> &DeviceId { | |
| 335 | match self { | |
| 336 | SavedDevice::OAuth { device_id, .. } => device_id, | |
| 337 | SavedDevice::MatrixAuth { device_id, .. } => device_id, | |
| 338 | } | |
| 339 | } | |
| 340 | pub fn client_id(&self) -> Option<&ClientId> { | |
| 341 | match self { | |
| 342 | SavedDevice::OAuth { client_id, .. } => Some(client_id), | |
| 343 | SavedDevice::MatrixAuth { .. } => None, | |
| 344 | } | |
| 345 | } | |
| 346 | pub fn is_user(&self, user: &UserId) -> bool { | |
| 347 | match self { | |
| 348 | SavedDevice::OAuth { user_id, .. } => user == user_id, | |
| 349 | SavedDevice::MatrixAuth { user_id, .. } => user == user_id, | |
| 350 | } | |
| 351 | } | |
| 323 | 352 | } |
| 324 | 353 | |
| 325 | 354 | impl From<Session> for MatrixSession { |
| @@ -1493,24 +1522,40 @@ impl ApplicationSettings { | ||
| 1493 | 1522 | /// The device ID saved at logout, if it was saved for this profile's |
| 1494 | 1523 | /// user. A missing or stale file is not an error: the next login then |
| 1495 | 1524 | /// simply gets a fresh device ID from the homeserver. |
| 1496 | pub fn read_saved_device(&self) -> Option<OwnedDeviceId> { | |
| 1525 | pub fn read_saved_device(&self) -> Option<SavedDevice> { | |
| 1497 | 1526 | let file = File::open(self.device_json.as_path()).ok()?; |
| 1498 | 1527 | let reader = BufReader::new(file); |
| 1499 | 1528 | let saved: SavedDevice = serde_json::from_reader(reader).ok()?; |
| 1500 | 1529 | |
| 1501 | if saved.user_id == self.profile.user_id { | |
| 1502 | Some(saved.device_id) | |
| 1530 | if saved.is_user(&self.profile.user_id) { | |
| 1531 | Some(saved) | |
| 1503 | 1532 | } else { |
| 1504 | 1533 | None |
| 1505 | 1534 | } |
| 1506 | 1535 | } |
| 1507 | 1536 | |
| 1508 | pub fn write_saved_device(&self, device_id: &DeviceId) -> Result<(), IambError> { | |
| 1537 | pub fn write_saved_device( | |
| 1538 | &self, | |
| 1539 | device_id: &DeviceId, | |
| 1540 | client_id: Option<ClientId>, | |
| 1541 | ) -> Result<(), IambError> { | |
| 1509 | 1542 | let file = File::create(self.device_json.as_path())?; |
| 1510 | 1543 | let writer = BufWriter::new(file); |
| 1511 | let saved = SavedDevice { | |
| 1512 | user_id: self.profile.user_id.clone(), | |
| 1513 | device_id: device_id.to_owned(), | |
| 1544 | ||
| 1545 | let saved = match client_id { | |
| 1546 | Some(client) => { | |
| 1547 | SavedDevice::OAuth { | |
| 1548 | user_id: self.profile.user_id.clone(), | |
| 1549 | device_id: device_id.to_owned(), | |
| 1550 | client_id: client, | |
| 1551 | } | |
| 1552 | }, | |
| 1553 | None => { | |
| 1554 | SavedDevice::MatrixAuth { | |
| 1555 | user_id: self.profile.user_id.clone(), | |
| 1556 | device_id: device_id.to_owned(), | |
| 1557 | } | |
| 1558 | }, | |
| 1514 | 1559 | }; |
| 1515 | 1560 | serde_json::to_writer(writer, &saved).map_err(IambError::from)?; |
| 1516 | 1561 | Ok(()) |
src/oauth.rs +15 −2
| @@ -15,6 +15,7 @@ use matrix_sdk::utils::local_server::{LocalServerBuilder, LocalServerRedirectHan | ||
| 15 | 15 | use tokio::io::AsyncBufReadExt; |
| 16 | 16 | use tokio::task::JoinHandle; |
| 17 | 17 | |
| 18 | use crate::config::SavedDevice; | |
| 18 | 19 | use crate::prelude::*; |
| 19 | 20 | |
| 20 | 21 | enum OAuthRedirectResponse { |
| @@ -22,7 +23,11 @@ enum OAuthRedirectResponse { | ||
| 22 | 23 | Stdin(UrlOrQuery), |
| 23 | 24 | } |
| 24 | 25 | |
| 25 | pub async fn oauth_login(client: Client, user_hint: &UserId) -> IambResult<()> { | |
| 26 | pub async fn oauth_login( | |
| 27 | client: Client, | |
| 28 | user_hint: &UserId, | |
| 29 | saved_device: Option<SavedDevice>, | |
| 30 | ) -> IambResult<()> { | |
| 26 | 31 | let oauth = client.oauth(); |
| 27 | 32 | |
| 28 | 33 | match oauth.server_metadata().await { |
| @@ -32,9 +37,17 @@ pub async fn oauth_login(client: Client, user_hint: &UserId) -> IambResult<()> { | ||
| 32 | 37 | server_metadata.issuer |
| 33 | 38 | ); |
| 34 | 39 | |
| 40 | let device = if let Some(SavedDevice::OAuth { device_id, client_id, .. }) = saved_device | |
| 41 | { | |
| 42 | oauth.restore_registered_client(client_id.clone()); | |
| 43 | Some(device_id) | |
| 44 | } else { | |
| 45 | None | |
| 46 | }; | |
| 47 | ||
| 35 | 48 | let (redirect_uri, server_handle) = LocalServerBuilder::new().spawn().await?; |
| 36 | 49 | let OAuthAuthorizationData { url, .. } = oauth |
| 37 | .login(redirect_uri, None, Some(client_metadata().into()), None) | |
| 50 | .login(redirect_uri, device, Some(client_metadata().into()), None) | |
| 38 | 51 | .user_id_hint(user_hint) |
| 39 | 52 | .build() |
| 40 | 53 | .await |
src/worker.rs +13 −6
| @@ -2172,8 +2172,8 @@ impl ClientWorker { | ||
| 2172 | 2172 | .matrix_auth() |
| 2173 | 2173 | .login_username(&self.settings.profile.user_id, &password) |
| 2174 | 2174 | .initial_device_display_name(initial_devname().as_str()); |
| 2175 | if let Some(device_id) = self.settings.read_saved_device() { | |
| 2176 | login = login.device_id(device_id.as_str()); | |
| 2175 | if let Some(device) = self.settings.read_saved_device() { | |
| 2176 | login = login.device_id(device.device_id().as_str()); | |
| 2177 | 2177 | } |
| 2178 | 2178 | let resp = login.send().await.map_err(IambError::from)?; |
| 2179 | 2179 | let session = MatrixSession::from(&resp); |
| @@ -2194,8 +2194,8 @@ impl ClientWorker { | ||
| 2194 | 2194 | } |
| 2195 | 2195 | }) |
| 2196 | 2196 | .initial_device_display_name(initial_devname().as_str()); |
| 2197 | if let Some(device_id) = self.settings.read_saved_device() { | |
| 2198 | login = login.device_id(device_id.as_str()); | |
| 2197 | if let Some(device) = self.settings.read_saved_device() { | |
| 2198 | login = login.device_id(device.device_id().as_str()); | |
| 2199 | 2199 | } |
| 2200 | 2200 | let resp = login.send().await.map_err(IambError::from)?; |
| 2201 | 2201 | |
| @@ -2203,7 +2203,12 @@ impl ClientWorker { | ||
| 2203 | 2203 | self.settings.write_session(session)?; |
| 2204 | 2204 | }, |
| 2205 | 2205 | LoginStyle::OAuth => { |
| 2206 | oauth_login(self.client.clone(), &self.settings.profile.user_id).await?; | |
| 2206 | oauth_login( | |
| 2207 | self.client.clone(), | |
| 2208 | &self.settings.profile.user_id, | |
| 2209 | self.settings.read_saved_device(), | |
| 2210 | ) | |
| 2211 | .await?; | |
| 2207 | 2212 | let session = self |
| 2208 | 2213 | .client |
| 2209 | 2214 | .oauth() |
| @@ -2226,12 +2231,14 @@ impl ClientWorker { | ||
| 2226 | 2231 | return Err(err); |
| 2227 | 2232 | } |
| 2228 | 2233 | |
| 2234 | let client_id = self.client.oauth().client_id().cloned(); | |
| 2235 | ||
| 2229 | 2236 | // Capture the device ID before logging out, while the session is |
| 2230 | 2237 | // still active. The next login reuses it, which keeps the existing |
| 2231 | 2238 | // SDK store valid: the store is keyed by user and device ID, and a |
| 2232 | 2239 | // freshly issued device ID would no longer match it. |
| 2233 | 2240 | if let Some(device_id) = self.client.device_id() { |
| 2234 | self.settings.write_saved_device(device_id)?; | |
| 2241 | self.settings.write_saved_device(device_id, client_id)?; | |
| 2235 | 2242 | } |
| 2236 | 2243 | |
| 2237 | 2244 | // Send the logout request. |