Skip to content

Instantly share code, notes, and snippets.

@lalabuy948
Created August 13, 2019 12:55
Show Gist options
  • Save lalabuy948/d025bba971719c77fffbc9bcd856d3e6 to your computer and use it in GitHub Desktop.
Save lalabuy948/d025bba971719c77fffbc9bcd856d3e6 to your computer and use it in GitHub Desktop.
Tornado basic auth
# https://pypi.org/project/tornado-http-auth/
import tornado.ioloop
from tornado.web import RequestHandler, Application
from tornado_http_auth import DigestAuthMixin, BasicAuthMixin, auth_required
credentials = {'user1': 'pass1'}
# Example 1 (using decorator).
class MainHandler(DigestAuthMixin, RequestHandler):
@auth_required(realm='Protected', auth_func=credentials.get)
def get(self):
self.write('Hello %s' % self._current_user)
# Example 2 (using prepare and get_authentciated_user).
class MainHandler(BasicAuthMixin, RequestHandler):
def prepare(self):
self.get_authenticated_user(check_credentials_func=credentials.get, realm='Protected')
def get(self):
self.write('Hello %s' % self._current_user)
app = Application([
(r'/', MainHandler),
])
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment