-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-refresh-token.sh
More file actions
executable file
·58 lines (48 loc) · 2 KB
/
get-refresh-token.sh
File metadata and controls
executable file
·58 lines (48 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash
# Configuration
CLIENT_ID="scp"
DEVICE_AUTH_URL="https://www.servercontrolpanel.de/realms/scp/protocol/openid-connect/auth/device"
TOKEN_URL="https://www.servercontrolpanel.de/realms/scp/protocol/openid-connect/token"
echo "Step 1: Requesting device code..."
RESPONSE=$(curl -s -X POST "$DEVICE_AUTH_URL" -d "client_id=$CLIENT_ID" -d "scope=offline_access openid")
DEVICE_CODE=$(echo "$RESPONSE" | grep -oP '"device_code":"\K[^"]+')
USER_CODE=$(echo "$RESPONSE" | grep -oP '"user_code":"\K[^"]+')
VERIFY_URL=$(echo "$RESPONSE" | grep -oP '"verification_uri_complete":"\K[^"]+')
INTERVAL=$(echo "$RESPONSE" | grep -oP '"interval":\K[0-9]+')
if [ -z "$DEVICE_CODE" ]; then
echo "Error: Could not obtain device code."
echo "Response: $RESPONSE"
exit 1
fi
echo "----------------------------------------------------------------"
echo "Please visit the following URL in your browser and log in:"
echo ""
echo "$VERIFY_URL"
echo ""
echo "User Code: $USER_CODE"
echo "----------------------------------------------------------------"
echo "Waiting for authentication..."
while true; do
sleep "$INTERVAL"
TOKEN_RESPONSE=$(curl -s -X POST "$TOKEN_URL" -d "grant_type=urn:ietf:params:oauth:grant-type:device_code" -d "device_code=$DEVICE_CODE" -d "client_id=$CLIENT_ID")
# Check if we got an error
ERROR=$(echo "$TOKEN_RESPONSE" | grep -oP '"error":"\K[^"]+')
if [ -z "$ERROR" ]; then
REFRESH_TOKEN=$(echo "$TOKEN_RESPONSE" | grep -oP '"refresh_token":"\K[^"]+')
if [ -n "$REFRESH_TOKEN" ]; then
echo ""
echo "Success! Your Refresh Token is:"
echo ""
echo "$REFRESH_TOKEN"
echo ""
echo "You can now use this token with the exporter:"
echo "./netcupscp-exporter --refresh-token $REFRESH_TOKEN"
exit 0
fi
elif [ "$ERROR" != "authorization_pending" ]; then
echo "Error: $ERROR"
echo "Response: $TOKEN_RESPONSE"
exit 1
fi
echo -n "."
done