This is an automated email from the ASF dual-hosted git repository. kmccusker pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-milagro-mfa-server.git
commit 89ec87d330253f354241b63bab5487e71344edfb Author: Pavlin Angelov <[email protected]> AuthorDate: Wed Jun 1 15:39:07 2016 +0300 Refactor: Extract business logic in separete file Extract mobile flow logic from the handler in separete file We want it to be more easy to test and change --- servers/rps/mobile_flow.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++ servers/rps/rps.py | 34 ++++------------------------- 2 files changed, 58 insertions(+), 30 deletions(-) diff --git a/servers/rps/mobile_flow.py b/servers/rps/mobile_flow.py new file mode 100644 index 0000000..638b115 --- /dev/null +++ b/servers/rps/mobile_flow.py @@ -0,0 +1,54 @@ +import uuid +import datetime + +from tornado.log import app_log as log +from tornado.options import options + +from mpin_utils import secrets +from mpin_utils.common import ( + Time, +) + + +class MobileFlow: + """ Holds Bussines logic for the Mobile flow """ + + def __init__(self, application, storage): + self.application = application + self.storage = storage + + def generate_wid(self): + # Generate request for MPinWIDServer for WID + wId = uuid.uuid4().hex + + while wId is None or (self.storage.find(stage="auth", wid=wId)): + if wId is None: + log.debug("WebId is None".format(wId)) + else: + log.debug("WebId {0} already exists. Generating a new one".format(wId)) + + wId = uuid.uuid4().hex + log.debug("New webId generated: {0}." .format(wId)) + + return wId + + def generate_qr(self, wId): + webOTT = secrets.generate_ott(options.OTTLength, self.application.server_secret.rng, "hex") + + nowTime = Time.syncedNow() + expirePinPadTime = nowTime + datetime.timedelta(seconds=options.accessNumberExpireSeconds) + expireTime = expirePinPadTime + datetime.timedelta(seconds=options.accessNumberExtendValiditySeconds) + + self.storage.add(stage="auth", expire_time=expireTime, webOTT=webOTT, wid=wId) + + qrUrl = options.rpsBaseURL + "#" + wId + + params = { + "ttlSeconds": options.accessNumberExpireSeconds, + "qrUrl": qrUrl, + "webOTT": webOTT, + "localTimeStart": Time.DateTimetoEpoch(nowTime), + "localTimeEnd": Time.DateTimetoEpoch(expirePinPadTime) + } + + return params diff --git a/servers/rps/rps.py b/servers/rps/rps.py index ed6231b..12986dd 100755 --- a/servers/rps/rps.py +++ b/servers/rps/rps.py @@ -27,7 +27,6 @@ import random import sys import time import urllib -import uuid from urlparse import urlparse import tornado.autoreload @@ -57,6 +56,8 @@ from dynamic_options import ( process_dynamic_options, ) +from mobile_flow import MobileFlow + if os.name == "posix": from mpDaemon import Daemon elif os.name == "nt": @@ -714,35 +715,8 @@ class RPSGetQrUrlHandler(BaseHandler): @tornado.web.asynchronous @tornado.gen.engine def post(self): - # Generate request for MPinWIDServer for WID - wId = uuid.uuid4().hex - - while wId is None or (self.storage.find(stage="auth", wid=wId)): - if wId is None: - log.debug("WebId is None".format(wId)) - else: - log.debug("WebId {0} already exists. Generating a new one".format(wId)) - - wId = uuid.uuid4().hex - - log.debug("New webId generated: {0}." .format(wId)) - - webOTT = secrets.generate_ott(options.OTTLength, self.application.server_secret.rng, "hex") - - nowTime = Time.syncedNow() - expirePinPadTime = nowTime + datetime.timedelta(seconds=options.accessNumberExpireSeconds) - expireTime = expirePinPadTime + datetime.timedelta(seconds=options.accessNumberExtendValiditySeconds) - - self.storage.add(stage="auth", expire_time=expireTime, webOTT=webOTT, wid=wId) - - qrUrl = options.rpsBaseURL + "#" + wId - params = { - "ttlSeconds": options.accessNumberExpireSeconds, - "qrUrl": qrUrl, - "webOTT": webOTT, - "localTimeStart": Time.DateTimetoEpoch(nowTime), - "localTimeEnd": Time.DateTimetoEpoch(expirePinPadTime) - } + mobileFlow = MobileFlow(self.application, self.storage) + params = mobileFlow.generate_qr(mobileFlow.generate_wid()) self.write(params) self.finish()
