volumeleaders-client 0.2.0

Browser-session API client for VolumeLeaders data
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! Browser session carrying authentication cookies and XSRF token.
//!
//! A [`Session`] holds the browser cookies and anti-forgery token that
//! VolumeLeaders requires for authenticated API calls. Cookie values and
//! the XSRF token are treated as secrets: the [`Debug`] implementation
//! shows cookie names but replaces all values with `[REDACTED]`.

use crate::error::{ClientError, Result};

/// Cookie domain used by VolumeLeaders browser sessions.
pub const COOKIE_DOMAIN: &str = "volumeleaders.com";

/// ASP.NET session cookie name required by VolumeLeaders.
pub const SESSION_COOKIE_NAME: &str = "ASP.NET_SessionId";

/// ASP.NET forms authentication cookie name required by VolumeLeaders.
pub const FORMS_AUTH_COOKIE_NAME: &str = ".ASPXAUTH";

/// Request verification cookie name that carries the XSRF token.
pub const REQUEST_VERIFICATION_COOKIE_NAME: &str = "__RequestVerificationToken";

/// Required cookie names checked during session validation.
const REQUIRED_COOKIE_NAMES: &[&str] = &[SESSION_COOKIE_NAME, FORMS_AUTH_COOKIE_NAME];

/// A single browser cookie with name, value, and domain.
#[derive(Clone, PartialEq, Eq)]
pub struct Cookie {
    name: String,
    value: String,
    domain: String,
}

impl Cookie {
    /// Creates a new cookie.
    pub fn new(
        name: impl Into<String>,
        value: impl Into<String>,
        domain: impl Into<String>,
    ) -> Self {
        Self {
            name: name.into(),
            value: value.into(),
            domain: domain.into(),
        }
    }

    /// Returns the cookie name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns the cookie value.
    pub fn value(&self) -> &str {
        &self.value
    }

    /// Returns the cookie domain.
    pub fn domain(&self) -> &str {
        &self.domain
    }
}

impl std::fmt::Debug for Cookie {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Cookie")
            .field("name", &self.name)
            .field("value", &"[REDACTED]")
            .field("domain", &self.domain)
            .finish()
    }
}

/// Browser session carrying authentication material for VolumeLeaders.
///
/// Use [`Session::new`] when you already have the XSRF token, or
/// [`Session::from_cookies`] to extract it from a
/// `__RequestVerificationToken` cookie automatically.
///
/// Call [`Session::validate`] before making API requests to catch
/// missing authentication material early with a descriptive error
/// listing ALL missing fields.
#[derive(PartialEq, Eq)]
pub struct Session {
    cookies: Vec<Cookie>,
    xsrf_token: String,
}

impl Session {
    /// Creates a session from cookies and an explicit XSRF token.
    ///
    /// The cookies are defensively cloned so mutations to the original
    /// `Vec` do not affect this session.
    pub fn new(cookies: Vec<Cookie>, xsrf_token: impl Into<String>) -> Self {
        Self {
            cookies: clone_cookies(&cookies),
            xsrf_token: xsrf_token.into(),
        }
    }

    /// Creates a session by extracting the XSRF token from a
    /// `__RequestVerificationToken` cookie.
    ///
    /// # Errors
    ///
    /// Returns [`ClientError::SessionValidation`] if no cookie named
    /// `__RequestVerificationToken` is found or its value is empty.
    pub fn from_cookies(cookies: Vec<Cookie>) -> Result<Self> {
        let xsrf_token = cookies
            .iter()
            .find(|c| c.name == REQUEST_VERIFICATION_COOKIE_NAME)
            .map(|c| c.value.clone())
            .unwrap_or_default();

        if xsrf_token.is_empty() {
            return Err(ClientError::SessionValidation {
                message: format!(
                    "missing {REQUEST_VERIFICATION_COOKIE_NAME} cookie for XSRF token"
                ),
            });
        }

        Ok(Self::new(cookies, xsrf_token))
    }

    /// Validates that all required authentication material is present.
    ///
    /// Checks for [`SESSION_COOKIE_NAME`] and [`FORMS_AUTH_COOKIE_NAME`]
    /// cookies, plus a non-empty XSRF token. All missing fields are
    /// collected and reported in a single error.
    ///
    /// # Errors
    ///
    /// Returns [`ClientError::SessionValidation`] listing every missing
    /// field when any required material is absent.
    pub fn validate(&self) -> Result<()> {
        let missing = self.missing_fields();
        if missing.is_empty() {
            return Ok(());
        }
        Err(ClientError::SessionValidation {
            message: format!("missing fields: {}", missing.join(", ")),
        })
    }

