decrypt_cookies/browser/
cookies.rs1use std::fmt::Display;
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Default, Clone)]
6#[derive(Serialize, Deserialize)]
7#[derive(Debug)]
8#[derive(PartialEq, Eq, PartialOrd, Ord)]
9#[non_exhaustive]
10pub struct LeetCodeCookies {
11 pub csrf: String,
12 pub session: String,
13 #[serde(skip)]
14 pub expiry: bool,
15}
16
17impl LeetCodeCookies {
18 pub fn is_completion(&self) -> bool {
19 !(self.expiry || self.csrf.is_empty() || self.session.is_empty())
20 }
21}
22
23impl std::fmt::Display for LeetCodeCookies {
24 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25 format!("LEETCODE_SESSION={};csrftoken={};", self.session, self.csrf).fmt(f)
26 }
27}
28
29pub trait CookiesInfo {
30 fn get_set_cookie_header(&self) -> String {
32 let mut properties = vec![
33 format!("{}={}", self.name(), self.value()),
34 format!("Path={}", self.path()),
35 ];
36 if !self.name().starts_with("__Host-") {
37 properties.push(format!("Domain={}", self.domain()));
38 }
39 if let Some(expiry) = self.expiry() {
40 properties.push(format!("Expires={}", expiry));
41 }
42 if self.is_secure() {
43 properties.push("Secure".to_owned());
44 }
45 if self.is_http_only() {
46 properties.push("HttpOnly".to_owned());
47 }
48 properties.push(format!("SameSite={}", self.same_site()));
49
50 properties.join("; ")
51 }
52
53 fn get_url(&self) -> String {
54 format!("https://{}{}", self.domain().trim_matches('.'), self.path())
55 }
56
57 fn name(&self) -> &str;
58 fn value(&self) -> &str;
59 fn path(&self) -> &str;
60 fn domain(&self) -> &str;
61 fn expiry(&self) -> Option<String>;
62 fn is_secure(&self) -> bool;
63 fn is_http_only(&self) -> bool;
64 fn same_site(&self) -> SameSite;
65}
66
67#[derive(Clone, Copy)]
68#[derive(Debug)]
69#[derive(Default)]
70#[derive(PartialEq, Eq, PartialOrd, Ord)]
71pub enum SameSite {
72 #[default]
73 None,
74 Lax,
75 Strict,
76}
77
78impl Display for SameSite {
79 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80 match self {
81 Self::None => "None",
82 Self::Lax => "Lax",
83 Self::Strict => "Strict",
84 }
85 .fmt(f)
86 }
87}