Added comments to functions

This commit is contained in:
Felipe 2013-07-05 06:54:15 -04:00
parent 8852fd6bf0
commit d33283603d
4 changed files with 14 additions and 1 deletions

View File

@ -14,7 +14,7 @@ pip install python-yubikey
## Register for an API key ## Register for an API key
``` ```
import yubikey from yubikey import Yubikey
yubi = Yubikey() yubi = Yubikey()
yubi.register('<INSERT OTP HERE>') yubi.register('<INSERT OTP HERE>')

5
tests.py Normal file
View File

@ -0,0 +1,5 @@
from yubikey import Yubikey
yubi = Yubikey(123)
yubi.verify('asdadadasdasdadadadadasdasdadsasdadadsadadad')

View File

@ -38,6 +38,7 @@ class YubicoWS(object):
self.api_ws = self._api_ws % choice(self._servers) self.api_ws = self._api_ws % choice(self._servers)
def register_api_key(self, email, otp): def register_api_key(self, email, otp):
"Registers an API Key with the servers"
data = { data = {
'email': email, 'email': email,
'otp': str.lower(otp) 'otp': str.lower(otp)
@ -50,6 +51,7 @@ class YubicoWS(object):
return ws_response return ws_response
def verify(self, yubikey_id, otp, key=None): def verify(self, yubikey_id, otp, key=None):
"Verifies the provided OTP with the server"
endpoint = 'verify' endpoint = 'verify'
url = self.api_ws + endpoint url = self.api_ws + endpoint
@ -86,9 +88,11 @@ class YubicoWS(object):
return ws_response return ws_response
def sign_otp(self, data, key): def sign_otp(self, data, key):
"Signs the OTP with the provided key"
return data return data
def parse_ws_response(self, text): def parse_ws_response(self, text):
"Parses the API key=value response into a dict"
data = {} data = {}
for line in text.strip().split('\n'): for line in text.strip().split('\n'):
key, value = line.split('=', 1) key, value = line.split('=', 1)
@ -96,6 +100,7 @@ class YubicoWS(object):
return data return data
def generate_nonce(self): def generate_nonce(self):
"Generates a random string"
chars = string.ascii_lowercase + string.digits chars = string.ascii_lowercase + string.digits
return ''.join(choice(chars) for x in range(40)) return ''.join(choice(chars) for x in range(40))
@ -115,6 +120,7 @@ class Yubikey(object):
self.key = key self.key = key
def register(self, email, otp): def register(self, email, otp):
"Registers this yubikey"
result = False result = False
if not self.id: if not self.id:
credentials = self.ws.register_api_key(email, otp) credentials = self.ws.register_api_key(email, otp)
@ -126,6 +132,7 @@ class Yubikey(object):
return result return result
def verify(self, otp): def verify(self, otp):
"Verify an OTP to check if its valid"
result = False result = False
if self.id: if self.id:
self.get_prefix(otp) self.get_prefix(otp)
@ -137,6 +144,7 @@ class Yubikey(object):
return result return result
def get_prefix(self, otp): def get_prefix(self, otp):
"Get prefix from an OTP if present"
if len(otp) > 32: if len(otp) > 32:
self.prefix = str.lower(otp[:-32]) self.prefix = str.lower(otp[:-32])

BIN
yubikey.pyc Normal file

Binary file not shown.