Skip to content
This repository has been archived by the owner on May 9, 2018. It is now read-only.

Adding User-Agent info to curl requests. #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 25 additions & 5 deletions authy-ssh
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)"
Expand Down Expand Up @@ -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 ]]
Expand Down Expand Up @@ -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: $?"
Expand Down Expand Up @@ -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

Expand All @@ -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 ]]
Expand Down Expand Up @@ -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* ]]
Expand Down
23 changes: 18 additions & 5 deletions tests/test_login.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]"
Expand Down