app.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import json # noqa
  2. import os
  3. import markupsafe
  4. from flask import Flask, redirect, request, session, url_for
  5. from flask_oauth import OAuth
  6. BASE_URL = os.environ.get("BASE_URL", "http://dev.getsentry.net:8000")
  7. CLIENT_ID = os.environ.get("CLIENT_ID")
  8. CLIENT_SECRET = os.environ.get("CLIENT_SECRET")
  9. REDIRECT_URI = "/authorized"
  10. SECRET_KEY = "development key"
  11. DEBUG = True
  12. app = Flask(__name__)
  13. app.debug = DEBUG
  14. app.secret_key = SECRET_KEY
  15. oauth = OAuth()
  16. sentry = oauth.remote_app(
  17. "sentry",
  18. base_url=BASE_URL,
  19. authorize_url=f"{BASE_URL}/oauth/authorize/",
  20. request_token_url=None,
  21. request_token_params={
  22. "scope": "project:releases event:read org:read org:write",
  23. "response_type": "code",
  24. },
  25. access_token_url=f"{BASE_URL}/oauth/token/",
  26. access_token_method="POST",
  27. access_token_params={"grant_type": "authorization_code"},
  28. consumer_key=CLIENT_ID,
  29. consumer_secret=CLIENT_SECRET,
  30. )
  31. @app.route("/")
  32. def index():
  33. access_token = session.get("access_token")
  34. if access_token is None:
  35. return ("<h1>Who are you?</h1>" '<p><a href="{}">Login with Sentry</a></p>').format(
  36. url_for("login")
  37. )
  38. from urllib.error import HTTPError, URLError
  39. from urllib.request import Request, urlopen
  40. headers = {"Authorization": f"Bearer {access_token}"}
  41. req = Request(f"{BASE_URL}/api/0/organizations/", None, headers)
  42. try:
  43. res = urlopen(req)
  44. except HTTPError as e:
  45. if e.code == 401:
  46. # Unauthorized - bad token
  47. session.pop("access_token", None)
  48. return redirect(url_for("login"))
  49. return markupsafe.Markup("{}\n{}").format(e.code, e.reason)
  50. except URLError as e:
  51. return markupsafe.Markup("{}").format(e)
  52. return markupsafe.Markup("<h1>Hi, {}!</h1>" "<pre>{}</pre>").format(
  53. json.loads(session["user"])["email"], json.dumps(json.loads(res.read()), indent=2)
  54. )
  55. @app.route("/login")
  56. def login():
  57. callback = url_for("authorized", _external=True)
  58. return sentry.authorize(callback=callback)
  59. @app.route(REDIRECT_URI)
  60. @sentry.authorized_handler
  61. def authorized(resp):
  62. if "error" in request.args:
  63. return markupsafe.Markup(
  64. "<h1>Error</h1>" "<p>{}</p>" '<p><a href="{}">Try again</a></p>'
  65. ).format(request.args["error"], url_for("login"))
  66. access_token = resp["access_token"]
  67. session["access_token"] = access_token
  68. session["user"] = json.dumps(resp["user"])
  69. return redirect(url_for("index"))
  70. @sentry.tokengetter
  71. def get_access_token():
  72. return session.get("access_token")
  73. def main():
  74. app.run()
  75. if __name__ == "__main__":
  76. main()