Skip to content

Instantly share code, notes, and snippets.

@stravos97
Created May 31, 2024 23:35
Show Gist options
  • Save stravos97/eb584a1adc9e64728b35d3b34b5f1763 to your computer and use it in GitHub Desktop.
Save stravos97/eb584a1adc9e64728b35d3b34b5f1763 to your computer and use it in GitHub Desktop.
This script creates secure password entries using the pass password manager. It sets a directory for password storage, prompts the user for a title, URL, username, and email, and optionally generates a secure password. The title is converted to camel case, and the user can choose an email from predefined options. Finally, it compiles the provided information and stores it as a new password entry in the pass system.
Ensure all CHANGE-ME placeholders are replaced with actual values before use.
#!/bin/bash
# Function to convert a string to camel case
to_camel_case() {
echo "$1" | awk -F'[[:space:]]' '{for(i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) tolower(substr($i,2))}1' OFS=""
}
# Define the base directory for the entries
PASSWORD_STORE_DIR="$HOME/.password-store/CHANGE-ME"
# Check if the password store directory exists
if [ ! -d "$HOME/.password-store" ]; then
echo "Error: Password store directory does not exist. Please initialize your password store first."
exit 1
fi
# Ensure the base directory exists
mkdir -p "$PASSWORD_STORE_DIR"
# Prompt for the Title
read -p "Enter the Title (this will be the name of the entry) [required]: " Title
# Convert the Title to camel case
Title=$(to_camel_case "$Title")
# Prompt for the URL
read -p "Enter the URL (leave empty if not applicable): " URL
# Prompt for the Username
read -p "Enter the Username [required]: " Username
# Email options
emails=(
"CHANGE-ME"
"CHANGE-ME"
"CHANGE-ME"
"CHANGE-ME"
"CHANGE-ME"
)
echo "Select an email address (leave blank if not applicable):"
echo "1) CHANGE-ME"
echo "2) CHANGE-ME"
echo "3) CHANGE-ME"
echo "4) CHANGE-ME"
echo "5) CHANGE-ME"
echo "Enter the number corresponding to your email choice, or leave blank to skip:"
read email_choice
case $email_choice in
1) email=${emails[0]};;
2) email=${emails[1]};;
3) email=${emails[2]};;
4) email=${emails[3]};;
5) email=${emails[4]};;
"") email="";;
*) echo "Invalid selection. No email will be added.";;
esac
# Prompt for the Password
read -sp "Enter the Password (leave empty to generate a secure password): " Password
echo
# Handle potential empty values
if [ -z "$Title" ] || [ -z "$Username" ]; then
echo "Title and Username are required. Please try again."
exit 1
fi
# Generate a secure password if not provided
if [ -z "$Password" ]; then
# Prompt for the Password length
read -p "Enter the desired password length (default is 22): " PasswordLength
PasswordLength=${PasswordLength:-22}
# Prompt for password type
echo "Select the type of password to generate:"
echo "1) Alphanumeric only"
echo "2) Alphanumeric with special characters"
read -p "Enter 1 or 2: " PasswordType
if [ "$PasswordType" == "1" ]; then
Password=$(LC_ALL=C tr -dc 'A-Za-z0-9' < /dev/urandom | head -c $PasswordLength)
else
Password=$(LC_ALL=C tr -dc 'A-Za-z0-9!@#$%^&*()_+~' < /dev/urandom | head -c $PasswordLength)
fi
echo "Generated secure password: $Password"
fi
# Define the entry path and content
ENTRY_PATH="CHANGE-ME/$Title"
# Build the content string
CONTENT="$Password
user: $Username"
if [ -n "$email" ]; then
CONTENT="$CONTENT
email: $email"
fi
if [ -n "$URL" ]; then
CONTENT="$CONTENT
url: $URL"
fi
# Create the entry in the pass store
echo "$CONTENT" | pass insert --multiline "$ENTRY_PATH"
echo "Password entry has been created successfully in the 'pass CHANGE-ME' directory."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment