| Crates.io | hessra-ffi |
| lib.rs | hessra-ffi |
| version | 0.3.3 |
| created_at | 2025-04-07 22:40:15.885471+00 |
| updated_at | 2025-08-04 21:19:49.990929+00 |
| description | C FFI bindings for Hessra token verification and configuration |
| homepage | |
| repository | https://github.com/hessra/hessra-sdk.rs |
| max_upload_size | |
| id | 1624789 |
| size | 80,974 |
C FFI bindings for the Hessra SDK token verification and configuration functionality.
This crate provides C-compatible bindings for the core functionality of the Hessra SDK, focused specifically on token verification and configuration management. It is designed to be used in contexts where the full Rust SDK would be impractical, such as in database plugins.
cargo build --release
The resulting library will be in target/release/libhessra_ffi.so (Linux), target/release/libhessra_ffi.dylib (macOS), or target/release/hessra_ffi.dll (Windows).
cargo build --release
The resulting library will be in target/release/libhessra_ffi.a (Unix) or target/release/hessra_ffi.lib (Windows).
Include the hessra_ffi.h header file in your project and link against the dynamic or static library.
#include "hessra_ffi.h"
int main() {
// Initialize the library
HessraResult result = hessra_init();
if (result != SUCCESS) {
// Handle error
return 1;
}
// Get version information
const char* version = hessra_version();
printf("Hessra version: %s\n", version);
hessra_string_free((char*)version);
return 0;
}
// Load a public key from a file
HessraPublicKey* public_key = NULL;
result = hessra_public_key_from_file("path/to/public_key.pem", &public_key);
if (result != SUCCESS) {
// Handle error
}
// Verify a token
result = hessra_token_verify(token_string, public_key, "subject", "resource");
if (result != SUCCESS) {
char* error_message = hessra_error_message(result);
printf("Verification failed: %s\n", error_message);
hessra_string_free(error_message);
}
// Don't forget to free resources
hessra_public_key_free(public_key);
// Create a new configuration
HessraConfig* config = NULL;
result = hessra_config_new(&config);
if (result != SUCCESS) {
// Handle error
}
// Set a public key in the configuration
result = hessra_config_set_public_key(config, public_key);
if (result != SUCCESS) {
// Handle error
}
// Get a public key from the configuration
HessraPublicKey* retrieved_key = NULL;
result = hessra_config_get_public_key(config, &retrieved_key);
if (result != SUCCESS) {
// Handle error
}
// Clean up
hessra_public_key_free(retrieved_key);
hessra_config_free(config);
A more comprehensive example can be found in the examples/test.c file, which demonstrates:
To build and run the example:
# Build the library
cargo build --release
# Build the example (Unix-like systems)
gcc -o test examples/test.c -L./target/release -lhessra_ffi -I./include
# Run the example (Linux)
LD_LIBRARY_PATH=./target/release ./test
# Run the example (macOS)
DYLD_LIBRARY_PATH=./target/release ./test
The FFI library is tested through:
examples/test.c file serves as a basic functional testTo run the memory safety tests:
# Build the example in debug mode
cargo build
gcc -o test_debug examples/test.c -L./target/debug -lhessra_ffi -I./include
# Run with Valgrind
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes \
--trace-children=yes LD_LIBRARY_PATH=./target/debug ./test_debug
Apache License 2.0