本文整理汇总了Python中flaskext.login.LoginManager类的典型用法代码示例。如果您正苦于以下问题:Python LoginManager类的具体用法?Python LoginManager怎么用?Python LoginManager使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了LoginManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: unauthorized_redirect
def unauthorized_redirect(app):
lm = LoginManager()
lm.login_view = "login"
lm.setup_app(app)
res = lm.unauthorized()
assert res.headers["Location"] == "/login?next=%2F"
assert LOGIN_MESSAGE in get_flashed_messages()
示例2: unauthorized_callback
def unauthorized_callback(app):
lm = LoginManager()
lm.login_view = "login"
@lm.unauthorized_handler
def unauth():
return "UNAUTHORIZED!"
lm.setup_app(app)
assert lm.unauthorized() == "UNAUTHORIZED!"
assert len(get_flashed_messages()) == 0
示例3: configure_login_manager
def configure_login_manager(app):
login_manager = LoginManager()
login_manager.setup_app(app)
login_manager.login_view = "login"
login_manager.login_message = u"You must login!"
@login_manager.user_loader
def load_user(user_id):
beaker_session = request.environ['beaker.session']
beaker_user_id = beaker_session['user_id']
#user = User()
#user.id = beaker_user_id #use user_id in beaker!
return user
示例4: create_users_blueprint
def create_users_blueprint(app, _db, login_redirect_view='main'):
global db
db = _db
from .models import User
from .forms import LoginForm, RegisterForm
users_blueprint = Blueprint('users', __name__, template_folder='templates', static_folder='static')
bcrypt = Bcrypt(app)
login_manager = LoginManager()
login_manager.setup_app(app)
login_manager.login_view = 'users.login'
@login_manager.user_loader
def user_loader(user_id):
return User.query.get(user_id)
@users_blueprint.route('/login/', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data).first()
if user is None or bcrypt.check_password_hash(user.password, form.password):
flash("Invalid email or password", "error")
else:
login_user(user)
flash("Login successful", 'success')
return redirect(url_for(login_redirect_view))
return render_template('users/login.html', form=form)
@users_blueprint.route('/logout/')
@login_required
def logout():
logout_user()
return redirect(url_for(login_redirect_view))
@users_blueprint.route('/register/', methods=['GET', 'POST'])
def register():
form = RegisterForm()
if form.validate_on_submit():
user = User(form.email.data, bcrypt.generate_password_hash(form.password.data))
db.session.add(user)
db.session.commit()
login_user(user)
flash("Login successful")
return redirect(url_for(login_redirect_view))
return render_template('users/register.html', form=form)
return users_blueprint
示例5: login_message
def login_message(app):
lm = LoginManager()
lm.login_view = "login"
lm.login_message = u"Log in or the owl will eat you."
lm.setup_app(app)
lm.unauthorized()
assert u"Log in or the owl will eat you." in get_flashed_messages()
示例6: init_app
def init_app(self, app):
if app is None:
return
blueprint = Blueprint(AUTH_CONFIG_KEY.lower(), __name__)
config = default_config.copy()
try:
config.update(app.config.get(AUTH_CONFIG_KEY, {}))
except:
pass
app.config[AUTH_CONFIG_KEY] = config
app.logger.debug("Auth Configuration: %s" % config)
# setup the login manager extension
login_manager = LoginManager()
login_manager.anonymous_user = Anonymous
login_manager.login_view = config[LOGIN_VIEW_KEY]
login_manager.setup_app(app)
# get some things form the config
Provider = get_class_from_config(AUTH_PROVIDER_KEY, config)
Encryptor = get_class_from_config(PASSWORD_ENCRYPTOR_KEY, config)
Form = get_class_from_config(LOGIN_FORM_CLASS_KEY, config)
# create the service and auth provider and add it to the app
# so it can be referenced elsewhere
app.login_manager = login_manager
app.password_encryptor = Encryptor(config[SALT_KEY])
app.auth_provider = Provider(Form)
DEBUG_LOGIN = "User %s logged in. Redirecting to: %s"
ERROR_LOGIN = "Unsuccessful auth attempt: %s. Redirecting to: %s"
DEBUG_LOGOUT = "User logged out, redirecting to: %s"
FLASH_INACTIVE = "Inactive user"
@login_manager.user_loader
def load_user(id):
try:
return user_service.get_user_with_id(id)
except Exception, e:
current_app.logger.error("Error getting user: %s" % e)
return None
示例7: setup_interactive
def setup_interactive(app):
lm = LoginManager()
lm.login_view = "login"
lm.user_loader(get_user)
@lm.unauthorized_handler
def unauth():
return "UNAUTHORIZED!"
lm.setup_app(app)
示例8: logging_in
def logging_in(app):
lm = LoginManager()
lm.login_view = "login"
lm.user_loader(get_user)
lm.setup_app(app)
app.preprocess_request()
assert not current_user.is_authenticated()
assert current_user.is_anonymous()
with assert_fired(user_logged_in):
login_user(notch)
assert current_user.name == u"Notch"
assert session["user_id"] == u"1"
示例9: page_not_found
########################## Error Handlers ##########################
@app.errorhandler(404)
def page_not_found(e):
return render_template('cms_404.html'), 404
@app.errorhandler(500)
def page_not_found(e):
return render_template('cms_500.html'), 500
########################## User Authentication and Registration ##########################
login_manager = LoginManager()
login_manager.setup_app(app)
login_manager.login_view = "/login/"
login_manager.login_message = u"You need to login to access this resource."
class User(UserMixin):
def __init__(self, email, key_id):
self.name = email
self.id = key_id
self.user = db.get(self.id)
def is_active(self):
if self.user.active == True:
return True
else:
示例10: LoginManager
import json
from urllib import urlencode
from urlparse import parse_qs, parse_qsl, urlsplit, urlunsplit
from flask import redirect, request, session, url_for
from flaskext.login import LoginManager, login_user, logout_user
import oauth2
import requests
from makerbase import app
from makerbase.models import User
login_manager = LoginManager()
login_manager.setup_app(app, add_context_processor=True)
login_manager.user_loader(User.get)
@app.route('/signin/github')
def signin_github():
try:
next_url = request.args['next']
except KeyError:
pass
else:
session['signin_next_url'] = next_url
urlparts = urlsplit(request.base_url)
params = {
'client_id': app.config['GITHUB_CLIENT_ID'],
'redirect_url': urlunsplit((urlparts.scheme, urlparts.netloc, url_for('complete_github'), None, None)),
示例11: Flask
from flask import Flask, redirect, request, url_for
www = Flask(__name__)
www.config.from_pyfile('../config.cfg')
from flaskext.login import LoginManager, current_user
login = LoginManager()
login.setup_app(www, add_context_processor=True)
login.login_view = 'login'
from flaskext.sqlalchemy import SQLAlchemy
db = SQLAlchemy(www)
from flaskext.mail import Mail
mail = Mail(www)
from flaskext.bcrypt import Bcrypt
bcrypt = Bcrypt(www)
from www import tweaks
from www import main
from www import user
from www.menu import register
from werkzeug.utils import import_string
from flask.ext import admin
from flask.ext.admin.datastore.sqlalchemy import SQLAlchemyDatastore
'''
example config line:
WWW_BLUEPRINTS=[('www.blog',{'url_prefix': '/blog'}, ('blog', '/blog')),\
('www.contact',{'url_prefix': '/contact'}, ('contact', '/contact')]
'''
示例12: Blueprint
ASC = pymongo.ASCENDING
import flask
from functools import wraps
from base import app, getDBConnection
from flask import render_template, request, abort, Blueprint, url_for, make_response
from flaskext.login import login_required, login_user, current_user, logout_user
login_page = Blueprint("users", __name__, template_folder='templates')
import utils
logger = utils.make_logger(login_page)
import re
allowed_usernames = re.compile("^[a-zA-Z0-9._-]+$")
from flaskext.login import LoginManager
login_manager = LoginManager()
import pwdmanager
from pwdmanager import LmfdbUser, LmfdbAnonymousUser
# TODO update this url, needed for the user login token
base_url = "http://www.l-functions.org"
@login_manager.user_loader
def load_user(userid):
from pwdmanager import LmfdbUser
return LmfdbUser(userid)
login_manager.login_view = "users.info"
# this anonymous user has the is_admin() method
示例13: LoginManager
import hashlib
from flaskext.login import LoginManager
from model.models import User
login_manager = LoginManager()
login_manager.login_view = "login"
@login_manager.user_loader
def load_user(user_id):
"""Used by Flask-Login to get a user by its id."""
try:
ret = User.query.get(user_id)
ret.authenticated = True
except Exception as ex:
print type(ex)
print ex
ret = None
return ret
def check_login(username, password):
"""Checks whether or not the given username and password are valid."""
user = User.query.filter(User.username ==
username).first()
if user:
hasher = hashlib.sha1()
salt, pwd = user.password.split('$', 2)
hasher.update(salt + password)
if hasher.hexdigest() == pwd:
user.authenticated = True
示例14: Flask
import datetime
from bson.objectid import ObjectId
from flask import Flask, render_template, flash, redirect, request, session
from flaskext.login import LoginManager, login_required, login_user, logout_user
import settings
from db import users, Comic, User, Bookmark
from forms import LoginForm
from mongoengine import *
app = Flask(__name__)
app.secret_key = settings.SECRET_KEY
login_manager = LoginManager()
login_manager.login_view = "login"
login_manager.session_protection = None
login_manager.setup_app(app)
@login_manager.user_loader
def load_user(userid):
result = users.find_one({'_id': ObjectId(userid)})
if result:
user = User(result)
return user
#views
@app.route('/')
@login_required
示例15: lm_creation_and_setup
def lm_creation_and_setup(app):
lm = LoginManager()
lm.setup_app(app)
assert app.login_manager is lm
assert lm._load_user in app.before_request_funcs[None]
assert lm._update_remember_cookie in app.after_request_funcs[None]