    /// Returns the session cookies.
    pub fn cookies(&self) -> &[Cookie] {
        &self.cookies
    }

    /// Returns the XSRF token.
    pub fn xsrf_token(&self) -> &str {
        &self.xsrf_token
    }

    /// Returns names of required authentication fields that are missing
    /// or empty.
    fn missing_fields(&self) -> Vec<&'static str> {
        let mut missing = Vec::new();
        for &name in REQUIRED_COOKIE_NAMES {
            let found = self
                .cookies
                .iter()
                .any(|c| c.name == name && !c.value.is_empty());
            if !found {
                missing.push(name);
            }
        }
        if self.xsrf_token.is_empty() {
            missing.push("xsrf_token");
        }
        missing
    }
}

impl Clone for Session {
    fn clone(&self) -> Self {
        Self {
            cookies: clone_cookies(&self.cookies),
            xsrf_token: self.xsrf_token.clone(),
        }
    }
}

impl std::fmt::Debug for Session {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let cookie_names: Vec<&str> = self.cookies.iter().map(|c| c.name.as_str()).collect();
        f.debug_struct("Session")
            .field("cookies", &cookie_names)
            .field("xsrf_token", &"[REDACTED]")
            .finish()
    }
}

/// Deep-copies a slice of cookies so the caller retains no shared references.
fn clone_cookies(cookies: &[Cookie]) -> Vec<Cookie> {
    cookies
        .iter()
        .map(|c| Cookie {
            name: c.name.clone(),
            value: c.value.clone(),
            domain: c.domain.clone(),
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Builds a minimal valid session for tests.
    fn valid_session() -> Session {
        Session::new(
            vec![
                Cookie::new(SESSION_COOKIE_NAME, "session-123", COOKIE_DOMAIN),
                Cookie::new(FORMS_AUTH_COOKIE_NAME, "auth-456", COOKIE_DOMAIN),
            ],
            "xsrf-789",
        )
    }

    #[test]
    fn valid_session_passes_validation() {
        let session = valid_session();
        assert!(session.validate().is_ok());
    }

    #[test]
    fn missing_session_cookie_fails_validation() {
        let session = Session::new(
            vec![Cookie::new(
                FORMS_AUTH_COOKIE_NAME,
                "auth-456",
                COOKIE_DOMAIN,
            )],
            "xsrf-789",
        );

        let err = session.validate().unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains(SESSION_COOKIE_NAME),
            "error should mention {SESSION_COOKIE_NAME}, got: {msg}"
        );
    }

    #[test]
    fn missing_auth_cookie_fails_validation() {
        let session = Session::new(
            vec![Cookie::new(
                SESSION_COOKIE_NAME,
                "session-123",
                COOKIE_DOMAIN,
            )],
            "xsrf-789",
        );

        let err = session.validate().unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains(FORMS_AUTH_COOKIE_NAME),
            "error should mention {FORMS_AUTH_COOKIE_NAME}, got: {msg}"
        );
    }

    #[test]
    fn empty_xsrf_token_fails_validation() {
        let session = Session::new(
            vec![
                Cookie::new(SESSION_COOKIE_NAME, "session-123", COOKIE_DOMAIN),
                Cookie::new(FORMS_AUTH_COOKIE_NAME, "auth-456", COOKIE_DOMAIN),
            ],
            "",
        );

        let err = session.validate().unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("xsrf_token"),
            "error should mention xsrf_token, got: {msg}"
        );
    }

    #[test]
    fn multiple_missing_fields_all_reported() {
        let session = Session::new(vec![], "");

        let err = session.validate().unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains(SESSION_COOKIE_NAME),
            "error should mention {SESSION_COOKIE_NAME}, got: {msg}"
        );
        assert!(
            msg.contains(FORMS_AUTH_COOKIE_NAME),
            "error should mention {FORMS_AUTH_COOKIE_NAME}, got: {msg}"
        );
        assert!(
            msg.contains("xsrf_token"),
            "error should mention xsrf_token, got: {msg}"
        );
    }

    #[test]
    fn from_cookies_extracts_xsrf_token() {
        let cookies = vec![
            Cookie::new(SESSION_COOKIE_NAME, "session-123", COOKIE_DOMAIN),
            Cookie::new(FORMS_AUTH_COOKIE_NAME, "auth-456", COOKIE_DOMAIN),
            Cookie::new(
                REQUEST_VERIFICATION_COOKIE_NAME,
                "xsrf-from-cookie",
                COOKIE_DOMAIN,
            ),
        ];

        let session = Session::from_cookies(cookies).unwrap();
        assert_eq!(session.xsrf_token(), "xsrf-from-cookie");
        assert!(session.validate().is_ok());
    }

    #[test]
    fn from_cookies_rejects_missing_verification_cookie() {
        let cookies = vec![
            Cookie::new(SESSION_COOKIE_NAME, "session-123", COOKIE_DOMAIN),
            Cookie::new(FORMS_AUTH_COOKIE_NAME, "auth-456", COOKIE_DOMAIN),
        ];

        let err = Session::from_cookies(cookies).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains(REQUEST_VERIFICATION_COOKIE_NAME),
            "error should mention {REQUEST_VERIFICATION_COOKIE_NAME}, got: {msg}"
        );
    }

    #[test]
    fn debug_shows_cookie_names_but_redacts_values() {
        let session = valid_session();
        let debug = format!("{session:?}");

        // Cookie names should appear
        assert!(
            debug.contains(SESSION_COOKIE_NAME),
            "Debug should show cookie name {SESSION_COOKIE_NAME}, got: {debug}"
        );
        assert!(
            debug.contains(FORMS_AUTH_COOKIE_NAME),
            "Debug should show cookie name {FORMS_AUTH_COOKIE_NAME}, got: {debug}"
        );

        // Values must NOT appear
        assert!(
            !debug.contains("session-123"),
            "Debug must not contain cookie value 'session-123'"
        );
        assert!(
            !debug.contains("auth-456"),
            "Debug must not contain cookie value 'auth-456'"
        );
        assert!(
            !debug.contains("xsrf-789"),
            "Debug must not contain xsrf_token value 'xsrf-789'"
        );

        // Redaction markers should appear
        assert!(
            debug.contains("[REDACTED]"),
            "Debug should contain [REDACTED], got: {debug}"
        );
    }

    #[test]
    fn clone_produces_independent_copy() {
        let original = valid_session();
        let cloned = original.clone();

        assert_eq!(original.xsrf_token(), cloned.xsrf_token());
        assert_eq!(original.cookies().len(), cloned.cookies().len());

        // Verify the cloned cookies are structurally equal but independent
        for (orig, copy) in original.cookies().iter().zip(cloned.cookies().iter()) {
            assert_eq!(orig.name(), copy.name());
            assert_eq!(orig.value(), copy.value());
            assert_eq!(orig.domain(), copy.domain());
            // Pointers must differ (independent allocations)
            assert!(
                !std::ptr::eq(orig, copy),
                "cloned cookies must not share pointers"
            );
        }
    }

    #[test]
    fn cookie_with_empty_value_treated_as_missing() {
        let session = Session::new(
            vec![
                Cookie::new(SESSION_COOKIE_NAME, "", COOKIE_DOMAIN),
                Cookie::new(FORMS_AUTH_COOKIE_NAME, "auth-456", COOKIE_DOMAIN),
            ],
            "xsrf-789",
        );

        let err = session.validate().unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains(SESSION_COOKIE_NAME),
            "empty cookie value should be treated as missing, got: {msg}"
        );
    }

    #[test]
    fn validation_error_is_auth_error() {
        let session = Session::new(vec![], "");
        let err = session.validate().unwrap_err();
        assert!(
            err.is_auth_error(),
            "SessionValidation should be classified as auth error"
        );
    }

    #[test]
    fn getters_return_correct_values() {
        let session = valid_session();
        assert_eq!(session.xsrf_token(), "xsrf-789");
        assert_eq!(session.cookies().len(), 2);
        assert_eq!(session.cookies()[0].name(), SESSION_COOKIE_NAME);
        assert_eq!(session.cookies()[0].value(), "session-123");
        assert_eq!(session.cookies()[0].domain(), COOKIE_DOMAIN);
    }
}