licers 1.1.0

Licers license-key verification: Ed25519-signed responses, floating seats, usage credits, remote variables, offline licenses.
Documentation

licers

License-key verification for Rust apps, against Licers.

Every answer from the license server is Ed25519-signed with your account key and checked here (ed25519-dalek, strict verification), with a fresh random nonce per call — so a cracked "fake license server", an edited response or a replayed one is refused.

cargo add licers

Your public key and product id are on the dashboard’s Integration page. The public key is safe to ship inside your app. Each example below is a complete file.

Check a key

Run this when your app starts. The first check binds the device to the key; past the key’s device limit a new device is refused.

In the dashboard: create a product, then a license key for it.

use licers::LicenseClient;

fn main() -> Result<(), licers::Error> {
    let client = LicenseClient::builder("YOUR_PUBLIC_KEY") // safe to ship inside your app
        .product_id("YOUR_PRODUCT_ID") // your product: refuses keys for your other products
        .app_version("2.4.1") // shows in Analytics -> App versions
        .build()?;
    let key = "XXXX-XXXX-XXXX-XXXX"; // the customer's license key

    let result = match client.validate(key) {
        Ok(result) => result,
        Err(e) if e.is_network() => {
            // Offline, or the server is down. That is not "unlicensed": try again,
            // or allow a few days since the last good check.
            eprintln!("Could not reach the license server. Please try again.");
            std::process::exit(1);
        }
        Err(e) => {
            // A forged or replayed answer, or a key for another of your products.
            eprintln!("License check failed: {e}");
            std::process::exit(1);
        }
    };

    if !result.valid {
        // Expired, revoked, device limit reached... the server says which.
        eprintln!("Not licensed: {}", result.error.unwrap_or_default());
        std::process::exit(1);
    }

    println!("Licensed. Expires: {}", result.expires_at.as_deref().unwrap_or("never"));
    Ok(())
}
  • A refused key is a normal answer with a message. An unreachable server is an error: never treat it as “unlicensed”.

validate returns Ok with valid == false when the server refuses the key, and Err for a forged answer or an unreachable server. err.is_network() tells them apart. The API is blocking and needs no async runtime; from async code, call it in spawn_blocking.

Entitlements

Switch features on per key: true/false, numbers or text. They come from the signed answer.

In the dashboard: set Entitlements (JSON) on the key or on its plan, for example {"pro": true, "seats": 5}.

use licers::LicenseClient;

fn main() -> Result<(), licers::Error> {
    let client = LicenseClient::builder("YOUR_PUBLIC_KEY") // safe to ship inside your app
        .product_id("YOUR_PRODUCT_ID") // your product: refuses keys for your other products
        .app_version("2.4.1") // shows in Analytics -> App versions
        .build()?;
    let key = "XXXX-XXXX-XXXX-XXXX"; // the customer's license key

    let result = client.validate(key)?;
    if !result.valid {
        eprintln!("Not licensed: {}", result.error.as_deref().unwrap_or_default());
        std::process::exit(1);
    }

    // Entitlements of this key, for example {"pro": true, "seats": 5, "tier": "gold"}.
    // They are read from the signed copy, so nobody can edit them on the way.
    if result.feature("pro").and_then(|v| v.as_bool()) == Some(true) {
        println!("Pro features unlocked");
    }

    let seats = result.feature("seats").and_then(|v| v.as_i64()).unwrap_or(1);
    let tier = result.feature("tier").and_then(|v| v.as_str()).unwrap_or("basic");
    println!("Seats: {seats} Tier: {tier} All: {:?}", result.features);
    Ok(())
}
  • Compare with true exactly. The text "false" is not the same as false.

Offline file

Check a signed .lic file with no internet at all, for air-gapped machines. Signature, product, expiry, device and operating system are all checked on the computer.

  1. Your app shows its device id, and the customer sends it to you.
  2. License keys → ⋯ → Offline license file: paste the device id, set how many days it lasts, download the .lic file.
  3. Send it to the customer. Your app reads it from disk.
use licers::{get_hwid, verify_offline_license, Error};

