Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
response: "my-widget-response".to_string(),
..Default::default()
},
Some(&["example.com"]),
["example.com"],
)
.await?;

Expand Down
15 changes: 9 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl TurnstileClient {
/// `valid_hostnames` is an optional list of hostnames to verify against. The function
/// will error if the hostname returned by the Turnstile API does not match any of the
/// provided hostnames.
/// When it is None, the hostname is not verified.
/// To skip hostname verification, set it to `None::<&str>`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

///
/// # Timeouts
///
Expand All @@ -147,7 +147,7 @@ impl TurnstileClient {
///
/// tokio::time::timeout(
/// Duration::from_secs(5),
/// client.siteverify(request, Some(&["example.com"]))
/// client.siteverify(request, ["example.com"])
/// ).await.ok()
/// # }
/// ```
Expand Down Expand Up @@ -180,7 +180,7 @@ impl TurnstileClient {
/// match client
/// .siteverify(
/// SiteVerifyRequest { response: token, ..Default::default() },
/// Some(&["example.com"]),
/// ["example.com"],
/// )
/// .await
/// {
Expand All @@ -201,7 +201,7 @@ impl TurnstileClient {
pub async fn siteverify(
&self,
request: SiteVerifyRequest,
valid_hostnames: Option<&[&str]>,
valid_hostnames: impl IntoIterator<Item: AsRef<str>>,
) -> Result<SiteVerifyResponse, TurnstileError> {
let body = SiteVerifyBody {
secret: self.secret.expose_secret(),
Expand Down Expand Up @@ -271,9 +271,12 @@ impl TurnstileClient {
return Err(TokenRejection::Unverified.into());
}

if let Some(valid_hostnames) = valid_hostnames
// If peeking does not work, it means `None` was passed.
let mut valid_hostnames = valid_hostnames.into_iter().peekable();
// Reject if none of the valid_hostnames matches the body's hostname field
if valid_hostnames.peek().is_some()
&& let Some(ref body_hostname) = body.hostname
&& !valid_hostnames.contains(&body_hostname.as_str())
&& !valid_hostnames.any(|h| h.as_ref() == body_hostname.as_str())
{
return Err(TokenRejection::HostnameMismatch(body_hostname.clone()).into());
}
Expand Down
14 changes: 7 additions & 7 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ async fn test_success() -> Result<()> {
response: "myresponse".to_string(),
..Default::default()
},
Some(&["example.com"]),
None::<&str>,
)
.await?;

Expand All @@ -141,7 +141,7 @@ async fn test_success_with_hostname() -> Result<()> {
response: "myresponse".to_string(),
..Default::default()
},
Some(&["example.com"]),
["example.com"],
)
.await?;

Expand All @@ -163,7 +163,7 @@ async fn test_reject_invalid_hostname() -> Result<()> {
response: "myresponse".to_string(),
..Default::default()
},
Some(&["evil.com"]),
["evil.com"],
)
.await;

Expand All @@ -188,7 +188,7 @@ async fn test_fail() -> Result<()> {
response: "myresponse".to_string(),
..Default::default()
},
Some(&["example.com"]),
["example.com"],
)
.await;

Expand Down Expand Up @@ -218,7 +218,7 @@ async fn test_error_codes_survive_http_400() -> Result<()> {
response: "myresponse".to_string(),
..Default::default()
},
None,
None::<&str>,
)
.await;

Expand All @@ -244,7 +244,7 @@ async fn test_token_already_spent() -> Result<()> {
response: "myresponse".to_string(),
..Default::default()
},
Some(&["example.com"]),
["example.com"],
)
.await;

Expand Down Expand Up @@ -281,7 +281,7 @@ async fn test_integration() -> Result<()> {
idempotency_key,
..Default::default()
},
Some(&["example.com"]),
["example.com"],
)
.await?;

Expand Down