<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Django Middleware Munging</title>
	<atom:link href="http://www.loggly.com/2009/12/django-middleware-munging/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.loggly.com/2009/12/django-middleware-munging/</link>
	<description>Log Management in the Cloud</description>
	<lastBuildDate>Fri, 20 Aug 2010 18:37:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: zrlram</title>
		<link>http://www.loggly.com/2009/12/django-middleware-munging/comment-page-1/#comment-16</link>
		<dc:creator>zrlram</dc:creator>
		<pubDate>Mon, 07 Dec 2009 17:24:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.loggly.com/?p=293#comment-16</guid>
		<description>After investigating the middleware solution for rewriting the usernames a little more, I found a slightly cleaner solution that intercepts the login view. It is nicer because you are not monitoring each Web POST to see whether a username is submitted.
Here is the basic idea:
&lt;ol&gt;&lt;li&gt;You redirect the login view to your own code.&lt;/LI&gt;
&lt;LI&gt;If you are not dealing with any POST data, you just show the authentication form from django.contrib.auth (auth.views.login())&lt;/LI&gt;
&lt;LI&gt;If you get POST data, you create the new username as a combination of the subdomain and the original username. Also extract the password from the post.&lt;/LI&gt;
&lt;LI&gt;Verify the authentication credentials through the authenticate() call. If you are dealing with correct credentials, you actually log the user in (auth.login()) and redirect the user to either the main page or the URL provided inside of the login call (redirect_to field).&lt;/LI&gt;
&lt;LI&gt;Upon a failed login, just log the failed login and call the auth.views.login() method.&lt;/LI&gt;

That&#039;s it. Here are the code snippets:

&lt;i&gt;urls.py:&lt;/i&gt;

&lt;code&gt;&lt;pre&gt;url(r&#039;^login/$&#039;,
        login,          #this is your own view. By default you used auth.views.login() here.
       {&#039;template_name&#039;: &#039;templates/login.html&#039;}, name=&#039;login&#039;),
&lt;/pre&gt;&lt;/code&gt;
&lt;i&gt;views.py:&lt;/i&gt;
&lt;code&gt;&lt;pre&gt;def login(request, template_name, redirect_field_name=REDIRECT_FIELD_NAME):

    if request.method == &quot;POST&quot;:
        subdomain = request.META[&#039;HTTP_HOST&#039;].split(&#039;.&#039;)[0]
        authuser = &quot;%s_%s&quot; % (subdomain, request.POST[&#039;username&#039;])
        password = request.POST[&#039;password&#039;]
        user = authenticate(username=authuser, password=password)
        if user:
            auth.login(request, user)
            redirect_to = request.REQUEST.get(redirect_field_name, &#039;&#039;)
            if not redirect_to or &#039;//&#039; in redirect_to or &#039; &#039; in redirect_to:
                redirect_to = settings.LOGIN_REDIRECT_URL
            return HttpResponseRedirect(redirect_to)
        else:
            logs.error(&#039;action=authentication,status=failure,user=&#039; + authuser);

    return auth.views.login(request, template_name)
&lt;/pre&gt;&lt;/code&gt;

Thanks to Seth for helping getting this to run.</description>
		<content:encoded><![CDATA[<p>After investigating the middleware solution for rewriting the usernames a little more, I found a slightly cleaner solution that intercepts the login view. It is nicer because you are not monitoring each Web POST to see whether a username is submitted.<br />
Here is the basic idea:</p>
<ol>
<li>You redirect the login view to your own code.</li>
<li>If you are not dealing with any POST data, you just show the authentication form from django.contrib.auth (auth.views.login())</li>
<li>If you get POST data, you create the new username as a combination of the subdomain and the original username. Also extract the password from the post.</li>
<li>Verify the authentication credentials through the authenticate() call. If you are dealing with correct credentials, you actually log the user in (auth.login()) and redirect the user to either the main page or the URL provided inside of the login call (redirect_to field).</li>
<li>Upon a failed login, just log the failed login and call the auth.views.login() method.</li>
<p>That&#8217;s it. Here are the code snippets:</p>
<p><i>urls.py:</i></p>
<p><code>
<pre>url(r'^login/$',
        login,          #this is your own view. By default you used auth.views.login() here.
       {'template_name': 'templates/login.html'}, name='login'),
</pre>
<p></code><br />
<i>views.py:</i><br />
<code>
<pre>def login(request, template_name, redirect_field_name=REDIRECT_FIELD_NAME):

    if request.method == "POST":
        subdomain = request.META['HTTP_HOST'].split('.')[0]
        authuser = "%s_%s" % (subdomain, request.POST['username'])
        password = request.POST['password']
        user = authenticate(username=authuser, password=password)
        if user:
            auth.login(request, user)
            redirect_to = request.REQUEST.get(redirect_field_name, '')
            if not redirect_to or '//' in redirect_to or ' ' in redirect_to:
                redirect_to = settings.LOGIN_REDIRECT_URL
            return HttpResponseRedirect(redirect_to)
        else:
            logs.error('action=authentication,status=failure,user=' + authuser);

    return auth.views.login(request, template_name)
</pre>
<p></code></p>
<p>Thanks to Seth for helping getting this to run.</ol>
]]></content:encoded>
	</item>
</channel>
</rss>
