-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
69 lines (59 loc) · 2.3 KB
/
app.py
File metadata and controls
69 lines (59 loc) · 2.3 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
59
60
61
62
63
64
65
66
67
68
69
import logging
import flask
import os
from flask import jsonify, request
import json
from bitly import Bitly
from helper import Helper
from flask_jwt_extended import create_access_token
from flask_jwt_extended import get_jwt_identity
from flask_jwt_extended import jwt_required
from flask_jwt_extended import JWTManager
jwt_username = os.environ.get('JWTUSER')
jwt_password = os.environ.get('JWTPASS')
jwt_secret_key = os.environ.get('JWT_SECRET_KEY')
token = os.environ.get('BITLYTOKEN')
app = flask.Flask(__name__)
app.config["DEBUG"] = True
app.config["JWT_SECRET_KEY"] = jwt_secret_key # Change this!
bitly_object = Bitly(token)
helper_object = Helper(token)
jwt = JWTManager(app)
@app.route("/login", methods=["POST"])
def login():
username = request.json.get("username", None)
password = request.json.get("password", None)
if username != jwt_username or password != jwt_password:
return jsonify({"msg": "Bad username or password"}), 401
access_token = create_access_token(identity=username)
return jsonify(access_token=access_token)
@app.route('/', methods=['GET'])
@jwt_required()
def home():
group = bitly_object.group_getter()
if 'FORBIDDEN' in group.values():
return jsonify({'message':'Bitly token is bad, broken or expired.'})
else:
group_links = bitly_object.bitlink_getter(group['default_group_guid'])
links = helper_object.json_snippet_builder(group_links)
data = helper_object.avg_calculator(links)
return jsonify(data)
def main():
# Security warnings for default credentials
if jwt_username == "test" and jwt_password == "test":
logger.warning('JWT username and password are default. Change this for production!')
if app.config['JWT_SECRET_KEY'] == "changethis":
logger.warning('JWT Secret is set to default. Change this for production!')
if not bitly_token:
logger.error('Bitly API token missing. Exiting.')
exit(1)
else:
group = bitly_object.group_getter()
if 'FORBIDDEN' in group.values():
logger.error("Bitly doesn't like your token. Replace or check it. Exiting.")
exit(1)
else:
# For production, consider using waitress or gunicorn
app.run(host="0.0.0.0", port=8080)
if __name__ == "__main__":
main()