Skip to content

Commit

Permalink
v2/auth: add Basic authentication and refactor
Browse files Browse the repository at this point in the history
This adds Basic authentication support.

Along the way, the auth module has been refactored with new enums and
structs to model the supported authentication mechanisms and make the
response header parsing more robust.

***Breaking API changes***

* The `Client::login` and `Client::authenticate` methods have been
  merged into the latter.
* `Client::is_auth` now performs a pure authentication test without
  attempting to authenticate, thus it no longer takes a token.
  • Loading branch information
steveej committed Jul 9, 2020
1 parent f193ad9 commit 5a4ec71
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 184 deletions.
22 changes: 0 additions & 22 deletions examples/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1 @@
extern crate futures;

pub async fn authenticate_client(
mut client: dkregistry::v2::Client,
login_scope: String,
) -> Result<dkregistry::v2::Client, dkregistry::errors::Error> {
if !client.is_v2_supported().await? {
return Err("API v2 not supported".into());
}

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

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

if !client.is_auth(Some(token.token())).await? {
Err("login failed".into())
} else {
println!("logged in!");
Ok(client.set_token(Some(token.token())).clone())
}
}
2 changes: 1 addition & 1 deletion examples/image-labels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async fn run(
let login_scope = format!("repository:{}:pull", image);
let version = dkr_ref.version();

let dclient = common::authenticate_client(client, login_scope).await?;
let dclient = client.authenticate(&[&login_scope]).await?;
let manifest = match dclient.get_manifest(&image, &version).await {
Ok(manifest) => Ok(manifest),
Err(e) => Err(format!("Got error {}", e)),
Expand Down
2 changes: 1 addition & 1 deletion examples/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ async fn run(

let login_scope = format!("repository:{}:pull", image);

let dclient = common::authenticate_client(client, login_scope).await?;
let dclient = client.authenticate(&[&login_scope]).await?;
let manifest = dclient.get_manifest(&image, &version).await?;
let layers_digests = manifest.layers_digests(None)?;

Expand Down
4 changes: 2 additions & 2 deletions examples/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async fn run(
.password(passwd)
.build()?;

let dclient = common::authenticate_client(client, login_scope).await?;
dclient.is_v2_supported().await?;
let dclient = client.authenticate(&[&login_scope]).await?;
dclient.is_auth().await?;
Ok(())
}
2 changes: 1 addition & 1 deletion examples/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async fn run(

let login_scope = format!("repository:{}:pull", image);

let dclient = common::authenticate_client(client, login_scope).await?;
let dclient = client.authenticate(&[&login_scope]).await?;

dclient
.get_tags(&image, Some(7))
Expand Down
2 changes: 1 addition & 1 deletion examples/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async fn run(

let login_scope = "";

let dclient = common::authenticate_client(client, login_scope.to_string()).await?;
let dclient = client.authenticate(&[&login_scope]).await?;
let manifest = dclient.get_manifest(&image, &version).await?;

let layers_digests = manifest.layers_digests(None)?;
Expand Down
Loading

0 comments on commit 5a4ec71

Please sign in to comment.