From 1c528904ba59d5cd81aeae9093917ed0df4826fe Mon Sep 17 00:00:00 2001 From: lukeify <5379845+lukeify@users.noreply.github.com> Date: Sun, 8 Sep 2024 13:00:48 +1200 Subject: [PATCH 1/3] fix: remove set -e --- src/email_alias.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/email_alias.sh b/src/email_alias.sh index c452852..4b77c82 100755 --- a/src/email_alias.sh +++ b/src/email_alias.sh @@ -14,8 +14,6 @@ # Expected utilities: # jq: Used to parse JSON responses from SimpleLogin. # -set -e - api_fqdn="https://app.simplelogin.io/api" auth_header="Authentication: $SIMPLE_LOGIN_API_TOKEN" From c06b89d44afea81edf8559f0609150285f1df20e Mon Sep 17 00:00:00 2001 From: lukeify <5379845+lukeify@users.noreply.github.com> Date: Sun, 8 Sep 2024 21:24:53 +1200 Subject: [PATCH 2/3] feat: check for existing email aliases first --- src/email_alias.sh | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/email_alias.sh b/src/email_alias.sh index 4b77c82..c3754b0 100755 --- a/src/email_alias.sh +++ b/src/email_alias.sh @@ -22,6 +22,18 @@ if [[ -z "$1" || -z "$SIMPLE_LOGIN_SUFFIX" || -z "$SIMPLE_LOGIN_API_TOKEN" ]]; t exit 1 fi +# Before creating a new email alias, check if one already exists for the given prefix. +existing_email_address=$(curl -s "${api_fqdn}/v2/aliases?page_id=0" \ + -H "$auth_header" \ + -H "Content-Type: application/json" \ + -d "{ \"query\": \"$1\" }" | jq -r ".aliases[] | select(.email | startswith(\"$1\")) | .email") + +if [ "$existing_email_address" ]; then + echo "$existing_email_address" | pbcopy + echo "Existing email alias \"$existing_email_address\" copied to your clipboard." + exit 0 +fi + # Chosen based on the discussion from https://security.stackexchange.com/a/183951 # This appears to generate cryptographically random characters. secure_chars=$(LC_ALL=C tr -dc 'a-z0-9' < /dev/urandom | head -c 16; echo) From 0e874b2c9ab739141df7a3f2bb4338fac0c65d1c Mon Sep 17 00:00:00 2001 From: lukeify <5379845+lukeify@users.noreply.github.com> Date: Tue, 5 Nov 2024 13:21:55 +1300 Subject: [PATCH 3/3] refactor: use function calls for api requests --- src/email_alias.sh | 69 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 16 deletions(-) diff --git a/src/email_alias.sh b/src/email_alias.sh index c3754b0..15a531b 100755 --- a/src/email_alias.sh +++ b/src/email_alias.sh @@ -14,8 +14,48 @@ # Expected utilities: # jq: Used to parse JSON responses from SimpleLogin. # -api_fqdn="https://app.simplelogin.io/api" -auth_header="Authentication: $SIMPLE_LOGIN_API_TOKEN" + +## +# Arguments: +# $1 The endpoint to make the request to. +# $2 The HTTP method to use. +# $3 The body of the request, if any. +# +simplelogin_api() { + local api_fqdn="https://app.simplelogin.io/api" + local http_method=${2:-"GET"} + local body="$3" + + if [[ -z "$body" ]]; then + curl -w "\n%{http_code}" -s -X "$http_method" "$api_fqdn/$1" \ + -H "Authentication: $SIMPLE_LOGIN_API_TOKEN" + else + curl -w "\n%{http_code}" -s -X "$http_method" "$api_fqdn/$1" \ + -H "Authentication: $SIMPLE_LOGIN_API_TOKEN" \ + -H "Content-Type: application/json" \ + -d "$body" + fi +} + +## +# Arguments: +# $1 The response from the API call. +# +status_code() { + local res + read -r -d '' res + echo "$res" | awk 'END{print}' +} + +## +# Arguments: +# $1 The response from the API call. +# +res_body() { + local res + read -r -d '' res + echo "$res" | awk 'NR==1{body=$0} END{print body}' +} if [[ -z "$1" || -z "$SIMPLE_LOGIN_SUFFIX" || -z "$SIMPLE_LOGIN_API_TOKEN" ]]; then echo "Error: Missing required prefix, SIMPLE_LOGIN_SUFFIX, or SIMPLE_LOGIN_API_TOKEN." >&2 @@ -23,10 +63,9 @@ if [[ -z "$1" || -z "$SIMPLE_LOGIN_SUFFIX" || -z "$SIMPLE_LOGIN_API_TOKEN" ]]; t fi # Before creating a new email alias, check if one already exists for the given prefix. -existing_email_address=$(curl -s "${api_fqdn}/v2/aliases?page_id=0" \ - -H "$auth_header" \ - -H "Content-Type: application/json" \ - -d "{ \"query\": \"$1\" }" | jq -r ".aliases[] | select(.email | startswith(\"$1\")) | .email") +existing_email_address=$(simplelogin_api "v2/aliases?page_id=0" "GET" "{ \"query\": \"$1\" }" | \ + res_body | \ + jq -r ".aliases[] | select(.email | startswith(\"$1\")) | .email") if [ "$existing_email_address" ]; then echo "$existing_email_address" | pbcopy @@ -39,10 +78,12 @@ fi secure_chars=$(LC_ALL=C tr -dc 'a-z0-9' < /dev/urandom | head -c 16; echo) # Get the Mailbox ID that the alias should be added to. -mailbox_id=$(curl -s "${api_fqdn}/v2/mailboxes" -H "$auth_header" | jq '.mailboxes[0].id') +mailbox_id=$(simplelogin_api "v2/mailboxes" | res_body | jq '.mailboxes[0].id') # Retrieve the signed suffix that the alias should be created for. -signed_suffix=$(curl -s "${api_fqdn}/v5/alias/options" -H "$auth_header" | jq -r ".suffixes[] | select(.suffix == \"$SIMPLE_LOGIN_SUFFIX\") | .signed_suffix") +signed_suffix=$(simplelogin_api "v5/alias/options" | \ + res_body | \ + jq -r ".suffixes[] | select(.suffix == \"$SIMPLE_LOGIN_SUFFIX\") | .signed_suffix") read -r -d '' json <