summaryrefslogtreecommitdiff
path: root/git-credential-gbw
blob: f9d9db3a2e65048217c441eb76e2104b0680324d (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
#!/bin/bash

# Function to fetch username and password from Bitwarden
get_credentials() {
    local item_name=$1

    # Fetch the login item
    item=$(bw get item "$item_name" --session "$BW_SESSION")

    # Extract username and password
    username=$(echo "$item" | jq -r '.login.username')
    password=$(echo "$item" | jq -r '.login.password')

    echo -e "username=$username\npassword=$password"
}

# Main logic to handle Git credential helper protocol
while read -r line; do
    case "$line" in
        protocol=*)
            protocol=${line#protocol=}
            ;;
        host=*)
            host=${line#host=}
            ;;
        username=*)
            username=${line#username=}
            ;;
        password=*)
            password=${line#password=}
            ;;
        *)
            ;;
    esac
done

if [ -z "$protocol" ] || [ -z "$host" ]; then
    exit 1
fi

# Define your Bitwarden item name
ITEM_NAME="$host"

# Fetch the credentials
credentials=$(get_credentials "$ITEM_NAME")

# Output the credentials in the format Git expects
echo "$credentials"