forked from pastorhudson/mtb-pykeybasebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook_bot.py
168 lines (132 loc) · 4.95 KB
/
webhook_bot.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import asyncio
import logging
from datetime import datetime
from flask import jsonify, render_template
import os
from werkzeug.exceptions import BadRequest, HTTPException
from botcommands.youtube_dlp import get_mp4
from flask import send_file
from yt_dlp.utils import DownloadError
from flask import Flask, request
from crud import s
from models import MessageQueue, ALGORITHM, JWT_SECRET_KEY, User, JWT_REFRESH_SECRET_KEY
from jose import jwt
from pydantic import ValidationError
from pydantic import BaseModel
from werkzeug.middleware.proxy_fix import ProxyFix
from flask import escape
app = Flask(__name__, template_folder='/app/www')
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1)
logging.basicConfig(level=logging.DEBUG)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/ytv')
def ytv():
if request.args.get('url'):
url = request.args.get('url')
try:
payload = get_mp4(url)
# print(payload)
except DownloadError as e:
payload = {'Error': str(e)}
return jsonify(payload)
try:
return send_file(payload['file'],
attachment_filename='v.mp4')
except Exception as e:
return jsonify(payload)
else:
return '<p>Wat?</p>'
@app.route('/add_message', methods=['POST'])
def add_message():
try:
data = request.get_json()
except BadRequest:
return jsonify({"error": "Invalid JSON data"}), 400
message = escape(data.get('message'))
# destination = escape(data.get('destination'))
sender = escape(data.get('sender'))
token = escape(data.get("token"))
client_ip = escape(request.remote_addr)
try:
user, conversation_id = asyncio.run(get_user(token))
except HTTPException as e:
logging.info(e)
return jsonify({"error": "Could Not Validate Credentials"}), 403
if not message or not token:
return jsonify({"error": "Missing data"}), 400
session = s
new_message = MessageQueue(message=message, destination=conversation_id, sender=sender, ip=client_ip, user=user)
session.add(new_message)
session.commit()
return {"message": "Message added successfully."}, 201
@app.route('/refresh', methods=['POST'])
def auth_refresh():
logging.info("refresh token")
try:
data = request.get_json()
except BadRequest:
return jsonify({"error": "Invalid JSON data"}), 400
token = escape(data.get("token"))
client_ip = escape(request.remote_addr)
logging.info(f"Client IP: {client_ip} attempting to refresh token")
try:
user, conversation_id = asyncio.run(check_refresh(token))
except HTTPException as e:
logging.info(e)
return jsonify({"error": str(e)}), 403
if not token:
return jsonify({"error": "Missing data"}), 400
return jsonify({"token": user.create_access_token(conversation_id=conversation_id),
"refresh_token": user.create_refresh_token(conversation_id=conversation_id)}), 200
class TokenSchema(BaseModel):
access_token: str
refresh_token: str
class TokenPayload(BaseModel):
user: str = None
conversation_id: str = None
exp: int = None
class RefreshTokenPayload(BaseModel):
user: str = None
conversation_id: str = None
exp: int = None
refresh_token: bool = False
async def get_user(token: str):
try:
payload = jwt.decode(
token, JWT_SECRET_KEY, algorithms=[ALGORITHM]
)
token_data = TokenPayload(**payload)
if datetime.fromtimestamp(token_data.exp) < datetime.now():
raise HTTPException("Token Expired")
except(jwt.JWTError, ValidationError):
raise HTTPException("Could not validate credentials")
user = s.query(User).filter(User.username == token_data.user).first()
conversation_id = token_data.conversation_id
if user is None:
raise HTTPException("Could not find user")
return user, conversation_id
async def check_refresh(token: str):
try:
payload = jwt.decode(
token, JWT_REFRESH_SECRET_KEY, algorithms=[ALGORITHM]
)
logging.info(payload)
token_data = RefreshTokenPayload(**payload)
logging.info(datetime.fromtimestamp(token_data.exp))
if datetime.fromtimestamp(token_data.exp) < datetime.now():
raise HTTPException("Token Expired")
except (jwt.JWTError, ValidationError):
raise HTTPException("Could not validate credentials")
logging.info("processed token")
user = s.query(User).filter(User.username == token_data.user).first()
conversation_id = token_data.conversation_id
logging.info("got user and convo")
if user is None:
raise HTTPException("Could not find user")
return user, conversation_id
if __name__ == '__main__':
# Bind to PORT if defined, otherwise default to 5000.
port = int(os.environ.get('PORT', 5000))
app.run(host='127.0.0.1', port=port)