Mercurial > crates > nonstick
comparison src/module.rs @ 72:47eb242a4f88
Fill out the PamHandle trait.
This updates the PamHandle trait to have methods for each Item,
and implements them on the LibPamHandle.
| author | Paul Fisher <paul@pfish.zone> |
|---|---|
| date | Wed, 04 Jun 2025 03:53:36 -0400 |
| parents | 58f9d2a4df38 |
| children | ac6881304c78 |
comparison
equal
deleted
inserted
replaced
| 71:58f9d2a4df38 | 72:47eb242a4f88 |
|---|---|
| 30 | 30 |
| 31 /// Authenticate the user. | 31 /// Authenticate the user. |
| 32 /// | 32 /// |
| 33 /// This is probably the first thing you want to implement. | 33 /// This is probably the first thing you want to implement. |
| 34 /// In most cases, you will want to get the user and password, | 34 /// In most cases, you will want to get the user and password, |
| 35 /// using [`LibPamHandle::get_user`] and [`LibPamHandle::get_authtok`], | 35 /// using [`PamHandle::get_user`] and [`PamModuleHandle::get_authtok`], |
| 36 /// and verify them against something. | 36 /// and verify them against something. |
| 37 /// | 37 /// |
| 38 /// See [the Module Writer's Guide entry for `pam_sm_authenticate`][mwg] | 38 /// See [the Module Writer's Guide entry for `pam_sm_authenticate`][mwg] |
| 39 /// for more information. | 39 /// for more information. |
| 40 /// | 40 /// |
| 253 /// # fn _do_test(conv: impl Conversation) -> Result<()> { | 253 /// # fn _do_test(conv: impl Conversation) -> Result<()> { |
| 254 /// let mut mux = ConversationMux(conv); | 254 /// let mut mux = ConversationMux(conv); |
| 255 /// let token = mux.masked_prompt("enter your one-time token")?; | 255 /// let token = mux.masked_prompt("enter your one-time token")?; |
| 256 /// # Ok(()) | 256 /// # Ok(()) |
| 257 /// # } | 257 /// # } |
| 258 pub struct ConversationMux<C: Conversation>(pub C); | 258 pub struct ConversationMux<'a, C: Conversation>(pub &'a mut C); |
| 259 | 259 |
| 260 impl<C: Conversation> Conversation for ConversationMux<C> { | 260 impl<C: Conversation> Conversation for ConversationMux<'_, C> { |
| 261 fn send(&mut self, messages: &[Message]) -> Result<Vec<Response>> { | 261 fn send(&mut self, messages: &[Message]) -> Result<Vec<Response>> { |
| 262 self.0.send(messages) | 262 self.0.send(messages) |
| 263 } | 263 } |
| 264 } | 264 } |
| 265 | 265 |
| 266 impl<C: Conversation> ConversationMux<C> { | 266 impl<C: Conversation> ConversationMux<'_, C> { |
| 267 /// Prompts the user for something. | 267 /// Prompts the user for something. |
| 268 pub fn prompt(&mut self, request: &str) -> Result<String> { | 268 pub fn prompt(&mut self, request: &str) -> Result<String> { |
| 269 let resp = self.send(&[Message::Prompt(request)])?.pop(); | 269 let resp = self.send(&[Message::Prompt(request)])?.pop(); |
| 270 match resp { | 270 match resp { |
| 271 Some(Response::Text(s)) => Ok(s), | 271 Some(Response::Text(s)) => Ok(s), |
| 522 panic!("messages is the wrong size ({len})", len = messages.len()) | 522 panic!("messages is the wrong size ({len})", len = messages.len()) |
| 523 } | 523 } |
| 524 } | 524 } |
| 525 } | 525 } |
| 526 | 526 |
| 527 let mut mux = ConversationMux(MuxTester); | 527 let mut tester = MuxTester; |
| 528 | |
| 529 let mut mux = ConversationMux(&mut tester); | |
| 528 assert_eq!("answer", mux.prompt("question").unwrap()); | 530 assert_eq!("answer", mux.prompt("question").unwrap()); |
| 529 assert_eq!( | 531 assert_eq!( |
| 530 SecureString::from("open sesame"), | 532 SecureString::from("open sesame"), |
| 531 mux.masked_prompt("password!").unwrap() | 533 mux.masked_prompt("password!").unwrap() |
| 532 ); | 534 ); |
