/usr/share/doc/python-lamson/confirmations.html is in python-lamson 1.0pre11-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 | <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>LamsonProject: Confirmations</title>
<meta name="keywords" content="python, email, smtp, automation, framework, auto responder, autoresponder, email agent, smtp agent, email engine, email processor, email processing, email proxy, smtp proxy">
<meta name="description" content="Lamson is a email automation framework written in Python. It can be used to automate email processing and simplify email application development.">
<link rel="stylesheet" href="/styles/global.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="/css/code.css" type="text/css" charset="utf-8" />
<!--[if IE 7]>
<style type="text/css" media="screen">
div#column_left ul.sidebar_menu li div.color{
display: none;
}
</style>
<![endif]-->
<link href="/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="/prettify.js"></script>
</head>
<body onload="prettyPrint()">
<div id="content_centered">
<div id="header">
<h1><img id="logo" src="/images/lamson.png" alt="Lamson Project(TM) - Pipes and aliases are so 1970." /></h1>
<ul id="header_menu">
<li><a href="/">Home</a></li>
<li><a href="/blog/">News</a></li>
<li><a href="/feed.xml">Feed</a></li>
<li><a href="/download.html">Download</a></li>
<li><a href="/docs/">Documentation</a></li>
<li><a href="/docs/api/">API</a></li>
</ul>
</div>
<div id="main_content">
<h1>Confirmations</h1>
You never know who’s going to send an email to your Lamson application. It may
be a real person, or a spam bot. Currently most of the spam bots aren’t very
intelligent (probably because they haven’t discovered Lamson yet) so a simple
easy way to weed out the randomness is to confirm people at important “choke
points” in your application.
<p>Lamson provides a simple <span class="caps">API</span> in
<a href="http://lamsonproject.org/docs/api/lamson.confirm-module.html">lamson.confirm</a>
to help you send out and verify confirmation emails. You provide a storage
class for keeping track of who you’re expecting to confirm, a template for your
message to send, and it does the rest. It’s even advanced enough to keep the
original email around so you can resend it after the confirmation.</p>
<h2>The General Theory</h2>
<p>The confirmation <span class="caps">API</span> assumes a few things about how you’re doing your
confirmations:</p>
<ol>
<li>You want them to reply to an email to confirm.</li>
<li>The address they reply to confirm has a “target” to differentiate it from other parts of your application that needs confirmation.</li>
<li>The address they reply to also has a hex formatted randomly generated <span class="caps">UUID</span> for security, and your storage can handle that length.</li>
<li>You want to store the message they sent so you can get it back after they confirm.</li>
</ol>
<p>With those fairly reasonable assumptions you only need to then setup the
confirmation <span class="caps">API</span> in your <code>config/settings.py</code> file and use it in your handlers,
preferrably at your <code>START</code> state and anywhere else that you need
to validate the user before they do something destructive.</p>
<h2>Simplest Usage</h2>
<p>The most basic usage of the
<a href="http://lamsonproject.org/docs/api/lamson.confirm-module.html">lamson.confirm</a>
<span class="caps">API</span> is to put it in your <code>config/settings.py</code> and then access it in your
handlers. You configure it like this:</p>
<pre class="code prettyprint">
from lamson import confirm
...
CONFIRM_STORAGE=confirm.ConfirmationStorage()
CONFIRM = confirm.ConfirmationEngine('run/pending')
</pre>
<p>This puts a variable in your settings.py that you can access from your handlers
to craft and verify the confirmation messages.</p>
<blockquote>
<p>By default uses a simple in-memory dict to store the confirmations. That’s
fine for testing and an initial deploy, but you’ll probably want to switch to a
permanent form of storage for later. Further on in this document you’ll see
how.</p>
</blockquote>
<p>Next, you need to use it in your <code>START</code> state, and then in a <code>CONFIRMING</code>
state. Here’s a simple example:</p>
<pre class="code prettyprint">
from config.settings import relay, CONFIRM
@route("start@(host)")
def START(message, host=None):
CONFIRM.send(relay, "start", message, "mail/start_confirm.msg", locals())
return CONFIRMING
@route("start-confirm-(id_number)@(host)", id_number='[a-z0-9]+')
def CONFIRMING(message, id_number=None, host=None):
original = CONFIRM.verify('start', message['from'], id_number)
if original:
welcome = view.respond(locals(), "mail/welcome.msg",
From='noreply@%(host)s',
To=message['from'],
Subject="Welcome")
relay.deliver(welcome)
return PROTECTING
else:
logging.warning("Invalid confirm from %s", message['from'])
return CONFIRMING
</pre>
<p>Here’s how the above code works:</p>
<ol>
<li>First we import the <span class="caps">CONFIRM</span> variable so we can use it. </li>
<li>In our <code>START</code> handler (which is accepts start@(host)) we use the <span class="caps">API</span> to send out the confirmation message they should reply-to. Notice how we give a “start” target as the second argument, this is important.</li>
<li>Then we transition to <span class="caps">CONFIRMING</span> and wait for them to reply to that message.</li>
<li>The user then replies to the message we sent, so we handle the <span class="caps">CONFIRMING</span> state. Notice that we are handling “start-confirm-(id_number)” as the initial message, with “start” being the target (2nd parameter) from our above <code>CONFIRM.send</code> call.</li>
<li>In <span class="caps">CONFIRMING</span> we use the <code>CONFRIM.verify</code> method to validate that it’s from the right person, to the right target (“start”) and that they got the secret (id_number) right.</li>
<li>Finally, if it’s right we send them a welcome message, and if it’s not we just ignore the message.</li>
</ol>
<p>An alternative to ignoring the failed confirmation from them is to cancel it
and go back to the <span class="caps">START</span> state for them. The danger with that method though is
a spam bot will get into a loop where you are sending them constant
confirmation messages in a loop between <span class="caps">START</span> and <span class="caps">CONFIRMING</span>. It’s best to
drop it, and maybe provide a “cancel” mechanism.</p>
<h2>Using Shelf Storage</h2>
<p>Other than a few other methods, there’s only a need to change the storage. The
simplest change is to provide a dict-like interface to the
<a href="http://lamsonproject.org/docs/api/lamson.confirm.ConfirmationEngine-class.html">lamson.confirm.ConfirmationEngine</a>
to store. Easiest available is the Python
<a href="http://docs.python.org/library/shelve.html">shelf</a> module which gives a dict
interface to various key/value storage backends.</p>
<p>To use one, just change your code in <code>config/settings.py</code> to be like this:</p>
<pre class="code prettyprint">
import shelve
from lamson import confirm
...
CONFIRM_STORAGE=confirm.ConfirmationStorage(db=shelve.open("run/confirmationsdb"))
CONFIRM = confirm.ConfirmationEngine('run/pending', CONFIRM_STORAGE)
</pre>
<p>All you do is create a
<a href="http://lamsonproject.org/docs/api/lamson.confirm.ConfirmationStorage-class.html">lamson.confirm.ConfirmationStorage</a>
and give the <code>db=</code> parameter a dict it can use. Everything else will be the
same.</p>
<blockquote>
<p> There might be thread issues with this, and it will definitely fail if you use mulitple processes. See the next
section on using a Django Model.</p>
</blockquote>
<h2>Using A Django <span class="caps">ORM</span> Model</h2>
<p>In the <a href="http://librelist.com/">librelist.com</a> example code you’ll find that it
stores the confirmations in the Django model in the <code>webapp/librelist</code>
directory. This is actually easily setup, so first read <a href="/docs/hooking_into_django.html">Hooking Into
Django</a> to learn how to access a Django <span class="caps">ORM</span>.
After that, you write a simple version of <code>ConfirmationStorage</code> that would look
something like this:</p>
<pre class="code prettyprint">
from webapp.librelist.models import Confirmation
class DjangoConfirmStorage():
def clear(self):
Confirmation.objects.all().delete()
def get(self, target, from_address):
confirmations = Confirmation.objects.filter(from_address=from_address,
list_name=target)
if confirmations:
return confirmations[0].expected_secret, confirmations[0].pending_message_id
else:
return None, None
def delete(self, target, from_address):
Confirmation.objects.filter(from_address=from_address,
list_name=target).delete()
def store(self, target, from_address, expected_secret, pending_message_id):
conf = Confirmation(from_address=from_address,
expected_secret = expected_secret,
pending_message_id = pending_message_id,
list_name=target)
conf.save()
</pre>
<p>This is from Librelist, so you see we just import the <code>Confirmation</code> model and
then wrap it with the <code>get</code>, <code>delete</code>, <code>set</code>, and <code>clear</code> methods that
<code>ConfirmationEngine</code> needs to run.</p>
<p>For completeness, here’s what the Django <code>Confirmation</code> model looks like:</p>
<pre class="code prettyprint">
class Confirmation(models.Model):
from_address = models.EmailField()
request_date = models.DateTimeField(auto_now_add=True)
expected_secret = models.CharField(max_length=50)
pending_message_id = models.CharField(max_length=200)
list_name = models.CharField(max_length=200)
def __unicode__(self):
return self.from_address
</pre>
<p>Final step is to configure it in your <code>config/settings.py</code> thusly:</p>
<pre class="code prettyprint">
from lamson import confirm
...
from app.model.confirmation import DjangoConfirmStorage
CONFIRM = confirm.ConfirmationEngine('run/pending', DjangoConfirmStorage())
</pre>
<p>That’s all there is to it. This is actually a nice setup because you can use
the Django Admin to manage it during your first deployments.</p>
<h2>Other <span class="caps">ORM</span></h2>
<p>For other <span class="caps">ORM</span> systems simply use the same pattern as the Django example above.
You just create a similar model, wrap it with your own version of
<code>ConfirmationStorage</code> and plug it into the <code>ConfirmationEngine</code> you use.</p>
<h2>Targets</h2>
<p>The only other thing to understand is why the <span class="caps">API</span> has a “target” parameter.
Let’s look at the call to <span class="caps">CONFIRM</span>.send again:</p>
<pre class="code prettyprint">
CONFIRM.send(relay, "start", message, "mail/start_confirm.msg", locals())
</pre>
<p>The “start” string as the second parameter acts as a the target. It says that
this user needs to confirm for the “start” target when they do their reply.
That’s why the <code>route</code> on <code>CONFIRMING</code> is then like this:</p>
<pre class="code prettyprint">
@route("start-confirm-(id_number)@(host)", id_number='[a-z0-9]+')
</pre>
<p>You could also make the above a pattern, for example in the Librelist
confirmations we’re confirming that the user is joining a certain
mailing list:</p>
<pre class="code prettyprint">
@route('(list_name)-confirm-(id_number)@(host)')
def CONFIRMING_SUBSCRIBE(message, list_name=None, id_number=None, host=None):
original = CONFIRM.verify(list_name, message['from'], id_number)
...
</pre>
<p>This way, the user could have multiple simultaneous confirmations going for
different lists and they won’t step on eachother.</p>
<p>Without this differentiator, you’d have to either restrict users to just one
confirmation at a time, or you’d end up getting the data all confused.</p>
<h2>Confirming Off A Web Link</h2>
<p>If you want people to go to a web link instead of simply replying, then you have to do the
following:</p>
<ol>
<li>Either write your own version, or subclass <code>ConfirmationEngine</code> so that it uses an address they can’t reply to.</li>
<li>Make sure you use an <span class="caps">ORM</span> that can access your database and store both the confirmation info, and each users’s state.</li>
<li>When the user hits the link you give them and does whatever you need, use the web framework’s <span class="caps">ORM</span> to validate their confirmation.</li>
<li>Once your web framework has validated their confirmation, then change their state <strong>in Lamson’s state</strong> using <strong>your web framework ORM</strong> out of <span class="caps">CONFIRMING</span> and into the next state.</li>
</ol>
<p>Assuming you’re doing this all with Python it should be fairly trivial.</p>
<h2>Confirm Only By Web Is Bad</h2>
<p>I would advise against this method though, since it doesn’t really confirm that
the email address you received worked. One of the purposes of doing a
confirmation email exchange is to make sure that this person can both <strong>send</strong>
and <strong>receive</strong>. If you have to point them at your web site, consider having
this process instead:</p>
<ol>
<li>Their first interaction with your service sends out an email that sends them to a web page, and transitions them to a <code>CONFIRMING</code> state <strong>but do not send them a confirmation reply address from Lamson.</strong> You’ll actually “delay” this until they fill out your web forms.</li>
<li>In your web framework, you have them fill out forms and such, and then send them the <strong>real</strong> confirmation message using Lamson. Since the Confirmation <span class="caps">API</span> is Python you could do this directly in any Python web framework. You’re basically moving the call to <code>CONFRIM.send</code> from your <code>START</code> handler into the web framework.</li>
<li>Then your web framework will have sent them a real confirmation email, not just a link, so when they reply, continue with the usual Lamson confirmation process described above.</li>
</ol>
<p>Doing it this way ends up being a good balance between too many clicks and replies, but too few to confirm that the end user can actually reply
to email you send them.</p>
<h2>The Pending Queue</h2>
<p>You should also notice in the above examples that the original message is
stored in a “pending queue” and then given back to you later. This is handy
for either finishing their original request without further intervention, or
inspecting what they original wanted to do. In the original Librelist code I
would take their first message, confirm them, and then pull it out of the
pending queue to send it on. This turned out to not work because socially
people “subscribe” with a garbage first message, but technically it worked
great.</p>
<blockquote>
<p>You may want to periodically go through this queue and purge any messages that aren’t found in the ConfirmationsStorage. Probably with a simple Python script and a cronjob.</p>
</blockquote>
<h2>Conclusion</h2>
<p>The Lamson confirmation <span class="caps">API</span> encapsulates a pattern for confirming potential
users. Feel free to suggest improvements to the <span class="caps">API</span> if you find further
patterns that are needed.</p>
</div>
<div id="column_left">
<ul class="sidebar_menu">
<li>
<div class="item">
<div class="color" style="background-color: #ff0000;"> </div>
<a href="/blog/">Latest News</a>
</div>
</li>
<li>
<div class="item">
<div class="color" style="background-color: #ff9900;"> </div>
<a href="/download.html">Download the Gear</a>
</div>
</li>
<li>
<div class="item">
<div class="color" style="background-color: #99cc00;"> </div>
<a href="/docs/getting_started.html">Getting Started</a>
</div>
</li>
<li>
<div class="item">
<div class="color" style="background-color: #3399ff;"> </div>
<a href="/docs/">Documentation</a>
</div>
</li>
<li>
<div class="item">
<div class="color" style="background-color: #ff3399;"> </div>
<a href="/docs/faq.html">Frequently Asked Questions</a>
</div>
</li>
<li>
<div class="item">
<div class="color" style="background-color: #006699;"> </div>
<a href="/about.html">About Lamson</a>
</div>
</li>
<li>
<div class="item">
<div class="color" style="background-color: #0099cc;"> </div>
<a href="/contact.html">Getting Help with Lamson</a>
</div>
</li>
</ul>
<div class="sidebar_item">
<h3>Quick Start</h3>
<p>See the download instructions for information on getting lamson, and read the getting started instructions to start your own application in less than 10 minutes.</p>
</div>
<br/>
<div class="sidebar_item">
<h3>Mailing Lists</h3>
<p>Lamson hosts its own <a href="/lists/">mailing lists</a> as well as provides a free open mailing list
service for anyone who needs one. Simply send an email to the list you want @librelist.com and it will
get you started.</p>
</div>
</div>
<div id="footer">
<div class="footer_content">
Lamson Project(TM) and all material on this site Copyright © 2009 <a href="http://zedshaw.com/" title="Zed Shaw's blog">Zed Shaw</a> unless otherwise stated.<br/>
Website Designed by <a href="http://kenkeiter.com/">Kenneth Keitner</a> and donated to the LamsonProject.
</div>
</div>
<!-- end:centered_content -->
</div>
</body>
</html>
|