linkedin-profile-validator 0.1.0

A Rust library to validate LinkedIn profile URLs by checking format and profile existence
Documentation
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! `LinkedIn` profile URL validation library.
//!
//! This crate provides tools to validate `LinkedIn` profile URLs by checking both
//! format correctness and profile existence through HTTP requests.
//!
//! # Features
//!
//! - Format validation without network calls
//! - Profile existence verification
//! - Async and sync APIs
//! - Rate limiting awareness
//!
//! # Examples
//!
//! ## Basic usage
//!
//! ```no_run
//! use linkedin_profile_validator::{LinkedInValidator, LinkedInUrlError};
//!
//! let validator = LinkedInValidator::new();
//! match validator.is_valid_linkedin_profile_url("https://www.linkedin.com/in/johndoe") {
//!     Ok(_) => println!("Profile exists!"),
//!     Err(LinkedInUrlError::ProfileNotFound) => println!("Profile not found"),
//!     Err(LinkedInUrlError::AuthenticationRequired) => println!("LinkedIn requires auth"),
//!     Err(e) => println!("Error: {}", e),
//! }
//! ```
//!
//! ## Format validation only
//!
//! ```
//! use linkedin_profile_validator::is_valid_linkedin_profile_format;
//!
//! if is_valid_linkedin_profile_format("https://www.linkedin.com/in/johndoe") {
//!     println!("Valid LinkedIn profile URL format");
//! }
//! ```

use regex::Regex;
use thiserror::Error;
use url::Url;

/// Errors that can occur during `LinkedIn` URL validation.
#[derive(Error, Debug)]
pub enum LinkedInUrlError {
    /// The provided URL has invalid format.
    #[error("Invalid URL format: {0}")]
    InvalidUrl(String),

    /// The URL is not from `LinkedIn` domain.
    #[error("Not a LinkedIn URL")]
    NotLinkedInUrl,

    /// The URL is from `LinkedIn` but not a profile URL.
    #[error("Not a LinkedIn profile URL")]
    NotProfileUrl,

    /// Network error occurred during validation.
    #[error("Network error: {0}")]
    NetworkError(#[from] reqwest::Error),

    /// The `LinkedIn` profile was not found (404).
    #[error("Profile not found (404)")]
    ProfileNotFound,

    /// `LinkedIn` requires authentication to verify the profile.
    #[error("Unable to verify - LinkedIn requires authentication")]
    AuthenticationRequired,
}

/// A `LinkedIn` profile validator that performs HTTP requests to verify profile existence.
///
/// # Example
///
/// ```no_run
/// use linkedin_profile_validator::LinkedInValidator;
///
/// let validator = LinkedInValidator::new();
/// let result = validator.is_valid_linkedin_profile_url("https://www.linkedin.com/in/johndoe");
/// ```
pub struct LinkedInValidator {
    client: reqwest::blocking::Client,
}

impl LinkedInValidator {
    /// Creates a new `LinkedIn` validator instance.
    ///
    /// # Panics
    ///
    /// Panics if the HTTP client cannot be built.
    #[must_use]
    pub fn new() -> Self {
        let client = reqwest::blocking::Client::builder()
            .user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
            .timeout(std::time::Duration::from_secs(10))
            .build()
            .unwrap();

        Self { client }
    }

    /// Validates a `LinkedIn` profile URL by checking format and existence.
    ///
    /// This method performs an HTTP request to verify if the profile actually exists.
    ///
    /// # Arguments
    ///
    /// * `url_str` - The `LinkedIn` profile URL to validate
    ///
    /// # Returns
    ///
    /// * `Ok(true)` - If the profile exists
    /// * `Err(LinkedInUrlError)` - If validation fails
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The URL format is invalid
    /// - The URL is not from `LinkedIn` domain
    /// - The URL is not a profile URL
    /// - Network request fails
    /// - The profile doesn't exist (404)
    /// - `LinkedIn` requires authentication
    ///
    /// # Example
    ///
    /// ```no_run
    /// use linkedin_profile_validator::LinkedInValidator;
    ///
    /// let validator = LinkedInValidator::new();
    /// match validator.is_valid_linkedin_profile_url("https://www.linkedin.com/in/johndoe") {
    ///     Ok(_) => println!("Valid profile"),
    ///     Err(e) => println!("Invalid: {}", e),
    /// }
    /// ```
    pub fn is_valid_linkedin_profile_url(&self, url_str: &str) -> Result<bool, LinkedInUrlError> {
        let url = Url::parse(url_str).map_err(|e| LinkedInUrlError::InvalidUrl(e.to_string()))?;

        if !is_linkedin_domain(&url) {
            return Err(LinkedInUrlError::NotLinkedInUrl);
        }

        if !is_profile_path(&url) {
            return Err(LinkedInUrlError::NotProfileUrl);
        }

        self.check_profile_exists(url_str)?;

        Ok(true)
    }

