def validate_otp(device_id, received_otp): otp_config = get_device_info(device_id) generated_otp = truncate_otp( otp=generate_otp(otp_expiry=otp_config.expiry, otp_secret=otp_config.secret), expected_length=otp_config.length) return generated_otp == received_otp
def test_validate_otp(self): otp = truncate_otp(otp=generate_otp( otp_expiry=30, otp_secret=self.response.json.get("secret"))) response = self.app.get("/validate/{}".format(self.device_id), json={"OTP": otp}) self.assertEqual(True, response.json.get("is_valid")) self.assertEqual(200, response.status_code)
def is_registered(url, device_id): return requests.get(url="{}/is_registered/{}".format(url, device_id)) def generate_device_otp(otp_secret, coefficient=None): device_otp = generate_otp(otp_secret=otp_secret, coefficient=coefficient) return truncate_otp(otp=device_otp) def get_parser(): parser = argparse.ArgumentParser() parser.add_argument('--device-id', type=int, action='store', required=True, help='id of the device') parser.add_argument('--server-addr', type=str, action='store', help='server address', default="http://127.0.0.1:5000") return parser if __name__ == '__main__': args_parse = get_parser().parse_args() is_device_registered = is_registered(url=args_parse.server_addr, device_id=args_parse.device_id) if not is_device_registered.json().get("is_registered"): register_device = register(url=args_parse.server_addr, device_id=args_parse.device_id) secret = register_device.json().get("secret") else: secret = get_device_secret(url=args_parse.server_addr, device_id=args_parse.device_id) print(truncate_otp(otp=generate_otp(otp_secret=secret)))
def generate_device_otp(otp_secret, coefficient=None): device_otp = generate_otp(otp_secret=otp_secret, coefficient=coefficient) return truncate_otp(otp=device_otp)
def test_truncate_otp(self): self.assertEqual("039319", truncate_otp("364039319", expected_length=6))