diff --git a/authy-ssh b/authy-ssh index 8f6cf94..da10c58 100755 --- a/authy-ssh +++ b/authy-ssh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -VERSION="1.6" +VERSION="1.7" AUTHY_URL="https://api.authy.com" APP_ROOT=`dirname $0` CONFIG_FILE="$APP_ROOT/authy-ssh.conf" @@ -59,6 +59,10 @@ function escape_number() { sed 's/[^0-9]*//g' <<< $* } +function os_version() { + echo `uname -srm` +} + function read_input() { read -t "$READ_TIMEOUT" input echo "$(escape_input $input)" @@ -92,6 +96,11 @@ function require_curl() { return $FAIL } +function user_agent() { + os="$(os_version)" + echo "User-Agent: AuthySSH/${VERSION} (${os})" +} + function find_sshd_config() { debug "Trying to find sshd_config file" if [[ -f /etc/sshd_config ]] @@ -413,7 +422,8 @@ function register_user_on_authy() { return $FAIL fi - response=`curl --connect-timeout 10 "${url}" -d user[email]="${email}" -d user[country_code]="${country_code}" -d user[cellphone]="${cellphone}" -s 2>/dev/null` + useragent="$(user_agent)" + response=`curl --connect-timeout 10 "${url}" -A "${useragent}" -d user[email]="${email}" -d user[country_code]="${country_code}" -d user[cellphone]="${cellphone}" -s 2>/dev/null` ok=true debug "[register-user] url: $url | response: $response | curl exit stats: $?" @@ -496,8 +506,17 @@ function login() { return $FAIL fi + size=${#authy_token} + if [[ $size -lt 6 || $size -gt 10 ]] + then + red "You have to enter a valid token." + return $FAIL + fi + + useragent="$(user_agent)" url="$AUTHY_URL/protected/json/verify/${authy_token}/${authy_id}?api_key=${AUTHY_API_KEY}&force=true" - response=`curl --connect-timeout 30 -sL -w "|%{http_code}" "${url}"` + + response=`curl --connect-timeout 30 -sL -w "|%{http_code}" -A "${useragent}" "${url}"` curl_exit_code=$? IFS='|' response_body=($response) # convert to array @@ -512,7 +531,7 @@ function login() { if [[ $default_verify_action == "disable" ]] then debug "Checking if authy service is up." - check_response=`curl --connect-timeout 10 -s "${AUTHY_URL}" -o /dev/null` + check_response=`curl --connect-timeout 10 -s "${AUTHY_URL}" -A "${useragent}" -o /dev/null` check_exit_code=$? if [[ $check_exit_code == 7 || $check_exit_code == 28 ]] @@ -552,7 +571,8 @@ function request_sms() { authy_id="$(escape_number $1)" url="$AUTHY_URL/protected/json/sms/${authy_id}?api_key=${AUTHY_API_KEY}&force=true" - response=`curl --connect-timeout 10 "${url}" 2>/dev/null` + useragent="$(user_agent)" + response=`curl --connect-timeout 10 -A "${useragent}" "${url}" 2>/dev/null` debug "[request sms] url: $url | response: $response | curl exit stats: $?" if [[ $response == *success*sent* ]] diff --git a/tests/test_login.rb b/tests/test_login.rb index cb29192..42cc36d 100644 --- a/tests/test_login.rb +++ b/tests/test_login.rb @@ -8,6 +8,19 @@ stdin.puts "1234" end + if read_until(stdout, /You have to enter a valid token/i) + green " [OK]" + else + red " [FAILED]" + end +end + +authy_ssh("login") do |stdin, stdout| + if read_until(stdout, /Authy Token/) + print "Sending invalid token: 123456" + stdin.puts "123456" + end + if read_until(stdout, /Invalid token/i) green " [OK]" else @@ -17,20 +30,20 @@ authy_ssh("login", "mode" => "test", "authy_token" => "32|21") do |stdin, stdout| if read_until(stdout, /Authy Token/) - print "Sending invalid token: #1-}2$34 5'6{7" - stdin.puts "#1-}2$34 5'6{7" + print "Sending invalid token: 0000000/12w#-}A$Rf s'Q{3A" + stdin.puts "0000000/12w#-}A$Rf s'Q{3A" end - if read_until(stdout, /Logging 2 with 1234567 in login mode./i) + if read_until(stdout, /Logging 2 with 0000000123 in login mode./i) green " [OK]" else red " [FAILED]" end end -authy_ssh("login", "AUTHY_TOKEN" => "32|21") do |stdin, stdout| +authy_ssh("login", "AUTHY_TOKEN" => "323|212") do |stdin, stdout| print "Loging in using the AUTHY_TOKEN env var" - if read_until(stdout, /Logging 2 with 3221 in login mode./i) + if read_until(stdout, /Logging 2 with 323212 in login mode./i) green " [OK]" else red " [FAILED]"