Created
October 9, 2024 15:33
-
-
Save TheLexoPlexx/736717651c8d5c68993c5923b62b5b62 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * | |
| [package] | |
| name = "supabase_jwt" | |
| version = "0.1.0" | |
| edition = "2021" | |
| [dependencies] | |
| clap = "3" | |
| chrono = { version = "0.4", features = ["serde"] } | |
| jsonwebtoken = "8" | |
| serde = { version = "1", features = ["derive"] } | |
| * | |
| */ | |
| use chrono::Utc; | |
| use clap::{App, Arg}; | |
| use jsonwebtoken::{encode, EncodingKey, Header}; | |
| use serde::Serialize; | |
| #[derive(Debug, Serialize)] | |
| struct Payload { | |
| role: String, | |
| iss: String, | |
| iat: i64, | |
| exp: i64, | |
| } | |
| fn main() -> Result<(), Box<dyn std::error::Error>> { | |
| let matches = App::new("Supabase JWT Generator") | |
| .arg(Arg::with_name("SECRET").required(true).takes_value(true)) | |
| .get_matches(); | |
| let secret = matches.value_of("SECRET").unwrap(); | |
| let now = Utc::now().timestamp(); | |
| let expiry = now + (2 * 365 * 24 * 60 * 60) as i64; // 2 years | |
| println!("issued_at {}", now); | |
| println!("expires {}", expiry); | |
| let payload_anon = Payload { | |
| role: "anon".to_string(), | |
| iss: "supabase".to_string(), | |
| iat: now, | |
| exp: expiry, | |
| }; | |
| let payload_service_role = Payload { | |
| role: "service_role".to_string(), | |
| iss: "supabase".to_string(), | |
| iat: now, | |
| exp: expiry, | |
| }; | |
| let encoded_anon = encode( | |
| &Header::default(), | |
| &payload_anon, | |
| &EncodingKey::from_secret(secret.as_bytes()), | |
| ) | |
| .expect("Failed to generate encoded anon from secret"); | |
| let encoded_sr = encode( | |
| &Header::default(), | |
| &payload_service_role, | |
| &EncodingKey::from_secret(secret.as_bytes()), | |
| ) | |
| .expect("Failed to generate encoded string from secret"); | |
| println!(" "); | |
| println!("JWT_SECRET={}", secret); | |
| println!("SERVICE_ROLE_KEY={}", encoded_sr); | |
| println!("ANON_KEY={}", encoded_anon); | |
| println!(" "); | |
| println!("SUPABASE_SERVICE_ROLE_KEY={}", encoded_sr); | |
| println!("NEXT_PUBLIC_SUPABASE_ANON_KEY={}", encoded_anon); | |
| println!(" "); | |
| Ok(()) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment