use async_trait::async_trait;
use chrono::{DateTime, Duration, Utc};
use url::Url;
use auths_core::ports::namespace::{
Ecosystem, NamespaceOwnershipProof, NamespaceVerifier, NamespaceVerifyError, PackageName,
PlatformContext, VerificationChallenge, VerificationMethod, VerificationToken,
};
use auths_verifier::CanonicalDid;
pub struct FakeNamespaceVerifier {
pub ecosystem: Ecosystem,
pub should_verify: bool,
pub challenge_instructions: String,
}
impl FakeNamespaceVerifier {
pub fn succeeding(ecosystem: Ecosystem) -> Self {
Self {
ecosystem,
should_verify: true,
challenge_instructions: "Test: complete the verification".to_string(),
}
}
pub fn failing(ecosystem: Ecosystem) -> Self {
Self {
ecosystem,
should_verify: false,
challenge_instructions: "Test: this will fail".to_string(),
}
}
}
#[async_trait]
impl NamespaceVerifier for FakeNamespaceVerifier {
fn ecosystem(&self) -> Ecosystem {
self.ecosystem
}
async fn initiate(
&self,
now: DateTime<Utc>,
package_name: &PackageName,
did: &CanonicalDid,
_platform: &PlatformContext,
) -> Result<VerificationChallenge, NamespaceVerifyError> {
#[allow(clippy::expect_used)]
let token =
VerificationToken::parse("auths-verify-deadbeef01234567").expect("test token is valid");
Ok(VerificationChallenge {
ecosystem: self.ecosystem,
package_name: package_name.clone(),
did: did.clone(),
token,
instructions: self.challenge_instructions.clone(),
expires_at: now + Duration::hours(1),
})
}
async fn verify(
&self,
now: DateTime<Utc>,
package_name: &PackageName,
_did: &CanonicalDid,
_platform: &PlatformContext,
_challenge: &VerificationChallenge,
) -> Result<NamespaceOwnershipProof, NamespaceVerifyError> {
if self.should_verify {
#[allow(clippy::expect_used)]
let proof_url =
Url::parse("https://test.example.com/proof").expect("test URL is valid");
Ok(NamespaceOwnershipProof {
ecosystem: self.ecosystem,
package_name: package_name.clone(),
proof_url,
method: VerificationMethod::ApiOwnership,
verified_at: now,
})
} else {
Err(NamespaceVerifyError::OwnershipNotConfirmed {
ecosystem: self.ecosystem,
package_name: package_name.as_str().to_string(),
})
}
}
}