-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail_alias.sh
More file actions
executable file
·107 lines (93 loc) · 3.38 KB
/
email_alias.sh
File metadata and controls
executable file
·107 lines (93 loc) · 3.38 KB
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
#!/usr/bin/env zsh
##
# Uses /dev/urandom to generate a cryptographically random string that is appended to a provided email prefix. Once
# generated, an alias is created using SimpleLogin. Once complete, the output is copied to the clipboard.
#
# Arguments:
# $1 The prefix that should be applied to the randomly-generated string of characters. This is provided by the user.
#
# Environment variables:
# SIMPLE_LOGIN_SUFFIX Shall be the domain name the email alias should be created with.
# SIMPLE_LOGIN_API_TOKEN Shall be the API token used to communicate with SimpleLogin.
#
# Expected utilities:
# jq: Used to parse JSON responses from SimpleLogin.
#
##
# 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
exit 1
fi
# Before creating a new email alias, check if one already exists for the given prefix.
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
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)
# Get the Mailbox ID that the alias should be added to.
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=$(simplelogin_api "v5/alias/options" | \
res_body | \
jq -r ".suffixes[] | select(.suffix == \"$SIMPLE_LOGIN_SUFFIX\") | .signed_suffix")
read -r -d '' json <<JSON
{
"alias_prefix": "$1.$secure_chars",
"signed_suffix": "$signed_suffix",
"mailbox_ids": ["$mailbox_id"],
"note": "$1"
}
JSON
sl_response=$(simplelogin_api "v3/alias/custom/new" "POST" "$json")
sl_response_code=$(echo "$sl_response" | status_code)
# Ensure the status code is 201.
if [ "$sl_response_code" -eq 201 ]; then
generated_address=$(echo "$sl_response" | res_body | jq -r ".alias")
echo "$generated_address" | pbcopy
echo "Generated email alias \"$generated_address\" and copied it to your clipboard."
else
echo "Could not generate email alias! Response code was $sl_response_code with error $(echo "$sl_response" | res_body)."
fi