summaryrefslogtreecommitdiff
path: root/pt
blob: bbbd4919ae7128a408dcbd353ec92f63a018208f (plain)
    1 #!/usr/bin/env bash
    2 #
    3 # Automatically types the username and password from the pass entry specified
    4 # as argument one. The required password-store backend is pass, the standard
    5 # unix password manager.
    6 #
    7 # A simple workflow for this may be to go to a website requiring a username and
    8 # password. Select the username field, open a program launcher such as rofi or
    9 # dmenu, then execute 'quicktype password-name'.
   10 #
   11 # Note that for this to work, each pass entry must match the following criteria
   12 # * The password is on the first line (this is the pass standard)
   13 # * The username can be anywhere in the file, but the line must look like
   14 #   'username: <password>'
   15 # 
   16 # Depends on: pass xdotool
   17 #
   18 
   19 
   20 export PASSWORD_STORE_DIR=~/.password-store
   21 
   22 
   23 #
   24 # Writes log output to terminal and notification daemon, if one is present.
   25 #
   26 function log {
   27   local msg="${@}"
   28 
   29   if [[ $(type -p notify-send) != '' ]]; then
   30     notify-send "$(basename ${0}):" "${msg}"
   31   fi
   32   echo ${msg}
   33 }
   34 
   35 
   36 #
   37 # Ensures required tools are available, returning code 1 and log if any of them
   38 # are not.
   39 #
   40 function check_env {
   41   # Verify pass is installed and in the PATH.
   42   if [[ $(type -p pass) == '' ]]; then
   43     log "Error: Could not find pass." && return 1
   44   fi
   45 
   46   # Verify xdotool is installed and in the path
   47   if [[ $(type -p xdotool) ==  '' ]]; then
   48     log "Error: Could not find xdotool." && return 1
   49   fi
   50 
   51   # Verify /dev/null is a character device, on the off chance someone with root
   52   # access is trying to capture output redirected there.
   53   local devstat=$(ls -l /dev/null)
   54   if [[ ${devstat:0:1} != 'c' ]]; then
   55     log "Error: /dev/null is not a character device."
   56     return 1
   57   fi
   58 
   59   return 0
   60 }
   61 
   62 
   63 function main {
   64   # Exit failure if no password was specified
   65   [[ -z ${1} ]] && log "Please specify a password to be typed" && exit 1
   66 
   67   check_env || exit 0
   68   
   69   # Check if the password exists in the store
   70   pass ${@} &> /dev/null
   71   [[ $? -gt 0 ]] && log "Error: '${@}' is not in the password store" && exit 1
   72   
   73   # Parse pass output into appropriate variables 
   74   local passfile="$(pass ${@})"
   75   local username=$(echo -e "${passfile}" | sed -n 's/username: \(.*\)/\1/p' | head -n 1)
   76   local pass=$(echo -e "${passfile}" | head -n 1)
   77   
   78   # Type the password
   79   xdotool type --delay 15 "${username}	${pass}"
   80 }
   81 
   82 main ${@}

Generated by cgit