Skip to content

Commit 6c6d5c5

Browse files
authored
Add environment variable override for cookie configuration (#195)
* Add environment variable override for cookie configuration Cookies can now be overridden using LEETCODE_CSRF, LEETCODE_SESSION, and LEETCODE_SITE environment variables when loading configuration. * Update README.md: add document for overriding cookies with environment variables.
1 parent f422206 commit 6c6d5c5

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,27 @@ csrf = '<your-leetcode-csrf-token>'
321321
session = '<your-leetcode-session-key>'
322322
```
323323

324+
#### Environment variables
325+
326+
The cookies can also be overridden by environment variables, which might be useful to exclude the sensitive information from the configuration file `leetcode.toml`. To do this, you can leave the `csrf` and `session` fields empty in the configuration file and override cookies settings via the environment variables `LEETCODE_CSRF`, `LEETCODE_SESSION`, and `LEETCODE_SITE`:
327+
328+
```toml
329+
[cookies]
330+
csrf = ''
331+
session = ''
332+
site = 'leetcode.com'
333+
```
334+
335+
Then set the environment variables:
336+
337+
```bash
338+
export LEETCODE_CSRF='<your-leetcode-csrf-token>'
339+
export LEETCODE_SESSION='<your-leetcode-session-key>'
340+
export LEETCODE_SITE='leetcode.cn' # or 'leetcode.com'
341+
```
342+
343+
Note that `cookies.site` in still required in the `leetcode.toml` to avoid exception during configuration file parsing, but can be overridden using environment variables.
344+
324345
## Programmable
325346

326347
If you want to filter LeetCode questions using custom Python scripts, add the following to your the configuration file:

src/config/cookies.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,27 @@ impl Display for Cookies {
6262
)
6363
}
6464
}
65+
66+
/// Override cookies from environment variables
67+
pub const LEETCODE_CSRF_ENV: &str = "LEETCODE_CSRF";
68+
pub const LEETCODE_SESSION_ENV: &str = "LEETCODE_SESSION";
69+
pub const LEETCODE_SITE_ENV: &str = "LEETCODE_SITE";
70+
71+
impl Cookies {
72+
/// Load cookies from environment variables, overriding any existing values
73+
/// if the environment variables are set.
74+
pub fn with_env_override(mut self) -> Self {
75+
if let Ok(csrf) = std::env::var(LEETCODE_CSRF_ENV) {
76+
self.csrf = csrf;
77+
}
78+
if let Ok(session) = std::env::var(LEETCODE_SESSION_ENV) {
79+
self.session = session;
80+
}
81+
if let Ok(site) = std::env::var(LEETCODE_SITE_ENV) {
82+
if let Ok(leetcode_site) = LeetcodeSite::from_str(&site) {
83+
self.site = leetcode_site;
84+
}
85+
}
86+
self
87+
}
88+
}

src/config/mod.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,19 @@ impl Config {
4646

4747
let s = fs::read_to_string(&conf)?;
4848
match toml::from_str::<Config>(&s) {
49-
Ok(config) => match config.cookies.site {
50-
cookies::LeetcodeSite::LeetcodeCom => Ok(config),
51-
cookies::LeetcodeSite::LeetcodeCn => {
52-
let mut config = config;
53-
config.sys.urls = sys::Urls::new_with_leetcode_cn();
54-
Ok(config)
49+
Ok(mut config) => {
50+
// Override config.cookies with environment variables
51+
config.cookies = config.cookies.with_env_override();
52+
53+
match config.cookies.site {
54+
cookies::LeetcodeSite::LeetcodeCom => Ok(config),
55+
cookies::LeetcodeSite::LeetcodeCn => {
56+
let mut config = config;
57+
config.sys.urls = sys::Urls::new_with_leetcode_cn();
58+
Ok(config)
59+
}
5560
}
56-
},
61+
}
5762
Err(e) => {
5863
let tmp = Self::root()?.join("leetcode.tmp.toml");
5964
Self::write_default(tmp)?;

0 commit comments

Comments
 (0)