hive_client/client/authentication/
mod.rs

1use crate::constants;
2use aws_cognito_srp::{SrpClient, TrackedDevice};
3use aws_config::BehaviorVersion;
4use std::sync::Arc;
5use tokio::sync::RwLock;
6
7mod challenge;
8mod confirm_device;
9mod error;
10mod login;
11mod refresh;
12mod user;
13
14pub use challenge::{ChallengeRequest, ChallengeResponse};
15pub use error::{AuthenticationError, DeviceConfirmationError, RefreshError};
16pub use user::{TrustedDevice, User};
17
18pub(crate) use login::LoginSession;
19pub(crate) use user::{Tokens, UntrustedDevice};
20
21#[derive(Debug)]
22pub(crate) struct HiveAuth {
23    cognito: aws_sdk_cognitoidentityprovider::Client,
24    user_srp_client: SrpClient<aws_cognito_srp::User>,
25    device_srp_client: Option<SrpClient<TrackedDevice>>,
26    session: Arc<RwLock<Option<LoginSession>>>,
27}
28
29impl HiveAuth {
30    #[must_use]
31    pub(crate) async fn new(user: &User, trusted_device: Option<&TrustedDevice>) -> Self {
32        let config = aws_config::defaults(BehaviorVersion::latest())
33            .region(constants::REGION)
34            .load()
35            .await;
36
37        let mut auth = Self {
38            cognito: aws_sdk_cognitoidentityprovider::Client::new(&config),
39            user_srp_client: SrpClient::new(
40                aws_cognito_srp::User::new(constants::POOL_ID, &user.username, &user.password),
41                constants::CLIENT_ID,
42                None,
43            ),
44            device_srp_client: None,
45            session: Arc::new(RwLock::new(None)),
46        };
47
48        auth.replace_trusted_device(trusted_device);
49
50        auth
51    }
52
53    pub(crate) fn replace_trusted_device(&mut self, trusted_device: Option<&TrustedDevice>) {
54        self.device_srp_client = trusted_device.map(|trusted_device| {
55            SrpClient::new(
56                TrackedDevice::new(
57                    constants::POOL_ID,
58                    &trusted_device.device_group_key,
59                    &trusted_device.device_key,
60                    &trusted_device.device_password,
61                ),
62                constants::CLIENT_ID,
63                None,
64            )
65        });
66    }
67}