1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
use crate::{Handle, SecureChannelTrustInfo, TrustPolicy}; use async_trait::async_trait; use ockam_core::{Address, Result, Routed, Worker}; use ockam_node::Context; use serde::{Deserialize, Serialize}; #[derive(Clone)] pub struct TrustPolicyImpl { handle: Handle, } impl TrustPolicyImpl { pub fn new(handle: Handle) -> Self { TrustPolicyImpl { handle } } } impl TrustPolicyImpl { pub async fn create(ctx: &Context, trust_policy: impl TrustPolicy) -> Result<Address> { let address = Address::random(0); ctx.start_worker(address.clone(), TrustPolicyWorker::new(trust_policy)) .await?; Ok(address) } } impl TrustPolicy for TrustPolicyImpl { fn check(&self, trust_info: &SecureChannelTrustInfo) -> Result<bool> { let response: TrustPolicyResponse = self.handle.call(TrustPolicyRequest { info: trust_info.clone(), })?; Ok(response.res) } } pub struct TrustPolicyWorker<T: TrustPolicy> { trust_policy: T, } impl<T: TrustPolicy> TrustPolicyWorker<T> { pub fn new(trust_policy: T) -> Self { TrustPolicyWorker { trust_policy } } } #[derive(Serialize, Deserialize)] pub struct TrustPolicyRequest { pub info: SecureChannelTrustInfo, } #[derive(Serialize, Deserialize)] pub struct TrustPolicyResponse { pub res: bool, } #[async_trait] impl<T: TrustPolicy> Worker for TrustPolicyWorker<T> { type Message = TrustPolicyRequest; type Context = Context; async fn handle_message( &mut self, ctx: &mut Self::Context, msg: Routed<Self::Message>, ) -> Result<()> { let route = msg.return_route(); let msg = msg.body(); let res = self.trust_policy.check(&msg.info)?; ctx.send(route, TrustPolicyResponse { res }).await?; Ok(()) } }