jwt_authentication/jwt_authentication.rs
1//! JWT Authentication Example
2//!
3//! This example demonstrates JWT-based authentication for wallet access.
4//! Enterprise customers may use external authorization providers to authorize users.
5//! This shows how to:
6//! - Register a subject on a user record in Privy
7//! - Exchange a valid JWT for that subject for a short-lived authorization key
8//! - Use the authorization key for wallet operations
9//!
10//! ## Required Environment Variables
11//! - `PRIVY_APP_ID`: Your Privy app ID
12//! - `PRIVY_APP_SECRET`: Your Privy app secret
13//! - `PRIVY_USER_JWT`: Valid JWT token for the user
14//!
15//! ## Usage
16//! ```bash
17//! cargo run --example jwt_authentication
18//! ```
19
20use anyhow::Result;
21use privy_rs::{PrivyClient, generated::types::AuthenticateBody};
22use tracing_subscriber::EnvFilter;
23
24#[tokio::main]
25async fn main() -> Result<()> {
26 tracing_subscriber::fmt()
27 .with_env_filter(
28 EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
29 )
30 .init();
31
32 // Get JWT from environment and initialize client
33 let user_jwt =
34 std::env::var("PRIVY_USER_JWT").expect("PRIVY_USER_JWT environment variable not set");
35 let client = PrivyClient::new_from_env()?;
36
37 tracing::info!("initialized privy client from environment");
38
39 let jwt_auth = client
40 .wallets()
41 .authenticate_with_jwt(&AuthenticateBody {
42 user_jwt,
43 encryption_type: None,
44 recipient_public_key: None,
45 })
46 .await?;
47
48 tracing::info!("got jwt auth: {:?}", jwt_auth);
49
50 Ok(())
51}