/usr/share/doc/python3-flask-login-doc/html/index.html is in python3-flask-login-doc 0.4.0-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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Flask-Login — Flask-Login 0.4.0 documentation</title>
<link rel="stylesheet" href="_static/flasky.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '0.4.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="top" title="Flask-Login 0.4.0 documentation" href="#" />
</head>
<body role="document">
<div class=indexwrapper>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="flask-login">
<h1>Flask-Login<a class="headerlink" href="#flask-login" title="Permalink to this headline">¶</a></h1>
<p>Flask-Login provides user session management for Flask. It handles the common
tasks of logging in, logging out, and remembering your users’ sessions over
extended periods of time.</p>
<p>It will:</p>
<ul class="simple">
<li>Store the active user’s ID in the session, and let you log them in and out
easily.</li>
<li>Let you restrict views to logged-in (or logged-out) users.</li>
<li>Handle the normally-tricky “remember me” functionality.</li>
<li>Help protect your users’ sessions from being stolen by cookie thieves.</li>
<li>Possibly integrate with Flask-Principal or other authorization extensions
later on.</li>
</ul>
<p>However, it does not:</p>
<ul class="simple">
<li>Impose a particular database or other storage method on you. You are
entirely in charge of how the user is loaded.</li>
<li>Restrict you to using usernames and passwords, OpenIDs, or any other method
of authenticating.</li>
<li>Handle permissions beyond “logged in or not.”</li>
<li>Handle user registration or account recovery.</li>
</ul>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#configuring-your-application" id="id1">Configuring your Application</a></li>
<li><a class="reference internal" href="#how-it-works" id="id2">How it Works</a></li>
<li><a class="reference internal" href="#your-user-class" id="id3">Your User Class</a></li>
<li><a class="reference internal" href="#login-example" id="id4">Login Example</a></li>
<li><a class="reference internal" href="#customizing-the-login-process" id="id5">Customizing the Login Process</a></li>
<li><a class="reference internal" href="#login-using-authorization-header" id="id6">Login using Authorization header</a></li>
<li><a class="reference internal" href="#custom-login-using-request-loader" id="id7">Custom Login using Request Loader</a></li>
<li><a class="reference internal" href="#anonymous-users" id="id8">Anonymous Users</a></li>
<li><a class="reference internal" href="#remember-me" id="id9">Remember Me</a><ul>
<li><a class="reference internal" href="#alternative-tokens" id="id10">Alternative Tokens</a></li>
<li><a class="reference internal" href="#fresh-logins" id="id11">Fresh Logins</a></li>
<li><a class="reference internal" href="#cookie-settings" id="id12">Cookie Settings</a></li>
</ul>
</li>
<li><a class="reference internal" href="#session-protection" id="id13">Session Protection</a></li>
<li><a class="reference internal" href="#localization" id="id14">Localization</a></li>
<li><a class="reference internal" href="#api-documentation" id="id15">API Documentation</a><ul>
<li><a class="reference internal" href="#module-flask_login" id="id16">Configuring Login</a></li>
<li><a class="reference internal" href="#login-mechanisms" id="id17">Login Mechanisms</a></li>
<li><a class="reference internal" href="#protecting-views" id="id18">Protecting Views</a></li>
<li><a class="reference internal" href="#user-object-helpers" id="id19">User Object Helpers</a></li>
<li><a class="reference internal" href="#utilities" id="id20">Utilities</a></li>
<li><a class="reference internal" href="#signals" id="id21">Signals</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="configuring-your-application">
<h2>Configuring your Application<a class="headerlink" href="#configuring-your-application" title="Permalink to this headline">¶</a></h2>
<p>The most important part of an application that uses Flask-Login is the
<code class="xref py py-obj docutils literal"><span class="pre">LoginManager</span></code> class. You should create one for your application somewhere in
your code, like this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">login_manager</span> <span class="o">=</span> <span class="n">LoginManager</span><span class="p">()</span>
</pre></div>
</div>
<p>The login manager contains the code that lets your application and Flask-Login
work together, such as how to load a user from an ID, where to send users when
they need to log in, and the like.</p>
<p>Once the actual application object has been created, you can configure it for
login with:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">login_manager</span><span class="o">.</span><span class="n">init_app</span><span class="p">(</span><span class="n">app</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="how-it-works">
<h2>How it Works<a class="headerlink" href="#how-it-works" title="Permalink to this headline">¶</a></h2>
<p>You will need to provide a <code class="xref py py-obj docutils literal"><span class="pre">user_loader</span></code> callback. This callback
is used to reload the user object from the user ID stored in the session. It
should take the <code class="xref py py-obj docutils literal"><span class="pre">unicode</span></code> ID of a user, and return the corresponding user
object. For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="nd">@login_manager</span><span class="o">.</span><span class="n">user_loader</span>
<span class="k">def</span> <span class="nf">load_user</span><span class="p">(</span><span class="n">user_id</span><span class="p">):</span>
<span class="k">return</span> <span class="n">User</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">user_id</span><span class="p">)</span>
</pre></div>
</div>
<p>It should return <code class="xref py py-obj docutils literal"><span class="pre">None</span></code> (<strong>not raise an exception</strong>) if the ID is not valid.
(In that case, the ID will manually be removed from the session and processing
will continue.)</p>
</div>
<div class="section" id="your-user-class">
<h2>Your User Class<a class="headerlink" href="#your-user-class" title="Permalink to this headline">¶</a></h2>
<p>The class that you use to represent users needs to implement these properties
and methods:</p>
<dl class="docutils">
<dt><code class="xref py py-obj docutils literal"><span class="pre">is_authenticated</span></code></dt>
<dd>This property should return <code class="xref py py-obj docutils literal"><span class="pre">True</span></code> if the user is authenticated, i.e. they
have provided valid credentials. (Only authenticated users will fulfill
the criteria of <code class="xref py py-obj docutils literal"><span class="pre">login_required</span></code>.)</dd>
<dt><code class="xref py py-obj docutils literal"><span class="pre">is_active</span></code></dt>
<dd>This property should return <code class="xref py py-obj docutils literal"><span class="pre">True</span></code> if this is an active user - in addition
to being authenticated, they also have activated their account, not been
suspended, or any condition your application has for rejecting an account.
Inactive accounts may not log in (without being forced of course).</dd>
<dt><code class="xref py py-obj docutils literal"><span class="pre">is_anonymous</span></code></dt>
<dd>This property should return <code class="xref py py-obj docutils literal"><span class="pre">True</span></code> if this is an anonymous user. (Actual
users should return <code class="xref py py-obj docutils literal"><span class="pre">False</span></code> instead.)</dd>
<dt><code class="xref py py-obj docutils literal"><span class="pre">get_id()</span></code></dt>
<dd>This method must return a <code class="xref py py-obj docutils literal"><span class="pre">unicode</span></code> that uniquely identifies this user,
and can be used to load the user from the <code class="xref py py-obj docutils literal"><span class="pre">user_loader</span></code>
callback. Note that this <strong>must</strong> be a <code class="xref py py-obj docutils literal"><span class="pre">unicode</span></code> - if the ID is natively
an <code class="xref py py-obj docutils literal"><span class="pre">int</span></code> or some other type, you will need to convert it to <code class="xref py py-obj docutils literal"><span class="pre">unicode</span></code>.</dd>
</dl>
<p>To make implementing a user class easier, you can inherit from <code class="xref py py-obj docutils literal"><span class="pre">UserMixin</span></code>,
which provides default implementations for all of these properties and methods.
(It’s not required, though.)</p>
</div>
<div class="section" id="login-example">
<h2>Login Example<a class="headerlink" href="#login-example" title="Permalink to this headline">¶</a></h2>
<p>Once a user has authenticated, you log them in with the <code class="xref py py-obj docutils literal"><span class="pre">login_user</span></code>
function.</p>
<blockquote>
<div>For example:</div></blockquote>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="nd">@app.route</span><span class="p">(</span><span class="s1">'/login'</span><span class="p">,</span> <span class="n">methods</span><span class="o">=</span><span class="p">[</span><span class="s1">'GET'</span><span class="p">,</span> <span class="s1">'POST'</span><span class="p">])</span>
<span class="k">def</span> <span class="nf">login</span><span class="p">():</span>
<span class="c1"># Here we use a class of some kind to represent and validate our</span>
<span class="c1"># client-side form data. For example, WTForms is a library that will</span>
<span class="c1"># handle this for us, and we use a custom LoginForm to validate.</span>
<span class="n">form</span> <span class="o">=</span> <span class="n">LoginForm</span><span class="p">()</span>
<span class="k">if</span> <span class="n">form</span><span class="o">.</span><span class="n">validate_on_submit</span><span class="p">():</span>
<span class="c1"># Login and validate the user.</span>
<span class="c1"># user should be an instance of your `User` class</span>
<span class="n">login_user</span><span class="p">(</span><span class="n">user</span><span class="p">)</span>
<span class="n">flask</span><span class="o">.</span><span class="n">flash</span><span class="p">(</span><span class="s1">'Logged in successfully.'</span><span class="p">)</span>
<span class="nb">next</span> <span class="o">=</span> <span class="n">flask</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'next'</span><span class="p">)</span>
<span class="c1"># next_is_valid should check if the user has valid</span>
<span class="c1"># permission to access the `next` url</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">next_is_valid</span><span class="p">(</span><span class="nb">next</span><span class="p">):</span>
<span class="k">return</span> <span class="n">flask</span><span class="o">.</span><span class="n">abort</span><span class="p">(</span><span class="mi">400</span><span class="p">)</span>
<span class="k">return</span> <span class="n">flask</span><span class="o">.</span><span class="n">redirect</span><span class="p">(</span><span class="nb">next</span> <span class="ow">or</span> <span class="n">flask</span><span class="o">.</span><span class="n">url_for</span><span class="p">(</span><span class="s1">'index'</span><span class="p">))</span>
<span class="k">return</span> <span class="n">flask</span><span class="o">.</span><span class="n">render_template</span><span class="p">(</span><span class="s1">'login.html'</span><span class="p">,</span> <span class="n">form</span><span class="o">=</span><span class="n">form</span><span class="p">)</span>
</pre></div>
</div>
<p><em>Warning:</em> You MUST validate the value of the <code class="xref py py-obj docutils literal"><span class="pre">next</span></code> parameter. If you do not,
your application will be vulnerable to open redirects.</p>
<p>It’s that simple. You can then access the logged-in user with the
<a class="reference internal" href="#flask_login.current_user" title="flask_login.current_user"><code class="xref py py-obj docutils literal"><span class="pre">current_user</span></code></a> proxy, which is available in every template:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>{% if current_user.is_authenticated %}
Hi {{ current_user.name }}!
{% endif %}
</pre></div>
</div>
<p>Views that require your users to be logged in can be
decorated with the <code class="xref py py-obj docutils literal"><span class="pre">login_required</span></code> decorator:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="nd">@app</span><span class="o">.</span><span class="n">route</span><span class="p">(</span><span class="s2">"/settings"</span><span class="p">)</span>
<span class="nd">@login_required</span>
<span class="k">def</span> <span class="nf">settings</span><span class="p">():</span>
<span class="k">pass</span>
</pre></div>
</div>
<p>When the user is ready to log out:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="nd">@app</span><span class="o">.</span><span class="n">route</span><span class="p">(</span><span class="s2">"/logout"</span><span class="p">)</span>
<span class="nd">@login_required</span>
<span class="k">def</span> <span class="nf">logout</span><span class="p">():</span>
<span class="n">logout_user</span><span class="p">()</span>
<span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="n">somewhere</span><span class="p">)</span>
</pre></div>
</div>
<p>They will be logged out, and any cookies for their session will be cleaned up.</p>
</div>
<div class="section" id="customizing-the-login-process">
<h2>Customizing the Login Process<a class="headerlink" href="#customizing-the-login-process" title="Permalink to this headline">¶</a></h2>
<p>By default, when a user attempts to access a <code class="xref py py-obj docutils literal"><span class="pre">login_required</span></code> view without
being logged in, Flask-Login will flash a message and redirect them to the
log in view. (If the login view is not set, it will abort with a 401 error.)</p>
<p>The name of the log in view can be set as <code class="xref py py-obj docutils literal"><span class="pre">LoginManager.login_view</span></code>.
For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">login_manager</span><span class="o">.</span><span class="n">login_view</span> <span class="o">=</span> <span class="s2">"users.login"</span>
</pre></div>
</div>
<p>The default message flashed is <code class="docutils literal"><span class="pre">Please</span> <span class="pre">log</span> <span class="pre">in</span> <span class="pre">to</span> <span class="pre">access</span> <span class="pre">this</span> <span class="pre">page.</span></code> To
customize the message, set <code class="xref py py-obj docutils literal"><span class="pre">LoginManager.login_message</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">login_manager</span><span class="o">.</span><span class="n">login_message</span> <span class="o">=</span> <span class="s2">u"Bonvolu ensaluti por uzi tiun paĝon."</span>
</pre></div>
</div>
<p>To customize the message category, set <code class="xref py py-obj docutils literal"><span class="pre">LoginManager.login_message_category</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">login_manager</span><span class="o">.</span><span class="n">login_message_category</span> <span class="o">=</span> <span class="s2">"info"</span>
</pre></div>
</div>
<p>When the log in view is redirected to, it will have a <code class="docutils literal"><span class="pre">next</span></code> variable in the
query string, which is the page that the user was trying to access.</p>
<p>If you would like to customize the process further, decorate a function with
<code class="xref py py-obj docutils literal"><span class="pre">LoginManager.unauthorized_handler</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="nd">@login_manager</span><span class="o">.</span><span class="n">unauthorized_handler</span>
<span class="k">def</span> <span class="nf">unauthorized</span><span class="p">():</span>
<span class="c1"># do stuff</span>
<span class="k">return</span> <span class="n">a_response</span>
</pre></div>
</div>
</div>
<div class="section" id="login-using-authorization-header">
<h2>Login using Authorization header<a class="headerlink" href="#login-using-authorization-header" title="Permalink to this headline">¶</a></h2>
<div class="admonition caution">
<p class="first admonition-title">Caution</p>
<p class="last">This method will be deprecated; use the <code class="xref py py-obj docutils literal"><span class="pre">request_loader</span></code>
below instead.</p>
</div>
<p>Sometimes you want to support Basic Auth login using the <code class="xref py py-obj docutils literal"><span class="pre">Authorization</span></code>
header, such as for api requests. To support login via header you will need
to provide a <code class="xref py py-obj docutils literal"><span class="pre">header_loader</span></code> callback. This callback should behave
the same as your <code class="xref py py-obj docutils literal"><span class="pre">user_loader</span></code> callback, except that it accepts
a header value instead of a user id. For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="nd">@login_manager</span><span class="o">.</span><span class="n">header_loader</span>
<span class="k">def</span> <span class="nf">load_user_from_header</span><span class="p">(</span><span class="n">header_val</span><span class="p">):</span>
<span class="n">header_val</span> <span class="o">=</span> <span class="n">header_val</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">'Basic '</span><span class="p">,</span> <span class="s1">''</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">header_val</span> <span class="o">=</span> <span class="n">base64</span><span class="o">.</span><span class="n">b64decode</span><span class="p">(</span><span class="n">header_val</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
<span class="k">pass</span>
<span class="k">return</span> <span class="n">User</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">filter_by</span><span class="p">(</span><span class="n">api_key</span><span class="o">=</span><span class="n">header_val</span><span class="p">)</span><span class="o">.</span><span class="n">first</span><span class="p">()</span>
</pre></div>
</div>
<p>By default the <code class="xref py py-obj docutils literal"><span class="pre">Authorization</span></code> header’s value is passed to your
<code class="xref py py-obj docutils literal"><span class="pre">header_loader</span></code> callback. You can change the header used with
the <code class="xref py py-obj docutils literal"><span class="pre">AUTH_HEADER_NAME</span></code> configuration.</p>
</div>
<div class="section" id="custom-login-using-request-loader">
<h2>Custom Login using Request Loader<a class="headerlink" href="#custom-login-using-request-loader" title="Permalink to this headline">¶</a></h2>
<p>Sometimes you want to login users without using cookies, such as using header
values or an api key passed as a query argument. In these cases, you should use
the <code class="xref py py-obj docutils literal"><span class="pre">request_loader</span></code> callback. This callback should behave the
same as your <code class="xref py py-obj docutils literal"><span class="pre">user_loader</span></code> callback, except that it accepts the
Flask request instead of a user_id.</p>
<p>For example, to support login from both a url argument and from Basic Auth
using the <code class="xref py py-obj docutils literal"><span class="pre">Authorization</span></code> header:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="nd">@login_manager</span><span class="o">.</span><span class="n">request_loader</span>
<span class="k">def</span> <span class="nf">load_user_from_request</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
<span class="c1"># first, try to login using the api_key url arg</span>
<span class="n">api_key</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'api_key'</span><span class="p">)</span>
<span class="k">if</span> <span class="n">api_key</span><span class="p">:</span>
<span class="n">user</span> <span class="o">=</span> <span class="n">User</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">filter_by</span><span class="p">(</span><span class="n">api_key</span><span class="o">=</span><span class="n">api_key</span><span class="p">)</span><span class="o">.</span><span class="n">first</span><span class="p">()</span>
<span class="k">if</span> <span class="n">user</span><span class="p">:</span>
<span class="k">return</span> <span class="n">user</span>
<span class="c1"># next, try to login using Basic Auth</span>
<span class="n">api_key</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">headers</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'Authorization'</span><span class="p">)</span>
<span class="k">if</span> <span class="n">api_key</span><span class="p">:</span>
<span class="n">api_key</span> <span class="o">=</span> <span class="n">api_key</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">'Basic '</span><span class="p">,</span> <span class="s1">''</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">api_key</span> <span class="o">=</span> <span class="n">base64</span><span class="o">.</span><span class="n">b64decode</span><span class="p">(</span><span class="n">api_key</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
<span class="k">pass</span>
<span class="n">user</span> <span class="o">=</span> <span class="n">User</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">filter_by</span><span class="p">(</span><span class="n">api_key</span><span class="o">=</span><span class="n">api_key</span><span class="p">)</span><span class="o">.</span><span class="n">first</span><span class="p">()</span>
<span class="k">if</span> <span class="n">user</span><span class="p">:</span>
<span class="k">return</span> <span class="n">user</span>
<span class="c1"># finally, return None if both methods did not login the user</span>
<span class="k">return</span> <span class="kc">None</span>
</pre></div>
</div>
</div>
<div class="section" id="anonymous-users">
<h2>Anonymous Users<a class="headerlink" href="#anonymous-users" title="Permalink to this headline">¶</a></h2>
<p>By default, when a user is not actually logged in, <a class="reference internal" href="#flask_login.current_user" title="flask_login.current_user"><code class="xref py py-obj docutils literal"><span class="pre">current_user</span></code></a> is set to
an <code class="xref py py-obj docutils literal"><span class="pre">AnonymousUserMixin</span></code> object. It has the following properties and methods:</p>
<ul class="simple">
<li><code class="xref py py-obj docutils literal"><span class="pre">is_active</span></code> and <code class="xref py py-obj docutils literal"><span class="pre">is_authenticated</span></code> are <code class="xref py py-obj docutils literal"><span class="pre">False</span></code></li>
<li><code class="xref py py-obj docutils literal"><span class="pre">is_anonymous</span></code> is <code class="xref py py-obj docutils literal"><span class="pre">True</span></code></li>
<li><code class="xref py py-obj docutils literal"><span class="pre">get_id()</span></code> returns <code class="xref py py-obj docutils literal"><span class="pre">None</span></code></li>
</ul>
<p>If you have custom requirements for anonymous users (for example, they need
to have a permissions field), you can provide a callable (either a class or
factory function) that creates anonymous users to the <code class="xref py py-obj docutils literal"><span class="pre">LoginManager</span></code> with:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">login_manager</span><span class="o">.</span><span class="n">anonymous_user</span> <span class="o">=</span> <span class="n">MyAnonymousUser</span>
</pre></div>
</div>
</div>
<div class="section" id="remember-me">
<h2>Remember Me<a class="headerlink" href="#remember-me" title="Permalink to this headline">¶</a></h2>
<p>“Remember Me” functionality can be tricky to implement. However, Flask-Login
makes it nearly transparent - just pass <code class="docutils literal"><span class="pre">remember=True</span></code> to the <code class="xref py py-obj docutils literal"><span class="pre">login_user</span></code>
call. A cookie will be saved on the user’s computer, and then Flask-Login
will automatically restore the user ID from that cookie if it is not in the
session. The cookie is tamper-proof, so if the user tampers with it (i.e.
inserts someone else’s user ID in place of their own), the cookie will merely
be rejected, as if it was not there.</p>
<p>That level of functionality is handled automatically. However, you can (and
should, if your application handles any kind of sensitive data) provide
additional infrastructure to increase the security of your remember cookies.</p>
<div class="section" id="alternative-tokens">
<h3>Alternative Tokens<a class="headerlink" href="#alternative-tokens" title="Permalink to this headline">¶</a></h3>
<p>Using the user ID as the value of the remember token means you must change the
user’s ID to invalidate their login sessions. One way to improve this is to use
an alternative session token instead of the user’s ID. For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="nd">@login_manager</span><span class="o">.</span><span class="n">user_loader</span>
<span class="k">def</span> <span class="nf">load_user</span><span class="p">(</span><span class="n">session_token</span><span class="p">):</span>
<span class="k">return</span> <span class="n">User</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">filter_by</span><span class="p">(</span><span class="n">session_token</span><span class="o">=</span><span class="n">session_token</span><span class="p">)</span><span class="o">.</span><span class="n">first</span><span class="p">()</span>
</pre></div>
</div>
<p>Then the <code class="xref py py-obj docutils literal"><span class="pre">get_id</span></code> method of your User class would return the session
token instead of the user’s ID:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">get_id</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="n">unicode</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">session_token</span><span class="p">)</span>
</pre></div>
</div>
<p>This way you are free to change the user’s session token to a new randomly
generated value when the user changes their password, which would ensure their
old authentication sessions will cease to be valid. Note that the session
token must still uniquely identify the user... think of it as a second user ID.</p>
</div>
<div class="section" id="fresh-logins">
<h3>Fresh Logins<a class="headerlink" href="#fresh-logins" title="Permalink to this headline">¶</a></h3>
<p>When a user logs in, their session is marked as “fresh,” which indicates that
they actually authenticated on that session. When their session is destroyed
and they are logged back in with a “remember me” cookie, it is marked as
“non-fresh.” <code class="xref py py-obj docutils literal"><span class="pre">login_required</span></code> does not differentiate between freshness, which
is fine for most pages. However, sensitive actions like changing one’s
personal information should require a fresh login. (Actions like changing
one’s password should always require a password re-entry regardless.)</p>
<p><code class="xref py py-obj docutils literal"><span class="pre">fresh_login_required</span></code>, in addition to verifying that the user is logged
in, will also ensure that their login is fresh. If not, it will send them to
a page where they can re-enter their credentials. You can customize its
behavior in the same ways as you can customize <code class="xref py py-obj docutils literal"><span class="pre">login_required</span></code>, by setting
<code class="xref py py-obj docutils literal"><span class="pre">LoginManager.refresh_view</span></code>, <code class="xref py py-obj docutils literal"><span class="pre">needs_refresh_message</span></code>, and
<code class="xref py py-obj docutils literal"><span class="pre">needs_refresh_message_category</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">login_manager</span><span class="o">.</span><span class="n">refresh_view</span> <span class="o">=</span> <span class="s2">"accounts.reauthenticate"</span>
<span class="n">login_manager</span><span class="o">.</span><span class="n">needs_refresh_message</span> <span class="o">=</span> <span class="p">(</span>
<span class="s2">u"To protect your account, please reauthenticate to access this page."</span>
<span class="p">)</span>
<span class="n">login_manager</span><span class="o">.</span><span class="n">needs_refresh_message_category</span> <span class="o">=</span> <span class="s2">"info"</span>
</pre></div>
</div>
<p>Or by providing your own callback to handle refreshing:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="nd">@login_manager</span><span class="o">.</span><span class="n">needs_refresh_handler</span>
<span class="k">def</span> <span class="nf">refresh</span><span class="p">():</span>
<span class="c1"># do stuff</span>
<span class="k">return</span> <span class="n">a_response</span>
</pre></div>
</div>
<p>To mark a session as fresh again, call the <code class="xref py py-obj docutils literal"><span class="pre">confirm_login</span></code> function.</p>
</div>
<div class="section" id="cookie-settings">
<h3>Cookie Settings<a class="headerlink" href="#cookie-settings" title="Permalink to this headline">¶</a></h3>
<p>The details of the cookie can be customized in the application settings.</p>
<table border="1" class="docutils">
<colgroup>
<col width="35%" />
<col width="65%" />
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td><code class="xref py py-obj docutils literal"><span class="pre">REMEMBER_COOKIE_NAME</span></code></td>
<td>The name of the cookie to store the “remember me”
information in. <strong>Default:</strong> <code class="docutils literal"><span class="pre">remember_token</span></code></td>
</tr>
<tr class="row-even"><td><code class="xref py py-obj docutils literal"><span class="pre">REMEMBER_COOKIE_DURATION</span></code></td>
<td>The amount of time before the cookie expires, as
a <code class="xref py py-obj docutils literal"><span class="pre">datetime.timedelta</span></code> object.
<strong>Default:</strong> 365 days (1 non-leap Gregorian year)</td>
</tr>
<tr class="row-odd"><td><code class="xref py py-obj docutils literal"><span class="pre">REMEMBER_COOKIE_DOMAIN</span></code></td>
<td>If the “Remember Me” cookie should cross domains,
set the domain value here (i.e. <code class="docutils literal"><span class="pre">.example.com</span></code>
would allow the cookie to be used on all
subdomains of <code class="docutils literal"><span class="pre">example.com</span></code>).
<strong>Default:</strong> <code class="xref py py-obj docutils literal"><span class="pre">None</span></code></td>
</tr>
<tr class="row-even"><td><code class="xref py py-obj docutils literal"><span class="pre">REMEMBER_COOKIE_PATH</span></code></td>
<td>Limits the “Remember Me” cookie to a certain path.
<strong>Default:</strong> <code class="docutils literal"><span class="pre">/</span></code></td>
</tr>
<tr class="row-odd"><td><code class="xref py py-obj docutils literal"><span class="pre">REMEMBER_COOKIE_SECURE</span></code></td>
<td>Restricts the “Remember Me” cookie’s scope to
secure channels (typically HTTPS).
<strong>Default:</strong> <code class="xref py py-obj docutils literal"><span class="pre">None</span></code></td>
</tr>
<tr class="row-even"><td><code class="xref py py-obj docutils literal"><span class="pre">REMEMBER_COOKIE_HTTPONLY</span></code></td>
<td>Prevents the “Remember Me” cookie from being
accessed by client-side scripts.
<strong>Default:</strong> <code class="xref py py-obj docutils literal"><span class="pre">False</span></code></td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section" id="session-protection">
<h2>Session Protection<a class="headerlink" href="#session-protection" title="Permalink to this headline">¶</a></h2>
<p>While the features above help secure your “Remember Me” token from cookie
thieves, the session cookie is still vulnerable. Flask-Login includes session
protection to help prevent your users’ sessions from being stolen.</p>
<p>You can configure session protection on the <code class="xref py py-obj docutils literal"><span class="pre">LoginManager</span></code>, and in the app’s
configuration. If it is enabled, it can operate in either <code class="xref py py-obj docutils literal"><span class="pre">basic</span></code> or <code class="xref py py-obj docutils literal"><span class="pre">strong</span></code>
mode. To set it on the <code class="xref py py-obj docutils literal"><span class="pre">LoginManager</span></code>, set the
<code class="xref py py-obj docutils literal"><span class="pre">session_protection</span></code> attribute to <code class="docutils literal"><span class="pre">"basic"</span></code> or <code class="docutils literal"><span class="pre">"strong"</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">login_manager</span><span class="o">.</span><span class="n">session_protection</span> <span class="o">=</span> <span class="s2">"strong"</span>
</pre></div>
</div>
<p>Or, to disable it:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">login_manager</span><span class="o">.</span><span class="n">session_protection</span> <span class="o">=</span> <span class="kc">None</span>
</pre></div>
</div>
<p>By default, it is activated in <code class="docutils literal"><span class="pre">"basic"</span></code> mode. It can be disabled in the
app’s configuration by setting the <code class="xref py py-obj docutils literal"><span class="pre">SESSION_PROTECTION</span></code> setting to <code class="xref py py-obj docutils literal"><span class="pre">None</span></code>,
<code class="docutils literal"><span class="pre">"basic"</span></code>, or <code class="docutils literal"><span class="pre">"strong"</span></code>.</p>
<p>When session protection is active, each request, it generates an identifier
for the user’s computer (basically, a secure hash of the IP address and user
agent). If the session does not have an associated identifier, the one
generated will be stored. If it has an identifier, and it matches the one
generated, then the request is OK.</p>
<p>If the identifiers do not match in <code class="xref py py-obj docutils literal"><span class="pre">basic</span></code> mode, or when the session is
permanent, then the session will simply be marked as non-fresh, and anything
requiring a fresh login will force the user to re-authenticate. (Of course,
you must be already using fresh logins where appropriate for this to have an
effect.)</p>
<p>If the identifiers do not match in <code class="xref py py-obj docutils literal"><span class="pre">strong</span></code> mode for a non-permanent session,
then the entire session (as well as the remember token if it exists) is
deleted.</p>
</div>
<div class="section" id="localization">
<h2>Localization<a class="headerlink" href="#localization" title="Permalink to this headline">¶</a></h2>
<p>By default, the <code class="xref py py-obj docutils literal"><span class="pre">LoginManager</span></code> uses <code class="docutils literal"><span class="pre">flash</span></code> to display messages when a user
is required to log in. These messages are in English. If you require
localization, set the <code class="xref py py-obj docutils literal"><span class="pre">localize_callback</span></code> attribute of <code class="xref py py-obj docutils literal"><span class="pre">LoginManager</span></code> to a
function to be called with these messages before they’re sent to <code class="docutils literal"><span class="pre">flash</span></code>,
e.g. <code class="docutils literal"><span class="pre">gettext</span></code>. This function will be called with the message and its return
value will be sent to <code class="docutils literal"><span class="pre">flash</span></code> instead.</p>
</div>
<div class="section" id="api-documentation">
<h2>API Documentation<a class="headerlink" href="#api-documentation" title="Permalink to this headline">¶</a></h2>
<p>This documentation is automatically generated from Flask-Login’s source code.</p>
<div class="section" id="module-flask_login">
<span id="configuring-login"></span><h3>Configuring Login<a class="headerlink" href="#module-flask_login" title="Permalink to this headline">¶</a></h3>
</div>
<div class="section" id="login-mechanisms">
<h3>Login Mechanisms<a class="headerlink" href="#login-mechanisms" title="Permalink to this headline">¶</a></h3>
<dl class="data">
<dt id="flask_login.current_user">
<code class="descclassname">flask_login.</code><code class="descname">current_user</code><a class="headerlink" href="#flask_login.current_user" title="Permalink to this definition">¶</a></dt>
<dd><p>A proxy for the current user.</p>
</dd></dl>
</div>
<div class="section" id="protecting-views">
<h3>Protecting Views<a class="headerlink" href="#protecting-views" title="Permalink to this headline">¶</a></h3>
</div>
<div class="section" id="user-object-helpers">
<h3>User Object Helpers<a class="headerlink" href="#user-object-helpers" title="Permalink to this headline">¶</a></h3>
</div>
<div class="section" id="utilities">
<h3>Utilities<a class="headerlink" href="#utilities" title="Permalink to this headline">¶</a></h3>
</div>
<div class="section" id="signals">
<h3>Signals<a class="headerlink" href="#signals" title="Permalink to this headline">¶</a></h3>
<p>See the <a class="reference external" href="http://flask.pocoo.org/docs/signals/">Flask documentation on signals</a> for information on how to use these
signals in your code.</p>
<dl class="data">
<dt id="flask_login.user_logged_in">
<code class="descclassname">flask_login.</code><code class="descname">user_logged_in</code><a class="headerlink" href="#flask_login.user_logged_in" title="Permalink to this definition">¶</a></dt>
<dd><p>Sent when a user is logged in. In addition to the app (which is the
sender), it is passed <code class="xref py py-obj docutils literal"><span class="pre">user</span></code>, which is the user being logged in.</p>
</dd></dl>
<dl class="data">
<dt id="flask_login.user_logged_out">
<code class="descclassname">flask_login.</code><code class="descname">user_logged_out</code><a class="headerlink" href="#flask_login.user_logged_out" title="Permalink to this definition">¶</a></dt>
<dd><p>Sent when a user is logged out. In addition to the app (which is the
sender), it is passed <code class="xref py py-obj docutils literal"><span class="pre">user</span></code>, which is the user being logged out.</p>
</dd></dl>
<dl class="data">
<dt id="flask_login.user_login_confirmed">
<code class="descclassname">flask_login.</code><code class="descname">user_login_confirmed</code><a class="headerlink" href="#flask_login.user_login_confirmed" title="Permalink to this definition">¶</a></dt>
<dd><p>Sent when a user’s login is confirmed, marking it as fresh. (It is not
called for a normal login.)
It receives no additional arguments besides the app.</p>
</dd></dl>
<dl class="data">
<dt id="flask_login.user_unauthorized">
<code class="descclassname">flask_login.</code><code class="descname">user_unauthorized</code><a class="headerlink" href="#flask_login.user_unauthorized" title="Permalink to this definition">¶</a></dt>
<dd><p>Sent when the <code class="xref py py-obj docutils literal"><span class="pre">unauthorized</span></code> method is called on a <code class="xref py py-obj docutils literal"><span class="pre">LoginManager</span></code>. It
receives no additional arguments besides the app.</p>
</dd></dl>
<dl class="data">
<dt id="flask_login.user_needs_refresh">
<code class="descclassname">flask_login.</code><code class="descname">user_needs_refresh</code><a class="headerlink" href="#flask_login.user_needs_refresh" title="Permalink to this definition">¶</a></dt>
<dd><p>Sent when the <code class="xref py py-obj docutils literal"><span class="pre">needs_refresh</span></code> method is called on a <code class="xref py py-obj docutils literal"><span class="pre">LoginManager</span></code>. It
receives no additional arguments besides the app.</p>
</dd></dl>
<dl class="data">
<dt id="flask_login.session_protected">
<code class="descclassname">flask_login.</code><code class="descname">session_protected</code><a class="headerlink" href="#flask_login.session_protected" title="Permalink to this definition">¶</a></dt>
<dd><p>Sent whenever session protection takes effect, and a session is either
marked non-fresh or deleted. It receives no additional arguments besides
the app.</p>
</dd></dl>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
</div>
</body>
</html>
|