-
Notifications
You must be signed in to change notification settings - Fork 1
/
add_item.sh
executable file
·67 lines (48 loc) · 1.72 KB
/
add_item.sh
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
#!/bin/bash
# shellcheck disable=2154
. lib/env.sh
log "add_item"
URL=$(jq -j .url <<< "${*}")
SITE=$(cut -d/ -f3 <<< "${URL}")
P='return text returned of (display dialog'
I='with icon caution'
T='with title "Add item"'
# Get name
CMD="${P} \"Enter name of new item\" ${I} ${T} default answer \"${SITE}\")"
NAME=$(2>&- osascript -e "${CMD}")
[ "${NAME}" == "" ] && exit
# Get username
SITE=$(cut -d/ -f3 <<< "${NAME}")
CMD="${P} \"Enter username for ${SITE}\" ${I} ${T} default answer \"${bwuser}\")"
USERNAME=$(2>&- osascript -e "${CMD}")
[ "${USERNAME}" == "" ] && exit
# Get password
GENERATED=$(curl -s "${API}/generate?length=20&uppercase&lowercase&number&special" | jq -j .data.data)
CMD="${P} \"Enter password for ${USERNAME}\" ${I} ${T} default answer \"${GENERATED}\" with hidden answer)"
PASSWORD=$(2>&- osascript -e "${CMD}")
[ "${PASSWORD}" == "" ] && exit
if [ "${PASSWORD}" != "${GENERATED}" ]; then
CMD="${P} \"Confirm password for ${USERNAME}\" ${I} ${T} default answer \"\" with hidden answer)"
CONFIRM=$(2>&- osascript -e "${CMD}")
if [ "${CONFIRM}" != "${PASSWORD}" ]; then
echo -n 'Add item,Passwords do not match'
exit
fi
fi
[ "${PASSWORD}" == "" ] && exit
# Build payload
PAYLOAD='{ "type": 1, "name": "'"${SITE}"'"'
PAYLOAD+=',"organizationId": '
if [ "${ORGANIZATION_ID}" == "" ]; then
PAYLOAD+='null'
else
PAYLOAD+='"'"${ORGANIZATION_ID}"'"'
fi
PAYLOAD+=',"login": {'
PAYLOAD+=' "username": "'"${USERNAME}"'"'
PAYLOAD+=',"password": "'"${PASSWORD}"'"'
PAYLOAD+=',"uris": [ { "match": null, "uri": "'"${URL}"'" }'
PAYLOAD+='] } }'
# Add item
S=$(curl -s -H 'Content-Type: application/json' -d "${PAYLOAD}" "${API}"/object/item | jq -j .success)
echo -n "Added ${SITE//,/ },Success: ${S}"