    fn check_profile_exists(&self, url: &str) -> Result<(), LinkedInUrlError> {
        let mut response = self.client.get(url).send()?;

        // LinkedIn returns 999 status for bot detection/rate limiting
        // In this case, we need to follow redirects manually
        if response.status().as_u16() == 999 {
            // Try with cookie header to bypass authwall
            response = self.client.get(url).header("Cookie", "sl=v=1&1").send()?;
        }

        // Check if redirected to 404 page
        let final_url = response.url().to_string();
        if final_url.contains("/404/") || final_url.contains("linkedin.com/404") {
            return Err(LinkedInUrlError::ProfileNotFound);
        }

        // Get response body
        let body = response.text()?;

        // Check for authwall (indicates we're being blocked)
        if body.contains("/authwall") || body.contains("sessionRedirect") {
            // When we hit authwall, we can't determine if profile exists
            return Err(LinkedInUrlError::AuthenticationRequired);
        }

        // Check for common error page indicators
        if body.contains("This page doesn't exist")
            || body.contains("This page doesn't exist")
            || body.contains("Page not found")
            || body.contains("Check the URL or return to LinkedIn home")
            || body.contains("return to LinkedIn home")
            || body.contains("Go to your feed") && body.contains("doesn't exist")
        {
            return Err(LinkedInUrlError::ProfileNotFound);
        }

        Ok(())
    }
}

fn is_linkedin_domain(url: &Url) -> bool {
    matches!(url.domain(), Some(domain) if domain == "linkedin.com" || domain == "www.linkedin.com")
}

fn is_profile_path(url: &Url) -> bool {
    let path = url.path();
    let profile_regex = Regex::new(r"^/in/[a-zA-Z0-9\-]+/?$").unwrap();
    profile_regex.is_match(path)
}

impl Default for LinkedInValidator {
    fn default() -> Self {
        Self::new()
    }
}

/// Validates a `LinkedIn` profile URL asynchronously.
///
/// This function performs an HTTP request to verify if the profile actually exists.
/// Use this for async contexts like web servers.
///
/// # Arguments
///
/// * `url` - The `LinkedIn` profile URL to validate
///
/// # Returns
///
/// * `Ok(true)` - If the profile exists
/// * `Err(LinkedInUrlError)` - If validation fails
///
/// # Errors
///
/// Returns an error if:
/// - The URL format is invalid
/// - The URL is not from `LinkedIn` domain
/// - The URL is not a profile URL
/// - Network request fails
/// - The profile doesn't exist (404)
/// - `LinkedIn` requires authentication
///
/// # Example
///
/// ```no_run
/// use linkedin_profile_validator::validate_linkedin_url_async;
///
/// # async fn example() {
/// match validate_linkedin_url_async("https://www.linkedin.com/in/johndoe").await {
///     Ok(_) => println!("Valid profile"),
///     Err(e) => println!("Invalid: {}", e),
/// }
/// # }
/// ```
pub async fn validate_linkedin_url_async(url: &str) -> Result<bool, LinkedInUrlError> {
    let url_parsed = Url::parse(url).map_err(|e| LinkedInUrlError::InvalidUrl(e.to_string()))?;

    if !is_linkedin_domain(&url_parsed) {
        return Err(LinkedInUrlError::NotLinkedInUrl);
    }

    if !is_profile_path(&url_parsed) {
        return Err(LinkedInUrlError::NotProfileUrl);
    }

    let client = reqwest::Client::builder()
        .user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
        .timeout(std::time::Duration::from_secs(10))
        .build()?;

    let mut response = client.get(url).send().await?;

    // LinkedIn returns 999 status for bot detection/rate limiting
    if response.status().as_u16() == 999 {
        // Try with cookie header to bypass authwall
        response = client.get(url).header("Cookie", "sl=v=1&1").send().await?;
    }

    // Check if redirected to 404 page
    let final_url = response.url().to_string();
    if final_url.contains("/404/") || final_url.contains("linkedin.com/404") {
        return Err(LinkedInUrlError::ProfileNotFound);
    }

    // Get response body
    let body = response.text().await?;

    // Check for authwall (indicates we're being blocked)
    if body.contains("/authwall") || body.contains("sessionRedirect") {
        return Err(LinkedInUrlError::AuthenticationRequired);
    }

    // Check for common error page indicators
    if body.contains("This page doesn't exist")
        || body.contains("This page doesn't exist")
        || body.contains("Page not found")
        || body.contains("Check the URL or return to LinkedIn home")
        || body.contains("return to LinkedIn home")
        || body.contains("Go to your feed") && body.contains("doesn't exist")
    {
        return Err(LinkedInUrlError::ProfileNotFound);
    }

    Ok(true)
}

/// Checks if a URL has valid `LinkedIn` profile format without making network calls.
///
/// This function only validates the URL format and does not check if the profile exists.
/// Use this for quick validation without network overhead.
///
/// # Arguments
///
/// * `url` - The URL to validate
///
/// # Returns
///
/// * `true` - If the URL has valid `LinkedIn` profile format
/// * `false` - If the URL is invalid or not a `LinkedIn` profile URL
///
/// # Example
///
/// ```
/// use linkedin_profile_validator::is_valid_linkedin_profile_format;
///
/// assert!(is_valid_linkedin_profile_format("https://www.linkedin.com/in/johndoe"));
/// assert!(!is_valid_linkedin_profile_format("https://www.google.com/in/johndoe"));
/// assert!(!is_valid_linkedin_profile_format("https://linkedin.com/company/microsoft"));
/// ```
#[must_use]
pub fn is_valid_linkedin_profile_format(url: &str) -> bool {
    let Ok(url_parsed) = Url::parse(url) else {
        return false;
    };

    is_linkedin_domain(&url_parsed) && is_profile_path(&url_parsed)
}

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

