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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use core::time::Duration;
pub const LONG_LIVED_USER_ACCESS_TOKEN_LIFETIME: Duration = Duration::from_secs(3600 * 24 * 60);
pub const SHORT_LIVED_USER_ACCESS_TOKEN_LIFETIME_MIN: Duration = Duration::from_secs(3600);
pub const SHORT_LIVED_USER_ACCESS_TOKEN_LIFETIME_MAX: Duration = Duration::from_secs(3600 * 2);
wrapping_macro::wrapping_string! {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LongLivedUserAccessToken(String);
}
wrapping_macro::wrapping_string! {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ShortLivedUserAccessToken(String);
}
wrapping_macro::wrapping_string! {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UserAccessToken(String);
}
impl From<LongLivedUserAccessToken> for UserAccessToken {
fn from(t: LongLivedUserAccessToken) -> Self {
Self(t.into_inner())
}
}
impl From<&LongLivedUserAccessToken> for UserAccessToken {
fn from(t: &LongLivedUserAccessToken) -> Self {
Self(t.inner().into())
}
}
impl From<ShortLivedUserAccessToken> for UserAccessToken {
fn from(t: ShortLivedUserAccessToken) -> Self {
Self(t.into_inner())
}
}
impl From<&ShortLivedUserAccessToken> for UserAccessToken {
fn from(t: &ShortLivedUserAccessToken) -> Self {
Self(t.inner().into())
}
}
wrapping_macro::wrapping_string! {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AppAccessToken(String);
}
impl AppAccessToken {
pub fn with_app_secret(app_id: u64, app_secret: impl AsRef<str>) -> Self {
Self(format!("{}|{}", app_id, app_secret.as_ref()))
}
pub fn app_id_and_app_secret(&self) -> Option<(u64, &str)> {
let mut split = self.0.split('|');
if let Some(app_id) = split.next().and_then(|x| x.parse::<u64>().ok()) {
if let Some(app_secret) =
split
.next()
.and_then(|x| if x.is_empty() { None } else { Some(x) })
{
if split.next().is_none() {
return Some((app_id, app_secret));
}
}
}
None
}
}
wrapping_macro::wrapping_string! {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PageAccessToken(String);
}
wrapping_macro::wrapping_string! {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ClientAccessToken(String);
}
impl ClientAccessToken {
pub fn new(app_id: u64, client_token: impl AsRef<str>) -> Self {
Self(format!("{}|{}", app_id, client_token.as_ref()))
}
}
wrapping_macro::wrapping_string! {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UserSessionInfoAccessToken(String);
}
wrapping_macro::wrapping_string! {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PageSessionInfoAccessToken(String);
}
wrapping_macro::wrapping! {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AccessTokenExpiresIn(usize);
}
impl core::fmt::Display for AccessTokenExpiresIn {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_app_access_token() {
assert_eq!(
AppAccessToken::with_app_secret(1, "x").app_id_and_app_secret(),
Some((1, "x"))
);
assert_eq!(
AppAccessToken::from("1|y").app_id_and_app_secret(),
Some((1, "y"))
);
assert!(AppAccessToken::from("1").app_id_and_app_secret().is_none());
assert!(AppAccessToken::from("1|").app_id_and_app_secret().is_none());
assert!(AppAccessToken::from("|x").app_id_and_app_secret().is_none());
}
}