Skip to content

Commit

Permalink
v2/auth: add authentication helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
steveej committed Jan 29, 2020
1 parent b9cfe09 commit 785f23a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 40 deletions.
20 changes: 20 additions & 0 deletions src/v2/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,26 @@ impl Client {
_ => Err(format!("is_auth: wrong HTTP status '{}'", status).into()),
}
}

pub async fn authenticate(mut self, login_scope: String) -> Result<Self> {
if !self.is_v2_supported().await? {
return Err("API v2 not supported".into());
}

if self.is_auth(None).await? {
return Ok(self);
}

let token = self.login(&[login_scope.as_str()]).await?;

if !self.is_auth(Some(token.token())).await? {
Err("login failed".into())
} else {
trace!("login succeeded");
self.set_token(Some(token.token()));
Ok(self)
}
}
}

/// This parses a Www-Authenticate header of value Bearer.
Expand Down
53 changes: 13 additions & 40 deletions tests/net/quay_io/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use dkregistry::mediatypes::MediaTypes;
use dkregistry::v2::authenticate_client;
use futures::future::FutureExt;
use futures::future::TryFutureExt;
use futures::prelude::*;
Expand Down Expand Up @@ -32,16 +31,16 @@ fn common_init(
};

let dclient = runtime
.block_on(authenticate_client(
.block_on(
dkregistry::v2::Client::configure()
.registry(REGISTRY)
.insecure_registry(false)
.username(user)
.password(password)
.build()
.unwrap(),
login_scope,
))
.unwrap()
.authenticate(login_scope),
)
.unwrap();

Some((runtime, dclient))
Expand Down Expand Up @@ -217,31 +216,18 @@ fn test_quayio_has_manifest() {
assert_eq!(has_manifest, Some(MediaTypes::ManifestV2S1Signed));
}

#[cfg(feature = "test-net-private")]
#[test]
fn test_quayio_auth_manifest() {
let image = "steveej/cincinnati-test";
let reference = "0.0.1";
let login_scope = format!("repository:{}:pull", image);
let (user, password) = match get_env() {
Some(t) => t,
None => return,
let (mut runtime, dclient) = if let Some(x) = common_init(Some(&login_scope)) {
x
} else {
return;
};

let mut runtime = Runtime::new().unwrap();

let dclient = runtime
.block_on(authenticate_client(
dkregistry::v2::Client::configure()
.registry(REGISTRY)
.insecure_registry(false)
.username(Some(user))
.password(Some(password))
.build()
.unwrap(),
login_scope,
))
.unwrap();

let fut_has_manifest = dclient.has_manifest(image, reference, None);

let has_manifest = runtime.block_on(fut_has_manifest).unwrap();
Expand Down Expand Up @@ -275,25 +261,12 @@ fn test_quayio_auth_layer_blob() {
let layer0_len: usize = 198;

let login_scope = format!("repository:{}:pull", image);
let (user, password) = match get_env() {
Some(t) => t,
None => return,
let (mut runtime, dclient) = if let Some(x) = common_init(Some(&login_scope)) {
x
} else {
return;
};

let mut runtime = Runtime::new().unwrap();
let dclient = runtime
.block_on(authenticate_client(
dkregistry::v2::Client::configure()
.registry(REGISTRY)
.insecure_registry(false)
.username(Some(user))
.password(Some(password))
.build()
.unwrap(),
login_scope,
))
.unwrap();

let fut_layer0_blob = async {
let digest = dclient
.get_manifest(image, reference)
Expand Down

0 comments on commit 785f23a

Please sign in to comment.