-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitch_auth.py
144 lines (117 loc) · 4.15 KB
/
twitch_auth.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Twitch OAuth Handling
from config import load_config
import yaml
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse, parse_qs, urlunparse, urlencode
import ssl
import requests
from os.path import exists
code = None
access_token = None
refresh_token = None
cfg = load_config()
redirect_uri = f'https://{cfg["server"]["host"]}:{cfg["server"]["port"]}/'
class HandleRequests(BaseHTTPRequestHandler):
keep_running = True
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
global code
self._set_headers()
path = urlparse(self.path)
# print(f"Incoming request {path}")
if not path.query:
request_payload = {
"client_id": cfg['twitch']['client_id'],
"force_verify": 'false',
"redirect_uri": redirect_uri,
"response_type": 'code',
"scope": 'chat:edit chat:read'
}
encoded_payload = urlencode(request_payload)
url = 'https://id.twitch.tv/oauth2/authorize?' + encoded_payload
self.wfile.write(f'<html><head><body><a href="{url}">Click here to auth with Twitch</a></body></head>'.encode('utf-8'))
else:
code = parse_qs(path.query)['code'][0]
print(f'Code: {code}')
HandleRequests.keep_running = False
# Authorisation Code Flow
# Launch HTTP Server, listen for request, direct user to Twitch,
# listen for response.
def auth_code_flow():
httpd = HTTPServer((cfg['server']['host'], cfg['server']['port']), HandleRequests)
httpd.socket = ssl.wrap_socket (httpd.socket,
keyfile="key.pem",
certfile='cert.pem', server_side=True)
print(f'Please open your browser at {redirect_uri}')
# Keep listening until we handle a post request
while HandleRequests.keep_running:
httpd.handle_request()
def read_code():
global access_token
global refresh_token
if exists("code.yml"):
print("Reading Twitch tokens")
with open("code.yml", "r") as ymlfile:
code = yaml.safe_load(ymlfile)
access_token = code['access_token']
refresh_token = code['refresh_token']
return True
return False
def write_code():
global access_token
global refresh_token
with open("code.yml", "w") as ymlfile:
print("Writing Twitch tokens")
yaml.dump({
'access_token': access_token,
'refresh_token': refresh_token
}, ymlfile)
def validate():
print("Validating Twitch tokens...", end='')
url = 'https://id.twitch.tv/oauth2/validate'
r = requests.get(url, headers={'Authorization': f'Oauth {access_token}'})
if r.status_code == 200:
print("valid")
return True
elif r.status_code == 401:
print("invalid")
return False
else:
raise Exception(f'Unrecognised status code on validate {r.status_code}')
def get_tokens():
print("Fetching Twitch tokens")
global access_token
global refresh_token
url = 'https://id.twitch.tv/oauth2/token'
request_payload = {
"client_id": cfg['twitch']['client_id'],
"client_secret": cfg['twitch']['client_secret'],
'redirect_uri': redirect_uri
}
if code:
request_payload['grant_type'] = 'authorization_code'
request_payload['code'] = code
if refresh_token:
request_payload['grant_type'] = 'refresh_token'
request_payload['refresh_token'] = refresh_token
r = requests.post(url, data=request_payload).json()
try:
access_token = r['access_token']
refresh_token = r['refresh_token']
print(f'Access token: {access_token}')
print(f'Refresh token: {refresh_token}')
except:
print("Unexpected response on redeeming auth code:")
print(r)
def oauth():
if read_code(): # Code exists
if not validate():
get_tokens()
else:
auth_code_flow() # User login auth
get_tokens()
write_code()
return access_token