FMN_bot/auth_helper.sh

59 lines
1.5 KiB
Bash
Raw Normal View History

2022-08-31 10:20:49 +00:00
#!/bin/sh
default_auth_browser()
{
w3m $@
}
mkdir -m 711 -p .app_sessions
echo 'Input instance (example.example)'
read instance
echo 'Input client/app name'
read client_name
export instance_point="https://$instance/api/v1"
auth_api_create_client()
{
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" \
--create-file-mode 0600
fi
}
auth_api_get_code()
{
auth_api_create_client
client_id=$(jq -r '.client_id' ".app_sessions/$instance")
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 > "$basedir"/.auth
chmod 600 "$basedir"/.auth
echo "$token" > "$basedir"/.auth
}
auth_api_get_token