parco_ws_security/
timestamp.rs

1use chrono::{DateTime, Duration, SecondsFormat, Utc};
2use parco_xml::xml;
3
4use crate::crypto::WSSUId;
5
6/// WS-Security timestamp defining message creation and expiration.
7#[derive(Clone, Debug)]
8pub struct Timestamp {
9    /// UTC time when the message was created.
10    pub created: DateTime<Utc>,
11    /// UTC time when the message expires.
12    pub expires: DateTime<Utc>,
13    /// the id used for the wssu:Id field, from [`WSSUId`](crate::crypto::WSSUId)
14    pub wssu_id: WSSUId,
15}
16
17impl Timestamp {
18    /// Creates a timestamp valid for the given duration from now. and a id from [`WSSUId`](crate::crypto::WSSUId)
19    pub fn now(expires_in: Duration, wssu_id: WSSUId) -> Self {
20        let now = Utc::now();
21        Self {
22            created: now,
23            expires: now + expires_in,
24            wssu_id,
25        }
26    }
27}
28
29xml! {
30    use Timestamp;
31
32    @ns {
33        wssu = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
34    }
35
36    wssu:Timestamp wssu:Id=(self.wssu_id.no_hash()) {
37        wssu:Created { (self.created.to_rfc3339_opts(SecondsFormat::Millis, true)) }
38        wssu:Expires { (self.expires.to_rfc3339_opts(SecondsFormat::Millis, true)) }
39    }
40}