Rust Requires Chilkat v11.0.0+
Rust
PRODA Get OAuth2 Access Token using JWT
See more PRODA Examples
Demonstrates how to get an OAuth2 access token for the PRODA Australian Government Online Services using a JWT.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// First create a JWT to be sent in the POST to https://vnd.proda.humanservices.gov.au/mga/sps/oauth/oauth20/token
let priv_key = chilkat::PrivateKey::new();
// Load an RSA private key from a PEM file.
// Chilkat provides alternative methods to load from other formats, or to load from a string or binary data.
if priv_key.load_encrypted_pem_file("qa_data/pem/rsa_passwd.pem", "passwd").is_err() {
println!("{}", priv_key.last_error_text());
return;
}
let jwt = chilkat::Jwt::new();
// Build the JOSE header
let jose = chilkat::JsonObject::new();
// Use RS256. Pass the string "RS384" or "RS512" to use RSA with SHA-384 or SHA-512.
let _ = jose.append_string("alg", "RS256").is_ok();
let _ = jose.append_string("typ", "JWT").is_ok();
let _ = jose.append_string("kid", "test-device").is_ok();
// Now build the JWT claims (also known as the payload)
let claims = chilkat::JsonObject::new();
let _ = claims.append_string("iss", "9646844092").is_ok();
let _ = claims.append_string("sub", "test-device").is_ok();
let _ = claims.append_string("aud", "https://proda.humanservices.gov.au").is_ok();
// Set the timestamp of when the JWT was created to now.
let cur_date_time = jwt.gen_numeric_date(0);
let _ = claims.add_int_at(-1, "iat", 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();
// 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(), &priv_key).unwrap_or_default();
// ---------------------------------------------------------------------
// Build and send the POST, which should look something like this:
// POST https://vnd.proda.humanservices.gov.au/mga/sps/oauth/oauth20/token HTTP/1.1
// Content-Type: application/x-www-form-urlencoded
// Content-Length: 666
// Host: vnd.proda.humanservices.gov.au
//
// grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=<jwt>&client_id=VendorClient03
let http = chilkat::Http::new();
let req = chilkat::HttpRequest::new();
req.set_http_verb("POST");
req.set_content_type("application/x-www-form-urlencoded");
// Add the request params.
req.add_param("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer");
req.add_param("assertion", &jwt_token);
req.add_param("client_id", "VendorClient03");
let resp = chilkat::HttpResponse::new();
if http.http_req("https://vnd.proda.humanservices.gov.au/mga/sps/oauth/oauth20/token", &req, &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
println!("Response status code = {}", resp.status_code());
println!("Response body:");
println!("{}", resp.body_str());