This file is indexed.

/usr/share/doc/python-social-auth-doc/html/_sources/configuration/flask.txt is in python-social-auth-doc 1:0.2.21+dfsg-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
Flask Framework
===============

Flask reusable applications are tricky (or I'm not capable enough). Here are
details on how to enable this application on Flask.


Dependencies
------------

The `Flask built-in app` depends on sqlalchemy_, there's initial support for
MongoEngine_ ORM too (check below for more details).


Enabling the application
------------------------

The applications define a `Flask Blueprint`_, which needs to be registered once
the Flask app is configured by::

    from social.apps.flask_app.routes import social_auth

    app.register_blueprint(social_auth)

For MongoEngine_ you need this setting::

    SOCIAL_AUTH_STORAGE = 'social.apps.flask_app.me.models.FlaskStorage'


Models Setup
------------

At the moment the models for python-social-auth_ are defined inside a function
because they need the reference to the current db instance and the User model
used on your project (check *User model reference* below). Once the Flask app
and the database are defined, call ``init_social`` to register the models::

    from social.apps.flask_app.default.models import init_social

    init_social(app, db)

For MongoEngine_::

    from social.apps.flask_app.me.models import init_social

    init_social(app, db)

So far I wasn't able to find another way to define the models on another way
rather than making it as a side-effect of calling this function since the
database is not available and ``current_app`` cannot be used on init time, just
run time.


User model reference
--------------------

The application keeps a reference to the User model used by your project,
define it by using this setting::

    SOCIAL_AUTH_USER_MODEL = 'foobar.models.User'

The value must be the import path to the User model.


Global user
-----------

The application expects the current logged in user accesible at ``g.user``,
define a handler like this to ensure that::

    @app.before_request
    def global_user():
        g.user = get_current_logged_in_user


Flask-Login
-----------

The application works quite well with Flask-Login_, ensure to have some similar
handlers to these::

    @login_manager.user_loader
    def load_user(userid):
        try:
            return User.query.get(int(userid))
        except (TypeError, ValueError):
            pass


    @app.before_request
    def global_user():
        g.user = login.current_user


    # Make current user available on templates
    @app.context_processor
    def inject_user():
        try:
            return {'user': g.user}
        except AttributeError:
            return {'user': None}


Remembering sessions
--------------------

The users session can be remembered when specified on login. The common
implementation for this feature is to pass a parameter from the login form
(``remember_me``, ``keep``, etc), to flag the action. Flask-Login_ will mark
the session as persistent if told so.

python-social-auth_ will check for a given name (``keep``) by default, but
since providers won't pass parameters back to the application, the value must
be persisted in the session before the authentication process happens.

So, the following setting is required for this to work::

    SOCIAL_AUTH_FIELDS_STORED_IN_SESSION = ['keep']

It's possible to override the default name with this setting::

    SOCIAL_AUTH_REMEMBER_SESSION_NAME = 'remember_me'

Don't use the value ``remember`` since that will clash with Flask-Login_ which
pops the value from the session.

Then just pass the parameter ``keep=1`` as a GET or POST parameter.


Exceptions handling
-------------------

The Django application has a middleware (that fits in the framework
architecture) to facilitate the different exceptions_ handling raised by
python-social-auth_. The same can be accomplished (even on a simpler way) in
Flask by defining an errorhandler_. For example the next code will redirect any
social-auth exception to a ``/socialerror`` URL::

    from social.exceptions import SocialAuthBaseException


    @app.errorhandler(500)
    def error_handler(error):
        if isinstance(error, SocialAuthBaseException):
            return redirect('/socialerror')


Be sure to set your debug and test flags to ``False`` when testing this on your
development environment, otherwise the exception will be raised and error
handlers won't be called.


.. _Flask Blueprint: http://flask.pocoo.org/docs/blueprints/
.. _Flask-Login: https://github.com/maxcountryman/flask-login
.. _python-social-auth: https://github.com/omab/python-social-auth
.. _Flask built-in app: https://github.com/omab/python-social-auth/tree/master/social/apps/flask_app
.. _sqlalchemy: http://www.sqlalchemy.org/
.. _exceptions: https://github.com/omab/python-social-auth/blob/master/social/exceptions.py
.. _errorhandler: http://flask.pocoo.org/docs/api/#flask.Flask.errorhandler
.. _MongoEngine: http://mongoengine.org