Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Before searching for code, be sure you read the documentation. <a href="http://docs.djangoproject.com/en/1.2/topics/auth/#other-authentication-sources">http://docs.djangoproject.com/en/1.2/topics/auth/#other-authentication-sources</a> Also read the supplied Django source.</p> <p>You want to create three things.</p> <ol> <li><p>Middleware to capture the token. This is where most of the work happens. It checks for the token, authenticates it (by confirming it with the identity manager) and then logs in the user.</p></li> <li><p>Authentication backend to find Users. This is a stub. All it does is create users as needed. Your identity manager has the details. You're just caching the current version of the user on Django's local DB.</p></li> </ol> <p>Here's the middleware (edited).</p> <pre><code>from django.contrib.auth import authenticate, login class CookieMiddleware( object ): """Authentication Middleware for OpenAM using a cookie with a token. Backend will get user. """ def process_request(self, request): if not hasattr(request, 'user'): raise ImproperlyConfigured() if "thecookiename" not in request.COOKIES: return token= request.COOKIES["thecookiename"] # REST request to OpenAM server for user attributes. token, attribute, role = identity_manager.get_attributes( token ) user = authenticate(remote_user=attribute['uid'][0]) request.user = user login(request, user) </code></pre> <p>The <code>identity_manager.get_attributes</code> is a separate class we wrote to validate the token and get details on the user from the IM source. This, of course, has to be mocked for testing purposes.</p> <p>Here's a backend (edited)</p> <pre><code>class Backend( RemoteUserBackend ): def authenticate(**credentials): """We could authenticate the token by checking with OpenAM Server. We don't do that here, instead we trust the middleware to do it. """ try: user= User.objects.get(username=credentials['remote_user']) except User.DoesNotExist: user= User.objects.create(username=credentials['remote_user'] ) # Here is a good place to map roles to Django Group instances or other features. return user </code></pre> <p>This does not materially change the decorators for authentication or authorization.</p> <p>To make sure of this, we actually refresh the User and Group information from our identity manager.</p> <p>Note that the middleware runs for every single request. Sometimes, it's okay to pass the token to the backed <code>authenticate</code> method. If the token exists in the local user DB, the request can proceed without contacting the identity manager.</p> <p>We, however, have complex rules and timeouts in the identity manager, so we have to examine every token to be sure it's valid. Once the middleware is sure the token is valid, we can then allow the backend to do any additional processing.</p> <p>This isn't our live code (it's a little too complex to make a good example.)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload