~palb91/bwm

ref: b8eca8dd492ca304e0d861f23f7fc49cf3924746 bwm/bwm -rwxr-xr-x 4.3 KiB
b8eca8dd — Pierre-Albéric TROUPLIN fix: dirty fix for keymap 1 year, 4 months ago
                                                                                
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
#!/bin/bash
#
# bwm - Bitwarden menu for Wayland
#
# © 2021, Pierre-Albéric TROUPLIN (palb91)

set -e


# Environments variables, default values
declare -A credentials

cmd=wtype
asset=pass
url=
selection=


# /!\ Dirty hack to force the use of a specific keymap /!\
keymap=( fr bepo )


# Utils
help() {
    cat <<HELP
bwm - Type a password from bitwarden in a Wayland environment.

Usage:
    ${0##*/} [-h]
    ${0##*/} [-u|-b|-o] [URL]
    ${0##*/} -c [-u|-o] [URL]

Options:
    -u, --user  Type the username instead of the password.
    -b, --both  Type the username and the password separated by a <Tab>.
    -o, --otp   Type the otp code if exists.
    -c, --copy  Copy instead of type.
    -h, --help  Print this help.

Argument:
    URL         If any, must be the last parameter. ${0##*/} will try to
                find only matching values from \`bw\`.
HELP

    exit "${1:-0}"
}

die() {
    printf '%s\n\n' "${*}" >&2
    help 1
}

check_dep() {
    if [[ -z "$(command -v "${1}")" ]]; then
        printf '%s is not installed\n' "${1}" >&2
        exit 1
    fi
}

menu() {
    column -ts$'\t' -o' :: ' -R2 \
        | bemenu -pselect:       \
        | sed 's/ .*// ; q'
}

use_Xwayland() {
    query='recurse(.nodes[]?, .floating_nodes[]?) | select(.focused).shell'
    [[ "$(swaymsg -t get_tree | jq -r "${query}")" = 'xwayland' ]]
}


# Parse arguments, check dependancies
parse_args() {
    while [[ "${#}" -gt 0 ]]; do
        case "${1}" in
            -h|--help) help                     ;;
            -c|--copy) cmd=wl-copy              ;;
            -u|--user) asset=user               ;;
            -b|--both) asset=both               ;;
            -o|--otp)  asset=totp               ;;
            -cu)       cmd=wl-copy asset=user   ;;
            -co)       cmd=wl-copy asset=totp   ;;
            -*)        die "Wrong option: ${1}" ;;
            *)         url="${1}"; break        ;;
        esac

        shift
    done

    [[ "${#}" -gt 1 ]] \
        && die "Wrong number of arguments."

    [[ "${cmd}" = 'wl-copy' ]] && [[ "${asset}" = 'both' ]] \
        && die 'Cannot copy both username and password.'

    # Use xdotool for Xwayland windows
    [[ "${cmd}" = 'wtype' ]] && use_Xwayland \
        && cmd=xdotool

    check_dep "${cmd}"
}


get_credentials() {
    # Gather bitwarden entries (no url means all entries)
    entries="$( bw list items --url "${url}" )"


    # Exit if not entry in bitwarden
    if [[ "${entries}" = '[]' ]]; then
        printf 'No credentials in bitwarden...\n' >&2
        exit 2
    fi


    # Gather matching entries
    _jq='if length == 1 then
           .[0] | .id + "\t" + .name + "\t" + .login.username
         else
           sort_by(.favorite == false)
               | .[]
               | .id + "\t" + .name + "\t" + .login.username
         end'

    mapfile -t match < <(printf '%s' "${entries}" | jq -r "${_jq}")

    # Get id, filter if there are several matchs
    id="${match[0]%% *}"
    if [[ "${#match[@]}" -gt 1 ]]; then
        check_dep bemenu
        id="$(printf "%s\n" "${match[@]}" | menu)"
    fi
    id="${id%%$'\t'*}"


    # Get corresponding json ojbect
    selection="$(printf '%s' "${entries}" \
                    | jq '.[] | select(.id == "'"${id}"'")')"


    # Exit if no values
    if [[ -z "${selection}" ]]; then
        printf 'Wrong selection. Exiting...\n' >&2
        exit 3
    fi


    # Gather credentials
    _jq='.login | .username + "\n" + .password + "\n" + .totp'

    item=()

    mapfile -t item < <(printf '%s' "${selection}" | jq -r "${_jq}")

    credentials[user]="${item[0]}"
    credentials[pass]="${item[1]}"
    credentials[totp]="${item[2]:+$(bw get totp "${id}")}"
}


# Run wtype, xdotool or wl-copy with asked content
type_or_copy() {
    set -- "${credentials[${asset}]}"

    if [[ "${cmd}" = 'wtype' ]]; then
        [[ "${asset}" = 'both' ]] \
            && set -- "${credentials[user]}" -k Tab "${credentials[pass]}"

        # Add a short delay before typing
        set -- -s 150 "${@}"

    elif [[ "${cmd}" = 'xdotool' ]]; then
        setxkbmap "${keymap[@]}"

        [[ "${asset}" = 'both' ]] \
            && set -- "${credentials[user]}"$'\t'"${credentials[pass]}"

        set -- sleep 0.15 type "${@}"
    fi

    exec "${cmd}" "${@}"
}


main() {
    parse_args "${@}"
    get_credentials
    type_or_copy
}

main "${@}"