pub struct PolicyBuilder { /* private fields */ }Expand description
A builder for conveniently defining a policy.
It is highly recommended to use the Self::new() (default) value
to create a policy with all options enabled, because each platform acts differently
when being requested to enable/disable various authentication methods.
Enabling all options is the safest way to ensure that the authentication prompt
will be displayed correctly on all platforms.
On Linux, at least one polkit action ID MUST be explicitly provided.
If not set (or empty), build() will return None.
Implementations§
Source§impl PolicyBuilder
impl PolicyBuilder
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Returns a new policy with sane defaults.
Examples found in repository?
52fn main() {
53 let context = Context::new(());
54 let mut policy = PolicyBuilder::new()
55 // On Linux You need to set action_ids.
56 // See: ./org.robius.authentication.policy file settings and (README: "Usage on Linux").
57 .action_ids([
58 "org.robius.authentication",
59 "org.robius.authentication.settings",
60 ])
61 .biometrics(Some(BiometricStrength::Strong))
62 .password(true)
63 .companion(true)
64 .build()
65 .unwrap();
66
67 if let Err(e) = policy.set_action_id("org.robius.authentication.settings") {
68 eprintln!("Invalid action_id: {:?}", e);
69 return;
70 }
71
72 let (tx, rx) = mpsc::channel();
73
74 // Start authentication. `res` only indicates whether the request was successfully
75 // initiated; the final result is delivered via the callback.
76 let res = context.authenticate(TEXT, &policy, move |result| {
77 let _ = tx.send(result);
78 });
79
80 if let Err(e) = res {
81 eprintln!("Failed to start authentication: {:?}", e);
82 return;
83 }
84
85 // Block until the callback produces a result.
86 match rx.recv() {
87 Ok(Ok(_)) => println!("Authentication successful"),
88 Ok(Err(e)) => println!("Authentication failed: {:?}", e),
89 Err(e) => eprintln!("Failed to receive auth result: {:?}", e),
90 }
91}Sourcepub fn action_ids<I, S>(self, ids: I) -> Self
pub fn action_ids<I, S>(self, ids: I) -> Self
Sets the list of action identifiers that require authorization.
On Linux systems, these map to polkit action_ids, which represent
specific privileged operations (such as modifying system settings or
powering off the device). The authentication backend uses the current
action ID to determine whether the action is permitted and whether
user authentication is required.
The first entry is used as the default action ID. To switch between
multiple action IDs at runtime on Linux, build a policy once and call
Policy::set_action_id before each authentication. The setter
validates against this list.
This only has an effect on linux.
Examples found in repository?
52fn main() {
53 let context = Context::new(());
54 let mut policy = PolicyBuilder::new()
55 // On Linux You need to set action_ids.
56 // See: ./org.robius.authentication.policy file settings and (README: "Usage on Linux").
57 .action_ids([
58 "org.robius.authentication",
59 "org.robius.authentication.settings",
60 ])
61 .biometrics(Some(BiometricStrength::Strong))
62 .password(true)
63 .companion(true)
64 .build()
65 .unwrap();
66
67 if let Err(e) = policy.set_action_id("org.robius.authentication.settings") {
68 eprintln!("Invalid action_id: {:?}", e);
69 return;
70 }
71
72 let (tx, rx) = mpsc::channel();
73
74 // Start authentication. `res` only indicates whether the request was successfully
75 // initiated; the final result is delivered via the callback.
76 let res = context.authenticate(TEXT, &policy, move |result| {
77 let _ = tx.send(result);
78 });
79
80 if let Err(e) = res {
81 eprintln!("Failed to start authentication: {:?}", e);
82 return;
83 }
84
85 // Block until the callback produces a result.
86 match rx.recv() {
87 Ok(Ok(_)) => println!("Authentication successful"),
88 Ok(Err(e)) => println!("Authentication failed: {:?}", e),
89 Err(e) => eprintln!("Failed to receive auth result: {:?}", e),
90 }
91}Sourcepub fn biometrics(self, strength: Option<BiometricStrength>) -> Self
pub fn biometrics(self, strength: Option<BiometricStrength>) -> Self
Configures biometric authentication with the given strength.
The strength only has an effect on Android, see BiometricStrength
for more details.
Examples found in repository?
52fn main() {
53 let context = Context::new(());
54 let mut policy = PolicyBuilder::new()
55 // On Linux You need to set action_ids.
56 // See: ./org.robius.authentication.policy file settings and (README: "Usage on Linux").
57 .action_ids([
58 "org.robius.authentication",
59 "org.robius.authentication.settings",
60 ])
61 .biometrics(Some(BiometricStrength::Strong))
62 .password(true)
63 .companion(true)
64 .build()
65 .unwrap();
66
67 if let Err(e) = policy.set_action_id("org.robius.authentication.settings") {
68 eprintln!("Invalid action_id: {:?}", e);
69 return;
70 }
71
72 let (tx, rx) = mpsc::channel();
73
74 // Start authentication. `res` only indicates whether the request was successfully
75 // initiated; the final result is delivered via the callback.
76 let res = context.authenticate(TEXT, &policy, move |result| {
77 let _ = tx.send(result);
78 });
79
80 if let Err(e) = res {
81 eprintln!("Failed to start authentication: {:?}", e);
82 return;
83 }
84
85 // Block until the callback produces a result.
86 match rx.recv() {
87 Ok(Ok(_)) => println!("Authentication successful"),
88 Ok(Err(e)) => println!("Authentication failed: {:?}", e),
89 Err(e) => eprintln!("Failed to receive auth result: {:?}", e),
90 }
91}Sourcepub fn password(self, password: bool) -> Self
pub fn password(self, password: bool) -> Self
Sets whether the policy supports passwords.
Examples found in repository?
52fn main() {
53 let context = Context::new(());
54 let mut policy = PolicyBuilder::new()
55 // On Linux You need to set action_ids.
56 // See: ./org.robius.authentication.policy file settings and (README: "Usage on Linux").
57 .action_ids([
58 "org.robius.authentication",
59 "org.robius.authentication.settings",
60 ])
61 .biometrics(Some(BiometricStrength::Strong))
62 .password(true)
63 .companion(true)
64 .build()
65 .unwrap();
66
67 if let Err(e) = policy.set_action_id("org.robius.authentication.settings") {
68 eprintln!("Invalid action_id: {:?}", e);
69 return;
70 }
71
72 let (tx, rx) = mpsc::channel();
73
74 // Start authentication. `res` only indicates whether the request was successfully
75 // initiated; the final result is delivered via the callback.
76 let res = context.authenticate(TEXT, &policy, move |result| {
77 let _ = tx.send(result);
78 });
79
80 if let Err(e) = res {
81 eprintln!("Failed to start authentication: {:?}", e);
82 return;
83 }
84
85 // Block until the callback produces a result.
86 match rx.recv() {
87 Ok(Ok(_)) => println!("Authentication successful"),
88 Ok(Err(e)) => println!("Authentication failed: {:?}", e),
89 Err(e) => eprintln!("Failed to receive auth result: {:?}", e),
90 }
91}Sourcepub fn companion(self, companion: bool) -> Self
pub fn companion(self, companion: bool) -> Self
Sets whether the policy supports authentication via a proximity companion device, e.g., Apple Watch.
This only has an effect on iOS and macOS.
Examples found in repository?
52fn main() {
53 let context = Context::new(());
54 let mut policy = PolicyBuilder::new()
55 // On Linux You need to set action_ids.
56 // See: ./org.robius.authentication.policy file settings and (README: "Usage on Linux").
57 .action_ids([
58 "org.robius.authentication",
59 "org.robius.authentication.settings",
60 ])
61 .biometrics(Some(BiometricStrength::Strong))
62 .password(true)
63 .companion(true)
64 .build()
65 .unwrap();
66
67 if let Err(e) = policy.set_action_id("org.robius.authentication.settings") {
68 eprintln!("Invalid action_id: {:?}", e);
69 return;
70 }
71
72 let (tx, rx) = mpsc::channel();
73
74 // Start authentication. `res` only indicates whether the request was successfully
75 // initiated; the final result is delivered via the callback.
76 let res = context.authenticate(TEXT, &policy, move |result| {
77 let _ = tx.send(result);
78 });
79
80 if let Err(e) = res {
81 eprintln!("Failed to start authentication: {:?}", e);
82 return;
83 }
84
85 // Block until the callback produces a result.
86 match rx.recv() {
87 Ok(Ok(_)) => println!("Authentication successful"),
88 Ok(Err(e)) => println!("Authentication failed: {:?}", e),
89 Err(e) => eprintln!("Failed to receive auth result: {:?}", e),
90 }
91}Sourcepub fn wrist_detection(self, wrist_detection: bool) -> Self
pub fn wrist_detection(self, wrist_detection: bool) -> Self
Sets whether the policy requires the companion device (Apple Watch) to be on the user’s wrist.
This only has an effect on Apple watchOS.
Sourcepub fn build(self) -> Option<Policy>
pub fn build(self) -> Option<Policy>
Constructs the policy.
Returns None if the specified configuration is not valid for the
current target.
Examples found in repository?
52fn main() {
53 let context = Context::new(());
54 let mut policy = PolicyBuilder::new()
55 // On Linux You need to set action_ids.
56 // See: ./org.robius.authentication.policy file settings and (README: "Usage on Linux").
57 .action_ids([
58 "org.robius.authentication",
59 "org.robius.authentication.settings",
60 ])
61 .biometrics(Some(BiometricStrength::Strong))
62 .password(true)
63 .companion(true)
64 .build()
65 .unwrap();
66
67 if let Err(e) = policy.set_action_id("org.robius.authentication.settings") {
68 eprintln!("Invalid action_id: {:?}", e);
69 return;
70 }
71
72 let (tx, rx) = mpsc::channel();
73
74 // Start authentication. `res` only indicates whether the request was successfully
75 // initiated; the final result is delivered via the callback.
76 let res = context.authenticate(TEXT, &policy, move |result| {
77 let _ = tx.send(result);
78 });
79
80 if let Err(e) = res {
81 eprintln!("Failed to start authentication: {:?}", e);
82 return;
83 }
84
85 // Block until the callback produces a result.
86 match rx.recv() {
87 Ok(Ok(_)) => println!("Authentication successful"),
88 Ok(Err(e)) => println!("Authentication failed: {:?}", e),
89 Err(e) => eprintln!("Failed to receive auth result: {:?}", e),
90 }
91}