aboutsummaryrefslogtreecommitdiff
path: root/scripts/bokftui-bw
blob: 985714dfea733b4f6ba2f2cf0603403556860ada (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
#!/bin/sh
# Start bokftui with a credential fetched from a Bitwarden/Vaultwarden item.
# Runs entirely as the invoking user — no root, no sudo.
#
#   bokftui-bw [item-or-username]
#
# The item is chosen from BOKF_BW_ITEM, the first argument (matched against
# the item name or the login user name), or — when several items match —
# an interactive picker. Candidates without an argument match
# BOKF_BW_MATCH (default "bokf") the same way.
#
#   BOKF_BW_FIELD  custom field that holds the secret (default: login.password)
#   BOKF_BW_KIND   password (default) exports BOKFD_PASSWORD;
#                  token exports BOKFD_TOKEN
#   BOKFTUI        binary to exec (default: build/bokftui in the checkout the
#                  script lives in, else bokftui from PATH)
#
# Works with the official Bitwarden CLI (bw) or with rbw. With bw the session
# key is cached in ~/.cache/bokf/bw-session (mode 0600) so the master password
# is only asked when the session expires or after a logout.
set -eu

item="${BOKF_BW_ITEM:-}"
if [ -z "$item" ] && [ "$#" -gt 0 ]; then
    item="$1"
    shift
fi
field="${BOKF_BW_FIELD:-}"
kind="${BOKF_BW_KIND:-password}"
BOKFTUI="${BOKFTUI:-}"
if [ -z "$BOKFTUI" ]; then
    root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
    if [ -x "$root/build/bokftui" ]; then
        BOKFTUI="$root/build/bokftui"
    else
        BOKFTUI=bokftui
    fi
fi

# $1: newline-separated "id<TAB>label" rows; prints the chosen id.
choose_item() {
    rows="$1"
    n=$(printf '%s\n' "$rows" | grep -c . || true)
    if [ "$n" -eq 0 ]; then
        echo "bokftui-bw: no matching item" >&2
        exit 1
    fi
    if [ "$n" -eq 1 ]; then
        printf '%s\n' "$rows" | cut -f1
        return
    fi
    if [ ! -t 0 ]; then
        echo "bokftui-bw: several items match; pass one as an argument" >&2
        printf '%s\n' "$rows" | cut -f2- >&2
        exit 1
    fi
    echo "Välj konto:" >&2
    printf '%s\n' "$rows" | awk -F'\t' '{printf "  %d) %s\n", NR, $2}' >&2
    printf 'Nummer: ' >&2
    read -r ans
    printf '%s\n' "$rows" | awk -F'\t' -v n="$ans" 'NR == n {print $1}'
}

if command -v rbw >/dev/null 2>&1; then
    if ! rbw unlocked >/dev/null 2>&1; then
        rbw unlock
    fi
    rbw sync >/dev/null 2>&1 || true
    if [ -z "$item" ]; then
        item=$(choose_item "$(rbw list 2>/dev/null |
            grep -i "${BOKF_BW_MATCH:-bokf}" |
            awk '{printf "%s\t%s\n", $0, $0}' || true)")
    fi
    if [ -n "$field" ]; then
        secret=$(rbw get --field "$field" "$item")
    else
        secret=$(rbw get "$item")
    fi
elif command -v bw >/dev/null 2>&1; then
    umask 077
    cache="${XDG_CACHE_HOME:-$HOME/.cache}/bokf/bw-session"
    if [ -f "$cache" ]; then
        BW_SESSION=$(cat "$cache")
        export BW_SESSION
    fi
    if ! bw status 2>/dev/null | jq -e '.status == "unlocked"' >/dev/null 2>&1
    then
        mkdir -p "$(dirname "$cache")"
        BW_SESSION=$(bw unlock --raw)
        export BW_SESSION
        printf '%s\n' "$BW_SESSION" > "$cache"
    fi
    bw sync >/dev/null 2>&1 || true
    items=$(bw list items 2>/dev/null || echo '[]')
    if [ -z "$item" ]; then
        rows=$(printf '%s' "$items" | jq -r --arg m "${BOKF_BW_MATCH:-bokf}" '
            .[] | select((.name | test($m; "i")) or
                         ((.login.username // "") | test($m; "i")))
            | "\(.id)\t\(.name) (\(.login.username // "-"))"')
    else
        rows=$(printf '%s' "$items" | jq -r --arg q "$item" '
            ([.[] | select(.id == $q or .name == $q)] as $exact
             | if ($exact | length) > 0 then $exact
               else [.[] | select((.name | test($q; "i")) or
                         ((.login.username // "") | test($q; "i")))]
               end)
            | .[] | "\(.id)\t\(.name) (\(.login.username // "-"))"')
    fi
    id=$(choose_item "$rows")
    if [ -n "$field" ]; then
        secret=$(printf '%s' "$items" | jq -r --arg id "$id" --arg f "$field" \
            '.[] | select(.id == $id) | .fields[]? |
             select(.name == $f) | .value' | head -n 1)
    else
        secret=$(printf '%s' "$items" | jq -r --arg id "$id" \
            '.[] | select(.id == $id) | .login.password')
    fi
    item_user=$(printf '%s' "$items" | jq -r --arg id "$id" \
        '.[] | select(.id == $id) | (.login.username // "")')
else
    echo "bokftui-bw: install rbw or the Bitwarden CLI (bw)" >&2
    exit 1
fi

if [ -z "$secret" ] || [ "$secret" = "null" ]; then
    echo "bokftui-bw: item has no secret to use" >&2
    exit 1
fi

case "$kind" in
    token)
        BOKFD_TOKEN="$secret"
        export BOKFD_TOKEN
        ;;
    password)
        BOKFD_PASSWORD="$secret"
        export BOKFD_PASSWORD
        if [ -n "${item_user:-}" ] && [ -z "${BOKFD_USER:-}" ]; then
            BOKFD_USER="$item_user"
            export BOKFD_USER
        fi
        ;;
    *)
        echo "bokftui-bw: BOKF_BW_KIND must be password or token" >&2
        exit 1
        ;;
esac

exec "$BOKFTUI" "$@"