FMN_bot/auth_helper.sh

62 lines
1.7 KiB
Bash
Raw Normal View History

2022-08-31 13:20:49 +03:00
#!/bin/sh
default_auth_browser()
{
$browser $@
2022-08-31 13:20:49 +03:00
}
mkdir -m 711 -p .app_sessions
echo 'Input instance (example.example)'
read instance
echo 'Input client/app name (Example: FMN_bot)'
2022-08-31 13:20:49 +03:00
read client_name
export instance_point="https://$instance/api/v1"
auth_api_create_client()
{
2022-09-05 00:43:02 +03:00
mkdir -m 711 -p ".app_sessions"
2022-08-31 13:20:49 +03:00
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' \
2022-09-05 00:43:02 +03:00
--output ".app_sessions/$instance"
chmod 600 ".app_sessions/$instance"
2022-08-31 13:20:49 +03:00
fi
}
auth_api_get_code()
{
auth_api_create_client
client_id=$(jq -r '.client_id' ".app_sessions/$instance")
2022-09-05 00:43:02 +03:00
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"
2022-08-31 13:20:49 +03:00
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')
2022-09-05 00:43:02 +03:00
echo > .auth
chmod 600 .auth
echo "$token" > .auth
2022-08-31 13:20:49 +03:00
}
auth_api_get_token
2022-09-05 00:43:02 +03:00