Log out""" % ( + url_for('flask_security.logout')) -The last thing you'll want to do is add a logout link to your templates. This -can be achieved with:: - - Logout - -Now, for instance, say you want to protect an admin area to users that are -administrators. You can use the `roles_accepted` decorator to prevent access. -The corresponding view would look like such:: - - @app.route('/admin') - @roles_accepted('admin') - def admin(): - return render_template('admin/index.html') - -And lastly, maybe you only want to show something in a template if a user has a -specific role:: - - {% if current_user.has_role('admin') %} - Admin Panel - {$ endif %} + if __name__ == '__main__': + app.run() -.. _additional-user-fields: +.. _models: -Additional User Fields ----------------------- -If you'd like to add additional fields to the user model you can use a mixin -class that specifies your additional fields. The following is an example of -how you might do this:: +Data Models +----------- - db = SQLAlchemy(app) - - class UserAccountMixin(): - first_name = db.Column(db.String(120)) - last_name = db.Column(db.String(120)) - - Security(app, SQLAlchemyUserDatastore(db, UserAccountMixin)) +TODO: Describe models and required fields for various features .. _flask-script-commands: @@ -176,12 +165,14 @@ Flask-Script Commands --------------------- Flask-Security comes packed with a few Flask-Script commands. They are: -* :class:`flask.ext.security.script.CreateUserCommand` -* :class:`flask.ext.security.script.CreateRoleCommand` -* :class:`flask.ext.security.script.AddRoleCommand` -* :class:`flask.ext.security.script.RemoveRoleCommand` -* :class:`flask.ext.security.script.DeactivateUserCommand` -* :class:`flask.ext.security.script.ActivateUserCommand` +* :class:`flask_security.script.CreateUserCommand` +* :class:`flask_security.script.CreateRoleCommand` +* :class:`flask_security.script.AddRoleCommand` +* :class:`flask_security.script.RemoveRoleCommand` +* :class:`flask_security.script.DeactivateUserCommand` +* :class:`flask_security.script.ActivateUserCommand` +* :class:`flask_security.script.ActivateUserCommand` +* :class:`flask_security.script.GenerateBlueprintCommand` Register these on your script manager for pure convenience. @@ -192,25 +183,63 @@ Configuration Values ==================== * :attr:`SECURITY_URL_PREFIX`: Specifies the URL prefix for the Security - blueprint -* :attr:`SECURITY_AUTH_PROVIDER`: Specifies the class to use as the - authentication provider. Such as `flask.ext.security.AuthenticationProvider` -* :attr:`SECURITY_PASSWORD_HASH`: Specifies the encryption method to use. e.g.: - plaintext, bcrypt, etc -* :attr:`SECURITY_USER_DATASTORE`: Specifies the property name to use for the - user datastore on the application instance -* :attr:`SECURITY_LOGIN_FORM`: Specifies the form class to use when processing - an authentication request -* :attr:`SECURITY_AUTH_URL`: Specifies the URL to to handle authentication -* :attr:`SECURITY_LOGOUT_URL`: Specifies the URL to process a logout request -* :attr:`SECURITY_LOGIN_VIEW`: Specifies the URL to redirect to when - authentication is required -* :attr:`SECURITY_POST_LOGIN`: Specifies the URL to redirect to after a user is - authenticated -* :attr:`SECURITY_POST_LOGOUT`: Specifies the URL to redirect to after a user - logs out + blueprint. * :attr:`SECURITY_FLASH_MESSAGES`: Specifies wether or not to flash messages - during authentication request + during security mechanisms. +* :attr:`SECURITY_PASSWORD_HASH`: Specifies the encryption method to use. e.g.: + plaintext, bcrypt, etc. +* :attr:`SECURITY_AUTH_URL`: Specifies the URL to to handle authentication. +* :attr:`SECURITY_LOGOUT_URL`: Specifies the URL to process a logout request. +* :attr:`SECURITY_REGISTER_URL`: Specifies the URL for user registrations. +* :attr:`SECURITY_RESET_URL`: Specifies the URL for password resets. +* :attr:`SECURITY_CONFIRM_URL`: Specifies the URL for account confirmations. +* :attr:`SECURITY_LOGIN_VIEW`: Specifies the URL to redirect to when + authentication is required. +* :attr:`SECURITY_CONFIRM_ERROR_VIEW`: Specifies the URL to redirect to when + an confirmation error occurs. +* :attr:`SECURITY_POST_LOGIN_VIEW`: Specifies the URL to redirect to after a + user logins in. +* :attr:`SECURITY_POST_LOGOUT_VIEW`: Specifies the URL to redirect to after a + user logs out. +* :attr:`SECURITY_POST_FORGOT_VIEW`: Specifies the URL to redirect to after a + user requests password reset instructions. +* :attr:`SECURITY_RESET_PASSWORD_ERROR_VIEW`: Specifies the URL to redirect to + after an error occurs during the password reset process. +* :attr:`SECURITY_POST_REGISTER_VIEW`: Specifies the URL to redirect to after a + user successfully registers. +* :attr:`SECURITY_POST_CONFIRM_VIEW`: Specifies the URL to redirect to after a + user successfully confirms their account. +* :attr:`SECURITY_UNAUTHORIZED_VIEW`: Specifies the URL to redirect to when a + user attempts to access a view they don't have permission to view. +* :attr:`SECURITY_DEFAULT_ROLES`: The default roles any new users should have. +* :attr:`SECURITY_CONFIRMABLE`: Enables confirmation features. Defaults to + `False`. +* :attr:`SECURITY_REGISTERABLE`: Enables user registration features. Defaults to + `False`. +* :attr:`SECURITY_RECOVERABLE`: Enables password reset/recovery features. + Defaults to `False`. +* :attr:`SECURITY_TRACKABLE`: Enables login tracking features. Defaults to + `False`. +* :attr:`SECURITY_CONFIRM_EMAIL_WITHIN`: Specifies the amount of time a user + has to confirm their account/email. Default is `5 days`. +* :attr:`SECURITY_RESET_PASSWORD_WITHIN`: Specifies the amount of time a user + has to reset their password. Default is `5 days`. +* :attr:`SECURITY_LOGIN_WITHOUT_CONFIRMATION`: Specifies if users can login + without first confirming their accounts. Defaults to `False` +* :attr:`SECURITY_EMAIL_SENDER`: Specifies the email address to send emails on + behalf of. Defaults to `no-reply@localhost`. +* :attr:`SECURITY_TOKEN_AUTHENTICATION_KEY`: Specifies the query string argument + to use during token authentication. Defaults to `auth_token`. +* :attr:`SECURITY_TOKEN_AUTHENTICATION_HEADER`: Specifies the header name to use + during token authentication. Defaults to `X-Auth-Token`. +* :attr:`SECURITY_CONFIRM_SALT`: Specifies the salt value to use for account + confirmation tokens. Defaults to `confirm-salt`. +* :attr:`SECURITY_RESET_SALT`: Specifies the salt value to use for password + reset tokens. Defaults to `reset-salt`. +* :attr:`SECURITY_AUTH_SALT`: Specifies the salt value to use for token based + authentication tokens. Defaults to `auth-salt`. +* :attr:`SECURITY_DEFAULT_HTTP_AUTH_REALM`: Specifies the default basic HTTP + authentication realm. Defaults to `Login Required`. .. _api: @@ -218,32 +247,36 @@ Configuration Values API === -.. autoclass:: flask_security.Security +.. autoclass:: flask_security.core.Security :members: -.. data:: flask_security.current_user +.. data:: flask_security.core.current_user A proxy for the current user. Protecting Views ---------------- -.. autofunction:: flask_security.login_required +.. autofunction:: flask_security.decorators.login_required -.. autofunction:: flask_security.roles_required +.. autofunction:: flask_security.decorators.roles_required -.. autofunction:: flask_security.roles_accepted +.. autofunction:: flask_security.decorators.roles_accepted + +.. autofunction:: flask_security.decorators.http_auth_required + +.. autofunction:: flask_security.decorators.auth_token_required User Object Helpers ------------------- -.. autoclass:: flask_security.UserMixin +.. autoclass:: flask_security.core.UserMixin :members: -.. autoclass:: flask_security.RoleMixin +.. autoclass:: flask_security.core.RoleMixin :members: -.. autoclass:: flask_security.AnonymousUser +.. autoclass:: flask_security.core.AnonymousUser :members: @@ -252,65 +285,13 @@ Datastores .. autoclass:: flask_security.datastore.UserDatastore :members: -.. autoclass:: flask_security.datastore.sqlalchemy.SQLAlchemyUserDatastore +.. autoclass:: flask_security.datastore.SQLAlchemyUserDatastore :members: :inherited-members: -.. autoclass:: flask_security.datastore.mongoengine.MongoEngineUserDatastore +.. autoclass:: flask_security.datastore.MongoEngineUserDatastore :members: :inherited-members: - - -Models ------- -.. autoclass:: flask_security.User - - .. attribute:: id - - User ID - - .. attribute:: username - - Username - - .. attribute:: email - - Email address - - .. attribute:: password - - Password - - .. attribute:: active - - Active state - - .. attribute:: roles - - User roles - - .. attribute:: created_at - - Created date - - .. attribute:: modified_at - - Modified date - - -.. autoclass:: flask_security.Role - - .. attribute:: id - - Role ID - - .. attribute:: name - - Role name - - .. attribute:: description - - Role description Exceptions @@ -331,6 +312,10 @@ Exceptions .. autoexception:: flask_security.RoleCreationError +.. autoexception:: flask_security.ConfirmationError + +.. autoexception:: flask_security.ResetPasswordError + Signals ------- diff --git a/flask_security/decorators.py b/flask_security/decorators.py index cba8658..5406e4f 100644 --- a/flask_security/decorators.py +++ b/flask_security/decorators.py @@ -87,7 +87,10 @@ def _check_http_auth(): def http_auth_required(realm): - """Decorator that protects endpoints using Basic HTTP authentication.""" + """Decorator that protects endpoints using Basic HTTP authentication. + The username should be set to the user's email address. + + :param realm: optional realm name""" def decorator(fn): @wraps(fn) @@ -109,7 +112,12 @@ def http_auth_required(realm): def auth_token_required(fn): - """Decorator that protects endpoints using token authentication.""" + """Decorator that protects endpoints using token authentication. The token + should be added to the request by the client by using a query string + variable with a name equal to the configuration value of + `SECURITY_TOKEN_AUTHENTICATION_KEY` or in a request header named that of + the configuration value of `SECURITY_TOKEN_AUTHENTICATION_HEADER` + """ @wraps(fn) def decorated(*args, **kwargs):