use std::sync::Arc;
use super::{AuthenticateReply, AuthenticationFailure, AuthenticationScheme, RpcAuthentication};
use crate::Connection;
use derive_deftly::Deftly;
use tor_rpc_connect::auth::RpcAuth;
use tor_rpcbase as rpc;
use tor_rpcbase::templates::*;
#[derive(Debug, serde::Deserialize, Deftly)]
#[derive_deftly(DynMethod)]
#[deftly(rpc(method_name = "auth:authenticate"))]
struct Authenticate {
scheme: AuthenticationScheme,
}
impl rpc::RpcMethod for Authenticate {
type Output = AuthenticateReply;
type Update = rpc::NoUpdates;
}
async fn authenticate_connection(
unauth: Arc<Connection>,
method: Box<Authenticate>,
ctx: Arc<dyn rpc::Context>,
) -> Result<AuthenticateReply, rpc::RpcError> {
match (method.scheme, &unauth.require_auth) {
(AuthenticationScheme::Inherent, RpcAuth::Inherent) => {}
(_, _) => return Err(AuthenticationFailure::IncorrectMethod.into()),
}
let auth = RpcAuthentication {};
let session = {
let mgr = unauth.mgr()?;
mgr.create_session(&auth)
};
let session = ctx.register_owned(session);
Ok(AuthenticateReply { session })
}
rpc::static_rpc_invoke_fn! {
authenticate_connection;
}