fn main() {
    // 1. Show this to the customer. They send it to you, and you create the file
    //    for that device: License keys -> ... -> Offline license file.
    println!("Device id: {}", get_hwid());

    // 2. The customer saves the file next to your app. No network is used here.
    let lic = match verify_offline_license(
        "YOUR_PUBLIC_KEY",
        "license.lic",
        None, // this device
        Some("YOUR_PRODUCT_ID"), // your product
    ) {
        Ok(lic) => lic,
        Err(Error::Signature(_)) => {
            eprintln!("The license file is missing, damaged or was edited.");
            std::process::exit(1);
        }
        Err(e) => {
            eprintln!("License file refused: {e}"); // expired, other device, OS or product
            std::process::exit(1);
        }
    };

    // 3. Use it. An invalid license is always an Err.
    let customer = lic.get("customer").and_then(|v| v.as_str()).unwrap_or("-");
    match lic.get("expires_at").and_then(|v| v.as_i64()) {
        Some(t) => println!("Licensed to: {customer} Expires (unix time): {t}"),
        None => println!("Licensed to: {customer} Expires: never"),
    }
    if lic.get("features").and_then(|f| f.get("pro")).and_then(|v| v.as_bool()) == Some(true) {
        println!("Pro features unlocked");
    }
}
  • Revoking a key can’t reach a file you already sent, so give files a short life.
  • The expiry uses the computer’s own clock.
  • A blank device id makes a file that works on any computer.

Sign in with email

Instead of a key: sign_in() shows a code and waits while the customer signs in to the customer portal with the email on their license and approves this computer. The token works wherever a key does, only on this device. Turn on "Sign in with email" for the product on the Products page first; the client needs product_id.

let token = client.sign_in(|s| println!("Open {} and enter {}", s.url, s.code))?;
// keep the token, then use it instead of the key:
let result = client.validate(&token)?;
if result.signed_out { /* signed out in the portal or dashboard: forget it, sign in again */ }
client.sign_out(&token)?;   // your app's "Sign out"

Calls return Error::SignedOut when the sign-in has ended. Waiting can end with Error::SignInDenied or Error::SignInExpired. start_sign_in() gives you the code and URL for your own window; s.wait(None) collects the token.

License server (no internet)

