Skip to content

Commit c72b819

Browse files
committed
examples: license_assertion_jwks verify + JWT via JWKS
1 parent b94723c commit c72b819

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ path = "src/lib.rs"
5151
name = "basic_usage"
5252
path = "examples/basic_usage.rs"
5353

54+
[[example]]
55+
name = "license_assertion_jwks"
56+
path = "examples/license_assertion_jwks.rs"
57+
5458
[profile.release]
5559
opt-level = 3
5660
lto = true

examples/license_assertion_jwks.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//! Verify a license, then validate `license_token` with JWKS (RS256).
2+
//!
3+
//! ```bash
4+
//! export LICENSECHAIN_API_KEY=...
5+
//! export LICENSECHAIN_LICENSE_KEY=...
6+
//! cargo run --example license_assertion_jwks
7+
//! ```
8+
9+
use licensechain::{LicenseChainClient, LicenseChainConfig};
10+
use std::time::Duration;
11+
12+
#[tokio::main]
13+
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
14+
let api_key = std::env::var("LICENSECHAIN_API_KEY").unwrap_or_default();
15+
let license_key = std::env::var("LICENSECHAIN_LICENSE_KEY").unwrap_or_default();
16+
if api_key.is_empty() || license_key.is_empty() {
17+
eprintln!("Set LICENSECHAIN_API_KEY and LICENSECHAIN_LICENSE_KEY");
18+
std::process::exit(1);
19+
}
20+
21+
let config = LicenseChainConfig {
22+
api_key,
23+
base_url: std::env::var("LICENSECHAIN_BASE_URL")
24+
.unwrap_or_else(|_| "https://api.licensechain.app".to_string()),
25+
timeout: Duration::from_secs(30),
26+
retries: 2,
27+
};
28+
let client = LicenseChainClient::new(config);
29+
30+
let details = client
31+
.verify_license_with_details(&license_key, None)
32+
.await?;
33+
let valid = details
34+
.get("valid")
35+
.and_then(|v| v.as_bool())
36+
.unwrap_or(false);
37+
if !valid {
38+
eprintln!("License not valid: {details}");
39+
std::process::exit(1);
40+
}
41+
42+
let token = details
43+
.get("license_token")
44+
.and_then(|v| v.as_str())
45+
.unwrap_or("");
46+
let jwks = details
47+
.get("license_jwks_uri")
48+
.and_then(|v| v.as_str())
49+
.unwrap_or("");
50+
if token.is_empty() || jwks.is_empty() {
51+
println!("No license_token or license_jwks_uri — enable LICENSE_JWT_* on Core API for this seller.");
52+
return Ok(());
53+
}
54+
55+
let claims = client
56+
.verify_license_assertion_jwt(token, jwks, None)
57+
.await?;
58+
println!("verified claims: {claims}");
59+
Ok(())
60+
}

0 commit comments

Comments
 (0)