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
use chrono::{NaiveDateTime, Utc};
use v_api::app::ResultCode;
use v_api::v_onto::individual::Individual;
#[derive(Debug, Clone)]
pub struct Ticket {
pub id: String,
pub user_uri: String,
pub user_login: String,
pub result: ResultCode,
pub start_time: i64,
pub end_time: i64,
}
impl Default for Ticket {
fn default() -> Self {
Ticket {
id: String::default(),
user_uri: String::default(),
user_login: String::default(),
result: ResultCode::AuthenticationFailed,
start_time: 0,
end_time: 0,
}
}
}
impl Ticket {
pub fn update_from_individual(&mut self, src: &mut Individual) {
let when = src.get_first_literal("ticket:when");
let duration = src.get_first_literal("ticket:duration").unwrap_or_default().parse::<i32>().unwrap_or_default();
self.id = src.get_id().to_owned();
self.user_uri = src.get_first_literal("ticket:accessor").unwrap_or_default();
self.user_login = src.get_first_literal("ticket:login").unwrap_or_default();
if self.user_uri.is_empty() {
error!("found a session ticket is not complete, the user can not be found.");
}
if !self.user_uri.is_empty() && (when.is_none() || duration < 10) {
error!("found a session ticket is not complete, we believe that the user has not been found.");
self.user_uri = String::default();
return;
}
let when = when.unwrap();
if let Ok(t) = NaiveDateTime::parse_from_str(&when, "%Y-%m-%dT%H:%M:%S%.f") {
self.start_time = t.timestamp();
self.end_time = self.start_time + duration as i64;
} else {
error!("fail parse field [ticket:when] = {}", when);
self.user_uri = String::default();
}
}
pub fn is_ticket_valid(&mut self) -> bool {
if self.result != ResultCode::Ok {
return false;
}
if Utc::now().timestamp() > self.end_time {
self.result = ResultCode::TicketExpired;
return false;
}
if self.user_uri.is_empty() {
self.result = ResultCode::NotReady;
return false;
}
true
}
}