v_common_module/
ticket.rs1use chrono::{NaiveDateTime, Utc};
2use v_api::app::ResultCode;
3use v_api::v_onto::individual::Individual;
4
5#[derive(Debug, Clone)]
6pub struct Ticket {
7 pub id: String,
8 pub user_uri: String,
10 pub user_login: String,
12 pub result: ResultCode,
14 pub start_time: i64,
16 pub end_time: i64,
18}
19
20impl Default for Ticket {
21 fn default() -> Self {
22 Ticket {
23 id: String::default(),
24 user_uri: String::default(),
25 user_login: String::default(),
26 result: ResultCode::AuthenticationFailed,
27 start_time: 0,
28 end_time: 0,
29 }
30 }
31}
32
33impl Ticket {
34 pub fn update_from_individual(&mut self, src: &mut Individual) {
35 let when = src.get_first_literal("ticket:when");
36 let duration = src.get_first_literal("ticket:duration").unwrap_or_default().parse::<i32>().unwrap_or_default();
37
38 self.id = src.get_id().to_owned();
39 self.user_uri = src.get_first_literal("ticket:accessor").unwrap_or_default();
40 self.user_login = src.get_first_literal("ticket:login").unwrap_or_default();
41
42 if self.user_uri.is_empty() {
43 error!("found a session ticket is not complete, the user can not be found.");
44 }
45
46 if !self.user_uri.is_empty() && (when.is_none() || duration < 10) {
47 error!("found a session ticket is not complete, we believe that the user has not been found.");
48 self.user_uri = String::default();
49 return;
50 }
51 let when = when.unwrap();
52
53 if let Ok(t) = NaiveDateTime::parse_from_str(&when, "%Y-%m-%dT%H:%M:%S%.f") {
54 self.start_time = t.timestamp();
55 self.end_time = self.start_time + duration as i64;
56 } else {
57 error!("fail parse field [ticket:when] = {}", when);
58 self.user_uri = String::default();
59 }
60 }
61
62 pub fn is_ticket_valid(&mut self) -> bool {
63 if self.result != ResultCode::Ok {
64 return false;
65 }
66
67 if Utc::now().timestamp() > self.end_time {
68 self.result = ResultCode::TicketExpired;
69 return false;
70 }
71
72 if self.user_uri.is_empty() {
73 self.result = ResultCode::NotReady;
74 return false;
75 }
76
77 true
78 }
79}