Skip to content

Commit

Permalink
feat: Add support for Azure Container Registry authentication (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryantbiggs authored Sep 4, 2024
1 parent f76167a commit bddfcbd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub enum Error {
ReferenceParse(#[from] crate::reference::ReferenceParseError),
#[error("requested operation requires that credentials are available")]
NoCredentials,
#[error("did not receive auth token")]
NoTokenReceived,
}

pub type Result<T> = std::result::Result<T, Error>;
Expand Down
29 changes: 28 additions & 1 deletion src/v2/auth.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::convert::{TryFrom, TryInto};

use log::{trace, warn};
use regex_lite::Regex;
use reqwest::{header::HeaderValue, RequestBuilder, StatusCode, Url};
Expand Down Expand Up @@ -34,6 +36,31 @@ pub struct BearerAuth {
refresh_token: Option<String>,
}

/// Used to support different response schemas of Bearer HTTP Authentication
#[derive(Debug, Clone, Default, Deserialize)]
pub struct MultiTokenBearerAuth {
token: Option<String>,
access_token: Option<String>,
expires_in: Option<u32>,
issued_at: Option<String>,
refresh_token: Option<String>,
}

impl TryFrom<MultiTokenBearerAuth> for BearerAuth {
type Error = Error;

fn try_from(value: MultiTokenBearerAuth) -> std::result::Result<Self, Error> {
let t = value.token.or(value.access_token).ok_or(Error::NoTokenReceived)?;

Ok(Self {
token: t,
expires_in: value.expires_in,
issued_at: value.issued_at,
refresh_token: value.refresh_token,
})
}
}

impl BearerAuth {
async fn try_from_header_content(
client: Client,
Expand Down Expand Up @@ -66,7 +93,7 @@ impl BearerAuth {
return Err(Error::UnexpectedHttpStatus(status));
}

let bearer_auth = r.json::<BearerAuth>().await?;
let bearer_auth: BearerAuth = r.json::<MultiTokenBearerAuth>().await?.try_into()?;

match bearer_auth.token.as_str() {
"unauthenticated" | "" => return Err(Error::InvalidAuthToken(bearer_auth.token)),
Expand Down

0 comments on commit bddfcbd

Please sign in to comment.