From 2d2bff09485103aef108ce63c7f17e7d4770829c Mon Sep 17 00:00:00 2001 From: Wesley Irvin Date: Sat, 21 Dec 2024 12:08:06 -0500 Subject: [PATCH] Write auth key to file This commit allows us to now write the auth key to the proper file. After writing and restarting tsdproxy everything seems to be getting picked up and is working successfully. --- src/lib.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7135856..bab6798 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ use std::error::Error; -use std::fs::read_to_string; +use std::fs::{read_to_string, File}; +use std::io::Write; use oauth2::basic::BasicClient; use oauth2::reqwest::http_client; @@ -9,16 +10,23 @@ mod types; use types::{AuthKeyRequest, AuthKeyResponse, ClientAuth}; pub fn run() -> Result<(), Box> { + println!("Reading Client ID And Secret From File"); let client_auth = read_client_auth()?; - println!("Client ID: {}", client_auth.id); - println!("Client Secret: {}", client_auth.secret); - + println!("Getting API Token With Client ID: {}", client_auth.id); let access_token = get_api_token(client_auth.id, client_auth.secret)?; + println!("Generating New Auth Key"); let auth_key = get_auth_key(access_token)?; - println!("Auth Key: {}", auth_key); + println!("Writing Auth Key to File"); + write_auth_key(auth_key)?; + + println!( + "Auth key has been successfully updated.\n +*** Make sure in your tsdproxy config you are pointing to the file '/config/.auth-key' to pick up the key. *** +*** Make sure you restart tsdproxy for the new keys to be used ***" + ); Ok(()) } @@ -81,3 +89,11 @@ fn get_auth_key(api_token: AccessToken) -> Result> { Ok(auth_response.get_auth_key()) } + +fn write_auth_key(auth_key: String) -> Result<(), Box> { + let mut keyfile = File::create("config/.auth-key")?; + + keyfile.write_all(auth_key.as_bytes())?; + + Ok(()) +}