Mercurial > crates > nonstick
annotate src/conv.rs @ 69:8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
This removes the old `Conversation` type and reworks the FFI types
used for PAM conversations.
This creates safe `TestResponse` and `BinaryResponse` structures in `conv`,
providing a safe way to pass response messages to PAM Conversations.
The internals of these types are allocated on the C heap, as required by PAM.
We also remove the Conversation struct, which was specific to the real PAM
implementation so that we can introduce a better abstraction.
Also splits a new `PamApplicationHandle` trait from `PamHandle`,
for the parts of a PAM handle that are specific to the application side
of a PAM transaction.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Sun, 01 Jun 2025 01:15:04 -0400 |
parents | bbe84835d6db |
children | 9f8381a1c09c |
rev | line source |
---|---|
69
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
1 //! The PAM conversation and associated Stuff. |
60
05cc2c27334f
The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents:
59
diff
changeset
|
2 |
69
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
3 use crate::pam_ffi::{BinaryResponseInner, NulError, TextResponseInner}; |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
4 use std::num::TryFromIntError; |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
5 use std::ops::Deref; |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
6 use std::result::Result as StdResult; |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
7 |
69
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
8 /// An owned text response to a PAM conversation. |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
9 /// |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
10 /// It points to a value on the C heap. |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
11 #[repr(C)] |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
12 struct TextResponse(*mut TextResponseInner); |
63
a7aa5ca0d00d
Move MessageStyle to conv, the only place it is used.
Paul Fisher <paul@pfish.zone>
parents:
62
diff
changeset
|
13 |
69
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
14 impl TextResponse { |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
15 /// Creates a text response. |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
16 pub fn new(text: impl AsRef<str>) -> StdResult<Self, NulError> { |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
17 TextResponseInner::alloc(text).map(Self) |
63
a7aa5ca0d00d
Move MessageStyle to conv, the only place it is used.
Paul Fisher <paul@pfish.zone>
parents:
62
diff
changeset
|
18 } |
a7aa5ca0d00d
Move MessageStyle to conv, the only place it is used.
Paul Fisher <paul@pfish.zone>
parents:
62
diff
changeset
|
19 } |
a7aa5ca0d00d
Move MessageStyle to conv, the only place it is used.
Paul Fisher <paul@pfish.zone>
parents:
62
diff
changeset
|
20 |
69
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
21 impl Deref for TextResponse { |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
22 type Target = TextResponseInner; |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
23 fn deref(&self) -> &Self::Target { |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
24 // SAFETY: We allocated this ourselves, or it was provided by PAM. |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
25 unsafe { &*self.0 } |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
26 } |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
27 } |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
28 |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
29 impl Drop for TextResponse { |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
30 /// Frees an owned response. |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
31 fn drop(&mut self) { |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
32 // SAFETY: We allocated this ourselves, or it was provided by PAM. |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
33 unsafe { TextResponseInner::free(self.0) } |
63
a7aa5ca0d00d
Move MessageStyle to conv, the only place it is used.
Paul Fisher <paul@pfish.zone>
parents:
62
diff
changeset
|
34 } |
a7aa5ca0d00d
Move MessageStyle to conv, the only place it is used.
Paul Fisher <paul@pfish.zone>
parents:
62
diff
changeset
|
35 } |
a7aa5ca0d00d
Move MessageStyle to conv, the only place it is used.
Paul Fisher <paul@pfish.zone>
parents:
62
diff
changeset
|
36 |
69
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
37 /// An owned binary response to a PAM conversation. |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
38 /// |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
39 /// It points to a value on the C heap. |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
40 #[repr(C)] |
69
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
41 struct BinaryResponse(*mut BinaryResponseInner); |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
42 |
69
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
43 impl BinaryResponse { |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
44 /// Creates a binary response with the given data. |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
45 pub fn new(data: impl AsRef<[u8]>, data_type: u8) -> StdResult<Self, TryFromIntError> { |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
46 BinaryResponseInner::alloc(data, data_type).map(Self) |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
47 } |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
48 } |
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
49 |
69
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
50 impl Deref for BinaryResponse { |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
51 type Target = BinaryResponseInner; |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
52 fn deref(&self) -> &Self::Target { |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
53 // SAFETY: We allocated this ourselves, or it was provided by PAM. |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
54 unsafe { &*self.0 } |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
55 } |
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
56 } |
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
57 |
69
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
58 impl Drop for BinaryResponse { |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
59 /// Frees an owned response. |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
60 fn drop(&mut self) { |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
61 // SAFETY: We allocated this ourselves, or it was provided by PAM. |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
62 unsafe { BinaryResponseInner::free(self.0) } |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
63 } |
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
64 } |
69
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
65 |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
66 #[cfg(test)] |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
67 mod test { |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
68 use super::{BinaryResponse, TextResponse}; |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
69 |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
70 #[test] |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
71 fn test_text_response() { |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
72 let resp = TextResponse::new("it's a-me!").unwrap(); |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
73 assert_eq!("it's a-me!", resp.contents().to_str().unwrap()); |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
74 } |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
75 #[test] |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
76 fn test_binary_response() { |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
77 let data = [123, 210, 55]; |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
78 let resp = BinaryResponse::new(&data, 99).unwrap(); |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
79 assert_eq!(&data, resp.contents()); |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
80 } |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
81 } |