|
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 | | -} |
| 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/v1".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