-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtlog.pyw
105 lines (85 loc) · 2.9 KB
/
tlog.pyw
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
import os
import time
import random
from flask import Flask, render_template, request, redirect
from cryptography.fernet import Fernet
import urllib.parse
import webbrowser
clientid = "83dnr5c7wspcobvhegzd0029gr9apq"
callback = "http://localhost:8080/callback?"
scope = "chat%3Aread+chat%3Aedit"
if os.path.exists('.key'):
#check if .key is hidden, if so, unhide it
if os.system('attrib .key') == 0:
os.system('attrib -h .key')
if os.path.exists('.storage'):
#check if .storage is hidden, if so, unhide it
if os.system('attrib .storage') == 0:
os.system('attrib -h .storage')
try:
with open('.key', 'r') as key:
enc_key = key.read()
except FileNotFoundError:
enc_key = Fernet.generate_key()
with open('.key', 'wb') as key:
key.write(enc_key)
#make .key hidden
os.system('attrib +h .key')
fern = Fernet(enc_key)
def oauth():
return f'https://id.twitch.tv/oauth2/authorize?client_id=83dnr5c7wspcobvhegzd0029gr9apq&force_verify=true&redirect_uri=http://localhost:8080/callback?&response_type=token&scope=chat%3Aread+chat%3Aedit'
app = Flask(__name__)
@app.route('/')
def index():
#use random string for state to prevent CSRF
rgen = random.getrandbits(256) + int(time.time())
#encode state as hex
state = str(hex(rgen))
print(state)
#save state to file
with open('.state', 'w') as f:
f.write(state)
#redirect to twitch oauth page
return redirect (oauth() + f'&state={state}')
@app.route('/callback')
def callback():
return render_template('callback.html')
@app.route('/auth')
def auth():
args = request.args.get('fragment')
urllib.parse.unquote(args)
print(args)
args = args.split('&')
for arg in args:
arg = arg.split('=')
if arg[0] == 'access_token':
access_token = arg[1]
elif arg[0] == 'state':
state = arg[1]
else:
pass
if state == open('.state', 'r').read():
print('state is valid')
with open('.storage', 'wb') as storage:
storage.write(fern.encrypt(access_token.encode('utf-8')))
storage.close()
with open('.storage', 'r') as storage:
encrypted = storage.read()
decrypted = fern.decrypt(encrypted.encode('utf-8')).decode('utf-8')
if decrypted == access_token:
print('token is valid')
else:
print('token is invalid')
os.remove('.state')
os.system('attrib +h .storage')
with open('.logsuccess', 'w') as f:
f.write('1')
return "Logged in successfully, you can close this window now."
else:
print('state is invalid')
with open('.logfail', 'w') as f:
f.write('1')
return "State is invalid, please try again in a few minutes."
if __name__ == '__main__':
webbrowser.open('http://localhost:8080')
app.run(port=8080)