use async_trait::async_trait;
use axum::body::Bytes;
use axum::extract::OriginalUri;
use axum::routing::{MethodRouter, get, post};
use axum::{Extension, Json, Router};
use kcode_k1_http::{
CanonicalUsername, Config, Identity, IdentityError, IdentityProvider, K1Http, Principal,
RegistrationPrincipal,
};
use kcode_k1_http_replay::{ReplayConfig, ReplayWindow};
use kcode_k1_http_testkit::{Candidate, Fixture, LookupBlock};
use serde::Deserialize;
use std::sync::Arc;
struct Adapter;
struct Provider {
identities: Vec<(CanonicalUsername, Identity)>,
blocked: Option<(CanonicalUsername, LookupBlock)>,
}
#[async_trait]
impl IdentityProvider for Provider {
async fn lookup(
&self,
username: &CanonicalUsername,
) -> Result<Option<Identity>, IdentityError> {
if let Some((blocked_username, block)) = &self.blocked
&& blocked_username == username
{
let release = block.release.notified();
tokio::pin!(release);
release.as_mut().enable();
block.entered.notify_one();
release.await;
}
Ok(self
.identities
.iter()
.find(|(candidate, _)| candidate == username)
.map(|(_, identity)| *identity))
}
}
#[derive(Deserialize)]
struct Known {
known: String,
}
async fn register(Extension(principal): Extension<RegistrationPrincipal>) -> String {
principal.username().as_str().to_owned()
}
async fn whoami(Extension(principal): Extension<Principal>) -> String {
principal.username().as_str().to_owned()
}
async fn target(OriginalUri(uri): OriginalUri) -> String {
uri.path_and_query()
.map(|value| value.as_str())
.unwrap_or("/")
.to_owned()
}
async fn echo(body: Bytes) -> Bytes {
body
}
async fn json(Json(value): Json<Known>) -> String {
value.known
}
async fn prepare(fixture: Fixture) -> Result<K1Http, String> {
let identities = fixture
.identities
.into_iter()
.map(|identity| {
CanonicalUsername::parse(&identity.username)
.map(|username| {
(
username,
Identity::new(identity.user_id, identity.public_key),
)
})
.map_err(|_| "invalid fixture identity".to_owned())
})
.collect::<Result<Vec<_>, _>>()?;
let blocked = fixture
.blocked_lookup
.map(|block| {
CanonicalUsername::parse(&block.username)
.map(|username| (username, block))
.map_err(|_| "invalid blocked identity".to_owned())
})
.transpose()?;
let replay = ReplayWindow::open(ReplayConfig {
epoch_file: fixture.epoch_file,
max_nonces_per_epoch: fixture.max_nonces_per_epoch,
})
.await
.map_err(|_| "replay unavailable".to_owned())?;
K1Http::new(
Config {
server_id: fixture.server_id,
public_origin: fixture.public_origin,
max_body_bytes: fixture.max_body_bytes,
},
replay,
Arc::new(Provider {
identities,
blocked,
}),
)
.map_err(|_| "configuration invalid".to_owned())
}
#[async_trait]
impl Candidate for Adapter {
async fn open(&self, fixture: Fixture) -> Result<Router, String> {
let app = prepare(fixture).await?;
let authenticated = Router::new()
.route("/whoami", get(whoami))
.route("/target", get(target))
.route("/echo", post(echo))
.route("/json", post(json));
Ok(app.router(post(register), kcode_k1_terms::endpoint(), authenticated))
}
}
#[async_trait]
impl kcode_k1_http_integration_testkit::Candidate for Adapter {
async fn compose(
&self,
fixture: Fixture,
registration: MethodRouter,
terms: MethodRouter,
authenticated: Router,
) -> Result<Router, String> {
Ok(prepare(fixture)
.await?
.router(registration, terms, authenticated))
}
}
#[tokio::test]
async fn integration() {
kcode_k1_http_integration_testkit::verify(&Adapter).await;
}