def register(): if request.method == 'POST': print 'Username: '******'Username'] print 'Password: '******'Password'] # Connect to database db = sqlite3.connect('google_authenticator.db') cursor = db.cursor() # Create secret and add user to database secret = base64.b32encode(os.urandom(10)).decode('utf-8') query = 'INSERT INTO USERS (USER, PASSWORD, GOOGLEAUTH) VALUES (\"' + request.form['Username'] + '\",\"' + request.form['Password'] + '\",\"' + secret + '\");' cursor.execute(query) db.commit() db.close() # Create unique QR code given secret, label, and issuer auth = OtpAuth(secret) secret_uri = auth.to_uri('totp', GALabel, GAIssuer) qr = QRCode() qr.add_data(secret_uri) qr.make() img = qr.make_image() #img.show() # Opens tmp QR code image print 'Secret: ', secret print 'Secret Uri: ', secret_uri # Display QR code in web browser return redirect(getQRCodeGoogleUrl(secret_uri)) return "Nothing to see here."
def test_to_uri_totp(f_provider,f_authKey): print "\n2.b test to uri totp" auth = OtpAuth(f_provider) expect = 'otpauth://totp/'+f_provider+'?secret='+f_authKey+'&issuer=' + f_provider assert auth.to_uri('totp', 'python', 'python') == expect print "provider <%s> authKey <%s> secret <%s> to_uri <%s>" % (f_provider, f_authKey, auth.secret, expect) #print dir(auth) print "completed successfully"
def create_tsa(username, user_hash): print str(user_hash) auth = OtpAuth(str(user_hash)) # a secret string s = auth.to_uri('totp', 'User:'******'Kuorra') img = qrcode.make(s) f = open("static/qr/output.png", "wb") img.save(f) f.close()
def test_to_uri_hotp(f_provider,f_authKey): print "\n3.b test to google hotp" auth = OtpAuth(f_provider) expect= 'otpauth://hotp/'+f_provider+'?secret=' + f_authKey + '&issuer='+f_provider+'&counter=4' assert auth.to_uri('hotp', 'python', 'python', 4) == expect print "provider <%s> authKey <%s> secret <%s> to_uri <%s>" % (f_provider, f_authKey, auth.secret, expect) #print auth.secret #print dir(auth.totp) print "completed successfully"
def nieuwe_gebruiker(): global db_auth Random = str( (''.join(random.choice('ABDJFHE34543234') for _ in range(16)))) db_auth = Random print(Random) auth = OtpAuth(Random) # Moet 16 lang zijn s = auth.to_uri('totp', 'Jelle Huisman', 'NS Fietsenstalling') import qrcode img = qrcode.make(s) img.show()
def send_email(orm, user, reset_url, mail_type): """ mail_type: register ro resetpw """ secret = user.secret if mail_type=="register" else user.resetpw logger.debug(secret) auth = OtpAuth(secret) uri = auth.to_uri('totp', user.email, 'userga') qrurl = "?".join(['https://chart.googleapis.com/chart', urlencode({'cht': 'qr', 'chs': '200x200', 'chl': uri})]) logger.debug(qrurl) logger.debug(mail_type) sendmail(user.email, secret, uri, qrurl, reset_url, mail_type)
def nieuwe_gebruiker(): global db_auth Random = str( (''.join(random.choice('ABDJFHE34543234') for _ in range(16)))) db_auth = Random print('test' + db_auth) auth = OtpAuth(Random) s = auth.to_uri('totp', 'NS', 'NS Fietsenstalling') qr = QRCode(version=1, error_correction=ERROR_CORRECT_L) qr.add_data(s) qr.make() img = qr.make_image() img.save("qrcode.png") return db_auth
def create_qr(self): id = str(self.id) auth = OtpAuth(app.config['SECRET_KEY'] + id) # a secret string email = self.email s = auth.to_uri('totp', email, 'Unit963') qr = qrcode.QRCode( version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=15, border=5, ) qr.add_data(s) img = qr.make_image(fill_color="#05528a", back_color="white") img.save('./MFA/static/QR/' + id + '.png')
def login(): if request.method == 'POST': print 'Username: '******'Username'] print 'Password: '******'Password'] print 'Google Auth Code: ', request.form['GoogleAuth'] # Connect to database and query for user&password db = sqlite3.connect('google_authenticator.db') cursor = db.cursor() cursor.execute('SELECT GOOGLEAUTH FROM USERS WHERE USER=\'' + request.form['Username'] + '\' AND PASSWORD=\'' + request.form['Password'] + '\';') secret = cursor.fetchone() db.close() # Query returns None if user&password don't exist if secret is None: return "Unsuccesful login attempt." # Verify google authentication code with secret from database else: # Generate the otpauth protocal string. secret = secret[0] print 'Secret: ', secret auth = OtpAuth(secret) secret_uri = auth.to_uri('totp', GALabel, GAIssuer) # algorithm type, label, issuer # Generate TOTP code given code uri code = auth.totp() # Generate time based code print 'Code Uri: ', secret_uri print 'Valid Google Auth Code: ', code # Compare code provided by user with valid code if auth.valid_totp(int(request.form['GoogleAuth'])): return "Successfully logged in!" else: print "Invalid Google Authenticator." return "Unsuccessful login attempt." return "Unsuccessful login attempt." return "Nothing to see here."
c = connect.cursor() def create_table(): c.execute( 'CREATE TABLE IF NOT EXISTS Fietsenstalling (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, Name TEXT, Adress TEXT, FietsNr INTEGER, PIN INTEGER, otpKEY TEXT)' ) #auth = OtpAuth('JBSWY3DPEHPK3PXP') # a secret string #auth.hotp() # generate a count based code, default count is 4 #auth.valid_hotp(330810) #auth.hotp(2) # generate a count based code, count is 2 #auth.valid_hotp(720111) #print(auth.totp()) # generate a time based code #print(auth.valid_totp(157930)) auth = OtpAuth('JBSWY3DPEHPK3PXP') # Moet 16 lang zijn s = auth.to_uri('totp', 'Jelle Huisman', 'NS Fietsenstalling') import qrcode img = qrcode.make(s) #img.show() controle = auth.valid_totp(int(input('Voer code in'))) print(controle) if controle == True: print('Code geaccepteerd') else: print('Helaas de code is onjuist')
def generate_qr_code(totp, username): otpa = OtpAuth(totp.secret) uri = otpa.to_uri( 'totp', 'BobCraft:{0}'.format(username), 'BobCraft') return qrcode.make(uri)