Rust Requires Chilkat v11.0.0+
Rust
ABN AMRO Create Signed JSON Web Token
See more ABN AMRO Examples
Demonstrates how to create a signed JWT to be used for authenticating requests to the ABN AMRO REST API's.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Create public/private key pair (RSA)
let rsa = chilkat::Rsa::new();
// Generate a 2048-bit key.
let privkey = chilkat::PrivateKey::new();
if rsa.gen_key(2048, &privkey).is_err() {
println!("{}", rsa.last_error_text());
return;
}
// Export the key to PEM files.
// Write one PEM file for the private key, and one for the public key.
let _ = privkey.save_pem_file("qa_data/pem/abnAmroPrivateKey.pem").is_ok();
let pubkey = chilkat::PublicKey::new();
let _ = privkey.to_public_key(&pubkey);
let _ = pubkey.save_pem_file(true, "qa_data/pem/abnAmroPublicKey.pem").is_ok();
// Note: Please share your public key along with your app name and developer email id at api.support@nl.abnamro.com.
// Token generation will not work unless public key is associated with your app.
// Create the JWT.
let jwt = chilkat::Jwt::new();
// Create the header:
// {
// "typ": "JWT",
// "alg": "RS256"
// }
let json_header = chilkat::JsonObject::new();
let _ = json_header.update_string("typ", "JWT");
let _ = json_header.update_string("alg", "RS256");
// Create the payload:
// {
// "nbf": 1499947668,
// "exp": 1499948668,
// "iss": "me",
// "sub": "anApiKey",
// "aud": "https://auth-sandbox.abnamro.com/oauth/token"
// }
let json_payload = chilkat::JsonObject::new();
let cur_date_time = jwt.gen_numeric_date(0);
// Set the "not process before" timestamp to now.
let _ = json_payload.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 _ = json_payload.add_int_at(-1, "exp", cur_date_time + 3600).is_ok();
let _ = json_payload.update_string("iss", "me");
let _ = json_payload.update_string("sub", "anApiKey");
let _ = json_payload.update_string("aud", "https://auth-sandbox.abnamro.com/oauth/token");
// Produce the smallest possible JWT:
jwt.set_auto_compact(true);
let Ok(jwt_str) = jwt.create_jwt_pk(&json_header.emit().unwrap_or_default(), &json_payload.emit().unwrap_or_default(), &privkey) else {
println!("{}", jwt.last_error_text());
return;
};
// Here is the JWT:
println!("{}", jwt_str);