summaryrefslogtreecommitdiff
path: root/tessen
blob: 26149d0e14f1f5a23bcb50b4f1bda032b318f3f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2021 Ayush Agarwal <ayush at fastmail dot in>
#
# tessen - a data selection interface for pass using bemenu or rofi on Wayland
# ------------------------------------------------------------------------------

# shell "strict" mode
set -uo pipefail
readonly PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
export PATH
umask 077

# initialize the global variables
readonly VERSION="1.1.0"
readonly PREFIX="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
readonly CLIP_TIME="${PASSWORD_STORE_CLIP_TIME:-15}"
BACKEND="${TESSEN_BACKEND:-bemenu}" # uses the value of TESSEN_BACKEND if set by user
BACKEND_OPTS=""
AT_TYPE="${TESSEN_AUTOTYPE-}" # uses the value of TESSEN_AUTOTYPE if set by user
PASSFILE=""
declare -A PASSDATA_ARR
USERNAME=""
PASSWORD=""

# display and get the shortened path of the password file
get_pass_file() {
  local tmp_pass_1 tmp_pass_2 tmp_pass_3

  # temporarily enable globbing to get the list of gpg files
  shopt -s nullglob globstar
  tmp_pass_1=("$PREFIX"/**/*.gpg)
  tmp_pass_2=("${tmp_pass_1[@]#"$PREFIX"/}")
  tmp_pass_3=("${tmp_pass_2[@]%.gpg}")
  shopt -u nullglob globstar

  PASSFILE="$(printf '%s\n' "${tmp_pass_3[@]}" | "$BACKEND" "$BACKEND_OPTS")"

  if ! [[ -e "$PREFIX/$PASSFILE".gpg ]]; then
    exit 1
  fi
}

# get the password data including every key-value pair inside the encrypted file
get_pass_data() {
  local passdata passdata_regex idx key val

  mapfile -t passdata < <(pass "$PASSFILE")
  # ASSUMPTION: the key can contain alphanumerics, spaces, hyphen, underscore
  #             the value can contain anything but it has to follow after a space
  passdata_regex="^[[:alnum:][:blank:]_-]+:[[:blank:]].+$"
  # ASSUMPTION: the basename of the gpg file is the username although one can still
  #             select a username field inside the file, if it exists
  USERNAME="${PASSFILE##*/}"
  # ASSUMPTION: the first line of $PASSFILE will contain the password
  PASSWORD="${passdata[0]}"

  # skip the password, validate each entry against $passdata_regex, store valid results
  # ASSUMPTION: each key is unique otherwise, the value of the last non-unique key will be used
  for idx in "${passdata[@]:1}"; do
    if [[ "$idx" =~ $passdata_regex ]]; then
      key="${idx%%:*}"
      val="${idx#*: }"
      PASSDATA_ARR["$key"]="$val"
    else
      continue
    fi
  done
}

# get the key that the user will choose to autotype or copy
get_key() {
  local ch="" flag=false key_arr=()

  if [[ "${1-}" == "pass_key_list" ]]; then
    key_arr=("autotype" "username" "password" "${!PASSDATA_ARR[@]}")
    shift
  elif [[ "${1-}" == "opt_key_list" ]]; then
    key_arr=("autotype" "copy")
    shift
  else
    exit 1
  fi

  _KEY="$(printf '%s\n' "${key_arr[@]}" | "$BACKEND" "$BACKEND_OPTS")"

  # validate the chosen key name
  for ch in "${key_arr[@]}"; do
    if [[ "$_KEY" == "$ch" ]]; then
      flag=true
      break
    fi
  done
  if [[ "$flag" == "false" ]]; then
    exit 1
  fi
}

# the 2nd, and possibly 3rd, stage of the menu
key_menu() {
  local tmp_key

  get_key pass_key_list

  if [[ "$_KEY" == "autotype" ]]; then
    auto_type username_password
    exit 0
  fi

  if [[ "${AT_TYPE-}" == "true" ]]; then
    auto_type "$_KEY"
    exit 0
  elif [[ "${AT_TYPE-}" == "false" ]]; then
    wld_copy "$_KEY"
  elif [[ -z "${AT_TYPE-}" ]]; then
    tmp_key="$_KEY"
    get_key opt_key_list
    if [[ "$_KEY" == "autotype" ]]; then
      auto_type "$tmp_key"
      exit 0
    elif [[ "$_KEY" == "copy" ]]; then
      wld_copy "$tmp_key"
    else
      exit 1
    fi
  fi
}

auto_type() {
  if [[ "${1-}" == "username_password" ]]; then
    printf '%s' "$USERNAME" | wtype -s 100 -
    wtype -s 100 -k Tab --
    printf '%s' "$PASSWORD" | wtype -s 100 -
    shift
  elif [[ "${1-}" == "username" ]]; then
    printf '%s' "$USERNAME" | wtype -s 100 -
    shift
  elif [[ "${1-}" == "password" ]]; then
    printf '%s' "$PASSWORD" | wtype -s 100 -
    shift
  elif [[ -n "${PASSDATA_ARR[${1-}]}" ]]; then
    printf '%s' "${PASSDATA_ARR[${1-}]}" | wtype -s 100 -
    shift
  else
    exit 1
  fi
}

