-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gmail-oauth2_bitwarden_secret-tool.sh
executable file
·221 lines (175 loc) · 6.01 KB
/
gmail-oauth2_bitwarden_secret-tool.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
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
#!/usr/bin/env bash
set -euo pipefail
echo_err() {
>&2 echo "$@"
}
assert_value() {
if [ -z "${!1}" ]; then
echo_err "${2}"
exit 1
fi
}
command="${1}"
shift 1
args=""
account_id=""
store_id="Gmail-OAuth2-Script"
secret_id="client.oauth2.com.google.mail"
while (( "$#" )); do
case "$1" in
--account-id)
account_id="${2}"
shift 2
;;
--store-id)
store_id="${2}"
shift 2
;;
--secret-id)
secret_id="${2}"
shift 2
;;
*)
args="${args} ${1}"
shift 1
;;
esac
done
eval set -- "${args}"
account_id="${1-"${account_id}"}"
assert_value "account_id" "missing: account_id"
__store_attributes() {
local -r key="${1}"
local -r account_id="${2:-""}"
local attributes="id ${store_id} service mail.google.com type oauth2"
if [ -n "${account_id}" ]; then
attributes="${attributes} account ${account_id}"
fi
attributes="${attributes} data ${key} script gmail-oauth2"
printf "${attributes}"
}
store_get() {
local -r key="${1}"
local -r account_id="${2:-""}"
local -r attributes=$(__store_attributes "${key}" "${account_id}")
local -r value=$(secret-tool lookup ${attributes} || true)
printf "${value}"
}
store_set() {
local -r key="${1}"
local -r value="${2}"
local -r account_id="${3:-""}"
local -r attributes=$(__store_attributes "${key}" "${account_id}")
printf "${value}" | secret-tool store --label="${store_id}/${key}" ${attributes}
}
get_client_id() {
local -r client_id=$(store_get "client_id")
printf "${client_id}"
}
get_client_secret() {
local -r client_secret=$(store_get "client_secret")
printf "${client_secret}"
}
get_refresh_token() {
local -r refresh_token=$(store_get "refresh_token" "${account_id}")
printf "${refresh_token}"
}
store_client_info() {
local client_id=$(get_client_id)
local client_secret=$(get_client_secret)
if [ -z "${client_id}" ] || [ -z "${client_secret}" ]; then
if [ -z "${BW_SESSION}" ]; then
echo_err ""
echo_err "[secret provider]: unlocking"
echo_err ""
export BW_SESSION=$(bw unlock --raw)
echo_err ""
fi
local -r client_blob=$(bw list items --search "${secret_id}" | jq -Mcr '.[0] | select(.!=null)')
assert_value "client_blob" "[secret provider]: secret(${secret_id}) not found"
client_id=$(echo "${client_blob}" | jq -r '.fields[] | select(.name=="client_id").value')
assert_value "client_id" "[secret provider]: client_id not found in secert(${secret_id})"
echo_err "[store provider]: storing client_id"
store_set "client_id" "${client_id}"
client_secret=$(echo "${client_blob}" | jq -r '.fields[] | select(.name=="client_secret").value')
assert_value "client_secret" "[secret provider]: client_secret not found in secret(${secret_id})"
echo_err "[store provider]: storing client_secret"
store_set "client_secret" "${client_secret}"
fi
}
store_refresh_token() {
local refresh_token=$(get_refresh_token)
if [ -z "${refresh_token}" ]; then
local -r client_id=$(get_client_id)
assert_value "client_id" "[store provider]: client_id not found"
local -r client_secret=$(get_client_secret)
assert_value "client_secret" "[store provider]: client_secret not found"
local -r scope="https://mail.google.com/"
local -r redirect_uri="http://127.0.0.1"
local -r response_type="code"
echo_err ""
echo_err "For authorization code, visit this url and follow the instructions:"
echo_err " https://accounts.google.com/o/oauth2/v2/auth?client_id=${client_id}&redirect_uri=${redirect_uri}&response_type=${response_type}&scope=${scope}&access_type=offline"
echo_err ""
echo_err "Wait till you're redirected to '${redirect_uri}' and copy the 'code' query param from the url."
echo_err ""
read -p "Enter authorization code: " authorization_code
echo_err ""
local -r grant_type="authorization_code"
local -r response=$(curl --silent \
--request POST \
--data "code=${authorization_code}&client_id=${client_id}&client_secret=${client_secret}&redirect_uri=${redirect_uri}&grant_type=${grant_type}" \
https://oauth2.googleapis.com/token)
refresh_token=$(echo "${response}" | jq -r '.refresh_token')
if [ "${refresh_token}" = "null" ]; then
echo_err "Could not extract refresh token: "
echo_err "${response}"
exit 1
fi
echo_err "[store provider] storing refresh_token"
store_set "refresh_token" "${refresh_token}" "${account_id}"
fi
}
refresh_access_token() {
local -r client_id=$(get_client_id)
assert_value "client_id" "[store provider]: client_id not found"
local -r client_secret=$(get_client_secret)
assert_value "client_secret" "[store provider]: client_secret not found"
local -r refresh_token=$(get_refresh_token)
assert_value "refresh_token" "[store provider]: refresh_token not found"
local -r grant_type="refresh_token"
local -r access_token_blob=$(curl --silent \
--request POST \
--data "client_id=${client_id}&client_secret=${client_secret}&refresh_token=${refresh_token}&grant_type=${grant_type}" \
"https://oauth2.googleapis.com/token")
local -r access_token=$(echo "${access_token_blob}" | jq -r '.access_token')
local -r expires_in=$(echo "${access_token_blob}" | jq -r '.expires_in')
local -r now=$(date +%s)
local -r expires_at=$((now + expires_in))
store_set "access_token" "${access_token}" "${account_id}"
store_set "access_token/expires_at" "${expires_at}" "${account_id}"
printf "${access_token}"
}
get_access_token() {
local -r access_token=$(store_get "access_token" "${account_id}")
local -r expires_at=$(store_get "access_token/expires_at" "${account_id}")
local -r now=$(date +%s)
if [[ "${access_token}" && "${expires_at}" && $now -lt $((expires_at - 60)) ]]; then
printf "${access_token}"
else
printf "$(refresh_access_token)"
fi
}
case "${command}" in
authorize)
store_client_info
store_refresh_token
;;
access_token)
get_access_token
;;
*)
echo_err "unsupported command: ${command}!"
exit 1
;;
esac