auth_test_demo/
auth_test_demo.rs1use docker_image_pusher::{error::Result, registry::RegistryClientBuilder};
6use std::env;
7
8#[tokio::main]
9async fn main() -> Result<()> {
10 println!("🔐 Docker Registry API v2 Authentication Test");
11 println!("==============================================");
12
13 let registry = env::var("DOCKER_REGISTRY")
15 .unwrap_or_else(|_| "https://registry.cn-beijing.aliyuncs.com".to_string());
16 let repository = env::var("DOCKER_REPOSITORY").unwrap_or_else(|_| "yoce/cblt".to_string());
17
18 println!("📋 Configuration:");
19 println!(" Registry: {}", registry);
20 println!(" Repository: {}", repository);
21 println!();
22
23 println!("🌐 Building Registry Client...");
25 let client = RegistryClientBuilder::new(registry.clone())
26 .with_timeout(300)
27 .with_verbose(true)
28 .build()?;
29 println!("✅ Registry Client built successfully");
30
31 println!();
33 println!("🔍 Test 1: Testing registry authentication challenge...");
34 let auth = docker_image_pusher::registry::auth::Auth::new();
35 let output = docker_image_pusher::logging::Logger::new(true);
36
37 match auth
39 .authenticate_with_registry(®istry, &repository, None, None, &output)
40 .await
41 {
42 Ok(token) => {
43 if let Some(token) = token {
44 println!(
45 "✅ Received authentication token: {}...",
46 &token[..20.min(token.len())]
47 );
48 } else {
49 println!("ℹ️ Registry does not require authentication");
50 }
51 }
52 Err(e) => {
53 println!("❌ Authentication test failed: {}", e);
54 println!(" This is expected if the registry requires credentials");
55 }
56 }
57
58 if let (Ok(username), Ok(password)) = (env::var("DOCKER_USERNAME"), env::var("DOCKER_PASSWORD"))
60 {
61 println!();
62 println!("🔍 Test 2: Testing with provided credentials...");
63 println!(" Username: {}", username);
64
65 match auth
66 .authenticate_with_registry(
67 ®istry,
68 &repository,
69 Some(&username),
70 Some(&password),
71 &output,
72 )
73 .await
74 {
75 Ok(token) => {
76 if let Some(token) = token {
77 println!("✅ Successfully authenticated with credentials");
78 println!(" Token: {}...", &token[..50.min(token.len())]);
79
80 println!();
82 println!("🔍 Test 3: Testing token with manifest access...");
83 match client
84 .pull_manifest(&repository, "yoce", &Some(token))
85 .await
86 {
87 Ok(manifest) => {
88 println!("✅ Successfully pulled manifest using token");
89 println!(" Manifest size: {} bytes", manifest.len());
90 }
91 Err(e) => {
92 println!("❌ Failed to pull manifest with token: {}", e);
93 }
94 }
95 } else {
96 println!("ℹ️ Authentication successful but no token required");
97 }
98 }
99 Err(e) => {
100 println!("❌ Authentication with credentials failed: {}", e);
101 }
102 }
103 } else {
104 println!();
105 println!("ℹ️ No credentials provided via DOCKER_USERNAME/DOCKER_PASSWORD");
106 println!(" Set these environment variables to test credential-based authentication");
107 }
108
109 println!();
110 println!("🏁 Authentication test completed");
111
112 Ok(())
113}