hive_client/client/authentication/
user.rs

1use chrono::{DateTime, Utc};
2use std::fmt::Debug;
3use std::ops::Add;
4use std::sync::Arc;
5
6#[derive(Debug)]
7/// A user registed with a Hive account.
8pub struct User {
9    /// The username of the user - this is the email address used
10    /// to register the account.
11    pub(crate) username: String,
12    pub(crate) password: String,
13    pub(crate) device: Option<Arc<TrustedDevice>>,
14}
15
16impl User {
17    #[must_use]
18    /// Create a new user with the given username and password.
19    ///
20    /// Optionally, a trusted device can be provided which will be used to authenticate the user without
21    /// the need to go through additional [`crate::authentication::ChallengeRequest`]s - like SMS MFA.
22    pub fn new<'a>(
23        username: &'a str,
24        password: &'a str,
25        trusted_device: Option<TrustedDevice>,
26    ) -> Self {
27        Self {
28            username: username.into(),
29            password: password.into(),
30            device: trusted_device.map(Arc::new),
31        }
32    }
33}
34
35#[derive(Debug)]
36pub enum AuthDevice {
37    Trusted(Arc<TrustedDevice>),
38    Untrusted(UntrustedDevice),
39}
40
41/// A trusted device is a device that has been confirmed.
42///
43/// Hive uses AWS Cognito for authentication, and a trusted device in a Hive account is actually a tracked
44/// device in AWS Cognito.
45///
46/// See the [AWS Cognito documentation](https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-device-tracking.html#user-pools-remembered-devices-getting-a-device-key) for more information.
47#[derive(Debug, Clone)]
48pub struct TrustedDevice {
49    #[allow(missing_docs)]
50    pub device_group_key: String,
51
52    #[allow(missing_docs)]
53    pub device_key: String,
54
55    #[allow(missing_docs)]
56    pub device_password: String,
57}
58
59impl TrustedDevice {
60    #[must_use]
61    /// Create a new trusted device which can be used to authenticate the user.
62    ///
63    /// ```rust
64    /// use hive_client::authentication::{TrustedDevice, User};
65    ///
66    /// // Create the trusted device with the device password, group key and key.
67    /// let trusted_device = TrustedDevice::new(
68    ///     "device_password",
69    ///     "device_group_key",
70    ///     "device_key"
71    /// );
72    ///
73    /// /// Assign the trusted device to the user.
74    /// let user = User::new(
75    ///     "username",
76    ///     "password",
77    ///     Some(trusted_device)
78    /// );
79    /// ```
80    pub fn new<'a>(
81        device_password: &'a str,
82        device_group_key: &'a str,
83        device_key: &'a str,
84    ) -> Self {
85        Self {
86            device_password: device_password.into(),
87            device_group_key: device_group_key.into(),
88            device_key: device_key.into(),
89        }
90    }
91}
92
93#[derive(Debug)]
94pub struct UntrustedDevice {
95    pub device_group_key: String,
96    pub device_key: String,
97}
98
99impl UntrustedDevice {
100    #[must_use]
101    pub(crate) fn new<'a>(device_group_key: &'a str, device_key: &'a str) -> Self {
102        Self {
103            device_group_key: device_group_key.into(),
104            device_key: device_key.into(),
105        }
106    }
107}
108
109#[derive(Debug)]
110pub struct Tokens {
111    pub(crate) id_token: String,
112    pub(crate) access_token: String,
113    pub(crate) refresh_token: String,
114    pub(crate) expires_at: DateTime<Utc>,
115}
116
117impl Tokens {
118    #[must_use]
119    pub fn new(
120        id_token: String,
121        access_token: String,
122        refresh_token: String,
123        expires_in: i32,
124    ) -> Self {
125        Self {
126            id_token,
127            access_token,
128            refresh_token,
129            expires_at: Utc::now().add(chrono::Duration::seconds(i64::from(expires_in))),
130        }
131    }
132}