Sample code for 30+ languages & platforms
Rust

bitzlato.com whoami

See more JSON Web Token (JWT) Examples

Demonstrates sending a request to the bitzlato.com whoami endpoint using an ES256 JWT token for authentication.

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

//  Use the following ECC key loaded from JWK format.
let jwk = chilkat::JsonObject::new();
let _ = jwk.update_string("kty", "EC").is_ok();
let _ = jwk.update_string("crv", "P-256").is_ok();
let _ = jwk.update_string("x", "...").is_ok();
let _ = jwk.update_string("y", "...").is_ok();
let _ = jwk.update_string("d", "...").is_ok();

let ecc_key = chilkat::PrivateKey::new();
if ecc_key.load_jwk(&jwk.emit().unwrap_or_default()).is_err() {
    println!("{}", ecc_key.last_error_text());
    return;
}

let jwt = chilkat::Jwt::new();

// Build the JOSE header
let jose = chilkat::JsonObject::new();
let _ = jose.append_string("format", "compact").is_ok();
let _ = jose.append_string("alg", "ES256").is_ok();

// Now build the JWT claims (also known as the payload)

// Our JWT claims will contain members as shown here:

// 	{
// 	  "email":"your_email@example.com",
// 	  "aud":"usr",
// 	  "iat":"1588286154",
// 	  "jti":"555D9123"
// 	}

let claims = chilkat::JsonObject::new();
let _ = claims.append_string("jti", "555D9123");
let _ = claims.append_string("email", "your_email@example.com");

// Set the timestamp of when the JWT was created to now minus 60 seconds
let cur_date_time = jwt.gen_numeric_date(-60);
let _ = claims.add_int_at(-1, "iat", cur_date_time).is_ok();

// Set the "not process before" timestamp to now minus 60 seconds
let _ = claims.add_int_at(-1, "nbf", cur_date_time).is_ok();

// Set the timestamp defining an expiration time (end time) for the token
// to be now + 1 hour (3600 seconds)
let _ = claims.add_int_at(-1, "exp", cur_date_time + 3600).is_ok();

let _ = claims.append_string("aud", "usr");

// Produce the smallest possible JWT:
jwt.set_auto_compact(true);

// Create the JWT token.  This is where the RSA signature is created.
let jwt_token = jwt.create_jwt_pk(&jose.emit().unwrap_or_default(), &claims.emit().unwrap_or_default(), &ecc_key).unwrap_or_default();

println!("{}", jwt_token);

// Send the HTTPS GET with the jwt_token used for Authorization.
let http = chilkat::Http::new();
http.set_auth_token(&jwt_token);
let Ok(response_str) = http.quick_get_str("https://bitzlato.com/api/auth/whoami") else {
    println!("{}", http.last_error_text());
    return;
};

println!("status code = {}", http.last_status());
println!("{}", response_str);