wld_copy() {
  if [[ "${1-}" == "username" ]]; then
    printf '%s' "$USERNAME" | wl-copy
    notify-send -t $((CLIP_TIME * 1000)) "Copied username to clipboard. Will clear in $CLIP_TIME seconds."
    shift
    clean
  elif [[ "${1-}" == "password" ]]; then
    printf '%s' "$PASSWORD" | wl-copy
    notify-send -t $((CLIP_TIME * 1000)) "Copied password to clipboard. Will clear in $CLIP_TIME seconds."
    shift
    clean
  elif [[ -n "${PASSDATA_ARR[${1-}]}" ]]; then
    printf '%s' "${PASSDATA_ARR[${1-}]}" | wl-copy
    notify-send -t $((CLIP_TIME * 1000)) "Copied ${1-} to clipboard. Will clear in $CLIP_TIME seconds."
    shift
    clean
  else
    exit 1
  fi
}

print_help() {
  printf '%s\n' "${0##*/} - data selection interface for password-store on wayland" ""
  printf '%s\n' "Usage: ${0##*/} [options]" ""
  printf '%s\n' "  tessen                        use bemenu and either autotype or copy data"
  printf '%s\n' "  tessen -b rofi -t             use rofi and always autotype data"
  printf '%s\n' "  tessen -c                     use bemenu and always copy data" ""
  printf '%s\n' "  -b, --backend, --backend=     choose 'bemenu' or 'rofi' as backend (default: bemenu)"
  printf '%s\n' "  -t, --autotype                always autotype data"
  printf '%s\n' "  -c, --clipboard               always copy data"
  printf '%s\n' "  -h, --help                    print this help menu"
  printf '%s\n' "  -v, --version                 print the version of tessen" ""
  printf '%s\n' "For more details, visit https://github.com/ayushnix/pass-tessen"
}

validate_backend() {
  if [[ "$BACKEND" == "bemenu" ]]; then
    bmn_opt=("-i -l 10 -w --scrollbar=autohide -n")
    export BEMENU_OPTS="${BEMENU_OPTS:-${bmn_opt[*]}}"
    readonly BACKEND="bemenu"
    readonly BACKEND_OPTS=""
    unset -v bmn_opt
  elif [[ "$BACKEND" == "rofi" ]]; then
    readonly BACKEND="rofi"
    readonly BACKEND_OPTS="-dmenu"
  else
    printf '%s\n' "Please specify a backend: bemenu|rofi" >&2
    exit 1
  fi
}

validate_clip_time() {
  local clip_regex

  clip_regex="^[[:digit:]]+$"

  if [[ "$CLIP_TIME" =~ $clip_regex ]]; then
    return 0
  else
    notify-send "invalid clipboard time provided"
    exit 1
  fi
}

clean() {
  {
    sleep "$CLIP_TIME"
    wl-copy --clear
  } > /dev/null 2>&1 &
  disown
  unset -v PASSFILE USERNAME PASSWORD PASSDATA_ARR CHOICE
}

die() {
  wl-copy --clear
  unset -v PASSFILE USERNAME PASSWORD PASSDATA_ARR CHOICE
}

main() {
  local _opt

  # exit if the password store directory doesn't exist
  if ! [[ -d "$PASS_STORE" ]]; then
    notify-send "password store not found"
    exit 1
  fi

  # parse any options given by the user
  while [[ "$#" -gt 0 ]]; do
    _opt="${1-}"
    case "$_opt" in
      -b | --backend)
        [[ "$#" -lt 2 ]] && {
          printf '%s\n' "Please specify a backend: bemenu|rofi" >&2
          exit 1
        }
        BACKEND="${2-}"
        validate_backend
        shift
        ;;
      --backend=*)
        BACKEND="${_opt##--backend=}"
        validate_backend
        ;;
      -t | --autotype)
        unset -v AT_TYPE 2> /dev/null || {
          printf '%s\n' "Please use either -t|--autotype or -c|--clipboard, not both" >&2
          exit 1
        }
        readonly AT_TYPE=true
        ;;
      -c | --clipboard)
        unset -v AT_TYPE 2> /dev/null || {
          printf '%s\n' "Please use either -t|--autotype or -c|--clipboard, not both" >&2
          exit 1
        }
        readonly AT_TYPE=false
        ;;
      -h | --help)
        print_help
        exit 0
        ;;
      -v | --version)
        printf '%s\n' "tessen version $VERSION"
        exit 0
        ;;
      --)
        shift
        break
        ;;
      *)
        printf '%s\n' "invalid argument detected" >&2
        exit 1
        ;;
    esac
    shift
  done
  unset -v _opt

  if unset -v BACKEND_OPTS 2> /dev/null; then
    validate_backend
  fi
  validate_clip_time

  readonly WTYPE
  readonly BACKEND
  readonly CLIP_TIME

  trap 'die' EXIT TERM
  get_pass_file
  get_pass_data
  choice_data

  if [[ "$WTYPE" -eq 1 ]]; then
    key_menu_autotype
  else
    key_menu_copy
  fi
  trap - EXIT TERM
}

main "$@"