xmpp_parsers/
confirm.rs

1// Copyright (c) 2025 Adrien Destugues <pulkomandy@pulkomandy.tk>
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7use xso::{AsXml, FromXml};
8
9use crate::iq::IqGetPayload;
10use crate::message::MessagePayload;
11use crate::ns;
12
13/// XEP-0070 "confirm" element used to request user confirmation before accessing a protected
14/// HTTP resource, or as a reply to such request.
15///
16/// This can be used as:
17/// - A message payload (when the request is sent to a bare JID or when receiving the reply)
18/// - A Set Iq payload (when the request is sent to a full JID as an Iq). The confirm element may
19///   also be present in responses, but should be ignored (the IQ id is used to identify the
20///   request in that case).
21#[derive(FromXml, AsXml, PartialEq, Eq, Hash, Debug, Clone)]
22#[xml(namespace = ns::HTTP_AUTH, name = "confirm")]
23pub struct Confirm {
24    /// HTTP method used to access the resource
25    #[xml(attribute)]
26    pub method: String,
27
28    /// URL being accessed and guarded by the authentication request
29    #[xml(attribute)]
30    pub url: String,
31
32    /// Identifier of the authentication request
33    #[xml(attribute)]
34    pub id: String,
35}
36
37impl IqGetPayload for Confirm {}
38impl MessagePayload for Confirm {}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43    use crate::ns;
44    use minidom::Element;
45
46    #[cfg(target_pointer_width = "32")]
47    #[test]
48    fn test_size() {
49        assert_size!(Confirm, 36);
50    }
51
52    #[cfg(target_pointer_width = "64")]
53    #[test]
54    fn test_size() {
55        assert_size!(Confirm, 72);
56    }
57
58    #[test]
59    fn test_simple() {
60        let elem: Element = "<confirm xmlns='http://jabber.org/protocol/http-auth' \
61            id='a7374jnjlalasdf82' method='GET' \
62            url='https://files.shakespeare.lit:9345/missive.html'/>"
63            .parse()
64            .unwrap();
65        let confirm = Confirm::try_from(elem).unwrap();
66        assert_eq!(confirm.method, "GET");
67        assert_eq!(confirm.id, "a7374jnjlalasdf82");
68        assert_eq!(
69            confirm.url,
70            "https://files.shakespeare.lit:9345/missive.html"
71        );
72    }
73
74    #[test]
75    fn test_serialise() {
76        let confirm = Confirm {
77            method: "GET".to_string(),
78            url: "https://files.shakespeare.lit/".to_string(),
79            id: "sesame".to_string(),
80        };
81        let elem: Element = confirm.into();
82        assert!(elem.is("confirm", ns::HTTP_AUTH));
83    }
84}