<s> '''<STR_LIT>''' <EOL> from flask import abort <EOL> from flask import flash <EOL> from flask import jsonify <EOL> from flask import redirect <EOL> from flask import render_template <EOL> from flask import request <EOL> from flask import url_for <EOL> from flask . ext . login import current_user <EOL> from flask . ext . login import login_required <EOL> from flask . ext . login import login_user <EOL> from flask . ext . login import logout_user <EOL> import requests <EOL> from werkzeug import security <EOL> from markwiki import app <EOL> from markwiki import login_manager <EOL> from markwiki import util <EOL> from markwiki . forms import AddUserForm <EOL> from markwiki . forms import LoginForm <EOL> from markwiki . forms import RegisterForm <EOL> from markwiki . models . user import User <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def administrate ( ) : <EOL> if current_user . name != app . config [ '<STR_LIT>' ] : <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT:index>' ) ) <EOL> return render_template ( '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ login_required <EOL> def add_user ( ) : <EOL> if current_user . name != app . config [ '<STR_LIT>' ] : <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT:index>' ) ) <EOL> form = AddUserForm ( ) <EOL> if form . validate_on_submit ( ) : <EOL> password = util . generate_password ( ) <EOL> pwhash = security . generate_password_hash ( password ) <EOL> user = User ( form . username . data , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:password>' , <EOL> pwhash ) <EOL> app . user_storage . create ( user ) <EOL> return render_template ( '<STR_LIT>' , <EOL> username = form . username . data , <EOL> password = password ) <EOL> return render_template ( '<STR_LIT>' , form = form ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def register ( ) : <EOL> if not app . config [ '<STR_LIT>' ] : <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT:index>' ) ) <EOL> form = RegisterForm ( ) <EOL> if form . validate_on_submit ( ) : <EOL> pwhash = security . generate_password_hash ( form . password . data ) <EOL> user = User ( form . username . data , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:password>' , <EOL> pwhash ) <EOL> app . user_storage . create ( user ) <EOL> login_user ( user ) <EOL> message = ( <EOL> '<STR_LIT>' . format ( <EOL> username = form . username . data ) <EOL> ) <EOL> flash ( message , category = '<STR_LIT:success>' ) <EOL> return redirect ( url_for ( '<STR_LIT:index>' ) ) <EOL> return render_template ( '<STR_LIT>' , form = form ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def login ( ) : <EOL> form = LoginForm ( ) <EOL> if form . validate_on_submit ( ) : <EOL> error = validate_login ( form ) <EOL> if error is None : <EOL> user = app . user_storage . find_by_name ( form . username . data ) <EOL> login_user ( user ) <EOL> flash ( '<STR_LIT>' , category = '<STR_LIT:success>' ) <EOL> return redirect ( form . next . data or url_for ( '<STR_LIT:index>' ) ) <EOL> else : <EOL> flash ( error ) <EOL> return render_template ( '<STR_LIT>' , form = form ) <EOL> def validate_login ( form ) : <EOL> '''<STR_LIT>''' <EOL> user = app . user_storage . find_by_name ( form . username . data ) <EOL> if user is None : <EOL> return '<STR_LIT>' <EOL> if not login_manager . authenticate ( user , form . password . data ) : <EOL> return '<STR_LIT>' <EOL> return None <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> def persona_login ( ) : <EOL> if '<STR_LIT>' not in request . form : <EOL> abort ( <NUM_LIT> ) <EOL> location = app . config [ '<STR_LIT>' ] <EOL> if location is None : <EOL> location = '<STR_LIT::>' . join ( [ '<STR_LIT:localhost>' , str ( app . config [ '<STR_LIT>' ] ) ] ) <EOL> assertion_info = { <EOL> '<STR_LIT>' : request . form [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : location , <EOL> } <EOL> r = requests . post ( '<STR_LIT>' , <EOL> data = assertion_info , verify = True ) <EOL> if not r . ok : <EOL> print ( '<STR_LIT>' ) <EOL> abort ( <NUM_LIT> ) <EOL> data = r . json ( ) <EOL> if data . get ( '<STR_LIT:status>' ) == '<STR_LIT>' : <EOL> user = app . user_storage . find_by_email ( data [ '<STR_LIT:email>' ] ) <EOL> if user is None : <EOL> password = util . generate_password ( ) <EOL> pwhash = security . generate_password_hash ( password ) <EOL> user = User ( data [ '<STR_LIT:email>' ] , <EOL> data [ '<STR_LIT:email>' ] , <EOL> '<STR_LIT>' , <EOL> pwhash ) <EOL> app . user_storage . create ( user ) <EOL> login_user ( user ) <EOL> return jsonify ( { <EOL> '<STR_LIT>' : request . form . get ( '<STR_LIT>' ) <EOL> } ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def logout ( ) : <EOL> logout_user ( ) <EOL> return redirect ( url_for ( '<STR_LIT:index>' ) ) </s>
