Skip to main content

saml_rs/browser/
forms.rs

1use crate::model::EndpointUrl;
2
3/// HTML form field emitted or consumed by browser POST bindings.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct FormField {
6    name: String,
7    value: String,
8}
9
10impl FormField {
11    /// Create a form field.
12    pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
13        Self {
14            name: name.into(),
15            value: value.into(),
16        }
17    }
18
19    /// Field name.
20    pub fn name(&self) -> &str {
21        &self.name
22    }
23
24    /// Field value.
25    pub fn value(&self) -> &str {
26        &self.value
27    }
28
29    pub(crate) fn into_pair(self) -> (String, String) {
30        (self.name, self.value)
31    }
32}
33
34/// Typed auto-submit POST form data.
35#[derive(Debug, Clone, PartialEq, Eq)]
36pub struct PostForm {
37    action: EndpointUrl,
38    fields: Vec<FormField>,
39}
40
41impl PostForm {
42    /// Create a POST form.
43    pub fn new(action: EndpointUrl, fields: Vec<FormField>) -> Self {
44        Self { action, fields }
45    }
46
47    /// Form action URL.
48    pub fn action(&self) -> &EndpointUrl {
49        &self.action
50    }
51
52    /// Hidden fields.
53    pub fn fields(&self) -> &[FormField] {
54        &self.fields
55    }
56
57    /// Return the first field value for a name.
58    pub fn value(&self, name: &str) -> Option<&str> {
59        self.fields
60            .iter()
61            .find(|field| field.name() == name)
62            .map(FormField::value)
63    }
64}