-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh-auth
executable file
·429 lines (337 loc) · 10.4 KB
/
ssh-auth
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!/usr/bin/env bash
VERSION=1
# Base URL information
DATA_RESIDENCY=eu
BASE_URL="https://$DATA_RESIDENCY.api.tru.id"
# API Check URLs
BACKEND_SERVER="<Your NGROK URL>"
REDIRECT_URL="$BACKEND_SERVER/complete-check"
PHONE_CHECK_URL="$BASE_URL/phone_check/v0.2/checks"
GET_PHONE_CHECK_URL="$BASE_URL/phone_check/v0.2/checks/"
CREATE_AUTH_TOKEN_URL="$BASE_URL/oauth2/v1/token"
APP_ROOT=`dirname $0`
CONFIG_FILE="$APP_ROOT/tru-id-ssh-auth.conf"
DEST_DIRECTORY="/usr/local/bin/"
# Empty Global Variables
BASIC_AUTH_USER=
BASIC_AUTH_PASSWORD=
ACCESS_TOKEN=
# Status Values
OK=0
FAIL=1
SEQ="seq"
function check_dependencies() {
if ! type "seq" > /dev/null 2>&1;
then
if ! type "jot" > /dev/null 2>&1;
then
echo "You need to have either 'seq' or 'jot' installed"
exit $FAIL
else
SEQ="jot"
fi
fi
if ! type "curl" > /dev/null 2>&1;
then
echo "You need curl installed on your system"
exit $FAIL
fi
if ! type "jq" > /dev/null 2>&1;
then
echo "You need jq installed on your system"
exit $FAIL
fi
if ! type "qr" > /dev/null 2>&1;
then
echo "You need the Python library qr installed on your system"
exit $FAIL
fi
}
function require_curl() {
which curl 2>&1 > /dev/null
if [ $? -eq 0 ]
then
return $OK
fi
curl --help 2>&1 > /dev/null
exit $FAIL
}
# Retrieves tru.ID credentials from project's `tru.json` file.
function get_credentials() {
FILE="${DEST_DIRECTORY}tru-id-ssh-auth/tru.json"
if [ -f "$FILE" ]; then
BASIC_AUTH_USER=$(jq -r ".credentials[].client_id" ${FILE})
BASIC_AUTH_PASSWORD=$(jq -r ".credentials[].client_secret" ${FILE})
return $OK
fi
echo "Unable to retrieve project credentials. Please make sure your project directory has a `tru.json` file."
exit $FAIL
}
# Creates an access token needed to create a PhoneCheck request.
function create_access_token() {
get_credentials
CREDENTIALS=$(echo -ne "$BASIC_AUTH_USER:$BASIC_AUTH_PASSWORD" | base64 -w 0);
# Make request to get access token
response=`curl \
--header "Authorization: Basic $CREDENTIALS" \
--header "Content-Type: application/x-www-form-urlencoded" \
--request POST $CREATE_AUTH_TOKEN_URL\
--data-urlencode "grant_type=client_credentials" \
--data-urlencode "scope=phone_check coverage" --silent`
curl_exit_code=$?
# Parses response to get the access token
ACCESS_TOKEN=$(jq -r .access_token <<< "${response}" )
if [ $curl_exit_code -ne 0 ]
then
echo $curl_exit_code
echo "Error running curl"
fi
}
function create_check() {
config_file="${DEST_DIRECTORY}tru-id-ssh-auth/tru-id-ssh-auth.conf"
current_user=$USER;
phone_number="";
while read line; do
if [[ $line =~ user=$current_user: ]] ; then
phone_number=$(echo "${line}" | sed s/user=${current_user}://g);
fi
done <${config_file}
if [ "$phone_number" == "" ]; then
echo "Phone Number is empty"
return 0
fi
# Checking Credentials are installed
get_credentials
# Creating an Access Token
create_access_token
# Making a Phone Check request"
response=`curl \
--header "Authorization: Bearer $ACCESS_TOKEN" \
--header "Content-Type: application/json" \
--request POST $PHONE_CHECK_URL\
--data-raw "{\"phone_number\":\"${phone_number}\", \"redirect_url\":\"${REDIRECT_URL}\"}" --silent`
curl_exit_code=$?
if [ $curl_exit_code -ne 0 ]
then
echo "Error running curl"
return 1;
fi
# Handling Phone Check Response
check_id=$(jq -r .check_id <<< "${response}" )
status=$(jq -r .status <<< "${response}" )
check_url=$(jq -r ._links.check_url.href <<< "${response}" )
# Generate QR code
qr --ascii "${check_url}" > "qrcode.txt"
sleep 1
cat ~/qrcode.txt
# Start polling
start_polling
return $CHECK_STATUS;
}
function start_polling() {
# Check every 5 seconds for status on Check.
interval_in_seconds=5
CHECK_STATUS=$FAIL
while true;
do
# Check status of phone check
response=`curl \
--header "Authorization: Bearer $ACCESS_TOKEN" \
--header "Content-Type: application/json" \
--request GET $GET_PHONE_CHECK_URL/${check_id} --silent`
curl_exit_code=$?
if [ $curl_exit_code -ne 0 ]
then
echo "Error running curl"
return $FAIL;
fi
status=$(jq -r .status <<< "${response}" )
match=$(jq -r .match <<< "${response}" )
# If check is complete, output
if [[ "$status" != "PENDING" && "$status" != "ACCEPTED" ]]; then
if [ "$status" == "COMPLETED" ]; then
if [ "$match" == "true" ]; then
CHECK_STATUS=$OK;
run_shell
else
echo "No match found!"
CHECK_STATUS=$FAIL
return $FAIL;
fi
elif [ "$status" == "EXPIRED" ]; then
echo "Check Expired";
CHECK_STATUS=$FAIL
return $FAIL;
elif [ "$status" == "ERROR" ]; then
echo "Check Error Received";
CHECK_STATUS=$FAIL
return $FAIL;
else
echo "$status"
echo "404, no status was found";
CHECK_STATUS=$FAIL
return $FAIL;
fi
break;
fi
# Otherwise continue
sleep $interval_in_seconds;
done
}
# Install SSH Auth plugin to the server
function install() {
config_file="${DEST_DIRECTORY}tru-id-ssh-auth/tru-id-ssh-auth.conf"
# Ensure our target directory is present
set -e
echo "Making directory in ${DEST_DIRECTORY}"
mkdir "/usr/local/bin/tru-id-ssh-auth"
set +e
if [[ ! -r `dirname "${DEST_DIRECTORY}"` ]]
then
echo "${DEST_DIRECTORY} is not writable. Try again using sudo"
return $FAIL
fi
echo "Copying /root/tru-id-ssh-auth/ to ${DEST_DIRECTORY}..."
cp -r "/root/tru-id-ssh-auth" "${DEST_DIRECTORY}"
echo "Setting up permissions..."
chmod 755 $DEST_DIRECTORY
if [[ ! -f ${config_file} ]]
then
echo "Generating initial config on ${config_file}..."
echo "header=tru.ID SSH Auth initialised." > "${config_file}"
else
echo "A config file was found on ${config_file}. Edit it manually if you want to change the API key"
fi
chmod 644 ${config_file}
# TODO, uncomment this if using on a server other than Docker
# OR Figure a way to restart openssh without Docker restarting
# add_force_command "${DEST_DIRECTORY}tru-id-ssh-auth/ssh-auth"
echo ""
echo "To enable tru.ID authentication on a user the following command: "
echo ""
echo " ${DEST_DIRECTORY}tru-id-ssh-auth/ssh-auth register-user <username> <phone-number-inc-country-code>"
echo " Example: ${DEST_DIRECTORY}tru-id-ssh-auth/ssh-auth register-user test 447000000000"
echo ""
echo "To uninstall tru.ID SSH type:"
echo ""
echo " ${DEST_DIRECTORY}tru-id-ssh-auth/ssh-auth uninstall"
echo ""
echo " Restart the SSH server to apply changes"
echo ""
}
function uninstall() {
find_sshd_config
if [[ $1 != "quiet" ]]
then
echo "Uninstalling tru.ID SSH from $SSHD_CONFIG..."
fi
if [[ -w $SSHD_CONFIG ]]
then
sed -ie '/^ForceCommand.*tru-id-ssh-auth.*/d' $SSHD_CONFIG
fi
if [[ $1 != "quiet" ]]
then
echo "tru.ID SSH was uninstalled."
echo "Now restart the ssh server to apply changes and then remove ${DEST_DIRECTORY}/tru-id-ssh-auth"
fi
}
function add_force_command() {
echo "Trying to add force command to $SSHD_CONFIG"
find_sshd_config
auth_ssh_command="$1"
if [[ -w $SSHD_CONFIG ]]
then
echo "Adding 'ForceCommand ${auth_ssh_command} login' to ${SSHD_CONFIG}"
uninstall "quiet" # remove previous installations
echo -e "\nForceCommand ${auth_ssh_command} login" >> ${SSHD_CONFIG}
echo ""
check_sshd_config_file
echo " MAKE SURE YOU DO NOT MOVE/REMOVE ${auth_ssh_command} BEFORE UNINSTALLING tru.ID SSH Plugin"
sleep 5
fi
}
function find_sshd_config() {
echo "Trying to find sshd_config file"
if [[ -f /etc/sshd_config ]]
then
SSHD_CONFIG="/etc/sshd_config"
elif [[ -f /etc/ssh/sshd_config ]]
then
SSHD_CONFIG="/etc/ssh/sshd_config"
else
echo "Cannot find sshd_config in your server. tru.ID SSH Auth will be enabled when you add the ForceCommand to it"
fi
}
function check_sshd_config_file() {
echo "Checking the validity of $SSHD_CONFIG file..."
sshd -t
if [ $? -ne 0 ]
then
echo "sshd_config file is invalid. MAKE SURE YOU DO NOT RESTART THE SSH SERVER UNTIL YOU FIX IT."
exit $FAIL
fi
}
# usage: register-user "user" "phone_number"
function register_user() {
config_file="${DEST_DIRECTORY}tru-id-ssh-auth/tru-id-ssh-auth.conf"
if [[ $2 && $3 ]]
then
echo "user=$2:$3" >> ${config_file}
echo "" >> ${config_file}
echo "User was registered"
else
echo "Cannot register user"
fi
}
# Once successful, allows user to proceed with access to server
function run_shell() {
if [[ "$SSH_ORIGINAL_COMMAND" != "" ]]
then
exec /bin/bash -c "${SSH_ORIGINAL_COMMAND}"
elif [ $SHELL ]
then
exec -l $SHELL
fi
exit $?
}
require_curl
# get the absolute path to the command
cd `dirname $0`
COMMAND="$PWD/`basename $0`"
cd - >/dev/null
case $1 in
version)
echo "tru.ID Auth SSH v${VERSION}"
exit 0
;;
create-check)
check_dependencies
create_check "$@"
;;
register-user)
check_dependencies
register_user "$@"
;;
install)
check_dependencies
install
;;
uninstall)
uninstall
;;
*)
cat <<__EOF__
Usage: ssh-auth <command> <arguments>
VERSION $VERSION
Available commands:
version
Outputs the tru.ID SSH Auth Version
install
Installs tru.ID SSH Plugin into OpenSSH Server
register-user
Takes 2 parameters, the username, and their phone number. Enabling their account to require tru.ID SSH Auth to proceed
uninstall
Uninstalls tru.ID SSH Plugin from OpenSSH Server
__EOF__
;;
esac