pub struct Secret { /* private fields */ }Expand description
Secure credential storage and management system.
This structure manages the complete lifecycle of sensitive credentials including user prompting, encryption, file storage, and decryption. It provides a high-level interface for secure credential handling.
ยงDesign Principles
- Lazy Loading: Passwords only decrypted when needed
- Immutable State: Credential changes create new instances
- Error Recovery: Graceful handling of encryption/decryption failures
- User Friendly: Clear prompts and error messages
ยงInternal State
The struct maintains both encrypted and decrypted state:
- File-based encrypted storage for persistence
- Memory-based decrypted storage for immediate use
- Cryptographic keys for encryption/decryption operations
ยงLifecycle
- Creation: Initialize with file path and user prompt
- Retrieval: Check for existing encrypted file
- Prompting: Secure password input when needed
- Encryption: AES encryption before file storage
- Decryption: AES decryption when retrieving
Implementationsยง
Sourceยงimpl Secret
impl Secret
Sourcepub fn new(secret_name: &str, prompt: &str) -> Self
pub fn new(secret_name: &str, prompt: &str) -> Self
Creates a new Secret instance for credential management.
This constructor initializes a new secret manager with the specified file storage location and user prompt text. It loads encryption keys from compile-time embedded metadata and prepares the file path within the applicationโs data directory.
ยงKey Management
Encryption keys are loaded from compile-time metadata:
- Keys embedded during build process for security
- Consistent keys across application installations
- No runtime key generation or derivation needed
ยงFile Path Resolution
The secret file path is resolved using the applicationโs data storage:
- Platform-appropriate user data directory
- Service-specific filename for credential isolation
- Automatic directory creation when needed
ยงArguments
secret_name- Filename for storing encrypted credentials (e.g., โ.jira_secretโ)prompt- User-facing text for password prompts (e.g., โEnter your Jira passwordโ)
ยงReturns
A new Secret instance ready for credential operations.
ยงExamples
use kasl::libs::secret::Secret;
// Jira password management
let jira_secret = Secret::new(".jira_secret", "Enter your Jira password");
// GitLab token management
let gitlab_secret = Secret::new(".gitlab_token", "Enter your GitLab API token");
// SI Server credentials
let si_secret = Secret::new(".si_credentials", "Enter your SI Server password");ยงError Handling
Path resolution errors are handled gracefully by falling back to the current directory if the data storage path cannot be created.
Sourcepub fn get_or_prompt(&self) -> Result<String>
pub fn get_or_prompt(&self) -> Result<String>
Retrieves password from cache or prompts user if not available.
This method implements the primary credential retrieval logic:
- Check if encrypted file exists and is readable
- Attempt to decrypt existing credentials
- Prompt user for new credentials if decryption fails
- Encrypt and store new credentials for future use
ยงCaching Behavior
- Cache Hit: Return decrypted password from file
- Cache Miss: Prompt user and store new password
- Decryption Error: Re-prompt user (file may be corrupted)
ยงError Recovery
If decryption fails (corrupted file, wrong keys, etc.), the method gracefully falls back to prompting the user for new credentials. This ensures the application can recover from storage corruption.
ยงReturns
Returns the userโs password, either from cache or fresh input.
ยงErrors
Returns an error if:
- User cancels password prompt
- File system operations fail
- Encryption operations fail
ยงExamples
use kasl::libs::secret::Secret;
let secret = Secret::new(".api_token", "Enter API token");
// First call prompts user and caches result
let token = secret.get_or_prompt()?;
// Subsequent calls use cached value
let same_token = secret.get_or_prompt()?;Sourcepub fn prompt(&self) -> Result<String>
pub fn prompt(&self) -> Result<String>
Prompts user for password and stores it securely.
This method handles the complete password input and storage workflow:
- Display secure password prompt (no echo)
- Encrypt the entered password
- Store encrypted data to file
- Return the entered password
ยงSecurity Features
- No Echo: Password characters not displayed on screen
- Immediate Encryption: Password encrypted before file storage
- Memory Clearing: Original password cleared after encryption
ยงUser Experience
The prompt uses a colorful theme for better visibility and provides clear instructions to the user. Password input is handled securely without displaying characters.
ยงReturns
Returns the password entered by the user.
ยงErrors
Returns an error if:
- User cancels password input (Ctrl+C)
- Password encryption fails
- File system write operations fail
ยงExamples
use kasl::libs::secret::Secret;
let secret = Secret::new(".password", "Enter your password");
// Force password prompt (ignores cache)
let password = secret.prompt()?;