FMN_bot/auth_helper.sh

62 lines
1.7 KiB
Bash
Executable file

#!/bin/sh
default_auth_browser()
{
$browser $@
}
mkdir -m 711 -p .app_sessions
echo 'Input instance (example.example)'
read instance
echo 'Input client/app name (Example: FMN_bot)'
read client_name
export instance_point="https://$instance/api/v1"
auth_api_create_client()
{
mkdir -m 711 -p ".app_sessions"
if [ ! -e ".app_sessions/$instance" ]; then
curl -s --compressed --url "$instance_point/apps" \
--data-urlencode "client_name=$client_name" \
--data-urlencode 'redirect_uris=urn:ietf:wg:oauth:2.0:oob' \
--data-urlencode 'scopes=read write follow' \
--output ".app_sessions/$instance"
chmod 600 ".app_sessions/$instance"
fi
}
auth_api_get_code()
{
auth_api_create_client
client_id=$(jq -r '.client_id' ".app_sessions/$instance")
echo "Auth link (if fail) https://$instance/oauth/authorize?client_id=$client_id&response_type=code&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=read+write+follow"
default_auth_browser "https://$instance/oauth/authorize?client_id=$client_id&response_type=code&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=read+write+follow"
echo 'Input token-code:'
read pass
}
auth_api_get_token()
{
auth_api_get_code
clear
client_id=$(jq -r '.client_id' ".app_sessions/$instance")
client_secret=$(jq -r '.client_secret' ".app_sessions/$instance")
token=$(curl -s --compressed --url "https://$instance/oauth/token" \
--data-urlencode 'grant_type=authorization_code' \
--data-urlencode "client_id=$client_id" \
--data-urlencode "client_secret=$client_secret" \
--data-urlencode "redirect_uri=urn:ietf:wg:oauth:2.0:oob" \
--data-urlencode 'scope=read write follow' \
--data-urlencode "code=$pass" | jq -r '.access_token')
echo > .auth
chmod 600 .auth
echo "$token" > .auth
}
auth_api_get_token