Commits

Thejesh GN committed a1ebef3

added validate and qrCodeURL methods

Comments (0)

Files changed (2)

 Welcome to g2fa. Google two factor auth for Python based apps.
 
 Repo:
-$hg clone http://code.thejeshgn.com/g2fa
+$hg clone http://code.thejeshgn.com/py-g2fa
 
 
 ### usage:
 
 
 def decode(data):
-	#return base64.b32decode(data)		
 	data 	= data.upper()
 	n	= 0
 	j	= 0
 	    (ord(hash[offset+3]) & 0xff)
 	) % pow(10, length)
 	
+ #Verifys a user inputted key against the current timestamp. Checks window
+def validate(b32secretKey, userOTP, window = 4):
+	timeStamp = timestamp()
+	timeStampInInt = int(timeStamp)
+	binarySeed = decode(b32secretKey)
+	timestampRange = range(timeStampInInt-window, timeStampInInt+window)
+	for ts in timestampRange:
+		o = otp(binarySeed, ts)
+		#print o
+		if int(o) == int(userOTP):
+			return True
+	return False
+
+def qrCodeURL(site, secretKey):
+	return "http://chart.apis.google.com/chart?cht=qr&chs=300x300&chl=otpauth://totp/"+site+"?secret="+secretKey+"&chld=H|0"
+
 
 #This is for testing only
 if __name__ == "__main__":
+	YOUR_SECRET_INITIAL_KEY = "KKK67SDNLXIOG65U"   # must be at least 16 base 32 characters, keep this secret
+	print qrCodeURL("http://g2fa-example.com", YOUR_SECRET_INITIAL_KEY)
+	userOTP = raw_input("Enter OTP: ")
 	TIME_STAMP = timestamp()
-	YOUR_SECRET_INITIAL_KEY = "KKK67SDNLXIOG65U"   # must be at least 16 base 32 characters, keep this secret
-	SECRET_KEY 	  = decode(YOUR_SECRET_INITIAL_KEY)
-	o =  otp(SECRET_KEY, TIME_STAMP)
-	print "TESTING PURPOSE"
-	print "TIME_STAMP ="+str(TIME_STAMP)
-	print "OTP ="+str(o)
-	YOUR_SITE = "g2fa-example.com"
-	print "http://chart.apis.google.com/chart?cht=qr&chs=300x300&chl=otpauth://totp/"+YOUR_SITE+"?secret="+YOUR_SECRET_INITIAL_KEY+"&chld=H|0"
+	print validate(YOUR_SECRET_INITIAL_KEY, int(userOTP), 4)	
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.