oauth_tester.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. """
  2. This is an example on how to integrate SimpleLogin
  3. with Requests-OAuthlib, a popular library to work with OAuth in Python.
  4. The step-to-step guide can be found on https://docs.simplelogin.io
  5. This example is based on
  6. https://requests-oauthlib.readthedocs.io/en/latest/examples/real_world_example.html
  7. """
  8. import os
  9. from flask import Flask, request, redirect, session, url_for
  10. from flask.json import jsonify
  11. from requests_oauthlib import OAuth2Session
  12. app = Flask(__name__)
  13. # this demo uses flask.session that requires the `secret_key` to be set
  14. app.secret_key = "very secret"
  15. # "prettify" the returned json in /profile
  16. app.config["JSONIFY_PRETTYPRINT_REGULAR"] = True
  17. # This client credential is obtained upon registration of a new SimpleLogin App on
  18. # https://app.simplelogin.io/developer/new_client
  19. # Please make sure to export these credentials to env variables:
  20. # export CLIENT_ID={your_client_id}
  21. # export CLIENT_SECRET={your_client_secret}
  22. client_id = os.environ.get("CLIENT_ID") or "client-id"
  23. client_secret = os.environ.get("CLIENT_SECRET") or "client-secret"
  24. # SimpleLogin urls
  25. authorization_base_url = "http://localhost:7777/oauth2/authorize"
  26. token_url = "http://localhost:7777/oauth2/token"
  27. userinfo_url = "http://localhost:7777/oauth2/userinfo"
  28. @app.route("/")
  29. def demo():
  30. """Step 1: User Authorization.
  31. Redirect the user/resource owner to the OAuth provider (i.e. SimpleLogin)
  32. using an URL with a few key OAuth parameters.
  33. """
  34. simplelogin = OAuth2Session(
  35. client_id, redirect_uri="http://127.0.0.1:5000/callback"
  36. )
  37. authorization_url, state = simplelogin.authorization_url(authorization_base_url)
  38. # State is used to prevent CSRF, keep this for later.
  39. session["oauth_state"] = state
  40. return redirect(authorization_url)
  41. # Step 2: User authorization, this happens on the provider.
  42. @app.route("/callback", methods=["GET"])
  43. def callback():
  44. """Step 3: Retrieving an access token.
  45. The user has been redirected back from the provider to your registered
  46. callback URL. With this redirection comes an authorization code included
  47. in the redirect URL. We will use that to obtain an access token.
  48. """
  49. simplelogin = OAuth2Session(client_id, state=session["oauth_state"])
  50. token = simplelogin.fetch_token(
  51. token_url, client_secret=client_secret, authorization_response=request.url
  52. )
  53. # At this point you can fetch protected resources but lets save
  54. # the token and show how this is done from a persisted token
  55. # in /profile.
  56. session["oauth_token"] = token
  57. return redirect(url_for(".profile"))
  58. @app.route("/profile", methods=["GET"])
  59. def profile():
  60. """Fetching a protected resource using an OAuth 2 token."""
  61. simplelogin = OAuth2Session(client_id, token=session["oauth_token"])
  62. return jsonify(simplelogin.get(userinfo_url).json())
  63. # This allows us to use a plain HTTP callback
  64. os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
  65. if __name__ == "__main__":
  66. app.secret_key = os.urandom(24)
  67. app.run(debug=True)