METADATA 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. Metadata-Version: 2.1
  2. Name: Flask-Login
  3. Version: 0.6.3
  4. Summary: User authentication and session management for Flask.
  5. Home-page: https://github.com/maxcountryman/flask-login
  6. Author: Matthew Frazier
  7. Author-email: leafstormrush@gmail.com
  8. Maintainer: Max Countryman
  9. License: MIT
  10. Project-URL: Documentation, https://flask-login.readthedocs.io/
  11. Project-URL: Changes, https://github.com/maxcountryman/flask-login/blob/main/CHANGES.md
  12. Project-URL: Source Code, https://github.com/maxcountryman/flask-login
  13. Project-URL: Issue Tracker, https://github.com/maxcountryman/flask-login/issues
  14. Classifier: Development Status :: 4 - Beta
  15. Classifier: Environment :: Web Environment
  16. Classifier: Framework :: Flask
  17. Classifier: Intended Audience :: Developers
  18. Classifier: License :: OSI Approved :: MIT License
  19. Classifier: Operating System :: OS Independent
  20. Classifier: Programming Language :: Python
  21. Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
  22. Classifier: Topic :: Software Development :: Libraries :: Python Modules
  23. Requires-Python: >=3.7
  24. Description-Content-Type: text/markdown
  25. License-File: LICENSE
  26. Requires-Dist: Flask >=1.0.4
  27. Requires-Dist: Werkzeug >=1.0.1
  28. # Flask-Login
  29. ![Tests](https://github.com/maxcountryman/flask-login/workflows/Tests/badge.svg)
  30. [![coverage](https://coveralls.io/repos/maxcountryman/flask-login/badge.svg?branch=main&service=github)](https://coveralls.io/github/maxcountryman/flask-login?branch=main)
  31. [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE)
  32. Flask-Login provides user session management for Flask. It handles the common
  33. tasks of logging in, logging out, and remembering your users' sessions over
  34. extended periods of time.
  35. Flask-Login is not bound to any particular database system or permissions
  36. model. The only requirement is that your user objects implement a few methods,
  37. and that you provide a callback to the extension capable of loading users from
  38. their ID.
  39. ## Installation
  40. Install the extension with pip:
  41. ```sh
  42. $ pip install flask-login
  43. ```
  44. ## Usage
  45. Once installed, the Flask-Login is easy to use. Let's walk through setting up
  46. a basic application. Also please note that this is a very basic guide: we will
  47. be taking shortcuts here that you should never take in a real application.
  48. To begin we'll set up a Flask app:
  49. ```python
  50. import flask
  51. app = flask.Flask(__name__)
  52. app.secret_key = 'super secret string' # Change this!
  53. ```
  54. Flask-Login works via a login manager. To kick things off, we'll set up the
  55. login manager by instantiating it and telling it about our Flask app:
  56. ```python
  57. import flask_login
  58. login_manager = flask_login.LoginManager()
  59. login_manager.init_app(app)
  60. ```
  61. To keep things simple we're going to use a dictionary to represent a database
  62. of users. In a real application, this would be an actual persistence layer.
  63. However it's important to point out this is a feature of Flask-Login: it
  64. doesn't care how your data is stored so long as you tell it how to retrieve it!
  65. ```python
  66. # Our mock database.
  67. users = {'foo@bar.tld': {'password': 'secret'}}
  68. ```
  69. We also need to tell Flask-Login how to load a user from a Flask request and
  70. from its session. To do this we need to define our user object, a
  71. `user_loader` callback, and a `request_loader` callback.
  72. ```python
  73. class User(flask_login.UserMixin):
  74. pass
  75. @login_manager.user_loader
  76. def user_loader(email):
  77. if email not in users:
  78. return
  79. user = User()
  80. user.id = email
  81. return user
  82. @login_manager.request_loader
  83. def request_loader(request):
  84. email = request.form.get('email')
  85. if email not in users:
  86. return
  87. user = User()
  88. user.id = email
  89. return user
  90. ```
  91. Now we're ready to define our views. We can start with a login view, which will
  92. populate the session with authentication bits. After that we can define a view
  93. that requires authentication.
  94. ```python
  95. @app.route('/login', methods=['GET', 'POST'])
  96. def login():
  97. if flask.request.method == 'GET':
  98. return '''
  99. <form action='login' method='POST'>
  100. <input type='text' name='email' id='email' placeholder='email'/>
  101. <input type='password' name='password' id='password' placeholder='password'/>
  102. <input type='submit' name='submit'/>
  103. </form>
  104. '''
  105. email = flask.request.form['email']
  106. if email in users and flask.request.form['password'] == users[email]['password']:
  107. user = User()
  108. user.id = email
  109. flask_login.login_user(user)
  110. return flask.redirect(flask.url_for('protected'))
  111. return 'Bad login'
  112. @app.route('/protected')
  113. @flask_login.login_required
  114. def protected():
  115. return 'Logged in as: ' + flask_login.current_user.id
  116. ```
  117. Finally we can define a view to clear the session and log users out:
  118. ```python
  119. @app.route('/logout')
  120. def logout():
  121. flask_login.logout_user()
  122. return 'Logged out'
  123. ```
  124. We now have a basic working application that makes use of session-based
  125. authentication. To round things off, we should provide a callback for login
  126. failures:
  127. ```python
  128. @login_manager.unauthorized_handler
  129. def unauthorized_handler():
  130. return 'Unauthorized', 401
  131. ```
  132. Documentation for Flask-Login is available on [ReadTheDocs](https://flask-login.readthedocs.io/en/latest/).
  133. For complete understanding of available configuration, please refer to the [source code](https://github.com/maxcountryman/flask-login).
  134. ## Contributing
  135. We welcome contributions! If you would like to hack on Flask-Login, please
  136. follow these steps:
  137. 1. Fork this repository
  138. 2. Make your changes
  139. 3. Install the dev requirements with `pip install -r requirements/dev.txt`
  140. 4. Submit a pull request after running `tox` (ensure it does not error!)
  141. Please give us adequate time to review your submission. Thanks!