<s> import urllib <EOL> from StringIO import StringIO <EOL> from base64 import b32encode <EOL> from flask import abort <EOL> from flask import flash <EOL> from flask import make_response <EOL> from flask import render_template <EOL> from flask import redirect <EOL> from flask import request <EOL> from flask import send_file <EOL> from flask import url_for <EOL> from flask . views import View <EOL> from flaskwallet import session <EOL> from settingsapp . models import Setting <EOL> import qrcode <EOL> from Crypto . Random import get_random_bytes <EOL> class OTPSetupView ( View ) : <EOL> def dispatch_request ( self ) : <EOL> return render_template ( '<STR_LIT>' ) <EOL> class QRView ( View ) : <EOL> """<STR_LIT>""" <EOL> def dispatch_request ( self ) : <EOL> secret = self . get_secret ( ) <EOL> if secret : <EOL> return send_file ( self . get_qr ( secret ) , mimetype = '<STR_LIT>' ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> def get_secret ( self ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> setting = session . query ( Setting ) . get ( name ) <EOL> if setting : <EOL> ret = False <EOL> else : <EOL> secret = get_random_bytes ( <NUM_LIT:10> ) <EOL> code = b32encode ( secret ) <EOL> setting = Setting ( name , code ) <EOL> session . add ( setting ) <EOL> session . commit ( ) <EOL> ret = code <EOL> return ret <EOL> def get_qr ( self , secret ) : <EOL> img_io = StringIO ( ) <EOL> qr = self . build_qr_image ( secret ) <EOL> qr . save ( img_io , '<STR_LIT>' ) <EOL> img_io . seek ( <NUM_LIT:0> ) <EOL> return img_io <EOL> def build_qr_image ( self , secret ) : <EOL> qr = qrcode . QRCode ( <EOL> version = <NUM_LIT:1> , <EOL> error_correction = qrcode . constants . ERROR_CORRECT_L , <EOL> box_size = <NUM_LIT:10> , <EOL> border = <NUM_LIT:4> , <EOL> ) <EOL> qr . add_data ( self . get_authenticator_url ( secret ) ) <EOL> qr . make ( fit = True ) <EOL> return qr . make_image ( ) <EOL> def get_authenticator_url ( self , secret ) : <EOL> """<STR_LIT>""" <EOL> user = '<STR_LIT>' <EOL> data = '<STR_LIT>' % ( user , secret , <EOL> '<STR_LIT>' ) <EOL> return data </s>
