Skip to main content

ri_jwt_manager_new

Function ri_jwt_manager_new 

Source
#[unsafe(no_mangle)]
pub extern "C" fn ri_jwt_manager_new( secret: *const c_char, expiry_secs: u64, ) -> *mut CRiJWTManager
Expand description

Creates a new CRiJWTManager instance with specified secret and expiration.

Initializes a JWT manager for token generation and validation. The manager uses HMAC-SHA256 (HS256) algorithm for signing tokens. The secret key must be kept confidential and should be at least 256 bits (32 bytes) for adequate security.

§Parameters

  • secret: Pointer to null-terminated C string containing the JWT signing secret. Must not be NULL. Empty strings are accepted but provide minimal security.
  • expiry_secs: Token expiration time in seconds from issuance. Tokens will be rejected as expired after this duration. Typical values range from 300 (5 minutes) for sensitive operations to 86400 (24 hours) for long-lived sessions.

§Returns

Pointer to newly allocated CRiJWTManager on success, or NULL if:

  • secret parameter is NULL
  • Memory allocation fails
  • Secret contains invalid UTF-8 sequences

§Security Considerations

The secret key should be:

  • Generated using cryptographically secure random number generator
  • Stored securely (environment variables, secrets manager)
  • Rotated periodically in production environments
  • Unique per environment (development, staging, production)

§Example

CRiJWTManager* jwt = ri_jwt_manager_new(
    "your-256-bit-secret-key-here",
    3600  // 1 hour expiration
);
if (jwt == NULL) {
    // Handle initialization failure
}