def post(self): """Attempts to create an account with the credentials provided in the post arguments. Successful creation logs the user in and sends them to '/'. Failure returns the user to the account create screen and tells them what went wrong. """ username = self.get_argument("username") password = self.get_argument("password").encode("utf-8") email = self.get_argument("email") try: now = curtime() username = username.lower() email = email.strip().lower() user = User({"username": username, "email": email, "date_joined": now}) user.set_password(password) user.validate() save_user(self.db_conn, user) except Exception, e: logging.error("Credentials failed") logging.error(e) return self.render_template("accounts/create.html")
def post(self): """Attempts to create an account with the credentials provided in the post arguments. Successful creation logs the user in and sends them to '/'. Failure returns the user to the account create screen and tells them what went wrong. """ username = self.get_argument('username') password = self.get_argument('password').encode('utf-8') email = self.get_argument('email') try: now = curtime() username = username.lower() email = email.strip().lower() user = User({ 'username': username, 'email': email, 'date_joined': now }) user.set_password(password) user.validate() save_user(self.db_conn, user) except Exception, e: logging.error('Credentials failed') logging.error(e) return self.render_template('accounts/create.html')
def load_user(db, username=None, email=None): """Loads a user document from MongoDB. """ query_dict = dict() if username: query_dict['username'] = '******' % (username.lower()) elif email: query_dict['email'] = email.lower() else: raise ValueError('Username or email field required') user_dict = db[USER_COLLECTION].find_one(query_dict) # In most cases, the python representation of the data is returned. User # documents are instantiated to provide access to commonly needed User # functions if user_dict is None: return None else: u = User(**user_dict) return u
def post(self): """Attempts to create an account with the credentials provided in the post arguments. Successful creation logs the user in and sends them to '/'. Failure returns the user to the account create screen and tells them what went wrong. """ username = self.get_argument('username') password = self.get_argument('password') email = self.get_argument('email') try: u = User.create_user(username, password, email) u.validate() save_user(self.db_conn, u) except Exception, e: logging.error('Credentials failed') logging.error(e) return self.render_template('accounts/create.html')
#!/usr/bin/env python from brubeck.request_handling import Brubeck, WebMessageHandler from brubeck.models import User from brubeck.auth import web_authenticated, UserHandlingMixin from brubeck.templating import Jinja2Rendering, load_jinja2_env from brubeck.connections import Mongrel2Connection import sys import logging ### ### Hardcoded authentication ### demo_user = User.create_user('jd', 'foo') class CustomAuthMixin(WebMessageHandler, UserHandlingMixin): """This Mixin provides a `get_current_user` implementation that validates auth against our hardcoded user: `demo_user` """ def get_current_user(self): """Attempts to load user information from cookie. If that fails, it looks for credentials as arguments. If then attempts auth with the found credentials. """ # Try loading credentials from cookie username = self.get_cookie('username') password = self.get_cookie('password') # Fall back to args if cookie isn't provided
#!/usr/bin/env python import sys import logging from brubeck.request_handling import Brubeck, WebMessageHandler from brubeck.models import User from brubeck.auth import authenticated, UserHandlingMixin ### Hardcode a user for the demo demo_user = User.create_user('jd', 'foo') class DemoHandler(WebMessageHandler, UserHandlingMixin): def get_current_user(self): """Attempts to load authentication credentials from a request and validate them. Returns an instantiated User if credentials were good. `get_current_user` is a callback triggered by decorating a function with @authenticated. """ username = self.get_argument('username') password = self.get_argument('password') if demo_user.username != username: logging.error('Auth fail: username incorrect') return if not demo_user.check_password(password): logging.error('Auth fail: password incorrect') return
#!/usr/bin/env python import sys import logging from brubeck.request_handling import Brubeck, WebMessageHandler from brubeck.models import User from brubeck.auth import web_authenticated, UserHandlingMixin from brubeck.templating import Jinja2Rendering, load_jinja2_env ### ### Hardcoded authentication ### demo_user = User.create_user("jd", "foo") class CustomAuthMixin(WebMessageHandler, UserHandlingMixin): """This Mixin provides a `get_current_user` implementation that validates auth against our hardcoded user: `demo_user` """ def get_current_user(self): """Attempts to load user information from cookie. If that fails, it looks for credentials as arguments. If then attempts auth with the found credentials. """ # Try loading credentials from cookie username = self.get_cookie("username") password = self.get_cookie("password")