-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a cookie auth system for the rpc endpoint
- Loading branch information
1 parent
ac1242a
commit 8b49c18
Showing
6 changed files
with
114 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//! Cookie-based authentication for the RPC server. | ||
use base64::Engine; | ||
use rand::RngCore; | ||
|
||
use std::{ | ||
fs::{remove_file, File}, | ||
io::{Read, Write}, | ||
}; | ||
|
||
/// The user field in the cookie (arbitrary, only for recognizability in debugging/logging purposes) | ||
pub const COOKIEAUTH_USER: &str = "__cookie__"; | ||
/// Default name for auth cookie file */ | ||
const COOKIEAUTH_FILE: &str = ".cookie"; | ||
|
||
/// Generate a new auth cookie and return the encoded password. | ||
pub fn generate() -> Option<String> { | ||
let mut data = [0u8; 32]; | ||
rand::thread_rng().fill_bytes(&mut data); | ||
let encoded_password = base64::prelude::BASE64_STANDARD.encode(data); | ||
let cookie_content = format!("{}:{}", COOKIEAUTH_USER, encoded_password); | ||
|
||
let mut file = File::create(COOKIEAUTH_FILE).ok()?; | ||
file.write_all(cookie_content.as_bytes()).ok()?; | ||
|
||
tracing::info!("RPC auth cookie generated successfully"); | ||
|
||
Some(encoded_password) | ||
} | ||
|
||
/// Get the encoded password from the auth cookie. | ||
pub fn get() -> Option<String> { | ||
let mut file = File::open(COOKIEAUTH_FILE).ok()?; | ||
let mut contents = String::new(); | ||
file.read_to_string(&mut contents).ok()?; | ||
|
||
let parts: Vec<&str> = contents.split(":").collect(); | ||
Some(parts[1].to_string()) | ||
} | ||
|
||
/// Delete the auth cookie. | ||
pub fn delete() -> Option<()> { | ||
remove_file(COOKIEAUTH_FILE).ok()?; | ||
tracing::info!("RPC auth cookie deleted successfully"); | ||
Some(()) | ||
} |
This file contains 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