Skip to main content

Requirements

  • Rust 1.70 or later
  • Crates used by the SDK:
    • hmac
    • sha2
    • serde
    • serde_json
    • ureq
    • base64
    • mac_address
    • hostname

Installation

The crate is not on crates.io yet. Add the SDK source via a path (clone, submodule, or copy) or a git dependency.
[dependencies]
authforge = { path = "../authforge-rust" }
Adjust the path to your checkout (for example vendor/authforge).

Quick start

use authforge::{AuthForgeClient, AuthForgeConfig, HeartbeatMode};

fn main() {
    let client = AuthForgeClient::new(AuthForgeConfig {
        app_id: "your-app-id".into(),
        app_secret: "your-app-secret".into(),
        heartbeat_mode: HeartbeatMode::Server,
        on_failure: Some(Box::new(|err| {
            eprintln!("Auth failed: {}", err);
            std::process::exit(1);
        })),
        ..Default::default()
    });

    match client.login("XXXX-XXXX-XXXX-XXXX") {
        Ok(result) => {
            println!("Authenticated!");
            println!("Session expires at unix time: {}", result.expires_in);
        }
        Err(err) => {
            eprintln!("Login failed: {:?}", err);
            std::process::exit(1);
        }
    }
}

Config struct reference

AuthForgeConfig {
    app_id: "your-app-id".into(),             // Required
    app_secret: "your-app-secret".into(),     // Required
    heartbeat_mode: HeartbeatMode::Server,    // Local or Server
    heartbeat_interval: 900,                  // Optional, seconds
    api_base_url: "https://auth.authforge.cc".into(), // Optional
    on_failure: None,                         // Optional callback
    request_timeout: 15,                      // Optional, seconds
}
FieldTypeDefaultDescription
app_idStringrequiredApplication ID from your AuthForge dashboard
app_secretStringrequiredApplication secret from your AuthForge dashboard
heartbeat_modeHeartbeatModeLocalLocal or Server heartbeat mode
heartbeat_intervalu64900Seconds between heartbeat checks
api_base_urlStringhttps://auth.authforge.ccAuthForge API base URL
on_failureOption<Box<dyn Fn(&str)+Send+Sync>>NoneInvoked when heartbeat fails
request_timeoutu6415Timeout for API requests

Methods reference

pub fn new(config: AuthForgeConfig) -> Self;
pub fn login(&self, license_key: &str) -> Result<LoginResult, AuthForgeError>;
pub fn logout(&self);
pub fn is_authenticated(&self) -> bool;
pub fn get_session_data(&self) -> Option<serde_json::Value>;
pub fn get_app_variables(&self) -> Option<std::collections::HashMap<String, serde_json::Value>>;
pub fn get_license_variables(&self) -> Option<std::collections::HashMap<String, serde_json::Value>>;

Error enum reference

AuthForgeError::InvalidApp
AuthForgeError::InvalidKey
AuthForgeError::Expired
AuthForgeError::Revoked
AuthForgeError::HwidMismatch
AuthForgeError::NoCredits
AuthForgeError::Blocked
AuthForgeError::RateLimited
AuthForgeError::ReplayDetected
AuthForgeError::SignatureMismatch
AuthForgeError::NetworkError(String)
AuthForgeError::Other(String)

Heartbeat modes

// SERVER mode: network heartbeats via /auth/heartbeat
let server_client = AuthForgeClient::new(AuthForgeConfig {
    app_id: "...".into(),
    app_secret: "...".into(),
    heartbeat_mode: HeartbeatMode::Server,
    ..Default::default()
});

// LOCAL mode: local expiry checks with no heartbeat network calls
let local_client = AuthForgeClient::new(AuthForgeConfig {
    app_id: "...".into(),
    app_secret: "...".into(),
    heartbeat_mode: HeartbeatMode::Local,
    ..Default::default()
});
See Heartbeat Modes for a detailed comparison.