Skip to content

Commit 08faec4

Browse files
author
hriprsd
authored
adding oauth token support
1 parent 4ab6576 commit 08faec4

2 files changed

Lines changed: 36 additions & 12 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ To setup an Incoming Webhook, go to
3434
- `url`: *Required.* The webhook URL as provided by Slack. Usually
3535
in the form: `https://hooks.slack.com/services/XXXX`.
3636

37+
- `token`: *Optional.* The slack token to use for the API calls. Usually
38+
in the form: `xoxb-XXXX`. ** Required parameter if `url` not specified **
39+
3740
- `insecure`: *Optional.* Connect to Slack insecurely - i.e. skip
3841
SSL validation. Defaults to `false` if not provided.
3942

out

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ if [[ ${disable} == "true" ]]; then
2424
exit 0
2525
fi
2626

27-
webhook_url=$( jq --raw-output '.source.url' <<< "${payload}")
27+
webhook_url="$( jq --raw-output '.source.url // "https://slack.com/api/chat.postMessage"' < "${payload}")"
28+
oauth_token="$( jq --raw-output '.source.token' < "${payload}")"
2829
allow_insecure=$(jq --raw-output '.source.insecure // "false"' <<< "${payload}")
2930
raw_ca_certs=$( jq '.source.ca_certs // []' <<< "${payload}")
3031

@@ -174,24 +175,44 @@ EOF
174175
)
175176
elif [[ "${silent}" == "true" ]]; then
176177
echo "INFO: using silent output"
177-
curl --silent --fail --show-error --location "${CURL_OPTIONS[@]}" \
178-
--request "POST" --url "${webhook_url}" \
179-
--data-raw "${compact_body}"
178+
if [[ -n ${oauth_token} ]]; then
179+
curl -s -X POST -H 'Content-type: application/json' \
180+
-H "Authorization: Bearer ${oauth_token}" \
181+
-T /tmp/compact_body.json ${CURL_OPTION} "${webhook_url}"
182+
else
183+
curl --silent --fail --show-error --location "${CURL_OPTIONS[@]}" \
184+
--request "POST" --url "${webhook_url}" \
185+
--data-raw "${compact_body}"
186+
fi
180187
curl_errors=$(( ${curl_errors} + $? ))
181188
elif [[ "${redact_hook}" == "true" ]]; then
182189
url_path=$(
183190
sed -Ee 's,https?://[^/]*(/[^?&#]*).*,\1,' <<< "${webhook_url}"
184191
)
185-
curl --verbose --silent --fail --show-error --location "${CURL_OPTIONS[@]}" \
186-
--request "POST" --url "${webhook_url}" \
187-
--data-raw "${compact_body}" 2>&1 \
188-
| sed -e "s#${url_path}#***WEBHOOK URL REDACTED***#g"
192+
if [[ -n ${oauth_token} ]]; then
193+
curl -v -X POST -H 'Content-type: application/json' \
194+
-H "Authorization: Bearer ${oauth_token}" \
195+
-T /tmp/compact_body.json \
196+
${CURL_OPTION} "${webhook_url}" 2>&1 | sed -e "s#${url_path}#***WEBHOOK URL REDACTED***#g"
197+
else
198+
curl --verbose --silent --fail --show-error --location "${CURL_OPTIONS[@]}" \
199+
--request "POST" --url "${webhook_url}" \
200+
--data-raw "${compact_body}" 2>&1 \
201+
| sed -e "s#${url_path}#***WEBHOOK URL REDACTED***#g"
202+
fi
189203
curl_errors=$(( ${curl_errors} + ${PIPESTATUS[0]} ))
190204
else
191-
curl --verbose --silent --fail --show-error --location "${CURL_OPTIONS[@]}" \
192-
--request "POST" --url "${webhook_url}" \
193-
--data-raw "${compact_body}" \
194-
| sed -e "s#${url_path}#***WEBHOOK URL REDACTED***#g"
205+
if [[ -n ${oauth_token} ]]; then
206+
curl -v -X POST -H 'Content-type: application/json' \
207+
-H "Authorization: Bearer ${oauth_token}" \
208+
-T /tmp/compact_body.json \
209+
${CURL_OPTION} "${webhook_url}" | sed -e "s#${url_path}#***WEBHOOK URL REDACTED***#g"
210+
else
211+
curl --verbose --silent --fail --show-error --location "${CURL_OPTIONS[@]}" \
212+
--request "POST" --url "${webhook_url}" \
213+
--data-raw "${compact_body}" \
214+
| sed -e "s#${url_path}#***WEBHOOK URL REDACTED***#g"
215+
fi
195216
curl_errors=$(( ${curl_errors} + ${PIPESTATUS[0]} ))
196217
fi
197218
fi

0 commit comments

Comments
 (0)