iota_network/randomness/
auth.rs1use std::{collections::HashSet, sync::Arc};
6
7use anemo_tower::auth::AuthorizeRequest;
8use arc_swap::ArcSwap;
9use bytes::Bytes;
10
11#[derive(Clone, Debug)]
12pub(crate) struct AllowedPeersUpdatable {
13 allowed_peers: Arc<ArcSwap<HashSet<anemo::PeerId>>>,
14}
15
16impl AllowedPeersUpdatable {
17 pub fn new(allowed_peers: Arc<HashSet<anemo::PeerId>>) -> Self {
18 Self {
19 allowed_peers: Arc::new(ArcSwap::new(allowed_peers)),
20 }
21 }
22
23 pub fn update(&self, allowed_peers: Arc<HashSet<anemo::PeerId>>) {
24 self.allowed_peers.store(allowed_peers);
25 }
26}
27
28impl AuthorizeRequest for AllowedPeersUpdatable {
29 fn authorize(&self, request: &mut anemo::Request<Bytes>) -> Result<(), anemo::Response<Bytes>> {
30 use anemo::types::response::{IntoResponse, StatusCode};
31
32 let peer_id = request
33 .peer_id()
34 .ok_or_else(|| StatusCode::InternalServerError.into_response())?;
35
36 if self.allowed_peers.load().contains(peer_id) {
37 Ok(())
38 } else {
39 Err(StatusCode::NotFound.into_response())
40 }
41 }
42}