Using Moth to Authenticate Users by Email¶
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 | #!/usr/bin/env python
# THIS CODE HAS NOT BEEN TESTED AND MAY NOT WORK.
# EVEN IF IT DOES, YOU SHOULDN'T RUN IT.
# IT IS INTENDED TO BE A GUIDE, NOT AN IMPLEMENTATION.
from base64 import b64decode, b64encode
from smtplib import SMTP
from tornado.web import RequestHandler
from moth import Moth
class LoginHandler(RequestHandler):
moth = Moth()
def get(self):
x = self.get_argument('x', '')
if x == '':
self.write('''<html><body><form method=POST>Enter your email: <input
type=email><input type=submit></form></body></html>''')
else:
email, token = b64decode(x).split('&')
email = email.split('=')[1]
token = token.split('=')[1]
if self.moth.auth_token(email=email, token=token) == True:
self.set_cookie('email', email)
self.redirect('/dashboard')
else:
self.redirect('/login')
def post(self):
email = self.get_argument('email')
fromaddr = "noreply@moth.com"
token = self.moth.create_token(email=email, expire=1)
auth_string = b64encode("user=%s&token=%s" % (email, token))
login_url = "https://login.moth.com/auth?x=%s" % auth_string
message = "From: %s\r\nTo: %s\r\n\r\nclick to log in:\n%s" % \
(fromaddr, email, user['fname'], login_url)
mail_server = SMTP('localhost')
mail_server.sendmail(fromaddr, email, message)
self.write('You should be receiving a login link shortly')
|
Using Moth to Authenticate Sessions¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #!/usr/bin/env python
# THIS CODE HAS NOT BEEN TESTED AND MAY NOT WORK.
# EVEN IF IT DOES, YOU SHOULDN'T RUN IT.
# IT IS INTENDED TO BE A GUIDE, NOT AN IMPLEMENTATION.
from tornado.web import RequestHandler
from moth import Moth
class BaseHandler(RequestHandler):
def get_current_user(self):
email = self.get_cookie('email', '')
session_token = self.get_cookie('session', '')
if email == '' or session == '':
return False
return Moth().auth_token(email=email, token=session_token)
|
Making Asynchronous Calls with AsyncMoth¶
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 | #!/usr/bin/env python
import logging
from tornado import gen
from tornado.ioloop import IOLoop
from tornado.web import Application, RequestHandler
from moth import AsyncMoth
class ExampleHandler(RequestHandler):
@gen.coroutine
def get(self):
email = 'ch@rlesthom.as'
want_retval = 'test retval'
moth = server.settings['moth']
### create_token ###
self.write('test create_token...<br>')
token = yield moth.create_token(email, retval=want_retval)
self.write(token + '<br>')
### auth_token ###
self.write('test auth_token...<br>')
have_retval = yield moth.auth_token(email, token)
self.write('"%s" should match "%s"<br>' % (want_retval, have_retval))
### auth_token for invalid email ###
self.write('test bad auth...<br>')
should_be_False = yield moth.auth_token('fake@f.com', token)
self.write('"%s" should be False<br>' % should_be_False)
### remove_token ###
self.write('test remove_token...<br>')
yield moth.remove_token(email, token)
### remove_user ###
self.write('test remove_user...<br>')
yield moth.remove_user(email)
self.finish()
server = Application([('/', ExampleHandler)], debug=True)
server.settings['moth'] = AsyncMoth('moth_test')
server.listen(9000)
IOLoop.instance().start()
|