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
160
161
162
163
164
use std::fmt::{Display, Formatter};
use lazy_static::lazy_static;
use regex::Regex;
/// The endpoint-specific user status in a [`Session`](crate::session::Session).
#[derive(Debug)]
#[non_exhaustive]
pub enum UserStatus {
/// User is online and the account is active.
Active {
/// The endpoint-specific token.
///
/// Can be an empty string iff the CAS has breaking changes.
token: String,
/// The username of the logged-in user.
///
/// Can be an empty string iff the portal (the default service under the CAS) has changes.
username: String,
},
/// User is online but the account needs reset.
NeedReset {
/// The endpoint-specific token.
///
/// Can be an empty string iff the CAS has breaking changes.
token: String,
},
/// User is online but the account is banned.
Banned {
/// The endpoint-specific token.
///
/// Can be an empty string iff the CAS has breaking changes.
token: String,
},
/// As a result of login action, it may mean:
/// - the credential is wrong
/// - the token is expired
/// - wechat has not authorized the login request yet
///
/// As a result of check status action, it may mean:
/// - the user session is expired
/// - no user has logged in
Rejected,
}
impl Display for UserStatus {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
UserStatus::Active { username, .. } => write!(f, "active#{}", username),
UserStatus::NeedReset { .. } => write!(f, "need reset"),
UserStatus::Banned { .. } => write!(f, "banned"),
UserStatus::Rejected => write!(f, "rejected"),
}
}
}
impl UserStatus {
/// Returns `true` if the status is [`Active`](UserStatus::Active).
///
/// # Examples
///
/// ```
/// # use neust::UserStatus;
/// let x = UserStatus::Active { username: "".to_owned(), token: "".to_owned() };
/// assert_eq!(x.is_active(), true);
///
/// let x = UserStatus::Rejected;
/// assert_eq!(x.is_active(), false);
/// ```
pub fn is_active(&self) -> bool {
matches!(self, UserStatus::Active { .. })
}
/// Returns `true` if the status is [`Rejected`](UserStatus::Rejected).
///
/// # Examples
///
/// ```
/// # use neust::UserStatus;
/// let x = UserStatus::Rejected;
/// assert_eq!(x.is_rejected(), true);
///
/// let x = UserStatus::Active { username: "".to_owned(), token: "".to_owned() };
/// assert_eq!(x.is_rejected(), false);
/// ```
pub fn is_rejected(&self) -> bool {
matches!(self, UserStatus::Rejected)
}
/// Get the username iff the status is [`Active`](UserStatus::Active).
///
/// # Examples
///
/// ```
/// # use neust::UserStatus;
/// let x = UserStatus::Active { username: "".to_owned(), token: "".to_owned() };
/// assert!(matches!(x.get_username(), Some(_)));
///
/// let x = UserStatus::Banned { token: "".to_owned() };
/// assert!(matches!(x.get_username(), None));
///
/// let x = UserStatus::Rejected;
/// assert!(matches!(x.get_username(), None));
/// ```
pub fn get_username(&self) -> Option<&str> {
match self {
UserStatus::Active { username, .. } => Some(username),
_ => None,
}
}
/// Get the token.
/// Returns [`None`] iff the status is [`Rejected`](UserStatus::Rejected).
///
/// # Examples
///
/// ```
/// # use neust::UserStatus;
/// let x = UserStatus::Active { username: "".to_owned(), token: "".to_owned() };
/// assert!(matches!(x.get_token(), Some(_)));
///
/// let x = UserStatus::Banned { token: "".to_owned() };
/// assert!(matches!(x.get_token(), Some(_)));
///
/// let x = UserStatus::Rejected;
/// assert!(matches!(x.get_token(), None));
/// ```
pub fn get_token(&self) -> Option<&str> {
match self {
UserStatus::Active { token, .. } => Some(token),
UserStatus::Banned { token } => Some(token),
UserStatus::NeedReset { token } => Some(token),
_ => None,
}
}
}
impl UserStatus {
pub(crate) fn from_response_html(html: &str, token: Option<String>) -> UserStatus {
lazy_static! {
static ref TITLE_RE: Regex = Regex::new(r"<title>(.+?)</title>").unwrap();
static ref USERNAME_RE: Regex = Regex::new(r#"var id_number = "(.+?)""#).unwrap();
}
let title = TITLE_RE
.captures(html)
.and_then(|cap| cap.get(1).map(|s| s.as_str()));
let username = USERNAME_RE
.captures(html)
.and_then(|cap| cap.get(1).map(|s| s.as_str()))
.map(|s| s.to_owned())
.unwrap_or_else(|| "".to_owned());
let token = token.unwrap_or_else(|| "".into());
match title {
Some("智慧东大--统一身份认证") => UserStatus::Rejected,
Some("智慧东大") => UserStatus::NeedReset { token },
Some("系统提示") => UserStatus::Banned { token },
_ => UserStatus::Active { token, username },
}
}
}