brokkr-broker::utils::pak Rust
Prefixed API Key (PAK) management utilities.
This module provides functionality for creating, verifying, and managing Prefixed API Keys using a singleton controller pattern.
Functions
brokkr-broker::utils::pak::create_pak_controller
pub
#![allow(unused)]
fn main() {
fn create_pak_controller (config : Option < & Settings > ,) -> Result < Arc < PrefixedApiKeyController < OsRng , Sha256 > > , & 'static str >
}
Creates or retrieves the PAK controller.
Parameters:
| Name | Type | Description |
|---|---|---|
config | - | Optional settings for initializing the controller. |
Returns:
Returns a Result containing the Arc-wrapped PAK controller or an error message.
Source
#![allow(unused)]
fn main() {
pub fn create_pak_controller(
config: Option<&Settings>,
) -> Result<Arc<PrefixedApiKeyController<OsRng, Sha256>>, &'static str> {
match (PAK_CONTROLLER.get(), config) {
(Some(controller), _) => Ok(controller.clone()),
(None, Some(cfg)) => {
let controller = PAK_CONTROLLER.get_or_init(|| {
info!("Initializing PAK_CONTROLLER for the first time");
Arc::new(create_pak_controller_inner(cfg).expect("Failed to create PAK controller"))
});
Ok(controller.clone())
}
(None, None) => Err("PAK_CONTROLLER not initialized and no config provided"),
}
}
}
brokkr-broker::utils::pak::create_pak_controller_inner
private
#![allow(unused)]
fn main() {
fn create_pak_controller_inner (config : & Settings ,) -> Result < PrefixedApiKeyController < OsRng , Sha256 > , Box < dyn std :: error :: Error > >
}
Internal function to create a new PAK controller.
Parameters:
| Name | Type | Description |
|---|---|---|
config | - | Settings for configuring the PAK controller. |
Returns:
Returns a Result containing the new PAK controller or an error.
Source
#![allow(unused)]
fn main() {
fn create_pak_controller_inner(
config: &Settings,
) -> Result<PrefixedApiKeyController<OsRng, Sha256>, Box<dyn std::error::Error>> {
// This function remains unchanged
let builder = PrefixedApiKeyController::configure()
.prefix(config.pak.prefix.clone().unwrap())
.short_token_length(config.pak.short_token_length.unwrap())
.short_token_prefix(config.pak.short_token_prefix.clone())
.long_token_length(config.pak.long_token_length.unwrap())
.rng_osrng()
.digest_sha256();
builder.finalize().map_err(|e| e.into())
}
}
brokkr-broker::utils::pak::create_pak
pub
#![allow(unused)]
fn main() {
fn create_pak () -> Result < (String , String) , Box < dyn std :: error :: Error > >
}
Generates a new Prefixed API Key and its hash.
Returns:
Returns a Result containing a tuple of the PAK string and its hash, or an error.
Source
#![allow(unused)]
fn main() {
pub fn create_pak() -> Result<(String, String), Box<dyn std::error::Error>> {
let controller = create_pak_controller(None)?;
// Generate PAK and hash
controller
.try_generate_key_and_hash()
.map(|(pak, hash)| (pak.to_string(), hash))
.map_err(|e| e.into())
}
}
brokkr-broker::utils::pak::verify_pak
pub
#![allow(unused)]
fn main() {
fn verify_pak (pak : String , stored_hash : String) -> bool
}
Verifies a Prefixed API Key against a stored hash.
Parameters:
| Name | Type | Description |
|---|---|---|
pak | - | The Prefixed API Key to verify. |
stored_hash | - | The previously stored hash to compare against. |
Returns:
Returns true if the PAK is valid, false otherwise.
Source
#![allow(unused)]
fn main() {
pub fn verify_pak(pak: String, stored_hash: String) -> bool {
let pak = PrefixedApiKey::from_string(pak.as_str()).expect("Failed to parse PAK");
let controller = create_pak_controller(None).expect("Failed to create PAK controller");
let computed_hash = controller.long_token_hashed(&pak);
stored_hash == computed_hash
}
}
brokkr-broker::utils::pak::generate_pak_hash
pub
#![allow(unused)]
fn main() {
fn generate_pak_hash (pak : String) -> String
}
Generates a hash for a given Prefixed API Key.
Parameters:
| Name | Type | Description |
|---|---|---|
pak | - | The Prefixed API Key to hash. |
Returns:
Returns the generated hash as a String.
Source
#![allow(unused)]
fn main() {
pub fn generate_pak_hash(pak: String) -> String {
let pak = PrefixedApiKey::from_string(pak.as_str()).expect("Failed to parse PAK");
let controller = create_pak_controller(None).expect("Failed to create PAK controller");
controller.long_token_hashed(&pak)
}
}