-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp-ui.py
128 lines (103 loc) · 3.5 KB
/
app-ui.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
# -*- coding: utf-8 -*-
from functools import wraps
from flask import Flask, jsonify, request, abort, render_template, redirect
from discord.BotApiModel import BotApiModel
import discord.manager as botMan
from dotenv import load_dotenv
import os
import jwt
app = Flask(__name__)
load_dotenv()
def jwt_auth_config():
alg = 'RS256'
key = open(os.getenv('JWT_KEY_PATH')).read()
issuer = os.getenv('JWT_ISSUER')
audience = os.getenv('JWT_AUDIENCE')
role = os.getenv('JWT_ROLE')
return (key, alg, issuer, audience, role)
(KEY, ALGORITHM, ISSUER, AUDIENCE, REQUIRED_ROLE) = jwt_auth_config()
def validate_token(token):
try:
decoded = jwt.decode(token, KEY, algorithms=[ALGORITHM], issuer=ISSUER, audience=AUDIENCE)
has_role = REQUIRED_ROLE in decoded['roles']
return has_role
except:
return False
def authorize(user = False):
def require_appkey(view_function):
@wraps(view_function)
def decorated_function(*args, **kwargs):
if request.cookies.get('identity-token') and validate_token(request.cookies.get('identity-token')):
return view_function(*args, **kwargs)
else:
if user:
return redirect(os.getenv('IDENTITY_URL'))
else:
abort(401)
return decorated_function
return require_appkey
@app.route('/api/discordbots', methods=['GET'])
@authorize()
def get_bots():
botModels = []
for bot in botMan.get_bots():
model = BotApiModel()
model.Id = bot.id
model.Name = bot.name
model.Description = bot.description
model.IsRunning = bot.is_running()
botModels.append(model.serialize())
return jsonify(botModels)
@app.route('/api/discordbots/runningbots', methods=['DELETE'])
@authorize()
def killall_bots():
botMan.killall()
return "Ok", 200
@app.route('/api/discordbots', methods=['POST'])
@authorize()
def add_bot():
github = request.form.get('github')
name = request.form.get('name')
description = request.form.get('description')
entry = request.form.get('entry')
environment = request.form.get('environment')
botMan.add_bot(github, name, description, entry, environment)
return "Ok", 200
@app.route('/api/discordbots/<string:id>/pull', methods=['POST'])
@authorize()
def pull_bot(id):
botMan.pull_bot(id)
return "Ok", 200
@app.route('/api/discordbots/<string:id>/config', methods=['POST'])
@authorize()
def update_config(id):
environment = request.form.get('environment')
botMan.update_env(id, environment)
return "Ok", 200
@app.route('/api/discordbots/<string:id>/config', methods=['GET'])
@authorize()
def get_config(id):
return jsonify(botMan.get_env(id))
@app.route('/api/discordbots/<string:id>', methods=['DELETE'])
@authorize()
def delete_bot(id):
botMan.remove_bot(id)
return "Ok", 200
@app.route('/api/discordbots/runningbots/<string:id>', methods=['DELETE'])
@authorize()
def kill_bot(id):
botMan.kill(id)
return "Ok", 200
@app.route('/api/discordbots/runningbots/<string:id>', methods=['POST'])
@authorize()
def run_bot(id):
botMan.start(id)
return "Ok", 200
@app.route('/', methods=['GET'])
@authorize(True)
def main_view():
return render_template('index.html')
if __name__ == "__main__":
app.config['JSON_AS_ASCII'] = False
app.run(port=9989)
botMan.killall()