Commit 5bfbcfd2c6
Unregistered key
Layout: unified · split
src/config/mod.rs +55 −10
| @@ -317,9 +317,38 @@ impl From<Session> for AuthSession { | |||
| 317 | /// (#775). Saving the ID lets the next login ask the homeserver for the | 317 | /// (#775). Saving the ID lets the next login ask the homeserver for the |
| 318 | /// same device and keep using that store. | 318 | /// same device and keep using that store. |
| 319 | #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] | 319 | #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] |
| 320 | pub struct SavedDevice { | 320 | #[serde(untagged)] |
| 321 | user_id: OwnedUserId, | 321 | pub enum SavedDevice { |
| 322 | device_id: OwnedDeviceId, | 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 | impl From<Session> for MatrixSession { | 354 | impl From<Session> for MatrixSession { |
| @@ -1493,24 +1522,40 @@ impl ApplicationSettings { | |||
| 1493 | /// The device ID saved at logout, if it was saved for this profile's | 1522 | /// The device ID saved at logout, if it was saved for this profile's |
| 1494 | /// user. A missing or stale file is not an error: the next login then | 1523 | /// user. A missing or stale file is not an error: the next login then |
| 1495 | /// simply gets a fresh device ID from the homeserver. | 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 | let file = File::open(self.device_json.as_path()).ok()?; | 1526 | let file = File::open(self.device_json.as_path()).ok()?; |
| 1498 | let reader = BufReader::new(file); | 1527 | let reader = BufReader::new(file); |
| 1499 | let saved: SavedDevice = serde_json::from_reader(reader).ok()?; | 1528 | let saved: SavedDevice = serde_json::from_reader(reader).ok()?; |
| 1500 | 1529 | ||
| 1501 | if saved.user_id == self.profile.user_id { | 1530 | if saved.is_user(&self.profile.user_id) { |
| 1502 | Some(saved.device_id) | 1531 | Some(saved) |
| 1503 | } else { | 1532 | } else { |
| 1504 | None | 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 | let file = File::create(self.device_json.as_path())?; | 1542 | let file = File::create(self.device_json.as_path())?; |
| 1510 | let writer = BufWriter::new(file); | 1543 | let writer = BufWriter::new(file); |
| 1511 | let saved = SavedDevice { | 1544 | |
| 1512 | user_id: self.profile.user_id.clone(), | 1545 | let saved = match client_id { |
| 1513 | device_id: device_id.to_owned(), | 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 | serde_json::to_writer(writer, &saved).map_err(IambError::from)?; | 1560 | serde_json::to_writer(writer, &saved).map_err(IambError::from)?; |
| 1516 | Ok(()) | 1561 | Ok(()) |
src/oauth.rs +15 −2
| @@ -15,6 +15,7 @@ use matrix_sdk::utils::local_server::{LocalServerBuilder, LocalServerRedirectHan | |||
| 15 | use tokio::io::AsyncBufReadExt; | 15 | use tokio::io::AsyncBufReadExt; |
| 16 | use tokio::task::JoinHandle; | 16 | use tokio::task::JoinHandle; |
| 17 | 17 | ||
| 18 | use crate::config::SavedDevice; | ||
| 18 | use crate::prelude::*; | 19 | use crate::prelude::*; |
| 19 | 20 | ||
| 20 | enum OAuthRedirectResponse { | 21 | enum OAuthRedirectResponse { |
| @@ -22,7 +23,11 @@ enum OAuthRedirectResponse { | |||
| 22 | Stdin(UrlOrQuery), | 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 | let oauth = client.oauth(); | 31 | let oauth = client.oauth(); |
| 27 | 32 | ||
| 28 | match oauth.server_metadata().await { | 33 | match oauth.server_metadata().await { |
| @@ -32,9 +37,17 @@ pub async fn oauth_login(client: Client, user_hint: &UserId) -> IambResult<()> { | |||
| 32 | server_metadata.issuer | 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 | let (redirect_uri, server_handle) = LocalServerBuilder::new().spawn().await?; | 48 | let (redirect_uri, server_handle) = LocalServerBuilder::new().spawn().await?; |
| 36 | let OAuthAuthorizationData { url, .. } = oauth | 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 | .user_id_hint(user_hint) | 51 | .user_id_hint(user_hint) |
| 39 | .build() | 52 | .build() |
| 40 | .await | 53 | .await |
src/worker.rs +13 −6
| @@ -2172,8 +2172,8 @@ impl ClientWorker { | |||
| 2172 | .matrix_auth() | 2172 | .matrix_auth() |
| 2173 | .login_username(&self.settings.profile.user_id, &password) | 2173 | .login_username(&self.settings.profile.user_id, &password) |
| 2174 | .initial_device_display_name(initial_devname().as_str()); | 2174 | .initial_device_display_name(initial_devname().as_str()); |
| 2175 | if let Some(device_id) = self.settings.read_saved_device() { | 2175 | if let Some(device) = self.settings.read_saved_device() { |
| 2176 | login = login.device_id(device_id.as_str()); | 2176 | login = login.device_id(device.device_id().as_str()); |
| 2177 | } | 2177 | } |
| 2178 | let resp = login.send().await.map_err(IambError::from)?; | 2178 | let resp = login.send().await.map_err(IambError::from)?; |
| 2179 | let session = MatrixSession::from(&resp); | 2179 | let session = MatrixSession::from(&resp); |
| @@ -2194,8 +2194,8 @@ impl ClientWorker { | |||
| 2194 | } | 2194 | } |
| 2195 | }) | 2195 | }) |
| 2196 | .initial_device_display_name(initial_devname().as_str()); | 2196 | .initial_device_display_name(initial_devname().as_str()); |
| 2197 | if let Some(device_id) = self.settings.read_saved_device() { | 2197 | if let Some(device) = self.settings.read_saved_device() { |
| 2198 | login = login.device_id(device_id.as_str()); | 2198 | login = login.device_id(device.device_id().as_str()); |
| 2199 | } | 2199 | } |
| 2200 | let resp = login.send().await.map_err(IambError::from)?; | 2200 | let resp = login.send().await.map_err(IambError::from)?; |
| 2201 | 2201 | ||
| @@ -2203,7 +2203,12 @@ impl ClientWorker { | |||
| 2203 | self.settings.write_session(session)?; | 2203 | self.settings.write_session(session)?; |
| 2204 | }, | 2204 | }, |
| 2205 | LoginStyle::OAuth => { | 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 | let session = self | 2212 | let session = self |
| 2208 | .client | 2213 | .client |
| 2209 | .oauth() | 2214 | .oauth() |
| @@ -2226,12 +2231,14 @@ impl ClientWorker { | |||
| 2226 | return Err(err); | 2231 | return Err(err); |
| 2227 | } | 2232 | } |
| 2228 | 2233 | ||
| 2234 | let client_id = self.client.oauth().client_id().cloned(); | ||
| 2235 | |||
| 2229 | // Capture the device ID before logging out, while the session is | 2236 | // Capture the device ID before logging out, while the session is |
| 2230 | // still active. The next login reuses it, which keeps the existing | 2237 | // still active. The next login reuses it, which keeps the existing |
| 2231 | // SDK store valid: the store is keyed by user and device ID, and a | 2238 | // SDK store valid: the store is keyed by user and device ID, and a |
| 2232 | // freshly issued device ID would no longer match it. | 2239 | // freshly issued device ID would no longer match it. |
| 2233 | if let Some(device_id) = self.client.device_id() { | 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 | // Send the logout request. | 2244 | // Send the logout request. |