server.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. import os
  2. import ssl
  3. import arrow
  4. import flask_profiler
  5. import sentry_sdk
  6. from flask import Flask, redirect, url_for, render_template, request, jsonify
  7. from flask_admin import Admin
  8. from flask_cors import cross_origin
  9. from flask_login import current_user
  10. from sentry_sdk.integrations.flask import FlaskIntegration
  11. from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
  12. from sentry_sdk.integrations.aiohttp import AioHttpIntegration
  13. from app import paddle_utils
  14. from app.admin_model import SLModelView, SLAdminIndexView
  15. from app.api.base import api_bp
  16. from app.auth.base import auth_bp
  17. from app.config import (
  18. DEBUG,
  19. DB_URI,
  20. FLASK_SECRET,
  21. SENTRY_DSN,
  22. URL,
  23. SHA1,
  24. PADDLE_MONTHLY_PRODUCT_ID,
  25. RESET_DB,
  26. FLASK_PROFILER_PATH,
  27. FLASK_PROFILER_PASSWORD,
  28. SENTRY_FRONT_END_DSN,
  29. )
  30. from app.dashboard.base import dashboard_bp
  31. from app.developer.base import developer_bp
  32. from app.discover.base import discover_bp
  33. from app.extensions import db, login_manager, migrate
  34. from app.jose_utils import get_jwk_key
  35. from app.log import LOG
  36. from app.models import (
  37. Client,
  38. User,
  39. ClientUser,
  40. GenEmail,
  41. RedirectUri,
  42. Subscription,
  43. PlanEnum,
  44. ApiKey,
  45. CustomDomain,
  46. LifetimeCoupon,
  47. Directory,
  48. Mailbox,
  49. DeletedAlias,
  50. )
  51. from app.monitor.base import monitor_bp
  52. from app.oauth.base import oauth_bp
  53. if SENTRY_DSN:
  54. LOG.d("enable sentry")
  55. sentry_sdk.init(
  56. dsn=SENTRY_DSN,
  57. integrations=[
  58. FlaskIntegration(),
  59. SqlalchemyIntegration(),
  60. AioHttpIntegration(),
  61. ],
  62. )
  63. # the app is served behin nginx which uses http and not https
  64. os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
  65. def create_app() -> Flask:
  66. app = Flask(__name__)
  67. app.url_map.strict_slashes = False
  68. app.config["SQLALCHEMY_DATABASE_URI"] = DB_URI
  69. app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
  70. app.secret_key = FLASK_SECRET
  71. app.config["TEMPLATES_AUTO_RELOAD"] = True
  72. # to avoid conflict with other cookie
  73. app.config["SESSION_COOKIE_NAME"] = "slapp"
  74. init_extensions(app)
  75. register_blueprints(app)
  76. set_index_page(app)
  77. jinja2_filter(app)
  78. setup_error_page(app)
  79. setup_favicon_route(app)
  80. setup_openid_metadata(app)
  81. init_admin(app)
  82. setup_paddle_callback(app)
  83. setup_do_not_track(app)
  84. if FLASK_PROFILER_PATH:
  85. LOG.d("Enable flask-profiler")
  86. app.config["flask_profiler"] = {
  87. "enabled": True,
  88. "storage": {"engine": "sqlite", "FILE": FLASK_PROFILER_PATH},
  89. "basicAuth": {
  90. "enabled": True,
  91. "username": "admin",
  92. "password": FLASK_PROFILER_PASSWORD,
  93. },
  94. "ignore": ["^/static/.*", "/git", "/exception"],
  95. }
  96. flask_profiler.init_app(app)
  97. return app
  98. def fake_data():
  99. LOG.d("create fake data")
  100. # Remove db if exist
  101. if os.path.exists("db.sqlite"):
  102. LOG.d("remove existing db file")
  103. os.remove("db.sqlite")
  104. # Create all tables
  105. db.create_all()
  106. # Create a user
  107. user = User.create(
  108. email="john@wick.com",
  109. name="John Wick",
  110. password="password",
  111. activated=True,
  112. is_admin=True,
  113. otp_secret="base32secret3232",
  114. can_use_multiple_mailbox=True,
  115. )
  116. db.session.commit()
  117. LifetimeCoupon.create(code="coupon", nb_used=10)
  118. db.session.commit()
  119. # Create a subscription for user
  120. Subscription.create(
  121. user_id=user.id,
  122. cancel_url="https://checkout.paddle.com/subscription/cancel?user=1234",
  123. update_url="https://checkout.paddle.com/subscription/update?user=1234",
  124. subscription_id="123",
  125. event_time=arrow.now(),
  126. next_bill_date=arrow.now().shift(days=10).date(),
  127. plan=PlanEnum.monthly,
  128. )
  129. db.session.commit()
  130. api_key = ApiKey.create(user_id=user.id, name="Chrome")
  131. api_key.code = "codeCH"
  132. api_key = ApiKey.create(user_id=user.id, name="Firefox")
  133. api_key.code = "codeFF"
  134. GenEmail.create_new(user.id, "e1@")
  135. GenEmail.create_new(user.id, "e2@")
  136. GenEmail.create_new(user.id, "e3@")
  137. CustomDomain.create(user_id=user.id, domain="ab.cd", verified=True)
  138. CustomDomain.create(
  139. user_id=user.id, domain="very-long-domain.com.net.org", verified=True
  140. )
  141. db.session.commit()
  142. Directory.create(user_id=user.id, name="abcd")
  143. Directory.create(user_id=user.id, name="xyzt")
  144. db.session.commit()
  145. # Create a client
  146. client1 = Client.create_new(name="Demo", user_id=user.id)
  147. client1.oauth_client_id = "client-id"
  148. client1.oauth_client_secret = "client-secret"
  149. client1.published = True
  150. db.session.commit()
  151. RedirectUri.create(client_id=client1.id, uri="https://ab.com")
  152. client2 = Client.create_new(name="Demo 2", user_id=user.id)
  153. client2.oauth_client_id = "client-id2"
  154. client2.oauth_client_secret = "client-secret2"
  155. client2.published = True
  156. db.session.commit()
  157. Mailbox.create(user_id=user.id, email="ab@cd.ef", verified=True)
  158. Mailbox.create(user_id=user.id, email="xy@zt.com", verified=False)
  159. db.session.commit()
  160. DeletedAlias.create(user_id=user.id, email="d1@ab.cd")
  161. DeletedAlias.create(user_id=user.id, email="d2@ab.cd")
  162. db.session.commit()
  163. @login_manager.user_loader
  164. def load_user(user_id):
  165. user = User.query.get(user_id)
  166. return user
  167. def register_blueprints(app: Flask):
  168. app.register_blueprint(auth_bp)
  169. app.register_blueprint(monitor_bp)
  170. app.register_blueprint(dashboard_bp)
  171. app.register_blueprint(developer_bp)
  172. app.register_blueprint(oauth_bp, url_prefix="/oauth")
  173. app.register_blueprint(oauth_bp, url_prefix="/oauth2")
  174. app.register_blueprint(discover_bp)
  175. app.register_blueprint(api_bp)
  176. def set_index_page(app):
  177. @app.route("/", methods=["GET", "POST"])
  178. def index():
  179. if current_user.is_authenticated:
  180. return redirect(url_for("dashboard.index"))
  181. else:
  182. return redirect(url_for("auth.login"))
  183. @app.after_request
  184. def after_request(res):
  185. # not logging /static call
  186. if (
  187. not request.path.startswith("/static")
  188. and not request.path.startswith("/admin/static")
  189. and not request.path.startswith("/_debug_toolbar")
  190. ):
  191. LOG.debug(
  192. "%s %s %s %s %s",
  193. request.remote_addr,
  194. request.method,
  195. request.path,
  196. request.args,
  197. res.status_code,
  198. )
  199. res.headers["X-Frame-Options"] = "deny"
  200. return res
  201. def setup_openid_metadata(app):
  202. @app.route("/.well-known/openid-configuration")
  203. @cross_origin()
  204. def openid_config():
  205. res = {
  206. "issuer": URL,
  207. "authorization_endpoint": URL + "/oauth2/authorize",
  208. "token_endpoint": URL + "/oauth2/token",
  209. "userinfo_endpoint": URL + "/oauth2/userinfo",
  210. "jwks_uri": URL + "/jwks",
  211. "response_types_supported": [
  212. "code",
  213. "token",
  214. "id_token",
  215. "id_token token",
  216. "id_token code",
  217. ],
  218. "subject_types_supported": ["public"],
  219. "id_token_signing_alg_values_supported": ["RS256"],
  220. # todo: add introspection and revocation endpoints
  221. # "introspection_endpoint": URL + "/oauth2/token/introspection",
  222. # "revocation_endpoint": URL + "/oauth2/token/revocation",
  223. }
  224. return jsonify(res)
  225. @app.route("/jwks")
  226. @cross_origin()
  227. def jwks():
  228. res = {"keys": [get_jwk_key()]}
  229. return jsonify(res)
  230. def setup_error_page(app):
  231. @app.errorhandler(400)
  232. def page_not_found(e):
  233. return render_template("error/400.html"), 400
  234. @app.errorhandler(401)
  235. def page_not_found(e):
  236. return render_template("error/401.html", current_url=request.full_path), 401
  237. @app.errorhandler(403)
  238. def page_not_found(e):
  239. return render_template("error/403.html"), 403
  240. @app.errorhandler(404)
  241. def page_not_found(e):
  242. return render_template("error/404.html"), 404
  243. @app.errorhandler(Exception)
  244. def error_handler(e):
  245. LOG.exception(e)
  246. return render_template("error/500.html"), 500
  247. def setup_favicon_route(app):
  248. @app.route("/favicon.ico")
  249. def favicon():
  250. return redirect("/static/favicon.ico")
  251. def jinja2_filter(app):
  252. def format_datetime(value):
  253. dt = arrow.get(value)
  254. return dt.humanize()
  255. app.jinja_env.filters["dt"] = format_datetime
  256. @app.context_processor
  257. def inject_stage_and_region():
  258. return dict(
  259. YEAR=arrow.now().year,
  260. URL=URL,
  261. SENTRY_DSN=SENTRY_FRONT_END_DSN,
  262. VERSION=SHA1,
  263. )
  264. def setup_paddle_callback(app: Flask):
  265. @app.route("/paddle", methods=["GET", "POST"])
  266. def paddle():
  267. LOG.debug(
  268. "paddle callback %s %s %s %s %s",
  269. request.form.get("alert_name"),
  270. request.form.get("email"),
  271. request.form.get("customer_name"),
  272. request.form.get("subscription_id"),
  273. request.form.get("subscription_plan_id"),
  274. )
  275. # make sure the request comes from Paddle
  276. if not paddle_utils.verify_incoming_request(dict(request.form)):
  277. LOG.error(
  278. "request not coming from paddle. Request data:%s", dict(request.form)
  279. )
  280. return "KO", 400
  281. if (
  282. request.form.get("alert_name") == "subscription_created"
  283. ): # new user subscribes
  284. user_email = request.form.get("email")
  285. user = User.get_by(email=user_email)
  286. if (
  287. int(request.form.get("subscription_plan_id"))
  288. == PADDLE_MONTHLY_PRODUCT_ID
  289. ):
  290. plan = PlanEnum.monthly
  291. else:
  292. plan = PlanEnum.yearly
  293. sub = Subscription.get_by(user_id=user.id)
  294. if not sub:
  295. LOG.d("create a new sub")
  296. Subscription.create(
  297. user_id=user.id,
  298. cancel_url=request.form.get("cancel_url"),
  299. update_url=request.form.get("update_url"),
  300. subscription_id=request.form.get("subscription_id"),
  301. event_time=arrow.now(),
  302. next_bill_date=arrow.get(
  303. request.form.get("next_bill_date"), "YYYY-MM-DD"
  304. ).date(),
  305. plan=plan,
  306. )
  307. else:
  308. LOG.d("update existing sub %s", sub)
  309. sub.cancel_url = request.form.get("cancel_url")
  310. sub.update_url = request.form.get("update_url")
  311. sub.subscription_id = request.form.get("subscription_id")
  312. sub.event_time = arrow.now()
  313. sub.next_bill_date = arrow.get(
  314. request.form.get("next_bill_date"), "YYYY-MM-DD"
  315. ).date()
  316. sub.plan = plan
  317. LOG.debug("User %s upgrades!", user)
  318. db.session.commit()
  319. elif request.form.get("alert_name") == "subscription_updated":
  320. subscription_id = request.form.get("subscription_id")
  321. LOG.debug("Update subscription %s", subscription_id)
  322. sub: Subscription = Subscription.get_by(subscription_id=subscription_id)
  323. sub.event_time = arrow.now()
  324. sub.next_bill_date = arrow.get(
  325. request.form.get("next_bill_date"), "YYYY-MM-DD"
  326. ).date()
  327. db.session.commit()
  328. elif request.form.get("alert_name") == "subscription_cancelled":
  329. subscription_id = request.form.get("subscription_id")
  330. LOG.error("Cancel subscription %s", subscription_id)
  331. sub: Subscription = Subscription.get_by(subscription_id=subscription_id)
  332. if sub:
  333. sub.cancelled = True
  334. db.session.commit()
  335. return "OK"
  336. def init_extensions(app: Flask):
  337. login_manager.init_app(app)
  338. db.init_app(app)
  339. migrate.init_app(app)
  340. def init_admin(app):
  341. admin = Admin(name="SimpleLogin", template_mode="bootstrap3")
  342. admin.init_app(app, index_view=SLAdminIndexView())
  343. admin.add_view(SLModelView(User, db.session))
  344. admin.add_view(SLModelView(Client, db.session))
  345. admin.add_view(SLModelView(GenEmail, db.session))
  346. admin.add_view(SLModelView(ClientUser, db.session))
  347. def setup_do_not_track(app):
  348. @app.route("/dnt")
  349. def do_not_track():
  350. return """
  351. <script src="/static/local-storage-polyfill.js"></script>
  352. <script>
  353. // Disable GoatCounter if this script is called
  354. store.set('goatcounter-ignore', 't');
  355. alert("GoatCounter disabled");
  356. window.location.href = "/";
  357. </script>
  358. """
  359. if __name__ == "__main__":
  360. app = create_app()
  361. # enable flask toolbar
  362. # app.config["DEBUG_TB_PROFILER_ENABLED"] = True
  363. # app.config["DEBUG_TB_INTERCEPT_REDIRECTS"] = False
  364. #
  365. # toolbar = DebugToolbarExtension(app)
  366. # enable to print all queries generated by sqlalchemy
  367. # app.config["SQLALCHEMY_ECHO"] = True
  368. # warning: only used in local
  369. if RESET_DB:
  370. LOG.warning("reset db, add fake data")
  371. with app.app_context():
  372. fake_data()
  373. if URL.startswith("https"):
  374. LOG.d("enable https")
  375. context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
  376. context.load_cert_chain("local_data/cert.pem", "local_data/key.pem")
  377. app.run(debug=True, host="0.0.0.0", port=7777, ssl_context=context)
  378. else:
  379. app.run(debug=True, host="0.0.0.0", port=7777)