2020-09-19 17:21:38 +02:00
|
|
|
#!/bin/bash
|
|
|
|
# common.sh - common functions for OPS app
|
|
|
|
|
|
|
|
# get_auth_string()
|
|
|
|
function get_auth_string() {
|
2020-10-05 02:19:01 +02:00
|
|
|
if ! session_verify "${cookies[sh_session]}"; then
|
2020-09-19 17:21:38 +02:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2020-11-13 15:47:32 +01:00
|
|
|
local user="$(session_get_username ${cookies[sh_session]})"
|
2020-10-05 19:03:03 +02:00
|
|
|
|
2020-10-05 19:06:36 +02:00
|
|
|
echo "Authorization: Bearer $(cat secret/authTokens.dat | grep -P "^$user:" | sed -s "s/$user://")"
|
2020-09-19 20:49:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_refresh_token() {
|
2020-10-05 02:19:01 +02:00
|
|
|
if ! session_verify "${cookies[sh_session]}"; then
|
2020-09-19 20:49:44 +02:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2020-11-13 15:47:32 +01:00
|
|
|
local user="$(session_get_username ${cookies[sh_session]})"
|
2020-10-05 19:03:03 +02:00
|
|
|
|
2020-10-05 19:06:36 +02:00
|
|
|
echo "$(cat secret/refreshTokens.dat | grep -P "^$user:" | sed -s "s/$user://")"
|
2020-09-19 17:21:38 +02:00
|
|
|
}
|
|
|
|
|
2020-09-19 20:40:20 +02:00
|
|
|
# check_if_user_exists(file)
|
|
|
|
function check_if_user_exists() {
|
2020-10-05 02:19:01 +02:00
|
|
|
if ! session_verify "${cookies[sh_session]}"; then
|
2020-09-19 20:40:20 +02:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2020-11-13 15:47:32 +01:00
|
|
|
local user=$(session_get_username ${cookies[sh_session]})
|
2020-10-05 19:06:36 +02:00
|
|
|
|
|
|
|
[[ $1 == "refreshToken" ]] && [[ $(cat secret/refreshTokens.dat | grep -P "^$user:") != "" ]] && return 1
|
|
|
|
[[ $1 == "authToken" ]] && [[ $(cat secret/authTokens.dat | grep -P "^$user:") != "" ]] && return 1
|
2020-09-19 20:40:20 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-09-19 17:21:38 +02:00
|
|
|
# add_account_refreshtoken(refreshtoken)
|
|
|
|
function add_account_refreshtoken() {
|
2020-10-05 02:19:01 +02:00
|
|
|
if ! session_verify "${cookies[sh_session]}"; then
|
2020-09-19 17:21:38 +02:00
|
|
|
exit 0
|
|
|
|
fi
|
2020-09-19 20:40:20 +02:00
|
|
|
|
|
|
|
if check_if_user_exists refreshToken; then
|
2020-10-05 02:19:01 +02:00
|
|
|
echo "$(session_get_username ${cookies[sh_session]}):$1" >> secret/refreshTokens.dat
|
2020-09-19 20:40:20 +02:00
|
|
|
fi
|
2020-09-19 17:21:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# add_account_authtoken(authtoken)
|
|
|
|
function add_account_authtoken() {
|
2020-10-05 02:19:01 +02:00
|
|
|
if ! session_verify "${cookies[sh_session]}"; then
|
2020-09-19 17:21:38 +02:00
|
|
|
exit 0
|
|
|
|
fi
|
2020-09-19 20:40:20 +02:00
|
|
|
|
|
|
|
if check_if_user_exists authToken; then
|
2020-10-05 02:19:01 +02:00
|
|
|
echo "$(session_get_username ${cookies[sh_session]}):$1" >> secret/authTokens.dat
|
2020-09-19 20:40:20 +02:00
|
|
|
fi
|
2020-10-05 00:46:42 +02:00
|
|
|
}
|
2020-10-05 02:19:01 +02:00
|
|
|
|
|
|
|
# update_account_authtoken(username)
|
|
|
|
function update_account_authtoken() {
|
2020-11-13 15:47:32 +01:00
|
|
|
local a=$(cat secret/refreshTokens.dat | grep -P "^$1\:")
|
|
|
|
local token=$(awk -F: '{print $2}' <<< $a);
|
|
|
|
local auth=''
|
2020-11-12 13:06:17 +01:00
|
|
|
while true; do
|
|
|
|
if [[ "$auth" == '' ]]; then
|
|
|
|
auth=$(curl -s -X POST -H "Content-type: application/json" --data '{"phoneOS": "Android", "refreshToken": "'"$token"'"}' https://api-inmobile-pl.easypack24.net/v1/authenticate | jq -r .authToken | sed -s 's/Bearer //g')
|
|
|
|
sleep 1
|
|
|
|
else
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
2020-10-05 02:19:01 +02:00
|
|
|
sed -E -i 's/^'"$1"':.*/'"$1:$auth"'/g' secret/authTokens.dat
|
|
|
|
}
|
2020-10-29 22:23:06 +01:00
|
|
|
|
|
|
|
# check_mail(mail)
|
|
|
|
function check_mail() {
|
|
|
|
if [[ $(cat secret/mail.dat | grep ":$1:") == '' ]]; then
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#get_account_mail(username)
|
|
|
|
function get_account_mail() {
|
|
|
|
[[ $1 == '' ]] && return
|
|
|
|
|
|
|
|
cat secret/mail.dat | grep "^$1:" | awk -F: '{print $2}'
|
|
|
|
}
|
|
|
|
|
2020-11-06 00:27:48 +01:00
|
|
|
#account_verified(username)
|
|
|
|
function account_verified() {
|
2020-10-29 22:23:06 +01:00
|
|
|
[[ $1 == '' ]] && return
|
2020-11-06 00:27:48 +01:00
|
|
|
if [[ "$(cat secret/mail.dat | grep -P "^$1:" | awk -F: '{print $4}')" != "yes" ]]; then
|
2020-10-29 22:23:06 +01:00
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
2020-11-06 00:27:48 +01:00
|
|
|
|
|
|
|
#account_gen_reset_code(username, force)
|
|
|
|
function account_gen_reset_code() {
|
|
|
|
[[ "$1" == '' ]] && return
|
|
|
|
|
|
|
|
if ! account_verified "$1"; then
|
2020-11-13 15:47:32 +01:00
|
|
|
local data="$(cat secret/mail.dat | grep -P "^$1:")"
|
2020-11-06 00:27:48 +01:00
|
|
|
|
2020-11-13 15:47:32 +01:00
|
|
|
local user="$1"
|
|
|
|
local mail="$(awk -F: '{print $2}' <<< "$data")"
|
|
|
|
local old_code="$(awk -F: '{print $3}' <<< "$data")"
|
|
|
|
local timestamp="$(awk -F: '{print $5}' <<< "$data")"
|
2020-11-06 00:27:48 +01:00
|
|
|
|
2020-11-13 15:47:32 +01:00
|
|
|
local new_timestamp="$(date "+%s")"
|
|
|
|
local new_code="$RANDOM"
|
2020-11-06 00:27:48 +01:00
|
|
|
|
|
|
|
if [[ $(date "+%s") -gt $((timestamp+3600)) || $2 == true ]]; then
|
|
|
|
sed -i "s/$user:$mail:$old_code:yes:$timestamp/$user:$mail:$new_code:yes:$new_timestamp/" secret/mail.dat
|
|
|
|
echo -n "$new_code"$(date "+%d%m%y") | sha1sum | awk '{print $1}' | cut -c 1-10
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 2
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#reset_pwd(username, hash, password)
|
|
|
|
function reset_pwd() {
|
|
|
|
[[ "$1" == '' || "$2" == '' || "$3" == '' ]] && return
|
2020-11-13 15:47:32 +01:00
|
|
|
local user="$1"
|
|
|
|
local hash="$2"
|
|
|
|
local pass="$3"
|
2020-11-06 00:27:48 +01:00
|
|
|
|
|
|
|
if [[ "$(echo -n $(cat secret/mail.dat | grep -P "^$user:" | awk -F: '{print $3}')$(date "+%d%m%y") | sha1sum | awk '{print $1}' | cut -c 1-10)" == "$hash" ]]; then
|
|
|
|
delete_account "$user"
|
|
|
|
register "$user" "$pass"
|
|
|
|
|
|
|
|
# prevents resetting the password with the same hash twice
|
|
|
|
account_gen_reset_code "$user" true >> /dev/null
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|