1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//! [Official doc](https://developers.facebook.com/docs/development/create-an-app/app-dashboard/data-deletion-callback)

use chrono::{serde::ts_seconds, DateTime, Utc};
use serde::Deserialize;
use serde_aux::field_attributes::deserialize_number_from_string;

use crate::ParseError;

#[derive(Deserialize, Debug, Clone)]
pub struct Payload {
    #[serde(deserialize_with = "deserialize_number_from_string")]
    pub user_id: u64,
    pub algorithm: String,
    #[serde(with = "ts_seconds")]
    pub issued_at: DateTime<Utc>,
    #[serde(with = "ts_seconds")]
    pub expires: DateTime<Utc>,
}
impl crate::Payload for Payload {
    fn algorithm(&self) -> Option<&str> {
        Some(&self.algorithm)
    }
}

pub fn parse(signed_request: &str, app_secret: &str) -> Result<Payload, ParseError> {
    crate::parse(signed_request, app_secret)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_de() {
        let json = r#"{"user_id":"218471","algorithm":"HMAC-SHA256","issued_at":1291836800,"expires":1291840400}"#;

        match serde_json::from_str::<Payload>(json) {
            Ok(payload) => {
                assert_eq!(payload.user_id, 218471);
                assert_eq!(payload.algorithm, "HMAC-SHA256");
                assert_eq!(payload.issued_at.timestamp(), 1291836800);
                assert_eq!(payload.expires.timestamp(), 1291840400);
            }
            Err(err) => panic!("{}", err),
        }
    }
}