Skip to main content

Module signed_url

Module signed_url 

Source
Expand description

Signed URL helpers — HMAC-SHA256 with optional expiry. See signed_url::sign / signed_url::verify. Signed URL helpers — tamper-evident URLs with optional expiry.

Common uses:

  • Magic-link login (emailed to the user)
  • Password reset confirmation links
  • Time-limited file download URLs
  • “Click here to verify your email” links

verify checks the signature and the expiry, nothing else. A link that leaks through a Referer header, browser history, a proxy log or a forwarded message keeps working until it expires.

For a link that logs someone in or changes data, enforce single use yourself: delete or rotate the record after the first click, or use the verify_single_use variants in crate::auth_flows.

§Quick start

ⓘ
use rustango::signed_url::{sign, verify};
use std::time::Duration;

let secret = b"my-app-secret";

// Issue a one-hour magic-link
let url = sign("https://app.example.com/auth/login?email=alice@x.com",
               secret,
               Some(Duration::from_secs(3600)));

// On the callback handler:
match verify(&incoming_url, secret) {
    Ok(()) => { /* identity confirmed */ }
    Err(e) => { /* expired or tampered */ }
}

§How it works

It appends ?signature=<base64>&expires=<unix_secs>. The signature is HMAC-SHA256 over the scheme, host, path and the sorted query without the signature itself. Sorting first means the same URL always signs the same, whatever order the params arrive in.

Enums§

SignedUrlError

Functions§

sign
Sign url with secret, with an optional lifetime from now.
sign_at
Like sign, but with the expiry given as unix seconds.
verify
Check the signature on url. It returns Ok(()) when the URL is genuine and, if it carries expires, still in date.
verify_at
Like verify, but with the current time given in unix seconds. Useful in tests.