本文整理汇总了Python中linotp.lib.HMAC.HmacOtp.generate方法的典型用法代码示例。如果您正苦于以下问题:Python HmacOtp.generate方法的具体用法?Python HmacOtp.generate怎么用?Python HmacOtp.generate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类linotp.lib.HMAC.HmacOtp
的用法示例。
在下文中一共展示了HmacOtp.generate方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getNextOtp
# 需要导入模块: from linotp.lib.HMAC import HmacOtp [as 别名]
# 或者: from linotp.lib.HMAC.HmacOtp import generate [as 别名]
def getNextOtp(self):
'''
access the nex validf otp
:return: otpval
:rtype: string
'''
log.debug("[getNextOtp] begin. starting to look for the next otp")
try:
### TODO - replace tokenLen
otplen = int(self.token.LinOtpOtpLen)
except ValueError as ex:
log.error("[getNextOtp] ValueError %r" % ex)
raise Exception(ex)
secret_obj = self.token.getHOtpKey()
counter = self.token.getOtpCounter()
#log.debug("serial: %s",serialNum)
hmac2otp = HmacOtp(secret_obj, counter, otplen)
nextotp = hmac2otp.generate(counter + 1)
log.debug("[getNextOtp] end. got the next otp value: nextOtp %r"
% nextotp)
return nextotp
示例2: getOtp
# 需要导入模块: from linotp.lib.HMAC import HmacOtp [as 别名]
# 或者: from linotp.lib.HMAC.HmacOtp import generate [as 别名]
def getOtp(self, curTime=None):
'''
get the next OTP value
:return: next otp value
:rtype: string
'''
log.debug("[getOtp] begin. Get the next OTP value for: curTime: %r" % (curTime))
try:
otplen = int(self.token.LinOtpOtpLen)
except ValueError as ex:
log.error("[getOtp]: Could not convert otplen - value error %r " % (ex))
raise Exception(ex)
self.hashlibStr = self.getFromTokenInfo("hashlib", 'sha1')
secretHOtp = self.token.getHOtpKey()
hmac2Otp = HmacOtp(secretHOtp, self.getOtpCount(), otplen, self.getHashlib(self.hashlibStr))
otpval = hmac2Otp.generate(inc_counter=False)
pin = self.token.getPin()
combined = "%s%s" % (otpval, pin)
if getFromConfig("PrependPin") == "True" :
combined = "%s%s" % (pin, otpval)
log.debug("[getOtp] end. Return opt is: (pin: %r, otpval: %r, combined: %r) " %
(pin, otpval, combined))
return (1, pin, otpval, combined)
示例3: get_multi_otp
# 需要导入模块: from linotp.lib.HMAC import HmacOtp [as 别名]
# 或者: from linotp.lib.HMAC.HmacOtp import generate [as 别名]
def get_multi_otp(self, count=0, epoch_start=0, epoch_end=0, curTime=None):
'''
return a dictionary of multiple future OTP values of the HOTP/HMAC token
:param count: how many otp values should be returned
:type count: int
:return: tuple of status: boolean, error: text and the OTP dictionary
'''
log.debug("[get_multi_otp] begin. Get a dictionary of multiple future OTP values for: count: %r, epoch_start: %r, epoch_end: %r, curTime: %r" %
(count, epoch_start, epoch_end, curTime))
otp_dict = {"type" : "HMAC", "otp": {}}
ret = False
error = "No count specified"
try:
otplen = int(self.token.LinOtpOtpLen)
except ValueError as ex:
log.error("[get_multi_otp]: Could not convert otplen - value error %r " % (ex))
raise Exception(ex)
secretHOtp = self.token.getHOtpKey()
hmac2Otp = HmacOtp(secretHOtp, self.getOtpCount(), otplen, self.getHashlib(self.hashlibStr))
log.debug("[get_multi_otp] retrieving %i OTP values for token %s" % (count, hmac2Otp))
if count > 0:
for i in range(count):
otpval = hmac2Otp.generate(self.getOtpCount() + i, inc_counter=False)
otp_dict["otp"][i] = otpval
ret = True
log.debug("[get_multi_otp] end. dictionary of multiple future OTP is: otp_dict: %r - status: %r - error %r" % (ret, error, otp_dict))
return (ret, error, otp_dict)
示例4: _getNextOtp
# 需要导入模块: from linotp.lib.HMAC import HmacOtp [as 别名]
# 或者: from linotp.lib.HMAC.HmacOtp import generate [as 别名]
def _getNextOtp(self):
"""
access the nex valid otp
:return: otpval
:rtype: string
"""
LOG.debug("[getNextOtp] begin. starting to look for the next otp")
try:
otplen = int(self.token.LinOtpOtpLen)
except ValueError as ex:
LOG.error("[getNextOtp] ValueError %r" % ex)
raise Exception(ex)
secObj = self._get_secret_object()
counter = self.token.getOtpCounter()
#log.debug("serial: %s",serialNum)
hmac2otp = HmacOtp(secObj, counter, otplen)
nextotp = hmac2otp.generate(counter + 1)
LOG.debug("[getNextOtp] end. got the next otp value: nextOtp %r"
% nextotp)
return nextotp
示例5: get_multi_otp
# 需要导入模块: from linotp.lib.HMAC import HmacOtp [as 别名]
# 或者: from linotp.lib.HMAC.HmacOtp import generate [as 别名]
def get_multi_otp(self, count=0, epoch_start=0, epoch_end=0, curTime=None):
'''
return a dictionary of multiple future OTP values of the HOTP/HMAC token
:param count: how many otp values should be returned
:type count: int
:return: tuple of status: boolean, error: text and the OTP dictionary
'''
log.debug("[get_multi_otp] begin. Get a dictionary of multiple future OTP values for: count: %r, epoch_start: %r, epoch_end: %r, curTime: %r" %
(count, epoch_start, epoch_end, curTime))
otp_dict = {"type" : "TOTP", "otp": {}}
ret = False
error = "No count specified"
try:
otplen = int(self.token.LinOtpOtpLen)
except ValueError:
return ret
secretHOtp = self.token.getHOtpKey()
self.hashlibStr = self.getFromTokenInfo("hashlib", "sha1")
timeStepping = int(self.getFromTokenInfo("timeStep", 30))
shift = int(self.getFromTokenInfo("timeShift", 0))
hmac2Otp = HmacOtp(secretHOtp, self.getOtpCount(),
otplen, self.getHashlib(self.hashlibStr))
tCounter = self.time2float(datetime.datetime.now())
if curTime:
tCounter = self.time2float(curTime)
## we don't need to round here as we have alread float
counter = int(((tCounter - shift) / timeStepping))
otp_dict["shift"] = shift
otp_dict["timeStepping"] = timeStepping
if count > 0:
for i in range(0, count):
otpval = hmac2Otp.generate(counter=counter + i, inc_counter=False)
timeCounter = ((counter + i) * timeStepping) + shift
otp_dict["otp"][ counter + i] = {
'otpval' : otpval,
'time' : datetime.datetime.fromtimestamp(timeCounter).strftime("%Y-%m-%d %H:%M:%S"),
}
ret = True
log.debug("[get_multi_otp] end. dictionary of multiple future OTP is: otp_dict: %r - status: %r - error %r" % (ret, error, otp_dict))
return (ret, error, otp_dict)
示例6: resync
# 需要导入模块: from linotp.lib.HMAC import HmacOtp [as 别名]
# 或者: from linotp.lib.HMAC.HmacOtp import generate [as 别名]
def resync(self, otp1, otp2, options=None):
'''
resync the token based on two otp values
- external method to do the resync of the token
:param otp1: the first otp value
:type otp1: string
:param otp2: the second otp value
:type otp2: string
:param options: optional token specific parameters
:type options: dict or None
:return: counter or -1 if otp does not exist
:rtype: int
'''
log.debug("[resync] .begin. Resync the token based on: %r, anOtpVal: %r, options: %r" % (otp1, otp2, options))
ret = False
try:
otplen = int(self.token.LinOtpOtpLen)
except ValueError as ex:
log.debug("[resync] otplen ValueError: %r ret: %r " % (ex, ret))
raise Exception(ex)
self.hashlibStr = self.getFromTokenInfo("hashlib", 'sha1')
secretHOtp = self.token.getHOtpKey()
counter = self.token.getOtpCounter()
syncWindow = self.token.getSyncWindow()
#log.debug("serial: %s",serialNum)
hmac2Otp = HmacOtp(secretHOtp, counter, otplen, self.getHashlib(self.hashlibStr))
counter = hmac2Otp.checkOtp(otp1, syncWindow)
if counter == -1:
log.debug("[resync] exit. First counter (-1) not found ret: %r" % (ret))
return ret
nextOtp = hmac2Otp.generate(counter + 1)
if nextOtp != otp2:
log.debug("[resync] exit. Failed to verify second otp: nextOtp: %r != otp2: %r ret: %r" % (nextOtp, otp2, ret))
return ret
ret = True
self.incOtpCounter(counter + 1, True)
log.debug("[resync] end. resync was successful: ret: %r" % (ret))
return ret
示例7: getOtp
# 需要导入模块: from linotp.lib.HMAC import HmacOtp [as 别名]
# 或者: from linotp.lib.HMAC.HmacOtp import generate [as 别名]
def getOtp(self, curTime=None):
'''
get the next OTP value
:return: next otp value
:rtype: string
'''
log.debug("[getOtp] begin. Get the next OTP value for: curTime: %r" % (curTime))
res = (-1, 0, 0, 0)
otplen = int(self.token.LinOtpOtpLen)
secretHOtp = self.token.getHOtpKey()
self.hashlibStr = self.getFromTokenInfo("hashlib", "sha1")
timeStepping = int(self.getFromTokenInfo("timeStep", 30))
shift = int(self.getFromTokenInfo("timeShift", 0))
hmac2Otp = HmacOtp(secretHOtp, self.getOtpCount(), otplen, self.getHashlib(self.hashlibStr))
tCounter = self.time2float(datetime.datetime.now())
if curTime:
tCounter = self.time2float(curTime)
## we don't need to round here as we have alread float
counter = int(((tCounter - shift) / timeStepping))
otpval = hmac2Otp.generate(counter=counter, inc_counter=False)
pin = self.token.getPin()
combined = "%s%s" % (otpval, pin)
if getFromConfig("PrependPin") == "True" :
combined = "%s%s" % (pin, otpval)
log.debug("[getOtp] end. Return opt is: (pin: %r, otpval: %r, combined: %r) " %
(pin, otpval, combined))
return (1, pin, otpval, combined)
示例8: test_scenario01
# 需要导入模块: from linotp.lib.HMAC import HmacOtp [as 别名]
# 或者: from linotp.lib.HMAC.HmacOtp import generate [as 别名]
#.........这里部分代码省略.........
"mozart": serial_token_mozart,
"beethoven": serial_token_beethoven
}
for user in user_token_dict:
driver.get(self.base_url + "/account/login")
driver.find_element_by_id("login").clear()
driver.find_element_by_id("login").send_keys("%[email protected]%s" % (user, test1_realm))
driver.find_element_by_id("password").clear()
driver.find_element_by_id("password").send_keys("Test123!")
driver.find_element_by_id("password").submit()
driver.find_element_by_xpath("//div[@id='tabs']/ul/li/a/span[text()='set PIN']").click()
time.sleep(1)
# driver.find_element_by_css_selector('#tokenDiv > ul > li > a').click()
driver.find_element_by_id('tokenDiv').find_element_by_partial_link_text(user_token_dict[user]).click()
driver.find_element_by_id("pin1").clear()
driver.find_element_by_id("pin1").send_keys(user + "newpin")
driver.find_element_by_id("pin2").clear()
driver.find_element_by_id("pin2").send_keys(user + "newpin")
driver.find_element_by_id("button_setpin").click()
time.sleep(1)
self.assertEqual("PIN set successfully", self.close_alert_and_get_its_text())
driver.find_element_by_link_text("Logout").click()
self._announce_test("10. Authentisierung der 4 Benutzer ###")
validate = Validate(self.http_protocol,
self.http_host,
self.http_port,
self.http_username,
self.http_password)
# Validate HOTP Token - bach
hotp = HmacOtp()
for counter in range(0, 20):
otp = "bachnewpin" + hotp.generate(counter=counter, key=seed_oath137332_bin)
access_granted, _ = validate.validate(user="[email protected]" +
test1_realm, password=otp)
self.assertTrue(access_granted, "OTP: " + otp + " for user " +
"[email protected]" + test1_realm + " returned False")
access_granted, _ = validate.validate(user="[email protected]" + test1_realm,
password="1234111111")
self.assertFalse(access_granted, "OTP: 1234111111 should be False for user bach")
# Validate Remote token - debussy
access_granted, _ = validate.validate(user="[email protected]" + test1_realm,
password="debussynewpin" + remote_token_otp)
self.assertTrue(access_granted, "OTP: " + remote_token_otp + " for user " +
"[email protected]" + test1_realm + " returned False")
access_granted, _ = validate.validate(user="[email protected]" + test1_realm,
password="1234111111")
self.assertFalse(access_granted, "OTP: 1234111111 should be False for user debussy")
# Validate Spass token - beethoven
access_granted, _ = validate.validate(user="[email protected]" + test1_realm,
password="beethovennewpin")
self.assertTrue(access_granted, "OTP: " + "beethovennewpin" + " for user " +
"[email protected]" + test1_realm + " returned False")
access_granted, _ = validate.validate(user="[email protected]" + test1_realm,
password="randominvalidpin")
self.assertFalse(access_granted, "OTP: randominvalidpin should be False for user beethoven")
# Validate mOTP token - mozart
current_epoch = time.time()
motp_otp = calculate_motp(
epoch=current_epoch,
key=motp_key,
pin=motp_pin
示例9: test_scenario01
# 需要导入模块: from linotp.lib.HMAC import HmacOtp [as 别名]
# 或者: from linotp.lib.HMAC.HmacOtp import generate [as 别名]
#.........这里部分代码省略.........
"debussy": serial_token_debussy,
"mozart": serial_token_mozart,
"beethoven": serial_token_beethoven
}
for user in user_token_dict:
driver.get(self.base_url + "/account/login")
driver.find_element_by_id("login").clear()
driver.find_element_by_id("login").send_keys("%[email protected]%s" % (user, test1_realm))
driver.find_element_by_id("password").clear()
driver.find_element_by_id("password").send_keys("Test123!")
driver.find_element_by_id("password").submit()
driver.find_element_by_xpath("//div[@id='tabs']/ul/li/a/span[text()='set PIN']").click()
time.sleep(1)
# driver.find_element_by_css_selector('#tokenDiv > ul > li > a').click()
driver.find_element_by_id('tokenDiv').find_element_by_partial_link_text(user_token_dict[user]).click()
driver.find_element_by_id("pin1").clear()
driver.find_element_by_id("pin1").send_keys(user + "newpin")
driver.find_element_by_id("pin2").clear()
driver.find_element_by_id("pin2").send_keys(user + "newpin")
driver.find_element_by_id("button_setpin").click()
time.sleep(1)
self.assertEqual("PIN set successfully", self.close_alert_and_get_its_text())
driver.find_element_by_link_text("Logout").click()
### 10. Authentisierung der 4 Benutzer ###
validate = Validate(self.http_protocol,
self.http_host,
self.http_username,
self.http_password)
# Validate HOTP Token - bach
hotp = HmacOtp()
for counter in range(0, 20):
otp = "bachnewpin" + hotp.generate(counter=counter, key=seed_oath137332_bin)
access_granted, _ = validate.validate(user="[email protected]" +
test1_realm, password=otp)
self.assertTrue(access_granted, "OTP: " + otp + " for user " +
"[email protected]" + test1_realm + " returned False")
access_granted, _ = validate.validate(user="[email protected]" + test1_realm,
password="1234111111")
self.assertFalse(access_granted, "OTP: 1234111111 should be False for user bach")
# Validate Remote token - debussy
access_granted, _ = validate.validate(user="[email protected]" + test1_realm,
password="debussynewpin" + remote_token_otp)
self.assertTrue(access_granted, "OTP: " + remote_token_otp + " for user " +
"[email protected]" + test1_realm + " returned False")
access_granted, _ = validate.validate(user="[email protected]" + test1_realm,
password="1234111111")
self.assertFalse(access_granted, "OTP: 1234111111 should be False for user debussy")
# Validate Spass token - beethoven
access_granted, _ = validate.validate(user="[email protected]" + test1_realm,
password="beethovennewpin")
self.assertTrue(access_granted, "OTP: " + "beethovennewpin" + " for user " +
"[email protected]" + test1_realm + " returned False")
access_granted, _ = validate.validate(user="[email protected]" + test1_realm,
password="randominvalidpin")
self.assertFalse(access_granted, "OTP: randominvalidpin should be False for user beethoven")
# Validate mOTP token - mozart
current_epoch = time.time()
motp_otp = calculate_motp(
epoch=current_epoch,
key=motp_key,
pin=motp_pin