-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathauthentication.rs
More file actions
85 lines (71 loc) · 3.02 KB
/
authentication.rs
File metadata and controls
85 lines (71 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use cups_rs::{
auth::{set_password_callback, get_password, do_authentication},
get_destination, create_job, Result,
};
use std::io::{self, Write};
fn main() -> Result<()> {
println!("CUPS Authentication Example");
// Set up a password callback for GUI-style authentication
println!("Setting up password callback...");
set_password_callback(Some(Box::new(|prompt, _http, method, resource| {
println!("Authentication required!");
println!("Prompt: {}", prompt);
println!("Method: {}", method);
println!("Resource: {}", resource);
print!("Enter password (or 'q' to quit): ");
io::stdout().flush().unwrap();
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
let input = input.trim();
if input == "q" || input.is_empty() {
println!("Authentication cancelled");
None
} else {
Some(input.to_string())
}
})))?;
// Test the password callback directly
println!("\nTesting password callback directly...");
match get_password("Test prompt:", None, "GET", "/test") {
Some(password) => println!("Got password: {}", "*".repeat(password.len())),
None => println!("No password provided"),
}
// Try to access a printer that might require authentication
let printer_name = std::env::args()
.nth(1)
.unwrap_or_else(|| "PDF".to_string());
println!("\nTrying to access printer: {}", printer_name);
match get_destination(&printer_name) {
Ok(destination) => {
println!("Successfully connected to: {}", destination.full_name());
// Try to create a job (this might trigger authentication)
println!("Attempting to create a job...");
match create_job(&destination, "Authentication test job") {
Ok(job) => {
println!("Job created successfully: ID {}", job.id);
}
Err(e) => {
println!("Job creation failed: {}", e);
// Try manual authentication
println!("Attempting manual authentication...");
match do_authentication(None, "POST", "/") {
Ok(_) => println!("Manual authentication successful"),
Err(auth_err) => println!("Manual authentication failed: {}", auth_err),
}
}
}
}
Err(e) => {
println!("Failed to access printer: {}", e);
}
}
// Test removing the callback
println!("\nRemoving password callback...");
set_password_callback(None)?;
match get_password("Test prompt after removal:", None, "GET", "/test") {
Some(password) => println!("Unexpected password: {}", password),
None => println!("Callback correctly removed - no password provided"),
}
println!("\nAuthentication example completed!");
Ok(())
}