Skip to main content

hive_client/client/authentication/
user.rs

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