For networks without internet, the customer runs the free Licers License Server (https://licers.com/docs#license-server). Point the client at it: LicenseClient::builder(pk).api_url("http://licensing-pc:8090").product_id(id), then validate("") or a floating session with an empty key. The SDK checks the server's file with your public key; entitlements, end date and seat limit come from that file.

Usage credits

Meter what the customer does: “100 exports”, “5,000 AI calls a month”. Spending is all or nothing.

In the dashboard: give the key a credit limit, and a daily or monthly refill if you want one.

use licers::{Error, LicenseClient};

fn main() -> Result<(), licers::Error> {
    let client = LicenseClient::builder("YOUR_PUBLIC_KEY") // safe to ship inside your app
        .product_id("YOUR_PRODUCT_ID") // your product: refuses keys for your other products
        .app_version("2.4.1") // shows in Analytics -> App versions
        .build()?;
    let key = "XXXX-XXXX-XXXX-XXXX"; // the customer's license key

    let result = client.validate(key)?; // first: this activates the device on the key
    if !result.valid {
        eprintln!("Not licensed: {}", result.error.unwrap_or_default());
        std::process::exit(1);
    }

    println!("Left: {:?}", client.get_usage(key)?.remaining()); // reads only; None = no limit

    match client.record_usage(key, 1) {
        // spent 1 credit, all or nothing
        Ok(usage) => println!("Done. {:?} credits left", usage.remaining()),
        Err(Error::UsageExceeded { usage, .. }) => {
            println!("Out of credits. Refill: {}", usage["resets_at"].as_str().unwrap_or("buy more"));
        }
        Err(e) => return Err(e),
    }
    Ok(())
}
  • Check the key on this device first.
  • After a network error the credits may already be spent: read the balance before trying again.

Remote variables

Values you change in the dashboard without shipping an update: a link, a message, a switch.

In the dashboard: add them on the Remote Variables page.

use licers::LicenseClient;

fn main() -> Result<(), licers::Error> {
    let client = LicenseClient::builder("YOUR_PUBLIC_KEY") // safe to ship inside your app
        .product_id("YOUR_PRODUCT_ID") // your product: refuses keys for your other products
        .app_version("2.4.1") // shows in Analytics -> App versions
        .build()?;
    let key = "XXXX-XXXX-XXXX-XXXX"; // the customer's license key

    let result = client.validate(key)?; // first: this activates the device on the key
    if !result.valid {
        eprintln!("Not licensed: {}", result.error.unwrap_or_default());
        std::process::exit(1);
    }

    // Values you set on the Remote Variables page. Signed, and only for valid keys.
    let url = client.get_variable(key, "download_url")?; // None when it isn't set
    let theme = client.get_variable(key, "theme")?.unwrap_or_else(|| "dark".to_string());
    let everything = client.get_variables(key)?; // all this key may see

    println!("{url:?} {theme} {everything:?}");
    Ok(())
}
  • Check the key on this device first.
  • Values are text. A product’s value wins over one for all products.

Floating seats

Let only N people run the app at the same time, on any computers.

In the dashboard: set Max concurrent sessions on the key.

use licers::{Error, FloatingSession, LicenseClient};
use std::sync::Arc;

fn main() -> Result<(), licers::Error> {
    let client = LicenseClient::builder("YOUR_PUBLIC_KEY") // safe to ship inside your app
        .product_id("YOUR_PRODUCT_ID") // your product: refuses keys for your other products
        .app_version("2.4.1") // shows in Analytics -> App versions
        .build()?;
    let key = "XXXX-XXXX-XXXX-XXXX"; // the customer's license key

    // Takes a seat and keeps it with heartbeats on a background thread.
    let on_lost = Box::new(|reason: &str| eprintln!("Seat lost: {reason}")); // save the user's work
    match FloatingSession::start(Arc::new(client), key, Some(on_lost)) {
        Ok(seat) => {
            println!("Seat taken: {} of {}", seat.seats_used, seat.seats_max);

            // ... your app runs here. Call this at safe points (before a save,
            // between jobs). It is an Err once the seat is gone.
            seat.ensure_active()?;
        } // dropping it frees the seat
        Err(Error::SeatUnavailable(message)) => eprintln!("No seat: {message}"),
        Err(e) => return Err(e),
    }
    Ok(())
}
  • If the server can’t be reached, the seat is kept until its last signed expiry and then given up.
  • A crashed app frees its seat by itself after the session timeout.

Updates

Tell the customer about a new version. Only a valid, active key is given the download link.

In the dashboard: add the version and its download link on the Releases page.

use licers::LicenseClient;

fn main() -> Result<(), licers::Error> {
    let client = LicenseClient::builder("YOUR_PUBLIC_KEY") // safe to ship inside your app
        .product_id("YOUR_PRODUCT_ID") // your product: refuses keys for your other products
        .app_version("2.4.1") // shows in Analytics -> App versions
        .build()?;
    let key = "XXXX-XXXX-XXXX-XXXX"; // the customer's license key

    let update = client.check_update(key, "2.4.1", "stable")?; // or "beta"

    if update.update_available {
        println!("New version: {}", update.version.unwrap_or_default());
        println!("What's new: {}", update.notes.unwrap_or_default());
        // The answer is signed, but download_url and notes are not: check the URL is
        // on your own host, and verify the file against a hash you publish.
        println!("Download: {}", update.download_url.unwrap_or_default());
    } else {
        println!("You have the latest version.");
    }
    Ok(())
}
  • The answer is signed, including “no update”. The download link is not: check its host and the file’s hash.

Errors

Error Meaning
Error::Network Server unreachable: retry, or apply a grace period
Error::Server The server answered 5xx: an outage, not a verdict on the key
Error::Signature Signature or nonce check failed: forged, edited or replayed (or a damaged license file)
Error::ProductMismatch Correctly signed, but for another of your products
Error::InvalidLicense The server (or an offline file) refused the key
Error::UsageExceeded Not enough credits; nothing was spent
Error::SeatUnavailable No free floating seat, or a held seat was lost
Error::Protocol Not a usable reply (a proxy or captive portal; redirects are never followed)

err.is_network() covers the first two, err.is_refusal() the refusals.

Device id

get_hwid() is SHA-256 of "licers:" + the OS machine id (Windows MachineGuid, Linux /etc/machine-id, macOS IOPlatformUUID) — the same value the Node and .NET SDKs produce on that machine. Where none is readable, a random id is stored once in the user's data folder (LICERS_HWID_DIR overrides). Use .hwid("...") on the builder to supply your own.

License

MIT