-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: convert auth client to access client
- Loading branch information
Showing
7 changed files
with
167 additions
and
68 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use std::collections::HashMap; | ||
use std::time::Duration; | ||
use std::time::Instant; | ||
|
||
use libp2p::PeerId; | ||
use serde::Deserialize; | ||
|
||
const ACCESS_TTL: Duration = Duration::from_secs(10 * 60); | ||
|
||
pub struct AccessClient { | ||
local_id: libp2p::PeerId, | ||
endpoint: reqwest::Url, | ||
client: reqwest::Client, | ||
cache: HashMap<PeerId, (bool, Instant)>, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct AccessClientResponse { | ||
data: bool, | ||
} | ||
|
||
impl AccessClient { | ||
pub fn new(local_id: libp2p::PeerId, endpoint: reqwest::Url) -> AccessClient { | ||
AccessClient { | ||
local_id, | ||
endpoint, | ||
client: reqwest::Client::new(), | ||
cache: HashMap::default(), | ||
} | ||
} | ||
|
||
async fn request_endpoint(&mut self, peer: &PeerId) -> Result<bool, reqwest::Error> { | ||
let url = self.endpoint.join("access-control").unwrap(); | ||
let params = [ | ||
("device", self.local_id.to_string()), | ||
("token", peer.to_string()), | ||
]; | ||
|
||
let response = self | ||
.client | ||
.get(url) | ||
.query(¶ms) | ||
.send() | ||
.await? | ||
.json::<AccessClientResponse>() | ||
.await?; | ||
|
||
Ok(response.data) | ||
} | ||
|
||
pub async fn is_valid(&mut self, peer: &PeerId) -> bool { | ||
self.cache | ||
.retain(|_, (_, created)| created.elapsed() < ACCESS_TTL); | ||
|
||
if let Some((valid, _)) = self.cache.get(peer) { | ||
return *valid; | ||
} | ||
|
||
match self.request_endpoint(peer).await { | ||
Err(e) => { | ||
tracing::error!( | ||
"error while requesting access endpoint (will return false) for {}: {}", | ||
peer, | ||
e | ||
); | ||
false | ||
} | ||
|
||
Ok(valid) => { | ||
self.cache.insert(*peer, (valid, Instant::now())); | ||
valid | ||
} | ||
} | ||
} | ||
|
||
pub fn expire(&mut self, token: &PeerId) { | ||
tracing::debug!("expire token: {} from cache", token); | ||
self.cache.remove(token); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.