Skip to main content

alibi_core/wire/
account.rs

1use crate::AuthAccount;
2use chrono::{DateTime, Utc};
3use serde::{Deserialize, Serialize};
4use std::borrow::Cow;
5/// Public account response shape.
6#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
7pub struct AccountView {
8    pub id: String,
9    #[serde(rename = "accountId")]
10    pub account_id: String,
11    #[serde(rename = "providerId")]
12    pub provider_id: String,
13    #[serde(rename = "userId")]
14    pub user_id: String,
15    #[serde(rename = "accessToken")]
16    pub access_token: Option<String>,
17    #[serde(rename = "refreshToken")]
18    pub refresh_token: Option<String>,
19    #[serde(rename = "idToken")]
20    pub id_token: Option<String>,
21    #[serde(rename = "accessTokenExpiresAt")]
22    #[serde(serialize_with = "crate::utils::datetime::serialize_optional")]
23    pub access_token_expires_at: Option<DateTime<Utc>>,
24    #[serde(rename = "refreshTokenExpiresAt")]
25    #[serde(serialize_with = "crate::utils::datetime::serialize_optional")]
26    pub refresh_token_expires_at: Option<DateTime<Utc>>,
27    pub scope: Option<String>,
28    #[serde(skip_serializing)]
29    pub password: Option<String>,
30    #[serde(rename = "createdAt")]
31    #[serde(serialize_with = "crate::utils::datetime::serialize")]
32    pub created_at: DateTime<Utc>,
33    #[serde(rename = "updatedAt")]
34    #[serde(serialize_with = "crate::utils::datetime::serialize")]
35    pub updated_at: DateTime<Utc>,
36}
37
38impl<T: AuthAccount> From<&T> for AccountView {
39    fn from(account: &T) -> Self {
40        Self {
41            id: account.id().into_owned(),
42            account_id: account.account_id().to_owned(),
43            provider_id: account.provider_id().to_owned(),
44            user_id: account.user_id().into_owned(),
45            access_token: account.access_token().map(str::to_owned),
46            refresh_token: account.refresh_token().map(str::to_owned),
47            id_token: account.id_token().map(str::to_owned),
48            access_token_expires_at: account.access_token_expires_at(),
49            refresh_token_expires_at: account.refresh_token_expires_at(),
50            scope: account.scope().map(str::to_owned),
51            password: account.password().map(str::to_owned),
52            created_at: account.created_at(),
53            updated_at: account.updated_at(),
54        }
55    }
56}
57
58impl AuthAccount for AccountView {
59    fn id(&self) -> Cow<'_, str> {
60        Cow::Borrowed(&self.id)
61    }
62    fn account_id(&self) -> &str {
63        &self.account_id
64    }
65    fn provider_id(&self) -> &str {
66        &self.provider_id
67    }
68    fn user_id(&self) -> Cow<'_, str> {
69        Cow::Borrowed(&self.user_id)
70    }
71    fn access_token(&self) -> Option<&str> {
72        self.access_token.as_deref()
73    }
74    fn refresh_token(&self) -> Option<&str> {
75        self.refresh_token.as_deref()
76    }
77    fn id_token(&self) -> Option<&str> {
78        self.id_token.as_deref()
79    }
80    fn access_token_expires_at(&self) -> Option<DateTime<Utc>> {
81        self.access_token_expires_at
82    }
83    fn refresh_token_expires_at(&self) -> Option<DateTime<Utc>> {
84        self.refresh_token_expires_at
85    }
86    fn scope(&self) -> Option<&str> {
87        self.scope.as_deref()
88    }
89    fn password(&self) -> Option<&str> {
90        self.password.as_deref()
91    }
92    fn created_at(&self) -> DateTime<Utc> {
93        self.created_at
94    }
95    fn updated_at(&self) -> DateTime<Utc> {
96        self.updated_at
97    }
98}