Skip to main content

captcha_sdk/
request.rs

1use std::net::IpAddr;
2
3use crate::CaptchaToken;
4
5/// Provider-independent input for one verification attempt.
6#[derive(Debug)]
7#[non_exhaustive]
8pub struct VerificationRequest {
9    /// Opaque token produced by the provider's client-side widget.
10    token: CaptchaToken,
11    /// End-user IP address, when available and appropriate to forward.
12    remote_ip: Option<IpAddr>,
13}
14
15impl VerificationRequest {
16    /// Creates a request containing the required client token.
17    #[must_use]
18    pub const fn new(token: CaptchaToken) -> Self {
19        Self {
20            token,
21            remote_ip: None,
22        }
23    }
24
25    /// Returns the opaque client token.
26    #[must_use]
27    pub const fn token(&self) -> &CaptchaToken {
28        &self.token
29    }
30
31    /// Returns the end-user IP address, when one was supplied.
32    #[must_use]
33    pub const fn remote_ip(&self) -> Option<IpAddr> {
34        self.remote_ip
35    }
36
37    /// Adds the end-user IP address.
38    #[must_use]
39    pub const fn with_remote_ip(mut self, remote_ip: IpAddr) -> Self {
40        self.remote_ip = Some(remote_ip);
41        self
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::VerificationRequest;
48    use crate::CaptchaToken;
49
50    #[test]
51    fn request_preserves_the_supplied_token() {
52        let raw_token = "request-preservation-test-token";
53        let token = CaptchaToken::new(raw_token).expect("token should be valid");
54
55        let request = VerificationRequest::new(token);
56
57        assert_eq!(request.token().expose(), raw_token);
58    }
59}