From 5226fd1f1a02ae0aedbdbc2f38f2a4b4785d5d3e Mon Sep 17 00:00:00 2001 From: "Pedro J. Molina" Date: Thu, 7 Oct 2021 13:23:36 +0200 Subject: [PATCH 1/2] add basic auth support --- README.md | 19 +++++++++++++++++++ smoke.sh | 18 +++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8304bf6..35ab878 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,24 @@ smoke_header "X-Forwarded-Proto: https" Existing custom headers can be unset with `remove_smoke_headers`. +### Basic Auth + +Basic Auth credentials can be passed using: `smoke_basic_auth [user] [pass]` +To unset credentials pass no arguments or empty ones. + +Full usage example: + +```bash +smoke_url "${API}/health" + smoke_assert_code 401 + +smoke_basic_auth user1 pass1 # set credentials +smoke_url_ok "${API}/health" # call with credentials + smoke_assert_code 200 + +smoke_basic_auth # unset +``` + ### CSRF tokens Web applications that are protected with CSRF tokens will need to extract a @@ -248,3 +266,4 @@ API |`smoke_host ` | set the host header to use | |`smoke_header
` | set additional request header | |`smoke_tcp_ok ` | open a tcp connection and check for a `Connected` response | +|`smoke_basic_auth ` | set Basic Auth credentials | diff --git a/smoke.sh b/smoke.sh index 4932637..b38a7f9 100644 --- a/smoke.sh +++ b/smoke.sh @@ -18,6 +18,11 @@ SMOKE_HEADERS=() ## "Public API" +smoke_basic_auth() { + SMOKE_BASIC_AUTH_USER="$1" + SMOKE_BASIC_AUTH_PASSWORD="$2" +} + smoke_csrf() { SMOKE_CSRF_TOKEN="$1" } @@ -189,6 +194,10 @@ _curl() { opt+=(-H "$header") done fi + if [[ -n "$SMOKE_BASIC_AUTH_USER" ]] + then + opt+=(-u "$SMOKE_BASIC_AUTH_USER:$SMOKE_BASIC_AUTH_PASSWORD") + fi curl "${opt[@]}" "$@" > $SMOKE_CURL_BODY } @@ -257,6 +266,13 @@ _smoke_print_success() { } _smoke_print_url() { - TEXT="$1" + TEXT="$@" echo "> $TEXT" + + if [[ -n "$SMOKE_BASIC_AUTH_USER" ]] + then + echo "> $${bold}${TEXT}${normal} with BasicAuth ${bold}${SMOKE_BASIC_AUTH_USER}:****${normal}" + else + echo "> ${bold}${TEXT}${normal}" + fi } From 9300fe593c996f05f702d02ae57d5a07d80f7da3 Mon Sep 17 00:00:00 2001 From: "Pedro J. Molina" Date: Thu, 7 Oct 2021 13:25:15 +0200 Subject: [PATCH 2/2] fix doc. same style --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 35ab878..635bdf1 100644 --- a/README.md +++ b/README.md @@ -148,7 +148,7 @@ Existing custom headers can be unset with `remove_smoke_headers`. ### Basic Auth -Basic Auth credentials can be passed using: `smoke_basic_auth [user] [pass]` +Basic Auth credentials can be passed using: `smoke_basic_auth ` To unset credentials pass no arguments or empty ones. Full usage example: