use crate::dao::repository::WebauthnCredentialRow;
use crate::error::{GarrisonError, GarrisonResult};
use webauthn_rs::prelude::{AuthenticationResult, Credential, CredentialID, Passkey};
#[derive(Debug, Clone)]
pub struct WebauthnCredential {
pub tenant_id: i64,
pub user_id: String,
pub credential_id: String,
pub sign_count: u32,
pub backup_eligible: bool,
pub backup_state: bool,
pub attestation_format: String,
pub created_at: i64,
pub updated_at: i64,
credential: Credential,
}
fn credential_id_to_base64(id: &CredentialID) -> GarrisonResult<String> {
serde_json::to_string(id)
.map(|s| s.trim_matches('"').to_string())
.map_err(|e| GarrisonError::Internal(format!("webauthn-cred-id-encode::{e}")))
}
impl WebauthnCredential {
pub fn from_ceremony(
tenant_id: i64,
user_id: &str,
credential: Credential,
) -> GarrisonResult<Self> {
let now = chrono::Utc::now().timestamp();
let attestation_format = serde_json::to_string(&credential.attestation_format)
.map_err(|e| GarrisonError::Internal(format!("webauthn-attestation-format::{e}")))?
.trim_matches('"')
.to_string();
let credential_id = credential_id_to_base64(&credential.cred_id)?;
Ok(Self {
tenant_id,
user_id: user_id.to_string(),
sign_count: credential.counter,
backup_eligible: credential.backup_eligible,
backup_state: credential.backup_state,
attestation_format,
credential_id,
created_at: now,
updated_at: now,
credential,
})
}
pub fn from_row(row: WebauthnCredentialRow) -> GarrisonResult<Self> {
let mut credential: Credential = serde_json::from_str(&row.public_key)
.map_err(|e| GarrisonError::Dao(format!("webauthn-cred-row-parse::{e}")))?;
let attestation_format = serde_json::to_string(&credential.attestation_format)
.map_err(|e| GarrisonError::Dao(format!("webauthn-cred-row-format::{e}")))?
.trim_matches('"')
.to_string();
let credential_id = credential_id_to_base64(&credential.cred_id)?;
if credential_id != row.credential_id {
return Err(GarrisonError::Dao(format!(
"webauthn-cred-row-identity-mismatch::credential_id={}",
row.credential_id
)));
}
credential.counter = row.sign_count;
credential.backup_eligible = row.backup_eligible;
credential.backup_state = row.backup_state;
Ok(Self {
tenant_id: row.tenant_id,
user_id: row.user_id,
credential_id,
sign_count: row.sign_count,
backup_eligible: row.backup_eligible,
backup_state: row.backup_state,
attestation_format,
created_at: row.created_at,
updated_at: row.updated_at,
credential,
})
}
pub fn to_row(&self) -> GarrisonResult<WebauthnCredentialRow> {
let public_key = serde_json::to_string(&self.credential)
.map_err(|e| GarrisonError::Internal(format!("webauthn-cred-row-encode::{e}")))?;
Ok(WebauthnCredentialRow {
tenant_id: self.tenant_id,
user_id: self.user_id.clone(),
credential_id: self.credential_id.clone(),
public_key,
sign_count: self.sign_count,
backup_eligible: self.backup_eligible,
backup_state: self.backup_state,
attestation: Some(self.attestation_format.clone()),
created_at: self.created_at,
updated_at: self.updated_at,
})
}
pub fn to_passkey(&self) -> Passkey {
Passkey::from(self.credential.clone())
}
pub fn apply_authentication_result(&mut self, res: &AuthenticationResult) -> bool {
let mut changed = false;
if res.counter() > self.sign_count {
self.sign_count = res.counter();
self.credential.counter = self.sign_count;
changed = true;
}
if res.backup_state() != self.backup_state {
self.backup_state = res.backup_state();
self.credential.backup_state = self.backup_state;
changed = true;
}
if res.backup_eligible() && !self.backup_eligible {
self.backup_eligible = true;
self.credential.backup_eligible = true;
changed = true;
}
changed
}
pub fn belongs_to(&self, tenant_id: i64, user_id: &str) -> bool {
self.tenant_id == tenant_id && self.user_id == user_id
}
#[cfg(all(test, feature = "db-sqlite"))]
pub(crate) fn protocol_credential(&self) -> &Credential {
&self.credential
}
}