Skip to content

Commit b3462cf

Browse files
committed
daemon: Use async_trait to prevent generics sprawl
1 parent b02efb7 commit b3462cf

7 files changed

Lines changed: 161 additions & 152 deletions

File tree

.vscode/tasks.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"tasks": [
44
{
55
"type": "meson",
6-
"target": "credentialsd",
6+
"target": "credentialsd/src/credentialsd:custom",
77
"mode": "build",
88
"problemMatcher": [
99
"$meson-gcc"
@@ -13,7 +13,7 @@
1313
},
1414
{
1515
"type": "meson",
16-
"target": "credentialsd-ui",
16+
"target": "credentialsd-ui/src/credentialsd-ui:custom",
1717
"mode": "build",
1818
"problemMatcher": [
1919
"$meson-gcc"

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

credentialsd/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ license = "LGPL-3.0-only"
77

88
[dependencies]
99
async-stream = "0.3.6"
10+
async-trait = "0.1.89"
1011
base64 = "0.22.1"
1112
credentialsd-common = { path = "../credentialsd-common" }
1213
futures-lite.workspace = true

credentialsd/src/dbus/flow_control.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! This module implements the service to allow the user to control the flow of
22
//! the credential request through the trusted UI.
33
4-
use std::future::Future;
54
use std::{collections::VecDeque, fmt::Debug, sync::Arc};
65

6+
use async_trait::async_trait;
77
use credentialsd_common::model::{
88
BackgroundEvent, Device, Error as CredentialServiceError, RequestingApplication, WebAuthnError,
99
};
@@ -341,13 +341,14 @@ enum SignalState {
341341
Active,
342342
}
343343

344+
#[async_trait]
344345
pub trait CredentialRequestController {
345-
fn request_credential(
346+
async fn request_credential(
346347
&self,
347348
requesting_app: Option<RequestingApplication>,
348349
request: CredentialRequest,
349350
window_handle: Option<WindowHandle>,
350-
) -> impl Future<Output = Result<CredentialResponse, WebAuthnError>> + Send;
351+
) -> Result<CredentialResponse, WebAuthnError>;
351352
}
352353

353354
pub struct CredentialRequestControllerClient {
@@ -359,6 +360,7 @@ pub struct CredentialRequestControllerClient {
359360
)>,
360361
}
361362

363+
#[async_trait]
362364
impl CredentialRequestController for CredentialRequestControllerClient {
363365
async fn request_credential(
364366
&self,

credentialsd/src/gateway/dbus.rs

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,36 @@ use credentialsd_common::{
1717
},
1818
};
1919

20-
use crate::{
21-
dbus::CredentialRequestController,
22-
webauthn::{AppId, NavigationContext, Origin},
23-
};
20+
use crate::webauthn::{AppId, NavigationContext, Origin};
2421

2522
use super::{
2623
check_origin_from_app, check_origin_from_privileged_client, get_app_info_from_pid,
27-
handle_create_credential, handle_get_client_capabilities, handle_get_credential,
28-
CredentialGateway,
24+
GatewayService,
2925
};
3026

3127
pub const SERVICE_NAME: &str = "xyz.iinuwa.credentialsd.Credentials";
3228
pub const SERVICE_PATH: &str = "/xyz/iinuwa/credentialsd/Credentials";
3329

34-
pub(super) async fn start_dbus_gateway<C: CredentialRequestController + Send + Sync + 'static>(
35-
controller: C,
30+
pub(super) async fn start_dbus_gateway(
31+
svc: Arc<AsyncMutex<GatewayService>>,
3632
) -> Result<Connection, zbus::Error> {
3733
zbus::connection::Builder::session()
3834
.inspect_err(|err| {
3935
tracing::error!("Failed to connect to D-Bus session: {err}");
4036
})?
4137
.name(SERVICE_NAME)?
42-
.serve_at(
43-
SERVICE_PATH,
44-
CredentialGateway {
45-
controller: Arc::new(AsyncMutex::new(controller)),
46-
},
47-
)?
38+
.serve_at(SERVICE_PATH, CredentialGateway { svc: svc.clone() })?
4839
.build()
4940
.await
5041
}
42+
43+
struct CredentialGateway {
44+
svc: Arc<AsyncMutex<GatewayService>>,
45+
}
46+
5147
/// These are public methods that can be called by arbitrary clients to begin a credential flow.
5248
#[interface(name = "xyz.iinuwa.credentialsd.Credentials1")]
53-
impl<C: CredentialRequestController + Send + Sync + 'static> super::CredentialGateway<C> {
49+
impl CredentialGateway {
5450
async fn create_credential(
5551
&self,
5652
#[zbus(header)] header: Header<'_>,
@@ -86,14 +82,17 @@ impl<C: CredentialRequestController + Send + Sync + 'static> super::CredentialGa
8682
let request_environment = check_origin_from_privileged_client(origin, top_origin)?;
8783
// Find out where this request is coming from (which application is requesting this)
8884
let requesting_app = query_connection_peer_binary(header, connection).await;
89-
let response = handle_create_credential(
90-
&self.controller,
91-
request,
92-
request_environment,
93-
requesting_app,
94-
parent_window.into(),
95-
)
96-
.await?;
85+
let response = self
86+
.svc
87+
.lock()
88+
.await
89+
.handle_create_credential(
90+
request,
91+
request_environment,
92+
requesting_app,
93+
parent_window.into(),
94+
)
95+
.await?;
9796
Ok(response)
9897
}
9998

@@ -132,19 +131,22 @@ impl<C: CredentialRequestController + Send + Sync + 'static> super::CredentialGa
132131
let request_environment = check_origin_from_privileged_client(origin, top_origin)?;
133132
// Find out where this request is coming from (which application is requesting this)
134133
let requesting_app = query_connection_peer_binary(header, connection).await;
135-
let response = handle_get_credential(
136-
&self.controller,
137-
request,
138-
request_environment,
139-
requesting_app,
140-
parent_window.into(),
141-
)
142-
.await?;
134+
let response = self
135+
.svc
136+
.lock()
137+
.await
138+
.handle_get_credential(
139+
request,
140+
request_environment,
141+
requesting_app,
142+
parent_window.into(),
143+
)
144+
.await?;
143145
Ok(response)
144146
}
145147

146148
async fn get_client_capabilities(&self) -> fdo::Result<GetClientCapabilitiesResponse> {
147-
let capabilities = handle_get_client_capabilities();
149+
let capabilities = self.svc.lock().await.handle_get_client_capabilities();
148150
Ok(capabilities)
149151
}
150152
}

0 commit comments

Comments
 (0)