|
12 | 12 | // |
13 | 13 | // SPDX-License-Identifier: Apache-2.0 |
14 | 14 |
|
| 15 | +use sea_orm::entity::*; |
| 16 | + |
15 | 17 | use super::super::types::WebauthnCredential; |
16 | | -use crate::db::entity::webauthn_credential; |
| 18 | +use crate::{db::entity::webauthn_credential, webauthn::WebauthnError}; |
17 | 19 |
|
18 | 20 | mod create; |
| 21 | +mod get; |
19 | 22 | mod list; |
| 23 | +mod update; |
20 | 24 |
|
21 | 25 | pub use create::create; |
| 26 | +pub use get::find; |
22 | 27 | pub use list::list; |
| 28 | +pub use update::update; |
23 | 29 |
|
24 | | -impl From<webauthn_credential::Model> for WebauthnCredential { |
25 | | - fn from(value: webauthn_credential::Model) -> Self { |
26 | | - Self { |
| 30 | +impl TryFrom<webauthn_credential::Model> for WebauthnCredential { |
| 31 | + type Error = WebauthnError; |
| 32 | + fn try_from(value: webauthn_credential::Model) -> Result<Self, Self::Error> { |
| 33 | + Ok(Self { |
| 34 | + created_at: value.created_at.and_utc(), |
27 | 35 | credential_id: value.credential_id, |
| 36 | + data: serde_json::from_str(&value.passkey)?, |
| 37 | + counter: value.counter.try_into()?, |
28 | 38 | description: value.description, |
29 | | - } |
| 39 | + internal_id: value.id, |
| 40 | + last_used_at: value.last_used_at.map(|x| x.and_utc()), |
| 41 | + r#type: value.r#type.into(), |
| 42 | + updated_at: value.last_updated_at.map(|x| x.and_utc()), |
| 43 | + user_id: value.user_id, |
| 44 | + }) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl TryFrom<WebauthnCredential> for webauthn_credential::ActiveModel { |
| 49 | + type Error = WebauthnError; |
| 50 | + |
| 51 | + fn try_from(value: WebauthnCredential) -> Result<Self, Self::Error> { |
| 52 | + Ok(Self { |
| 53 | + id: if value.internal_id == 0 { |
| 54 | + NotSet |
| 55 | + } else { |
| 56 | + Set(value.internal_id) |
| 57 | + }, |
| 58 | + user_id: Set(value.user_id), |
| 59 | + credential_id: Set(value.credential_id), |
| 60 | + description: value.description.map(Set).unwrap_or(NotSet).into(), |
| 61 | + passkey: Set(serde_json::to_string(&value.data)?), |
| 62 | + counter: Set(value.counter.try_into()?), |
| 63 | + r#type: Set(value.r#type.to_string()), |
| 64 | + aaguid: NotSet, |
| 65 | + created_at: Set(value.created_at.naive_utc()), |
| 66 | + last_used_at: value |
| 67 | + .last_used_at |
| 68 | + .map(|x| Set(Some(x.naive_utc()))) |
| 69 | + .unwrap_or(NotSet), |
| 70 | + last_updated_at: value |
| 71 | + .updated_at |
| 72 | + .map(|x| Set(Some(x.naive_utc()))) |
| 73 | + .unwrap_or(NotSet), |
| 74 | + }) |
30 | 75 | } |
31 | 76 | } |
32 | 77 |
|
@@ -78,13 +123,14 @@ mod tests { |
78 | 123 | .into() |
79 | 124 | } |
80 | 125 |
|
81 | | - pub(super) fn get_mock<S: AsRef<str>>(id: S) -> webauthn_credential::Model { |
| 126 | + pub(super) fn get_mock<S: Into<String>>(id: S) -> webauthn_credential::Model { |
82 | 127 | webauthn_credential::Model { |
83 | 128 | id: 1, |
84 | | - user_id: id.as_ref().to_string(), |
| 129 | + user_id: id.into(), |
85 | 130 | credential_id: "cred".into(), |
86 | 131 | description: Some("fake".into()), |
87 | 132 | passkey: serde_json::to_string(&get_fake_passkey()).unwrap(), |
| 133 | + counter: 0, |
88 | 134 | r#type: "cross-platform".into(), |
89 | 135 | aaguid: Some("aaguid".into()), |
90 | 136 | created_at: NaiveDateTime::default(), |
|
0 commit comments