Skip to content

Commit

Permalink
Do not cache token
Browse files Browse the repository at this point in the history
  • Loading branch information
termoshtt committed Jun 4, 2024
1 parent 60c3a6a commit 181a14c
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions ocipkg/src/distribution/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::distribution::*;
use anyhow::{bail, ensure, Result};
use anyhow::{bail, Result};
use oci_spec::{distribution::*, image::*};
use url::Url;

Expand All @@ -12,8 +12,6 @@ pub struct Client {
name: Name,
/// Loaded authentication info from filesystem
auth: StoredAuth,
/// Cached token
token: Option<String>,
}

impl Client {
Expand All @@ -24,7 +22,6 @@ impl Client {
url,
name,
auth,
token: None,
})
}

Expand All @@ -37,21 +34,14 @@ impl Client {
}

fn call(&mut self, req: ureq::Request) -> Result<ureq::Response> {
if self.token.is_none() {
// Try get token
let try_req = req.clone();
let challenge = match try_req.call() {
Ok(res) => return Ok(res),
Err(e) => AuthChallenge::try_from(e)?,
};
self.token = Some(self.auth.challenge(&challenge)?);
}
ensure!(self.token.is_some());
let try_req = req.clone();
let challenge = match try_req.call() {
Ok(res) => return Ok(res),
Err(e) => AuthChallenge::try_from(e)?,
};
let token = self.auth.challenge(&challenge)?;
Ok(req
.set(
"Authorization",
&format!("Bearer {}", self.token.as_ref().unwrap()),
)
.set("Authorization", &format!("Bearer {}", token))
.call()?)
}

Expand Down

0 comments on commit 181a14c

Please sign in to comment.