    #[test]
    fn test_valid_profile_format() {
        // Test with real valid profiles
        assert!(is_valid_linkedin_profile_format(
            "https://www.linkedin.com/in/hamze/"
        ));
        assert!(is_valid_linkedin_profile_format(
            "https://www.linkedin.com/in/hamzeghalebi/"
        ));
        assert!(is_valid_linkedin_profile_format(
            "https://www.linkedin.com/in/johndoe"
        ));
        assert!(is_valid_linkedin_profile_format(
            "https://linkedin.com/in/jane-doe"
        ));
        assert!(is_valid_linkedin_profile_format(
            "https://www.linkedin.com/in/john-doe-123/"
        ));
    }

    #[test]
    fn test_invalid_profile_format() {
        assert!(!is_valid_linkedin_profile_format(
            "https://www.google.com/in/johndoe"
        ));
        assert!(!is_valid_linkedin_profile_format(
            "https://linkedin.com/company/microsoft"
        ));
        assert!(!is_valid_linkedin_profile_format("https://linkedin.com/"));
        assert!(!is_valid_linkedin_profile_format("not-a-url"));
    }

    #[test]
    fn test_real_valid_profile() {
        let validator = LinkedInValidator::new();
        // This is a valid LinkedIn profile
        match validator.is_valid_linkedin_profile_url("https://www.linkedin.com/in/hamze/") {
            Ok(true) => (),
            Ok(false) => panic!("Expected profile to be valid"),
            Err(LinkedInUrlError::AuthenticationRequired) => {
                println!("LinkedIn requires authentication - cannot verify profile existence");
            }
            Err(e) => panic!("Expected profile to be valid or require auth, got error: {e}"),
        }
    }

    #[test]
    fn test_real_invalid_profile() {
        let validator = LinkedInValidator::new();
        // This LinkedIn profile doesn't exist - LinkedIn shows error page
        match validator.is_valid_linkedin_profile_url("https://www.linkedin.com/in/hamzeghalebi/") {
            Ok(_) => {
                // LinkedIn might be allowing access sometimes, especially after multiple requests
                // This is inconsistent behavior from LinkedIn
                println!("Warning: LinkedIn allowed access to profile page - cannot determine if profile actually exists");
            }
            Err(LinkedInUrlError::ProfileNotFound) => (),
            Err(LinkedInUrlError::AuthenticationRequired) => {
                println!("LinkedIn requires authentication - cannot verify profile existence");
            }
            Err(e) => panic!("Expected ProfileNotFound or AuthenticationRequired error, got: {e}"),
        }
    }

    #[tokio::test]
    async fn test_async_valid_profile() {
        // Test async validation with valid profile
        match validate_linkedin_url_async("https://www.linkedin.com/in/hamze/").await {
            Ok(true) => (),
            Ok(false) => panic!("Expected profile to be valid"),
            Err(LinkedInUrlError::AuthenticationRequired) => {
                println!("LinkedIn requires authentication - cannot verify profile existence");
            }
            Err(e) => panic!("Expected profile to be valid or require auth, got error: {e}"),
        }
    }

    #[tokio::test]
    async fn test_async_invalid_profile() {
        // Test async validation with invalid profile that shows error page
        match validate_linkedin_url_async("https://www.linkedin.com/in/hamzeghalebi/").await {
            Ok(_) => {
                // LinkedIn might be allowing access sometimes, especially after multiple requests
                // This is inconsistent behavior from LinkedIn
                println!("Warning: LinkedIn allowed access to profile page - cannot determine if profile actually exists");
            }
            Err(LinkedInUrlError::ProfileNotFound) => (),
            Err(LinkedInUrlError::AuthenticationRequired) => {
                println!("LinkedIn requires authentication - cannot verify profile existence");
            }
            Err(e) => panic!("Expected ProfileNotFound or AuthenticationRequired error, got: {e}"),
        }
    }

    #[test]
    #[ignore = "Debug test to inspect LinkedIn response"]
    fn debug_linkedin_response() {
        let client = reqwest::blocking::Client::builder()
            .user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
            .timeout(std::time::Duration::from_secs(10))
            .build()
            .unwrap();

        let url = "https://www.linkedin.com/in/hamzeghalebi/";
        let response = client.get(url).send().unwrap();

        println!("Status: {}", response.status());
        println!("Final URL: {}", response.url());

        let body = response.text().unwrap();
        println!("Body length: {}", body.len());
        println!(
            "First 2000 chars:\n{}",
            &body.chars().take(2000).collect::<String>()
        );
    }
}