<s> import os <EOL> import base64 <EOL> from flask import Flask , render_template , redirect , url_for , flash , session , abort <EOL> from werkzeug . security import generate_password_hash , check_password_hash <EOL> from flask . ext . sqlalchemy import SQLAlchemy <EOL> from flask . ext . login import LoginManager , UserMixin , login_user , logout_user , current_user <EOL> from flask . ext . bootstrap import Bootstrap <EOL> from flask . ext . wtf import Form <EOL> from wtforms import StringField , PasswordField , SubmitField , RadioField <EOL> from wtforms . validators import Required , Length , EqualTo <EOL> from random import randint <EOL> from twilio . rest import TwilioRestClient <EOL> import twilio <EOL> app = Flask ( __name__ ) <EOL> app . config . from_object ( '<STR_LIT>' ) <EOL> bootstrap = Bootstrap ( app ) <EOL> db = SQLAlchemy ( app ) <EOL> lm = LoginManager ( app ) <EOL> def sendToken ( username , token ) : <EOL> user = User . query . filter_by ( username = username ) . first ( ) <EOL> client = TwilioRestClient ( ) <EOL> if user . method == '<STR_LIT>' : <EOL> try : <EOL> message = client . messages . create ( <EOL> body = "<STR_LIT>" + str ( token ) , <EOL> to = user . phone , <EOL> from_ = app . config [ '<STR_LIT>' ] , <EOL> ) <EOL> flash ( '<STR_LIT>' ) <EOL> except twilio . TwilioRestException as e : <EOL> print e <EOL> flash ( u'<STR_LIT>' , '<STR_LIT:error>' ) <EOL> elif user . method == '<STR_LIT>' : <EOL> try : <EOL> call = client . calls . create ( to = user . phone , from_ = app . config [ '<STR_LIT>' ] , <EOL> url = "<STR_LIT>" + str ( token ) + "<STR_LIT:&>" ) <EOL> flash ( '<STR_LIT>' ) <EOL> except twilio . TwilioRestException as e : <EOL> print e <EOL> flash ( u'<STR_LIT>' , '<STR_LIT:error>' ) <EOL> def generateToken ( ) : <EOL> return "<STR_LIT>" <EOL> class User ( UserMixin , db . Model ) : <EOL> """<STR_LIT>""" <EOL> __tablename__ = '<STR_LIT>' <EOL> id = db . Column ( db . Integer , primary_key = True ) <EOL> username = db . Column ( db . String ( <NUM_LIT:64> ) , index = True ) <EOL> password_hash = db . Column ( db . String ( <NUM_LIT> ) ) <EOL> phone = db . Column ( db . String ( <NUM_LIT:64> ) ) <EOL> method = db . Column ( db . String ( <NUM_LIT:16> ) ) <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( User , self ) . __init__ ( ** kwargs ) <EOL> @ property <EOL> def password ( self ) : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> @ password . setter <EOL> def password ( self , password ) : <EOL> self . password_hash = generate_password_hash ( password ) <EOL> def verify_password ( self , password ) : <EOL> return check_password_hash ( self . password_hash , password ) <EOL> @ lm . user_loader <EOL> def load_user ( user_id ) : <EOL> """<STR_LIT>""" <EOL> return User . query . get ( int ( user_id ) ) <EOL> class RegisterForm ( Form ) : <EOL> """<STR_LIT>""" <EOL> username = StringField ( '<STR_LIT>' , validators = [ Required ( ) , Length ( <NUM_LIT:1> , <NUM_LIT:64> ) ] ) <EOL> password = PasswordField ( '<STR_LIT>' , validators = [ Required ( ) ] ) <EOL> password_again = PasswordField ( '<STR_LIT>' , <EOL> validators = [ Required ( ) , EqualTo ( '<STR_LIT:password>' ) ] ) <EOL> phone = StringField ( '<STR_LIT>' , validators = [ Required ( ) , Length ( <NUM_LIT:1> , <NUM_LIT:64> ) ] ) <EOL> method = RadioField ( '<STR_LIT>' , choices = [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> submit = SubmitField ( '<STR_LIT>' ) <EOL> class LoginForm ( Form ) : <EOL> """<STR_LIT>""" <EOL> username = StringField ( '<STR_LIT>' , validators = [ Required ( ) , Length ( <NUM_LIT:1> , <NUM_LIT:64> ) ] ) <EOL> password = PasswordField ( '<STR_LIT>' , validators = [ Required ( ) ] ) <EOL> submit = SubmitField ( '<STR_LIT>' ) <EOL> class TwoFactorForm ( Form ) : <EOL> """<STR_LIT>""" <EOL> token = StringField ( '<STR_LIT>' , validators = [ Required ( ) , Length ( <NUM_LIT:6> , <NUM_LIT:6> ) ] ) <EOL> submit = SubmitField ( '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT:/>' ) <EOL> def index ( ) : <EOL> return render_template ( '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def voice ( ) : <EOL> return render_template ( '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def register ( ) : <EOL> """<STR_LIT>""" <EOL> if current_user . is_authenticated ( ) : <EOL> return redirect ( url_for ( '<STR_LIT:index>' ) ) <EOL> form = RegisterForm ( ) <EOL> if form . validate_on_submit ( ) : <EOL> user = User . query . filter_by ( username = form . username . data ) . first ( ) <EOL> if user is not None : <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> user = User ( username = form . username . data , password = form . password . data , phone = form . phone . data , method = form . method . data ) <EOL> db . session . add ( user ) <EOL> db . session . commit ( ) <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 verification ( ) : <EOL> """<STR_LIT>""" <EOL> if current_user . is_authenticated ( ) : <EOL> return redirect ( url_for ( '<STR_LIT:index>' ) ) <EOL> form = TwoFactorForm ( ) <EOL> if form . validate_on_submit ( ) : <EOL> user = User . query . filter_by ( username = session [ '<STR_LIT:username>' ] ) . first ( ) <EOL> if session [ '<STR_LIT>' ] == form . token . data : <EOL> login_user ( user ) <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT:index>' ) ) <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> return render_template ( '<STR_LIT>' , form = form ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def login ( ) : <EOL> """<STR_LIT>""" <EOL> if current_user . is_authenticated ( ) : <EOL> return redirect ( url_for ( '<STR_LIT:index>' ) ) <EOL> form = LoginForm ( ) <EOL> if form . validate_on_submit ( ) : <EOL> user = User . query . filter_by ( username = form . username . data ) . first ( ) <EOL> if user is None or not user . verify_password ( form . password . data ) : <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> session [ '<STR_LIT:username>' ] = form . username . data <EOL> session [ '<STR_LIT>' ] = generateToken ( ) <EOL> sendToken ( session [ '<STR_LIT:username>' ] , session [ '<STR_LIT>' ] ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> return render_template ( '<STR_LIT>' , form = form ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def logout ( ) : <EOL> """<STR_LIT>""" <EOL> logout_user ( ) <EOL> return redirect ( url_for ( '<STR_LIT:index>' ) ) <EOL> db . create_all ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> app . run ( host = '<STR_LIT>' , debug = True ) </s>
