/usr/share/doc/python-flask-openid-doc/html/index.html is in python-flask-openid-doc 1.2.5+dfsg-2.
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 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 | <!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-OpenID — Flask-OpenID</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: '1.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="top" title="Flask-OpenID" href="#" />
</head>
<body>
<div class=indexwrapper>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="module-flask_openid">
<span id="flask-openid"></span><h1>Flask-OpenID<a class="headerlink" href="#module-flask_openid" title="Permalink to this headline">¶</a></h1>
<p>Flask-OpenID is an extension to <a class="reference external" href="http://flask.pocoo.org/">Flask</a> that allows you to add <a class="reference external" href="http://openid.net/">OpenID</a>
based authentication to your website in a matter of minutes. It depends
on Flask and <a class="reference external" href="http://openidenabled.com/python-openid/">python-openid</a> 2.x. You can install the requirements from
PyPI with <cite>easy_install</cite> or <cite>pip</cite> or download them by hand.</p>
<div class="section" id="features">
<h2>Features<a class="headerlink" href="#features" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li>support for OpenID 2.x</li>
<li>friendly API</li>
<li>perfect integration into Flask</li>
<li>basic support for AX and SReg extensions to OpenID that make it possible
to fetch basic profile information from a user’s OpenID provider.</li>
</ul>
</div>
<div class="section" id="installation">
<h2>Installation<a class="headerlink" href="#installation" title="Permalink to this headline">¶</a></h2>
<p>Install the extension with one of the following commands:</p>
<div class="highlight-python"><div class="highlight"><pre>$ easy_install Flask-OpenID
</pre></div>
</div>
<p>or alternatively if you have <cite>pip</cite> installed:</p>
<div class="highlight-python"><div class="highlight"><pre>$ pip install Flask-OpenID
</pre></div>
</div>
</div>
<div class="section" id="how-to-use">
<h2>How to Use<a class="headerlink" href="#how-to-use" title="Permalink to this headline">¶</a></h2>
<p>To integrate Flask-OpenID into your application you need to create an
instance of the <a class="reference internal" href="#flask_openid.OpenID" title="flask_openid.OpenID"><tt class="xref py py-class docutils literal"><span class="pre">OpenID</span></tt></a> object first:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">flask.ext.openid</span> <span class="kn">import</span> <span class="n">OpenID</span>
<span class="n">oid</span> <span class="o">=</span> <span class="n">OpenID</span><span class="p">(</span><span class="n">app</span><span class="p">,</span> <span class="s">'/path/to/store'</span><span class="p">,</span> <span class="n">safe_roots</span><span class="o">=</span><span class="p">[])</span>
</pre></div>
</div>
<p>By default it will use the filesystem as store for information needed by
OpenID for the authentication process. You can alternatively implement
your own store that uses the database or a no-sql server. For more
information about that, consult the python-openid documentation.</p>
<p>The path to the store can also be specified with the
<tt class="docutils literal"><span class="pre">OPENID_FS_STORE_PATH</span></tt> configuration variable.</p>
<p>Alternatively the object can be instantiated without the application in
which case it can later be registered for an application with the
<a class="reference internal" href="#flask_openid.OpenID.init_app" title="flask_openid.OpenID.init_app"><tt class="xref py py-meth docutils literal"><span class="pre">init_app()</span></tt></a> method.</p>
<p>The list of URL roots that are safe to redirect the user to are passed via
<cite>safe_roots</cite>. Whenever the url root of the <tt class="docutils literal"><span class="pre">'next'</span></tt> request argument is not in
this list, the user will get redirected to the app root. All urls that are local
to the current app are always regared as trusted. This security mechanism
can be disabled by leaving <cite>safe_roots</cite> out, but this is not suggested.</p>
<p>The current logged in user has to be memorized somewhere, we will use the
<tt class="docutils literal"><span class="pre">'openid'</span></tt> key in the <cite>session</cite>. This can be implemented in a
<cite>before_request</cite> function:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">flask</span> <span class="kn">import</span> <span class="n">g</span><span class="p">,</span> <span class="n">session</span>
<span class="nd">@app.before_request</span>
<span class="k">def</span> <span class="nf">lookup_current_user</span><span class="p">():</span>
<span class="n">g</span><span class="o">.</span><span class="n">user</span> <span class="o">=</span> <span class="bp">None</span>
<span class="k">if</span> <span class="s">'openid'</span> <span class="ow">in</span> <span class="n">session</span><span class="p">:</span>
<span class="n">openid</span> <span class="o">=</span> <span class="n">session</span><span class="p">[</span><span class="s">'openid'</span><span class="p">]</span>
<span class="n">g</span><span class="o">.</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">openid</span><span class="o">=</span><span class="n">openid</span><span class="p">)</span><span class="o">.</span><span class="n">first</span><span class="p">()</span>
</pre></div>
</div>
<p>This assumes the openid used for a user is stored in the user table
itself. As you can see from the example above, we’re using SQLAlchemy
here, but feel free to use a different storage backend. It’s just
important that you can somehow map from openid URL to user.</p>
<p>Next you need to define a login handling function. This function is a
standard view function that is additionally decorated as
<a class="reference internal" href="#flask_openid.OpenID.loginhandler" title="flask_openid.OpenID.loginhandler"><tt class="xref py py-meth docutils literal"><span class="pre">loginhandler()</span></tt></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@app.route</span><span class="p">(</span><span class="s">'/login'</span><span class="p">,</span> <span class="n">methods</span><span class="o">=</span><span class="p">[</span><span class="s">'GET'</span><span class="p">,</span> <span class="s">'POST'</span><span class="p">])</span>
<span class="nd">@oid.loginhandler</span>
<span class="k">def</span> <span class="nf">login</span><span class="p">():</span>
<span class="k">if</span> <span class="n">g</span><span class="o">.</span><span class="n">user</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
<span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="n">oid</span><span class="o">.</span><span class="n">get_next_url</span><span class="p">())</span>
<span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s">'POST'</span><span class="p">:</span>
<span class="n">openid</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">form</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'openid'</span><span class="p">)</span>
<span class="k">if</span> <span class="n">openid</span><span class="p">:</span>
<span class="k">return</span> <span class="n">oid</span><span class="o">.</span><span class="n">try_login</span><span class="p">(</span><span class="n">openid</span><span class="p">,</span> <span class="n">ask_for</span><span class="o">=</span><span class="p">[</span><span class="s">'email'</span><span class="p">,</span> <span class="s">'nickname'</span><span class="p">],</span>
<span class="n">ask_for_optional</span><span class="o">=</span><span class="p">[</span><span class="s">'fullname'</span><span class="p">])</span>
<span class="k">return</span> <span class="n">render_template</span><span class="p">(</span><span class="s">'login.html'</span><span class="p">,</span> <span class="nb">next</span><span class="o">=</span><span class="n">oid</span><span class="o">.</span><span class="n">get_next_url</span><span class="p">(),</span>
<span class="n">error</span><span class="o">=</span><span class="n">oid</span><span class="o">.</span><span class="n">fetch_error</span><span class="p">())</span>
</pre></div>
</div>
<p>What’s happening inside the login handler is that first we try to figure
out if the user is already logged in. In that case we return to where we
just came from (<a class="reference internal" href="#flask_openid.OpenID.get_next_url" title="flask_openid.OpenID.get_next_url"><tt class="xref py py-meth docutils literal"><span class="pre">get_next_url()</span></tt></a> can do that for us). When
the data is submitted we get the openid the user entered and try to login
with that information. Additionally we ask the openid provider for email,
nickname and the user’s full name, where we declare full name as optional.
If that information is available, we can use it to simplify the account
creation process in our application.</p>
<p>The template also needs the URL we want to return to, because it has to
forward that information in the form. If an error happened,
<a class="reference internal" href="#flask_openid.OpenID.fetch_error" title="flask_openid.OpenID.fetch_error"><tt class="xref py py-meth docutils literal"><span class="pre">fetch_error()</span></tt></a> will return that error message for us.</p>
<p>This is what a login template typically looks like:</p>
<div class="highlight-html+jinja"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">extends</span> <span class="s2">"layout.html"</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">block</span> <span class="nv">title</span> <span class="cp">%}</span>Sign in<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">block</span> <span class="nv">body</span> <span class="cp">%}</span>
<span class="nt"><h2></span>Sign in<span class="nt"></h2></span>
<span class="nt"><form</span> <span class="na">action=</span><span class="s">""</span> <span class="na">method=</span><span class="s">post</span><span class="nt">></span>
<span class="cp">{%</span> <span class="k">if</span> <span class="nv">error</span> <span class="cp">%}</span><span class="nt"><p</span> <span class="na">class=</span><span class="s">error</span><span class="nt">><strong></span>Error:<span class="nt"></strong></span> <span class="cp">{{</span> <span class="nv">error</span> <span class="cp">}}</span><span class="nt"></p></span><span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
<span class="nt"><p></span>
OpenID:
<span class="nt"><input</span> <span class="na">type=</span><span class="s">text</span> <span class="na">name=</span><span class="s">openid</span> <span class="na">size=</span><span class="s">30</span><span class="nt">></span>
<span class="nt"><input</span> <span class="na">type=</span><span class="s">submit</span> <span class="na">value=</span><span class="s">"Sign in"</span><span class="nt">></span>
<span class="nt"><input</span> <span class="na">type=</span><span class="s">hidden</span> <span class="na">name=</span><span class="s">next</span> <span class="na">value=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">next</span> <span class="cp">}}</span><span class="s">"</span><span class="nt">></span>
<span class="nt"></form></span>
<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>See how <cite>error</cite> and <cite>next</cite> are used. The name of the form field <cite>next</cite> is
required, so don’t change it.</p>
</div>
<div class="section" id="responding-to-successful-logins">
<h2>Responding to Successful Logins<a class="headerlink" href="#responding-to-successful-logins" title="Permalink to this headline">¶</a></h2>
<p>Next we have to define a function that is called after the login was
successful. The responsibility of that function is to remember the user
that just logged in and to figure out if it’s a new user to the system or
one with an existing profile (if you want to use profiles).</p>
<p>Such a function is decorated with <a class="reference internal" href="#flask_openid.OpenID.after_login" title="flask_openid.OpenID.after_login"><tt class="xref py py-meth docutils literal"><span class="pre">after_login()</span></tt></a> and must
remember the user in the session and redirect to the proper page:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">flask</span> <span class="kn">import</span> <span class="n">flash</span>
<span class="nd">@oid.after_login</span>
<span class="k">def</span> <span class="nf">create_or_login</span><span class="p">(</span><span class="n">resp</span><span class="p">):</span>
<span class="n">session</span><span class="p">[</span><span class="s">'openid'</span><span class="p">]</span> <span class="o">=</span> <span class="n">resp</span><span class="o">.</span><span class="n">identity_url</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">openid</span><span class="o">=</span><span class="n">resp</span><span class="o">.</span><span class="n">identity_url</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="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">flash</span><span class="p">(</span><span class="s">u'Successfully signed in'</span><span class="p">)</span>
<span class="n">g</span><span class="o">.</span><span class="n">user</span> <span class="o">=</span> <span class="n">user</span>
<span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="n">oid</span><span class="o">.</span><span class="n">get_next_url</span><span class="p">())</span>
<span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="n">url_for</span><span class="p">(</span><span class="s">'create_profile'</span><span class="p">,</span> <span class="nb">next</span><span class="o">=</span><span class="n">oid</span><span class="o">.</span><span class="n">get_next_url</span><span class="p">(),</span>
<span class="n">name</span><span class="o">=</span><span class="n">resp</span><span class="o">.</span><span class="n">fullname</span> <span class="ow">or</span> <span class="n">resp</span><span class="o">.</span><span class="n">nickname</span><span class="p">,</span>
<span class="n">email</span><span class="o">=</span><span class="n">resp</span><span class="o">.</span><span class="n">email</span><span class="p">))</span>
</pre></div>
</div>
<p>The <cite>resp</cite> object passed is a <a class="reference internal" href="#flask_openid.OpenIDResponse" title="flask_openid.OpenIDResponse"><tt class="xref py py-class docutils literal"><span class="pre">OpenIDResponse</span></tt></a> object with all the
information you might desire. As you can see, we memorize the user’s
openid and try to get the user with that OpenID from the database. If
that fails we redirect the user to a page to create a new profile and also
forward the name (or nickname if no name is provided) and the email
address. Please keep in mind that an openid provider does not have to
support these profile information and not every value you ask for will be
there. If it’s missing it will be <cite>None</cite>. Again make sure to not lose
the information about the next URL.</p>
</div>
<div class="section" id="creating-a-profile">
<h2>Creating a Profile<a class="headerlink" href="#creating-a-profile" title="Permalink to this headline">¶</a></h2>
<p>A typical page to create such a profile might look like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@app.route</span><span class="p">(</span><span class="s">'/create-profile'</span><span class="p">,</span> <span class="n">methods</span><span class="o">=</span><span class="p">[</span><span class="s">'GET'</span><span class="p">,</span> <span class="s">'POST'</span><span class="p">])</span>
<span class="k">def</span> <span class="nf">create_profile</span><span class="p">():</span>
<span class="k">if</span> <span class="n">g</span><span class="o">.</span><span class="n">user</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">or</span> <span class="s">'openid'</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">session</span><span class="p">:</span>
<span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="n">url_for</span><span class="p">(</span><span class="s">'index'</span><span class="p">))</span>
<span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s">'POST'</span><span class="p">:</span>
<span class="n">name</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">form</span><span class="p">[</span><span class="s">'name'</span><span class="p">]</span>
<span class="n">email</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">form</span><span class="p">[</span><span class="s">'email'</span><span class="p">]</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">name</span><span class="p">:</span>
<span class="n">flash</span><span class="p">(</span><span class="s">u'Error: you have to provide a name'</span><span class="p">)</span>
<span class="k">elif</span> <span class="s">'@'</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">email</span><span class="p">:</span>
<span class="n">flash</span><span class="p">(</span><span class="s">u'Error: you have to enter a valid email address'</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">flash</span><span class="p">(</span><span class="s">u'Profile successfully created'</span><span class="p">)</span>
<span class="n">db_session</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">User</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">email</span><span class="p">,</span> <span class="n">session</span><span class="p">[</span><span class="s">'openid'</span><span class="p">]))</span>
<span class="n">db_session</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>
<span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="n">oid</span><span class="o">.</span><span class="n">get_next_url</span><span class="p">())</span>
<span class="k">return</span> <span class="n">render_template</span><span class="p">(</span><span class="s">'create_profile.html'</span><span class="p">,</span> <span class="nb">next</span><span class="o">=</span><span class="n">oid</span><span class="o">.</span><span class="n">get_next_url</span><span class="p">())</span>
</pre></div>
</div>
<p>If you’re using the same names for the URL parameters in the step before
and in this form, you have nice looking and simple templates:</p>
<div class="highlight-html+jinja"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">extends</span> <span class="s2">"layout.html"</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">block</span> <span class="nv">title</span> <span class="cp">%}</span>Create Profile<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">block</span> <span class="nv">body</span> <span class="cp">%}</span>
<span class="nt"><h2></span>Create Profile<span class="nt"></h2></span>
<span class="nt"><p></span>
Hey! This is the first time you signed in on this website. In
order to proceed we need a couple of more information from you:
<span class="nt"><form</span> <span class="na">action=</span><span class="s">""</span> <span class="na">method=</span><span class="s">post</span><span class="nt">></span>
<span class="nt"><dl></span>
<span class="nt"><dt></span>Name:
<span class="nt"><dd><input</span> <span class="na">type=</span><span class="s">text</span> <span class="na">name=</span><span class="s">name</span> <span class="na">size=</span><span class="s">30</span> <span class="na">value=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">request.values.name</span> <span class="cp">}}</span><span class="s">"</span><span class="nt">></span>
<span class="nt"><dt></span>E-Mail:
<span class="nt"><dd><input</span> <span class="na">type=</span><span class="s">text</span> <span class="na">name=</span><span class="s">email</span> <span class="na">size=</span><span class="s">30</span> <span class="na">value=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">request.values.email</span> <span class="cp">}}</span><span class="s">"</span><span class="nt">></span>
<span class="nt"></dl></span>
<span class="nt"><p></span>
<span class="nt"><input</span> <span class="na">type=</span><span class="s">submit</span> <span class="na">value=</span><span class="s">"Create profile"</span><span class="nt">></span>
<span class="nt"><input</span> <span class="na">type=</span><span class="s">hidden</span> <span class="na">name=</span><span class="s">next</span> <span class="na">value=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">next</span> <span class="cp">}}</span><span class="s">"</span><span class="nt">></span>
<span class="nt"></form></span>
<span class="nt"><p></span>
If you don't want to proceed, you can <span class="nt"><a</span> <span class="na">href=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">url_for</span><span class="o">(</span><span class="s1">'logout'</span><span class="o">)</span>
<span class="cp">}}</span><span class="s">"</span><span class="nt">></span>sign out<span class="nt"></a></span> again.
<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="logging-out">
<h2>Logging Out<a class="headerlink" href="#logging-out" title="Permalink to this headline">¶</a></h2>
<p>The logout function is very simple, it just has to unset the openid from
the session and redirect back to where the user was before:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@app.route</span><span class="p">(</span><span class="s">'/logout'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">logout</span><span class="p">():</span>
<span class="n">session</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">'openid'</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
<span class="n">flash</span><span class="p">(</span><span class="s">u'You were signed out'</span><span class="p">)</span>
<span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="n">oid</span><span class="o">.</span><span class="n">get_next_url</span><span class="p">())</span>
</pre></div>
</div>
</div>
<div class="section" id="advanced-usage">
<h2>Advanced usage<a class="headerlink" href="#advanced-usage" title="Permalink to this headline">¶</a></h2>
<p>Flask-OpenID can also work with any python-openid extension.
To use this, pass a list of instantiated request openid.extension.Extension
objects in the <cite>extensions</cite> field of <a class="reference internal" href="#flask_openid.OpenID.try_login" title="flask_openid.OpenID.try_login"><tt class="xref py py-meth docutils literal"><span class="pre">try_login()</span></tt></a>.
The responses of these extensions are available during the <tt class="xref py py-meth docutils literal"><span class="pre">after_login()</span></tt>
function, as entries in resp.extensions.</p>
</div>
<div class="section" id="full-example">
<h2>Full Example<a class="headerlink" href="#full-example" title="Permalink to this headline">¶</a></h2>
<p>To see the full code of that example, you can download the code <a class="reference external" href="http://github.com/mitsuhiko/flask-openid">from
github</a>.</p>
</div>
<div class="section" id="changes">
<h2>Changes<a class="headerlink" href="#changes" title="Permalink to this headline">¶</a></h2>
<div class="section" id="id1">
<h3>1.2<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>The safe_roots argument and URL security system was added.</li>
<li>The OpenID extensions system was added.</li>
</ul>
</div>
<div class="section" id="id2">
<h3>1.0<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>the OpenID object is not registered to an application which allows
configuration values to be used and is also consistent with other
Flask extensions.</li>
</ul>
</div>
</div>
<div class="section" id="api-references">
<h2>API References<a class="headerlink" href="#api-references" title="Permalink to this headline">¶</a></h2>
<p>The full API reference:</p>
<dl class="class">
<dt id="flask_openid.OpenID">
<em class="property">class </em><tt class="descclassname">flask_openid.</tt><tt class="descname">OpenID</tt><big>(</big><em>app=None</em>, <em>fs_store_path=None</em>, <em>store_factory=None</em>, <em>fallback_endpoint=None</em>, <em>extension_responses=None</em>, <em>safe_roots=None</em>, <em>url_root_as_trust_root=False</em><big>)</big><a class="headerlink" href="#flask_openid.OpenID" title="Permalink to this definition">¶</a></dt>
<dd><p>Simple helper class for OpenID auth. Has to be created in advance
like a <tt class="xref py py-class docutils literal"><span class="pre">Flask</span></tt> object.</p>
<p>There are two usage modes which work very similar. One is binding
the instance to a very specific Flask application:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="n">__name__</span><span class="p">)</span>
<span class="n">db</span> <span class="o">=</span> <span class="n">OpenID</span><span class="p">(</span><span class="n">app</span><span class="p">)</span>
</pre></div>
</div>
<p>The second possibility is to create the object once and configure the
application later to support it:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">oid</span> <span class="o">=</span> <span class="n">OpenID</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">create_app</span><span class="p">():</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="n">__name__</span><span class="p">)</span>
<span class="n">oid</span><span class="o">.</span><span class="n">init_app</span><span class="p">(</span><span class="n">app</span><span class="p">)</span>
<span class="k">return</span> <span class="n">app</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>app</strong> – the application to register this openid controller with.</li>
<li><strong>fs_store_path</strong> – if given this is the name of a folder where the
OpenID auth process can store temporary
information. If neither is provided a temporary
folder is assumed. This is overridden by the
<tt class="docutils literal"><span class="pre">OPENID_FS_STORE_PATH</span></tt> configuration key.</li>
<li><strong>store_factory</strong> – alternatively a function that creates a
python-openid store object.</li>
<li><strong>fallback_endpoint</strong> – optionally a string with the name of an URL
endpoint the user should be redirected to
if the HTTP referrer is unreliable. By
default the user is redirected back to the
application’s index in that case.</li>
<li><strong>extension_responses</strong> – a list of OpenID Extensions Response class.</li>
<li><strong>safe_roots</strong> – a list of trust roots to support returning to</li>
<li><strong>url_root_as_trust_root</strong> – whether to use the url_root as trust_root</li>
</ul>
</td>
</tr>
</tbody>
</table>
<dl class="method">
<dt id="flask_openid.OpenID.after_login">
<tt class="descname">after_login</tt><big>(</big><em>f</em><big>)</big><a class="headerlink" href="#flask_openid.OpenID.after_login" title="Permalink to this definition">¶</a></dt>
<dd><p>This function will be called after login. It must redirect to
a different place and remember the user somewhere. The session
is not modified by SimpleOpenID. The decorated function is
passed a <a class="reference internal" href="#flask_openid.OpenIDResponse" title="flask_openid.OpenIDResponse"><tt class="xref py py-class docutils literal"><span class="pre">OpenIDResponse</span></tt></a> object.</p>
</dd></dl>
<dl class="method">
<dt id="flask_openid.OpenID.attach_reg_info">
<tt class="descname">attach_reg_info</tt><big>(</big><em>auth_request</em>, <em>keys</em>, <em>optional_keys</em><big>)</big><a class="headerlink" href="#flask_openid.OpenID.attach_reg_info" title="Permalink to this definition">¶</a></dt>
<dd><p>Attaches sreg and ax requests to the auth request.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Internal:</th><td class="field-body"></td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="flask_openid.OpenID.errorhandler">
<tt class="descname">errorhandler</tt><big>(</big><em>f</em><big>)</big><a class="headerlink" href="#flask_openid.OpenID.errorhandler" title="Permalink to this definition">¶</a></dt>
<dd><p>Called if an error occurs with the message. By default
<tt class="docutils literal"><span class="pre">'openid_error'</span></tt> is added to the session so that <a class="reference internal" href="#flask_openid.OpenID.fetch_error" title="flask_openid.OpenID.fetch_error"><tt class="xref py py-meth docutils literal"><span class="pre">fetch_error()</span></tt></a>
can fetch that error from the session. Alternatively it makes sense
to directly flash the error for example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@oid.errorhandler</span>
<span class="k">def</span> <span class="nf">on_error</span><span class="p">(</span><span class="n">message</span><span class="p">):</span>
<span class="n">flash</span><span class="p">(</span><span class="s">u'Error: '</span> <span class="o">+</span> <span class="n">message</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>
<dl class="method">
<dt id="flask_openid.OpenID.fetch_error">
<tt class="descname">fetch_error</tt><big>(</big><big>)</big><a class="headerlink" href="#flask_openid.OpenID.fetch_error" title="Permalink to this definition">¶</a></dt>
<dd><p>Fetches the error from the session. This removes it from the
session and returns that error. This method is probably useless
if <a class="reference internal" href="#flask_openid.OpenID.errorhandler" title="flask_openid.OpenID.errorhandler"><tt class="xref py py-meth docutils literal"><span class="pre">errorhandler()</span></tt></a> is used.</p>
</dd></dl>
<dl class="method">
<dt id="flask_openid.OpenID.get_current_url">
<tt class="descname">get_current_url</tt><big>(</big><big>)</big><a class="headerlink" href="#flask_openid.OpenID.get_current_url" title="Permalink to this definition">¶</a></dt>
<dd><p>the current URL + next.</p>
</dd></dl>
<dl class="method">
<dt id="flask_openid.OpenID.get_next_url">
<tt class="descname">get_next_url</tt><big>(</big><big>)</big><a class="headerlink" href="#flask_openid.OpenID.get_next_url" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the URL where we want to redirect to. This will
always return a valid URL.</p>
</dd></dl>
<dl class="method">
<dt id="flask_openid.OpenID.get_success_url">
<tt class="descname">get_success_url</tt><big>(</big><big>)</big><a class="headerlink" href="#flask_openid.OpenID.get_success_url" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the internal success URL.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Internal:</th><td class="field-body"></td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="flask_openid.OpenID.init_app">
<tt class="descname">init_app</tt><big>(</big><em>app</em><big>)</big><a class="headerlink" href="#flask_openid.OpenID.init_app" title="Permalink to this definition">¶</a></dt>
<dd><p>This callback can be used to initialize an application for the
use with this openid controller.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.0.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="flask_openid.OpenID.loginhandler">
<tt class="descname">loginhandler</tt><big>(</big><em>f</em><big>)</big><a class="headerlink" href="#flask_openid.OpenID.loginhandler" title="Permalink to this definition">¶</a></dt>
<dd><p>Marks a function as login handler. This decorator injects some
more OpenID required logic. Always decorate your login function with
this decorator.</p>
</dd></dl>
<dl class="method">
<dt id="flask_openid.OpenID.signal_error">
<tt class="descname">signal_error</tt><big>(</big><em>msg</em><big>)</big><a class="headerlink" href="#flask_openid.OpenID.signal_error" title="Permalink to this definition">¶</a></dt>
<dd><p>Signals an error. It does this by storing the message in the
session. Use <a class="reference internal" href="#flask_openid.OpenID.errorhandler" title="flask_openid.OpenID.errorhandler"><tt class="xref py py-meth docutils literal"><span class="pre">errorhandler()</span></tt></a> to this method.</p>
</dd></dl>
<dl class="method">
<dt id="flask_openid.OpenID.try_login">
<tt class="descname">try_login</tt><big>(</big><em>identity_url</em>, <em>ask_for=None</em>, <em>ask_for_optional=None</em>, <em>extensions=None</em>, <em>immediate=False</em><big>)</big><a class="headerlink" href="#flask_openid.OpenID.try_login" title="Permalink to this definition">¶</a></dt>
<dd><p>This tries to login with the given identity URL. This function
must be called from the login_handler. The <cite>ask_for</cite> and
<cite>ask_for_optional`parameter can be a set of values to be asked
from the openid provider, where keys in `ask_for</cite> are marked as
required, and keys in <cite>ask_for_optional</cite> are marked as optional.</p>
<p>The following strings can be used in the <cite>ask_for</cite> and
<cite>ask_for_optional</cite> parameters:
<tt class="docutils literal"><span class="pre">aim</span></tt>, <tt class="docutils literal"><span class="pre">blog</span></tt>, <tt class="docutils literal"><span class="pre">country</span></tt>, <tt class="docutils literal"><span class="pre">dob</span></tt> (date of birth), <tt class="docutils literal"><span class="pre">email</span></tt>,
<tt class="docutils literal"><span class="pre">fullname</span></tt>, <tt class="docutils literal"><span class="pre">gender</span></tt>, <tt class="docutils literal"><span class="pre">icq</span></tt>, <tt class="docutils literal"><span class="pre">image</span></tt>, <tt class="docutils literal"><span class="pre">jabber</span></tt>, <tt class="docutils literal"><span class="pre">language</span></tt>,
<tt class="docutils literal"><span class="pre">msn</span></tt>, <tt class="docutils literal"><span class="pre">nickname</span></tt>, <tt class="docutils literal"><span class="pre">phone</span></tt>, <tt class="docutils literal"><span class="pre">postcode</span></tt>, <tt class="docutils literal"><span class="pre">skype</span></tt>,
<tt class="docutils literal"><span class="pre">timezone</span></tt>, <tt class="docutils literal"><span class="pre">website</span></tt>, <tt class="docutils literal"><span class="pre">yahoo</span></tt></p>
<p><cite>extensions</cite> can be a list of instances of OpenID extension requests
that should be passed on with the request. If you use this, please make
sure to pass the Response classes of these extensions when initializing
OpenID.</p>
<p><cite>immediate</cite> can be used to indicate this request should be a so-called
checkid_immediate request, resulting in the provider not showing any
UI.
Note that this adds a new possible response: SetupNeeded, which is the
server saying it doesn’t have enough information yet to authorized or
reject the authentication (probably, the user needs to sign in or
approve the trust root).</p>
</dd></dl>
</dd></dl>
<dl class="class">
<dt id="flask_openid.OpenIDResponse">
<em class="property">class </em><tt class="descclassname">flask_openid.</tt><tt class="descname">OpenIDResponse</tt><big>(</big><em>resp</em>, <em>extensions</em><big>)</big><a class="headerlink" href="#flask_openid.OpenIDResponse" title="Permalink to this definition">¶</a></dt>
<dd><p>Passed to the <cite>after_login</cite> function. Provides all the information
sent from the OpenID provider. The profile information has to be
requested from the server by passing a list of fields as <cite>ask_for</cite> to
the <a class="reference internal" href="#flask_openid.OpenID.try_login" title="flask_openid.OpenID.try_login"><tt class="xref py py-meth docutils literal"><span class="pre">try_login()</span></tt></a> function.</p>
<dl class="attribute">
<dt id="flask_openid.OpenIDResponse.aim">
<tt class="descname">aim</tt><em class="property"> = None</em><a class="headerlink" href="#flask_openid.OpenIDResponse.aim" title="Permalink to this definition">¶</a></dt>
<dd><p>AIM messenger address as string</p>
</dd></dl>
<dl class="attribute">
<dt id="flask_openid.OpenIDResponse.blog">
<tt class="descname">blog</tt><em class="property"> = None</em><a class="headerlink" href="#flask_openid.OpenIDResponse.blog" title="Permalink to this definition">¶</a></dt>
<dd><p>URL of blog as string</p>
</dd></dl>
<dl class="attribute">
<dt id="flask_openid.OpenIDResponse.country">
<tt class="descname">country</tt><em class="property"> = None</em><a class="headerlink" href="#flask_openid.OpenIDResponse.country" title="Permalink to this definition">¶</a></dt>
<dd><p>the country of the user as specified by ISO3166</p>
</dd></dl>
<dl class="attribute">
<dt id="flask_openid.OpenIDResponse.date_of_birth">
<tt class="descname">date_of_birth</tt><em class="property"> = None</em><a class="headerlink" href="#flask_openid.OpenIDResponse.date_of_birth" title="Permalink to this definition">¶</a></dt>
<dd><p>date of birth as <a class="reference external" href="http://docs.python.org/library/datetime.html#datetime.datetime" title="(in Python v2.7)"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object.</p>
</dd></dl>
<dl class="attribute">
<dt id="flask_openid.OpenIDResponse.email">
<tt class="descname">email</tt><em class="property"> = None</em><a class="headerlink" href="#flask_openid.OpenIDResponse.email" title="Permalink to this definition">¶</a></dt>
<dd><p>the email address of the user</p>
</dd></dl>
<dl class="attribute">
<dt id="flask_openid.OpenIDResponse.extensions">
<tt class="descname">extensions</tt><em class="property"> = None</em><a class="headerlink" href="#flask_openid.OpenIDResponse.extensions" title="Permalink to this definition">¶</a></dt>
<dd><p>Hash of the response object from the OpenID Extension by the</p>
</dd></dl>
<dl class="attribute">
<dt id="flask_openid.OpenIDResponse.fullname">
<tt class="descname">fullname</tt><em class="property"> = None</em><a class="headerlink" href="#flask_openid.OpenIDResponse.fullname" title="Permalink to this definition">¶</a></dt>
<dd><p>the full name of the user</p>
</dd></dl>
<dl class="attribute">
<dt id="flask_openid.OpenIDResponse.gender">
<tt class="descname">gender</tt><em class="property"> = None</em><a class="headerlink" href="#flask_openid.OpenIDResponse.gender" title="Permalink to this definition">¶</a></dt>
<dd><p>the gender of the user (<tt class="docutils literal"><span class="pre">f</span></tt> for femail and <tt class="docutils literal"><span class="pre">m</span></tt> for male)</p>
</dd></dl>
<dl class="attribute">
<dt id="flask_openid.OpenIDResponse.icq">
<tt class="descname">icq</tt><em class="property"> = None</em><a class="headerlink" href="#flask_openid.OpenIDResponse.icq" title="Permalink to this definition">¶</a></dt>
<dd><p>icq messenger number as string</p>
</dd></dl>
<dl class="attribute">
<dt id="flask_openid.OpenIDResponse.identity_url">
<tt class="descname">identity_url</tt><em class="property"> = None</em><a class="headerlink" href="#flask_openid.OpenIDResponse.identity_url" title="Permalink to this definition">¶</a></dt>
<dd><p>the openid the user used for sign in</p>
</dd></dl>
<dl class="attribute">
<dt id="flask_openid.OpenIDResponse.image">
<tt class="descname">image</tt><em class="property"> = None</em><a class="headerlink" href="#flask_openid.OpenIDResponse.image" title="Permalink to this definition">¶</a></dt>
<dd><p>URL to profile image as string</p>
</dd></dl>
<dl class="attribute">
<dt id="flask_openid.OpenIDResponse.jabber">
<tt class="descname">jabber</tt><em class="property"> = None</em><a class="headerlink" href="#flask_openid.OpenIDResponse.jabber" title="Permalink to this definition">¶</a></dt>
<dd><p>jabber address as string</p>
</dd></dl>
<dl class="attribute">
<dt id="flask_openid.OpenIDResponse.language">
<tt class="descname">language</tt><em class="property"> = None</em><a class="headerlink" href="#flask_openid.OpenIDResponse.language" title="Permalink to this definition">¶</a></dt>
<dd><p>the user’s preferred language as specified by ISO639</p>
</dd></dl>
<dl class="attribute">
<dt id="flask_openid.OpenIDResponse.month_of_birth">
<tt class="descname">month_of_birth</tt><em class="property"> = None</em><a class="headerlink" href="#flask_openid.OpenIDResponse.month_of_birth" title="Permalink to this definition">¶</a></dt>
<dd><p>the month of birth of the user as integer (1 based)</p>
</dd></dl>
<dl class="attribute">
<dt id="flask_openid.OpenIDResponse.msn">
<tt class="descname">msn</tt><em class="property"> = None</em><a class="headerlink" href="#flask_openid.OpenIDResponse.msn" title="Permalink to this definition">¶</a></dt>
<dd><p>msn name as string</p>
</dd></dl>
<dl class="attribute">
<dt id="flask_openid.OpenIDResponse.nickname">
<tt class="descname">nickname</tt><em class="property"> = None</em><a class="headerlink" href="#flask_openid.OpenIDResponse.nickname" title="Permalink to this definition">¶</a></dt>
<dd><p>desired nickname of the user</p>
</dd></dl>
<dl class="attribute">
<dt id="flask_openid.OpenIDResponse.phone">
<tt class="descname">phone</tt><em class="property"> = None</em><a class="headerlink" href="#flask_openid.OpenIDResponse.phone" title="Permalink to this definition">¶</a></dt>
<dd><p>phone number of the user as string</p>
</dd></dl>
<dl class="attribute">
<dt id="flask_openid.OpenIDResponse.postcode">
<tt class="descname">postcode</tt><em class="property"> = None</em><a class="headerlink" href="#flask_openid.OpenIDResponse.postcode" title="Permalink to this definition">¶</a></dt>
<dd><p>free text that should conform to the user’s country’s postal system</p>
</dd></dl>
<dl class="attribute">
<dt id="flask_openid.OpenIDResponse.skype">
<tt class="descname">skype</tt><em class="property"> = None</em><a class="headerlink" href="#flask_openid.OpenIDResponse.skype" title="Permalink to this definition">¶</a></dt>
<dd><p>skype name as string</p>
</dd></dl>
<dl class="attribute">
<dt id="flask_openid.OpenIDResponse.timezone">
<tt class="descname">timezone</tt><em class="property"> = None</em><a class="headerlink" href="#flask_openid.OpenIDResponse.timezone" title="Permalink to this definition">¶</a></dt>
<dd><p>timezone string from the TimeZone database</p>
</dd></dl>
<dl class="attribute">
<dt id="flask_openid.OpenIDResponse.website">
<tt class="descname">website</tt><em class="property"> = None</em><a class="headerlink" href="#flask_openid.OpenIDResponse.website" title="Permalink to this definition">¶</a></dt>
<dd><p>URL of website as string</p>
</dd></dl>
<dl class="attribute">
<dt id="flask_openid.OpenIDResponse.yahoo">
<tt class="descname">yahoo</tt><em class="property"> = None</em><a class="headerlink" href="#flask_openid.OpenIDResponse.yahoo" title="Permalink to this definition">¶</a></dt>
<dd><p>yahoo messenger address as string</p>
</dd></dl>
<dl class="attribute">
<dt id="flask_openid.OpenIDResponse.year_of_birth">
<tt class="descname">year_of_birth</tt><em class="property"> = None</em><a class="headerlink" href="#flask_openid.OpenIDResponse.year_of_birth" title="Permalink to this definition">¶</a></dt>
<dd><p>the year of birth of the user as integer</p>
</dd></dl>
</dd></dl>
<dl class="data">
<dt id="flask_openid.COMMON_PROVIDERS">
<tt class="descclassname">flask_openid.</tt><tt class="descname">COMMON_PROVIDERS</tt><a class="headerlink" href="#flask_openid.COMMON_PROVIDERS" title="Permalink to this definition">¶</a></dt>
<dd><p>a dictionary of common provider name -> login URL mappings. This can
be used to implement “click button to login” functionality.</p>
<p>Currently contains general purpose entrypoints for the following
providers: <tt class="docutils literal"><span class="pre">google</span></tt>, <tt class="docutils literal"><span class="pre">yahoo</span></tt>, <tt class="docutils literal"><span class="pre">aol</span></tt>, and <tt class="docutils literal"><span class="pre">steam</span></tt>.</p>
</dd></dl>
</div>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
</div>
</body>
</html>
|