| 9 | | .. note:: |
|---|
| 10 | | |
|---|
| 11 | | This version of AuthKit (0.4) is nothing like the old AuthKit 0.2 |
|---|
| 12 | | which used to be hosted in the Pylons SVN. This version is a |
|---|
| 13 | | generic framework, the old version was a specific implementation. |
|---|
| 14 | | |
|---|
| 15 | | .. contents:: Table of Contents |
|---|
| 16 | | .. sectnum:: |
|---|
| 17 | | |
|---|
| 18 | | .. warning:: |
|---|
| 19 | | |
|---|
| 20 | | AuthKit has not been audited by a security expert, please use with |
|---|
| 21 | | caution at your own risk (or better yet, report security holes). |
|---|
| 22 | | |
|---|
| 23 | | Any comments, suggestions or improvements would be gratefully received on the |
|---|
| 24 | | `Pylons mailing list <http://groups.google.com/group/pylons-discuss>`_. |
|---|
| 25 | | |
|---|
| 26 | | Credits |
|---|
| 27 | | ======= |
|---|
| 28 | | |
|---|
| 29 | | The middleware components of AuthKit make heavy use of `Paste |
|---|
| 30 | | <http://pythonpaste.org>`_. |
|---|
| 31 | | |
|---|
| 32 | | The Basics |
|---|
| 33 | | ========== |
|---|
| 34 | | |
|---|
| 35 | | When writing a web application you frequently come across the problem of |
|---|
| 36 | | needing to restrict which visitors can see which pages. This problem can be |
|---|
| 37 | | split into two parts. |
|---|
| 38 | | |
|---|
| 39 | | Authentication |
|---|
| 40 | | |
|---|
| 41 | | The process of proving who the visitor is. Usually they will |
|---|
| 42 | | enter a username and password to prove their identity and the web application |
|---|
| 43 | | will set a cookie so that when they visit other pages on the site the |
|---|
| 44 | | application will be able to remember who the visitor is without needing to ask |
|---|
| 45 | | for their username and password again. |
|---|
| 46 | | |
|---|
| 47 | | Authorization |
|---|
| 48 | | |
|---|
| 49 | | The process of checking that an authenticated vistor has |
|---|
| 50 | | permission to see a particular page they are requesting. |
|---|
| 51 | | |
|---|
| 52 | | These two parts are quite separate but often become confused becuase |
|---|
| 53 | | information about who can be authenticated and what authorization permissions |
|---|
| 54 | | each user has is often stored in the same place, for example a database or LDAP |
|---|
| 55 | | repository. |
|---|
| 56 | | |
|---|
| 57 | | Because authorisation and authentication information typically comes from a |
|---|
| 58 | | variety of different sources any one solution (eg an SQL database) is not |
|---|
| 59 | | necessarily going to fit your requirements AuthKit provides a framework you |
|---|
| 60 | | can tweak to fit your requirements. |
|---|
| 61 | | |
|---|
| 62 | | AuthKit is therefore a WSGI framework that provides a sensible structure for implementing |
|---|
| 63 | | your own auth system. It also provides some sample implementations of the |
|---|
| 64 | | framework so that you can add full authentication and authorisation to your |
|---|
| 65 | | Web Server Gateway Interface project with just a few lines of code if you |
|---|
| 66 | | prefer not to imlement your own system. |
|---|
| 67 | | |
|---|
| 68 | | This document will take you through some different ways of restricting access |
|---|
| 69 | | to your site with AuthKit starting simply and getting more sophisticated. |
|---|
| 70 | | |
|---|
| 71 | | The AuthKit Architecture |
|---|
| 72 | | ======================== |
|---|
| 73 | | |
|---|
| 74 | | The AuthKit functionality is separated into the following classes of components: |
|---|
| 75 | | |
|---|
| 76 | | Authentication Middleware |
|---|
| 77 | | |
|---|
| 78 | | All authentication can be handled transparently by a middleware component so |
|---|
| 79 | | that your application doesn't need to worry about how to sign users in. This |
|---|
| 80 | | means you can change how your users are authenticated without changing any |
|---|
| 81 | | of your application code. |
|---|
| 82 | | |
|---|
| 83 | | The authentication middleware can intercept 401 and 403 responses so that |
|---|
| 84 | | the rest of you application doesn't even need to use AuthKit in order for |
|---|
| 85 | | the authenticate part to work. |
|---|
| 86 | | |
|---|
| 87 | | In a WSGI application it is as simple as: |
|---|
| 88 | | |
|---|
| 89 | | .. code-block:: Python |
|---|
| 90 | | |
|---|
| 91 | | start_response('403 Access denied', []) |
|---|
| 92 | | |
|---|
| 93 | | In Pylons this is as simple as writing: |
|---|
| 94 | | |
|---|
| 95 | | .. code-block:: Python |
|---|
| 96 | | |
|---|
| 97 | | abort(401) |
|---|
| 98 | | |
|---|
| 99 | | The authenticate middleware supports the methods HTTP basic, HTTP digest, |
|---|
| 100 | | form and cookie (with sign out), OpenID passurl or internal forward to |
|---|
| 101 | | application. The middleware is either configured directly, from a generic |
|---|
| 102 | | config file or a paste deploy setup. |
|---|
| 103 | | |
|---|
| 104 | | If you are using paste deploy you can add the middleware and set:: |
|---|
| 105 | | |
|---|
| 106 | | authkit.enable = false |
|---|
| 107 | | |
|---|
| 108 | | in your config to disable it. One way of enabling it is to do something |
|---|
| 109 | | like this:: |
|---|
| 110 | | |
|---|
| 111 | | authkit.enable = true |
|---|
| 112 | | authkit.setup.method = basic |
|---|
| 113 | | authkit.users = james:bananas |
|---|
| 114 | | ben:apples |
|---|
| 115 | | |
|---|
| 116 | | Any 401 status will now be handled and james can sign in with password |
|---|
| 117 | | bananas and ben with apples. Nice and easy eh? |
|---|
| 118 | | |
|---|
| 119 | | Of course there is a lot more to AuthKit. All the options are described |
|---|
| 120 | | in this manual. |
|---|
| 121 | | |
|---|
| 122 | | Permission Objects |
|---|
| 123 | | |
|---|
| 124 | | An auth system should provide a way to check user permissions and has to |
|---|
| 125 | | facilitate the developer in authorising a user at any point in the application |
|---|
| 126 | | stack that the developer feels is appropriate. |
|---|
| 127 | | |
|---|
| 128 | | AuthKit solves this requirement with permission objects used like this:: |
|---|
| 129 | | |
|---|
| 130 | | from authkit.permissions import * |
|---|
| 131 | | permission1 = UserIn(users=['ben','james']) |
|---|
| 132 | | permission2 = And(RemoteUser(), Not(UserIn(['james']))) |
|---|
| 133 | | |
|---|
| 134 | | You can use existing permissions or define them yourself. |
|---|
| 135 | | |
|---|
| 136 | | Permissions have access to the WSGI ``environ`` dictionary and |
|---|
| 137 | | ``start_response`` callable so as well as being based on information from a |
|---|
| 138 | | database or LDAP repository they could be based on information from the request |
|---|
| 139 | | or even the resposne and have all the flexibility and power of WSGI middleware. |
|---|
| 140 | | |
|---|
| 141 | | Authorization Objects |
|---|
| 142 | | |
|---|
| 143 | | Permission objects need to be checked in different ways depending on where |
|---|
| 144 | | in the applicaiton stack the check occurs. There are different authorization |
|---|
| 145 | | objects for use in different parts of a WSGI application but they all have |
|---|
| 146 | | the same effect of requiring the user to be authorized based on the |
|---|
| 147 | | permission object |
|---|
| 148 | | |
|---|
| 149 | | If a permission check fails a NotAuthorizedError or NotAuthenticatedError |
|---|
| 150 | | is raised by the authorisation object. This stops the request and is eventually |
|---|
| 151 | | handled by the ``httpexceptions`` middleware which turns it into a response |
|---|
| 152 | | with a 401 or 403 status code to be handled by the authentication middleware. |
|---|
| 153 | | |
|---|
| 154 | | Using the above features it is possible to build a sophisticated auth system. |
|---|
| 155 | | AuthKit goes further though and also defines other objects which you can use |
|---|
| 156 | | as they stand, ignore completely or modify for your use: |
|---|
| 157 | | |
|---|
| 158 | | User Management API |
|---|
| 159 | | |
|---|
| 160 | | AuthKit provides a simple read-only, extensible user management API and |
|---|
| 161 | | permissions objects to use it. It allows you to get started straight away. |
|---|
| 162 | | |
|---|
| 163 | | If you want to be able to use the exisitng permissions you can implement |
|---|
| 164 | | a user management API compatible with the AuthKit one and the existing |
|---|
| 165 | | funcationality will work. |
|---|
| 166 | | |
|---|
| 167 | | If you want to start from scratch, all the authentication methods provide |
|---|
| 168 | | a means of handling user management yourself. |
|---|
| 169 | | |
|---|
| 170 | | Framework Adaptors |
|---|
| 171 | | |
|---|
| 172 | | Although AuthKit provides a totally generic API it also has built in |
|---|
| 173 | | support for configuration via Paste Deploy config files and comes with |
|---|
| 174 | | the authkit.pylons_adaptors module which provides tools for integrating |
|---|
| 175 | | AuthKit into Pylons quickly and easily. |
|---|
| 176 | | |
|---|
| 177 | | It is hoped other framework developers will also implement version of |
|---|
| 178 | | AuthKit and the developers would be keen to assist with any such efforts. |
|---|
| 179 | | |
|---|
| 180 | | The best way to expalin the various components and how they all fit together is |
|---|
| 181 | | with some examples so lets get started. |
|---|
| 182 | | |
|---|
| 183 | | Authentication Middleware |
|---|
| 184 | | ========================= |
|---|
| 185 | | |
|---|
| 186 | | Configuration |
|---|
| 187 | | ------------- |
|---|
| 188 | | |
|---|
| 189 | | First of all we need a middleware component to intercept responses with 401 |
|---|
| 190 | | status codes so that the user can be prompted to sign in. This is done using |
|---|
| 191 | | ``authkit.authenticate`` middleware: |
|---|
| 192 | | |
|---|
| 193 | | .. code-block:: Python |
|---|
| 194 | | |
|---|
| 195 | | from authkit import authenticate |
|---|
| 196 | | app = authenticate.middleware( |
|---|
| 197 | | app, |
|---|
| 198 | | config_paste=None, |
|---|
| 199 | | config_file=None, |
|---|
| 200 | | prefix='authkit.', |
|---|
| 201 | | **options |
|---|
| 202 | | ) |
|---|
| 203 | | |
|---|
| 204 | | The authenticate middleware takes the WSGI application as the first parameter. |
|---|
| 205 | | You can then choose to specify all the configuration options directly as |
|---|
| 206 | | keyword arguments in the Python code that sets up the middleware or to use a |
|---|
| 207 | | `Paste deploy config file <http://pythonpaste.org/deploy>`_. If you choose to |
|---|
| 208 | | use a config file you can either specify the filename with ``config_file`` or |
|---|
| 209 | | if you have already parsed the config file you can specify ``config_paste`` |
|---|
| 210 | | which should be the ``paste.deploy.CONFIG['app_conf']`` dictionary. |
|---|
| 211 | | |
|---|
| 212 | | AuthKit automatically converts any ``.`` characters in the config file keys to |
|---|
| 213 | | ``_`` characters and it also strips off the ``prefix`` defined when inilialising |
|---|
| 214 | | the middleware. If you don't set ``prefix`` it defaults to ``authkit.``. This |
|---|
| 215 | | means that options such as ``cookie_signoutpath`` used by AuthKit can be written in |
|---|
| 216 | | the slightly more descriptive and clean form ``authkit.cookie.signoutpath`` in the |
|---|
| 217 | | config file. |
|---|
| 218 | | |
|---|
| 219 | | .. note :: |
|---|
| 220 | | |
|---|
| 221 | | Throughout the documentation we will describe configuration options in terms |
|---|
| 222 | | of the configuration file keys because this is likely to be how you configure |
|---|
| 223 | | AuthKit but in examples we will use the direct AuthKit options because we |
|---|
| 224 | | don't want to clutter the examples with config parsing code. Please be aware |
|---|
| 225 | | that the two ways of specifying AuthKit options have the same effect. |
|---|
| 226 | | |
|---|
| 227 | | If you are using Paste Deploy or Pylons you can put these options in the |
|---|
| 228 | | ``[app:main]`` section and setup your middleware stack as follows: |
|---|
| 229 | | |
|---|
| 230 | | .. code-block:: Python |
|---|
| 231 | | |
|---|
| 232 | | from authkit.authenticate import middleware |
|---|
| 233 | | from paste.deploy import CONFIG |
|---|
| 234 | | app = middleware(app, config_paste=CONFIG['app_conf')) |
|---|
| 235 | | |
|---|
| 236 | | If you want to load the configuration from a plain file you can do so as follows: |
|---|
| 237 | | |
|---|
| 238 | | .. code-block:: Python |
|---|
| 239 | | |
|---|
| 240 | | from authkit.authenticate import middleware |
|---|
| 241 | | app = middleware(app, config_file='/path/to/authkit.conf') |
|---|
| 242 | | |
|---|
| 243 | | You can still specify parameters in conjunction with using a config file. Any |
|---|
| 244 | | parameters specified in code will replace those specified in the config file |
|---|
| 245 | | and a warning will be issued. |
|---|
| 246 | | |
|---|
| 247 | | There are some options such as ``authkit.users.valid`` and |
|---|
| 248 | | ``authkit.users.digest`` which take functions when used as parameters |
|---|
| 249 | | from within code. In a config file these varibles should specify the module |
|---|
| 250 | | path of the function to use in the format ``module_path:function``. For |
|---|
| 251 | | example:: |
|---|
| 252 | | |
|---|
| 253 | | authkit.users.valid = authkit.authenticate:valid_password |
|---|
| 254 | | |
|---|
| 255 | | Advanced Configuration Options |
|---|
| 256 | | ------------------------------ |
|---|
| 257 | | |
|---|
| 258 | | You can also specify some other configuration variables: |
|---|
| 259 | | |
|---|
| 260 | | authkit.enable |
|---|
| 261 | | If no options have been supplied in code when configuring the AuthKit |
|---|
| 262 | | middleware it is assumed that authkit has been deployed as part of a framework |
|---|
| 263 | | and will therefore not be enabled unless ``authkit.enable`` is set to ``true``. |
|---|
| 264 | | Setting ``authkit.enable`` to ``false`` in the config file will always disable |
|---|
| 265 | | the authkit middleware. If no value is set the authkit middleware is disables |
|---|
| 266 | | and a warning logged. |
|---|
| 267 | | |
|---|
| 268 | | authkit.catch |
|---|
| 269 | | If specified should be a comma separated list of status codes you would |
|---|
| 270 | | like the authkit middleware to respond to. For example if you want 403 |
|---|
| 271 | | Unauthorized pages to also prompt the visitor to sign in you could do this:: |
|---|
| 272 | | |
|---|
| 273 | | authkit.catch = 401, 403 |
|---|
| 274 | | |
|---|
| 275 | | If you wanted only authenticated users to see error reports you could use:: |
|---|
| 276 | | |
|---|
| 277 | | authkit.catch = 500 |
|---|
| 278 | | |
|---|
| 279 | | If you wanted all pages to require an authenticated user you could use:: |
|---|
| 280 | | |
|---|
| 281 | | authkit.catch = * |
|---|
| 282 | | |
|---|
| 283 | | If not specified authkit responds only to 401 status codes. |
|---|
| 284 | | |
|---|
| 285 | | authkit.exclude |
|---|
| 286 | | If ``authkit.catch`` is set to ``*`` you can specifically set codes not to |
|---|
| 287 | | catch using ``authkit.exclude``. For example to require authentication for all |
|---|
| 288 | | status codes except 500 and 404 you could do this:: |
|---|
| 289 | | |
|---|
| 290 | | authkit.catch = * |
|---|
| 291 | | authkit.exclude = 500, 404 |
|---|
| 292 | | |
|---|
| 293 | | Authentication Methods |
|---|
| 294 | | ---------------------- |
|---|
| 295 | | |
|---|
| 296 | | Once the basic configuration is set up the first thing to decide is how you |
|---|
| 297 | | want to authenticate users. The options for the ``method`` parameter are: |
|---|
| 298 | | |
|---|
| 299 | | ``basic`` |
|---|
| 300 | | |
|---|
| 301 | | Use HTTP basic authorisation |
|---|
| 302 | | |
|---|
| 303 | | ``digest`` |
|---|
| 304 | | |
|---|
| 305 | | Use HTTP digest authentication |
|---|
| 306 | | |
|---|
| 307 | | ``form`` |
|---|
| 308 | | |
|---|
| 309 | | Use a cookie and simple HTML form generated by the middleware |
|---|
| 310 | | |
|---|
| 311 | | ``forward`` |
|---|
| 312 | | |
|---|
| 313 | | Internally forward the request to a specified URL so the |
|---|
| 314 | | application can present a form to the user and handle sign in itself |
|---|
| 315 | | |
|---|
| 316 | | ``passurl`` |
|---|
| 317 | | |
|---|
| 318 | | Use an OpenID passurl for authentication |
|---|
| 319 | | |
|---|
| 320 | | As mentioned before you can specify this in your code as |
|---|
| 321 | | ``method="basic"`` or in your config file as ``authkit.setup.method = |
|---|
| 322 | | basic``. If you try to specify the same option using both methods an |
|---|
| 323 | | error is raised, otherwise AuthKit tries to use all the options from |
|---|
| 324 | | both sources. |
|---|
| 325 | | |
|---|
| 326 | | Each of these options is described in detail in the following sections. |
|---|
| 327 | | |
|---|
| 328 | | Basic HTTP/1.0 Authentication |
|---|
| 329 | | ----------------------------- |
|---|
| 330 | | |
|---|
| 331 | | The ``basic`` method is an implementation of basic authentication as described in |
|---|
| 332 | | HTTP/1.0 specification [1]_ . |
|---|
| 333 | | |
|---|
| 334 | | .. warning:: Do not use this example in production sites unless you are using |
|---|
| 335 | | SSL or need to work with very out-dated clients because the password entered is |
|---|
| 336 | | transmitted in plain text, instead use `Digest HTTP/1.1 Authentication`_ |
|---|
| 337 | | described below. |
|---|
| 338 | | |
|---|
| 339 | | HTTP Basic Authentication is perhaps the easiest way to add authentication to |
|---|
| 340 | | your website. When your application returns a 401 status code the visitor's |
|---|
| 341 | | browser will promot them for a username and password if they haven't already |
|---|
| 342 | | signed in. |
|---|
| 343 | | |
|---|
| 344 | | The AuthKit middleware will check the username and password and sign in the |
|---|
| 345 | | visitor only if password was correct. |
|---|
| 346 | | |
|---|
| 347 | | The code looks like this: |
|---|
| 348 | | |
|---|
| 349 | | .. include:: ../examples/docs/basic.py |
|---|
| 350 | | :literal: |
|---|
| 351 | | |
|---|
| 352 | | The ``realm`` parameter is an identifier for the authority that is |
|---|
| 353 | | requesting authorization. It is shown to the user and should be unique within |
|---|
| 354 | | the domain it is being used. If it isn't specified, a default of ``AuthKit`` is |
|---|
| 355 | | used. |
|---|
| 356 | | |
|---|
| 357 | | The parameter ``users_valid`` should be a function that returns ``True`` |
|---|
| 358 | | if the username and password are correct and ``False`` otherwise. The example |
|---|
| 359 | | above will allow anyone with a password that is the same as their username to |
|---|
| 360 | | sign in. In this case not entering a username or password is therefore allowed. |
|---|
| 361 | | This isn't very secure so you should customise the function to suit |
|---|
| 362 | | your setup. |
|---|
| 363 | | |
|---|
| 364 | | .. note:: |
|---|
| 365 | | |
|---|
| 366 | | Bear in mind that in authentication systems usernames are usually |
|---|
| 367 | | case *insensitive* and passwords are case *sensitive*. Your users will |
|---|
| 368 | | probably expect your system to follow this general rule. |
|---|
| 369 | | |
|---|
| 370 | | If the visitor presses Cancel they will be shown the 403 Unauthorized response. |
|---|
| 371 | | |
|---|
| 372 | | If a user has been signed in the ``REMOTE_USER`` environment variable will be |
|---|
| 373 | | set with their username so you can access it in your application code as |
|---|
| 374 | | ``environ['REMOTE_USER']``. |
|---|
| 375 | | |
|---|
| 376 | | The example above is available in the ``examples/docs`` directory and can be |
|---|
| 377 | | run with:: |
|---|
| 378 | | |
|---|
| 379 | | python basic.py |
|---|
| 380 | | |
|---|
| 381 | | If you run the program you will be able to visit http://localhost:8080 but will |
|---|
| 382 | | be prompted to sign in if you visit http://localhost:8080/private and any |
|---|
| 383 | | username password combination where the username is the same as the password |
|---|
| 384 | | will sign you in any you will notice the ``REMOTE_USER`` variable will be set. |
|---|
| 385 | | |
|---|
| 386 | | .. note:: |
|---|
| 387 | | |
|---|
| 388 | | HTTP authentication does not easily support signing out so you will |
|---|
| 389 | | need to close the browser to test the example again. |
|---|
| 390 | | |
|---|
| 391 | | Digest HTTP/1.1 Authentication |
|---|
| 392 | | ------------------------------ |
|---|
| 393 | | |
|---|
| 394 | | This module implements ``Digest`` authentication as described by RFC 2617 [2]_ |
|---|
| 395 | | . At this time, this implementation does not provide for further challenges, |
|---|
| 396 | | nor does it support Authentication-Info header. It also uses md5, and an |
|---|
| 397 | | option to use sha would be a good thing. |
|---|
| 398 | | |
|---|
| 399 | | Digest authentication is similar to basic authentication but rather than |
|---|
| 400 | | sending the password unencrypted you send an encrypted digest so the security |
|---|
| 401 | | is slightly better. |
|---|
| 402 | | |
|---|
| 403 | | The code looks like this: |
|---|
| 404 | | |
|---|
| 405 | | .. include:: ../examples/docs/digest.py |
|---|
| 406 | | :literal: |
|---|
| 407 | | |
|---|
| 408 | | Note that the ``digest()`` function takes different parameters from the |
|---|
| 409 | | ``valid()`` function we used in the HTTP basic authentication. Also rather than |
|---|
| 410 | | returing ``True`` or ``False`` the function should use the |
|---|
| 411 | | ``digest_password()`` function from ``authkit.middleware.digest`` to return a |
|---|
| 412 | | digest. |
|---|
| 413 | | |
|---|
| 414 | | Form-Based Authentication |
|---|
| 415 | | ------------------------- |
|---|
| 416 | | |
|---|
| 417 | | One of the drawbacks of HTTP authentication is that the user can't easily sign |
|---|
| 418 | | out without closing the browser window. A form and cookie based solution is |
|---|
| 419 | | therefore preferable for most web applications. |
|---|
| 420 | | |
|---|
| 421 | | With this method if the user accesses a page that returns a 401 status code, the |
|---|
| 422 | | AuthKit middleware intercepts it and displays a form for the user to sign in. |
|---|
| 423 | | The template to use is supplied when configuring the middleware or a default is |
|---|
| 424 | | used if none is specified. If the user's sign in is incorrect they are shown |
|---|
| 425 | | the form again. |
|---|
| 426 | | |
|---|
| 427 | | Since the authentication form is submitted (via POST) neither the PATH_INFO nor |
|---|
| 428 | | the QUERY_STRING are accessed, and hence the current path remains _unaltered_ |
|---|
| 429 | | through the entire authentication process. If authentication succeeds, the |
|---|
| 430 | | REQUEST_METHOD is converted from a POST to a GET and the page the user was |
|---|
| 431 | | trying to view is displayed, so that a redirect is unnecessary (unlike most |
|---|
| 432 | | form auth implementations). |
|---|
| 433 | | |
|---|
| 434 | | Here is a working example: |
|---|
| 435 | | |
|---|
| 436 | | .. include:: ../examples/docs/form.py |
|---|
| 437 | | :literal: |
|---|
| 438 | | |
|---|
| 439 | | The ``form`` method can use the same ``valid()`` authenticate function as that |
|---|
| 440 | | used by the HTTP basic method but this example uses an alternative method |
|---|
| 441 | | making use of the AuthKit user management API. This is described in detail |
|---|
| 442 | | later but provides a very quick and easy way of specifying users, passwords, |
|---|
| 443 | | groups and roles. |
|---|
| 444 | | |
|---|
| 445 | | The example also specifies a ``cookie_secret`` which is a string used to help |
|---|
| 446 | | make the encryption on the cookie more random and a ``cookie_signoutpath`` |
|---|
| 447 | | parameter which is a special path which should automatically sign the user out |
|---|
| 448 | | when visited but will still display the resulting page so that your application |
|---|
| 449 | | can display a signed out message. There are lots of cookie options you can |
|---|
| 450 | | specify including the name, and expire time and more. These are all described |
|---|
| 451 | | in the cookie options section later. |
|---|
| 452 | | |
|---|
| 453 | | If you run the example and visiting http://localhost:8080/private the sign in |
|---|
| 454 | | form will be displayed. If you sign in with a username that is the same as the |
|---|
| 455 | | password you will be signed in. If you visit http://localhost:8080/signout you |
|---|
| 456 | | will see the data as usual but if you press refresh you will notice that you |
|---|
| 457 | | have also been signed out. |
|---|
| 458 | | |
|---|
| 459 | | .. note:: |
|---|
| 460 | | |
|---|
| 461 | | If you would like your application to handle the creation of the form, the |
|---|
| 462 | | sign in process and sign out manually so that you have complete control over |
|---|
| 463 | | the authentication process you should use the ``forward`` method. |
|---|
| 464 | | |
|---|
| 465 | | You can also configure the template used in the form. There are three ways to |
|---|
| 466 | | do this. The first is to specify the ``form_template`` option with the new |
|---|
| 467 | | template. |
|---|
| 468 | | |
|---|
| 469 | | The template should contain the characters ``%s`` which will be replaced with |
|---|
| 470 | | the correct action URL. Any other ``%`` characters should be escaped by |
|---|
| 471 | | writing them as ``%%``. The form should also contain fields named ``username`` |
|---|
| 472 | | and ``password`` and should use the ``POST`` method. Here is an example:: |
|---|
| 473 | | |
|---|
| 474 | | form_template = """\ |
|---|
| 475 | | <html> |
|---|
| 476 | | <head><title>Please Login!</title></head> |
|---|
| 477 | | <body> |
|---|
| 478 | | <h1>Please Login</h1> |
|---|
| 479 | | <form action="%s" method="post"> |
|---|
| 480 | | <dl> |
|---|
| 481 | | <dt>Username:</dt> |
|---|
| 482 | | <dd><input type="text" name="username"></dd> |
|---|
| 483 | | <dt>Password:</dt> |
|---|
| 484 | | <dd><input type="password" name="password"></dd> |
|---|
| 485 | | </dl> |
|---|
| 486 | | <input type="submit" name="authform" /> |
|---|
| 487 | | <hr /> |
|---|
| 488 | | </form> |
|---|
| 489 | | </body> |
|---|
| 490 | | </html> |
|---|
| 491 | | """ |
|---|
| 492 | | |
|---|
| 493 | | You can also specify ``form_template_file`` which should be a file containing |
|---|
| 494 | | the template with the same escaping as described above or ``form_template_obj`` |
|---|
| 495 | | which should be the Paste eval_import style path to a string object in an |
|---|
| 496 | | existing module containing the template or a callable that returns a string. |
|---|
| 497 | | For example:: |
|---|
| 498 | | |
|---|
| 499 | | form_template_obj = authkit.authenticate.form:template |
|---|
| 500 | | |
|---|
| 501 | | Forwarding |
|---|
| 502 | | ---------- |
|---|
| 503 | | |
|---|
| 504 | | Arguably the most flexible and powerful system for authentication is for the |
|---|
| 505 | | middleware to internally forward the request to a different part of the |
|---|
| 506 | | application if a 401 status is intercepted. The application itself is then |
|---|
| 507 | | responsible for displaying that page to take care of displaying a sign in form, |
|---|
| 508 | | authenticate a user, set a cookie and sign the user out in whichever |
|---|
| 509 | | way the application author thinks is best. |
|---|
| 510 | | |
|---|
| 511 | | The foward middleware sets up the cookie middleware used by the ``form`` method |
|---|
| 512 | | but you still need to manually set the cookie after the user has signed in |
|---|
| 513 | | using your own custom sign in form which you would have implemented yourself. |
|---|
| 514 | | |
|---|
| 515 | | The cookie can be set like this: |
|---|
| 516 | | |
|---|
| 517 | | .. code-block:: Python |
|---|
| 518 | | |
|---|
| 519 | | environ['paste.auth_tkt.set_user'](username) |
|---|
| 520 | | |
|---|
| 521 | | If you specify a ``cookie_signoutpath`` path when you set up the authentication |
|---|
| 522 | | middleware the user will be signed out automatically when visiting that path |
|---|
| 523 | | but you will still need to display a "signed out" path at that URL or the user |
|---|
| 524 | | will see a 404 page and think they haven't been signed out. |
|---|
| 525 | | |
|---|
| 526 | | Alternatively you can sign out the user manually by setting a cookie with the |
|---|
| 527 | | same name but no value or using the ``environ['paste.auth_tkt.logout_user']()`` |
|---|
| 528 | | method documented here: http://pythonpaste.org/module-paste.auth.auth_tkt.html |
|---|
| 529 | | |
|---|
| 530 | | Here is a simple complete example: |
|---|
| 531 | | |
|---|
| 532 | | .. include:: ../examples/docs/forward.py |
|---|
| 533 | | :literal: |
|---|
| 534 | | |
|---|
| 535 | | .. note:: |
|---|
| 536 | | |
|---|
| 537 | | If you are using any of the examples shown so far with permissions objects (described later) |
|---|
| 538 | | you will also need to include the paste httpexceptions middleware otherwise |
|---|
| 539 | | the ``NotAuthorizedError`` and ``NotAuthenticatedError`` errors will not be |
|---|
| 540 | | converted to status codes which the authenticate middleware use. |
|---|
| 541 | | |
|---|
| 542 | | You can set this up by adding the following lines before you add the |
|---|
| 543 | | authenticate middleware: |
|---|
| 544 | | |
|---|
| 545 | | .. code-block:: Python |
|---|
| 546 | | |
|---|
| 547 | | from paste import httpexceptions |
|---|
| 548 | | test_app = httpexceptions.middleware(test_app) |
|---|
| 549 | | |
|---|
| 550 | | A full description of the cookie options you can use is described in the cookie options |
|---|
| 551 | | section. |
|---|
| 552 | | |
|---|
| 553 | | OpenID Passurl |
|---|
| 554 | | -------------- |
|---|
| 555 | | |
|---|
| 556 | | Perhaps the simplest way of handling authentication in your application is to |
|---|
| 557 | | use OpenID. With OpenID the user enters their identity URL and the middleware |
|---|
| 558 | | contacts the identity provider associated with the URL. The identity provider |
|---|
| 559 | | then handles the authentication and replies to tell the middleware if the user |
|---|