ronald martinez avatar ronald martinez committed 86223bd

update viernes

Comments (0)

Files changed (6)

+import settings
+
+from generate_token import Token
+from functools import wraps
+from database import Database
+
+from flask import (session, redirect, url_for,
+    render_template, request, jsonify, Blueprint)
+
+controller = Blueprint('controller', __name__)
+
+token = Token(settings.WAIT_FOR_TOKEN)
+
+
+def autehnticated(f):
+    @wraps(f)
+    def decorated(*args, **kwargs):
+        if not session.get('email'):
+            return redirect(url_for('.login'))
+        return f(*args, **kwargs)
+    return decorated
+
+
+@controller.route('/')
+def index():
+
+    email = session.get('email')
+
+    if email:
+        return redirect(url_for('.get_token'))
+
+    return render_template(
+        'index.html'
+    )
+
+
+@controller.route('/login', methods=['POST', 'GET'])
+def login():
+
+    if request.method == 'POST':
+
+        db = Database()
+        email = request.form.get('email')
+        exists_email = db.get(email)
+        db.close()
+
+        if exists_email:
+            session['email'] = email
+
+        return redirect(
+            url_for('.register' if not exists_email else '.get_token')
+        )
+
+    else:
+        return render_template(
+            'login.html',
+        )
+
+
+@controller.route('/logout')
+def logout():
+
+    session.pop('email', None)
+    return redirect(url_for('.login'))
+
+
+@controller.route('/get_token')
+@autehnticated
+def get_token():
+
+    db = Database()
+    email = session.get('email')
+    code = token.generate_code(db.get(email))
+    db.close()
+
+    return render_template(
+        'token.html',
+        code=code,
+        wait_for_token=settings.WAIT_FOR_TOKEN * 1000,
+        email=email
+    )
+
+
+@controller.route('/get_token_each_time', methods=['GET'])
+@autehnticated
+def get_token_each_time():
+
+    db = Database()
+    email = session.get('email')
+    code = token.generate_code(db.get(email))
+    db.close()
+
+    return jsonify(code=code)
+
+
+@controller.route('/check', methods=['POST', 'GET'])
+def check():
+
+    email = session.get('email')
+    status_code = None
+
+    if request.method == 'POST':
+
+        email = request.form.get('email')
+        code = request.form.get('code')
+
+        db = Database()
+
+        if not db.get(email) or not code:
+            status_code = 3
+        else:
+            status_code = 1 if token.verify_code(
+                db.get(email), code) else 2
+
+        db.close()
+
+    return render_template(
+        'check.html',
+        status_code=status_code,
+        email=email
+    )
+
+
+@controller.route('/register', methods=['POST', 'GET'])
+def register():
+
+    status_code = None
+
+    if request.method == 'POST':
+
+        email = request.form.get('email')
+        name = request.form.get('name')
+
+        if not email or not name:
+            status_code = 1
+
+        else:
+
+            db = Database()
+
+            if db.get(email):
+                status_code = 2
+
+            else:
+                db.set(email, token.generate_seed(email))
+                db.set('%s_name' % email, name)
+                session['email'] = email
+                db.close()
+
+                return redirect(url_for('.get_token'))
+
+            db.close()
+
+    return render_template(
+        'register.html',
+        status_code=status_code
+    )
+import settings
+import dbm
+
+
+class Database(object):
+
+    db = None
+
+    def __init__(self):
+        self.db = dbm.open(settings.DATABASE_FILE, 'c')
+
+    def get(self, k):
+        return self.db.get(k)
+
+    def set(self, k, v):
+        self.db[k] = v
+
+    def close(self):
+        self.db.close()

generate_token.py

+import time
+import hashlib
+import uuid
+import settings
+
+
+class Token(object):
+
+    def __init__(self, seconds):
+
+        self.seconds = seconds
+
+    def _get_cuurent_time(self):
+
+        return str(int(time.time() / self.seconds))
+
+    def generate_code(self, seed):
+
+        time = self._get_cuurent_time()
+        ziff = hashlib. sha224(seed + time).hexdigest()
+        return ''.join([x for x in ziff if x.isdigit()][:settings.CODE_LEN])
+
+    def generate_seed(self, key):
+
+        return '%s%s%s' % (
+            str(uuid.uuid4()).replace('-', ''),
+            key,
+            settings.SECRET_KEY
+        )
+
+    def verify_code(self, seed, code):
+
+        return code == self.generate_code(seed)
-import dbm
-import uuid
-import datetime
-import hashlib
-import flask
-from functools import wraps
+import settings
 
-app = flask.Flask(__name__)
-app.debug = True
-app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
+from flask import Flask
+from controller import controller
 
-#filename = '/var/lib/openshift/8c4153892ceb43388ab5a17e692e7981/
-#app-root/runtime/repo/wsgi/macfri'
+app = Flask(__name__)
 
-DATABASE_FILE = 'm4cf1'
-#WAIT_FOR_TOKEN = 30000
-WAIT_FOR_TOKEN = 15000  # 10 seconds
-
-
-class Database(object):
-
-    db = None
-
-    def __init__(self):
-        self.db = dbm.open(DATABASE_FILE, 'c')
-
-    def get(self, k):
-        return self.db.get(k)
-
-    def set(self, k, v):
-        self.db[k] = v
-
-    def close(self):
-        self.db.close()
-
-
-def generate_clock(clock=None):
-
-    if not clock:
-
-        base_datetime = datetime.datetime.strptime(
-            str(datetime.datetime.now())[:19],
-            '%Y-%m-%d %H:%M:%S'
-        )
-
-        return str(base_datetime).replace(
-                '-', '').replace(' ', '').replace(':', '')[:-2] + '00'
-
-    else:
-
-        time = datetime.datetime.strptime(clock,
-            '%Y%m%d%H%M%S') + datetime.timedelta(seconds=60)
-
-        time = str(time).replace(
-            '-', '').replace(' ', '').replace(':', '')
-
-        return time
-
-
-def generate_code(seed):
-
-    ziff = hashlib. sha224(seed).hexdigest()
-    return ''.join([x for x in ziff if x.isdigit()][:6])
-
-
-def generate_seed(clock, master_seed=None):
-
-    if not master_seed:
-        master_seed = str(uuid.uuid4()).replace('-', '')
-    return master_seed + clock
-
-
-def autehnticated(f):
-    @wraps(f)
-    def decorated(*args, **kwargs):
-        if not flask.session.get('email'):
-
-            return flask.redirect(flask.url_for('login'))
-
-            #flask.abort(403)
-        return f(*args, **kwargs)
-    return decorated
-
-
-@app.route('/')
-def index():
-
-    email = flask.session.get('email')
-
-    if email:
-
-        return flask.redirect(flask.url_for('get_token'))
-
-    return flask.render_template(
-        'index.html'
-    )
-
-
-@app.route('/login', methods=['POST', 'GET'])
-def login():
-
-    if flask.request.method == 'POST':
-
-        db = Database()
-        email = flask.request.form.get('email')
-        exists_email = db.get(email)
-        db.close()
-
-        if not exists_email:
-            return flask.redirect(flask.url_for('register'))
-        else:
-            flask.session['email'] = email
-            return flask.redirect(flask.url_for('get_token'))
-
-    else:
-        return flask.render_template(
-            'login.html',
-        )
-
-
-@app.route('/logout')
-def logout():
-
-    flask.session.pop('email', None)
-    return flask.redirect(flask.url_for('login'))
-
-
-@app.route('/get_token')
-@autehnticated
-def get_token():
-
-    db = Database()
-
-    email = flask.session.get('email')
-    seed = db.get(email)
-    db.close()
-
-    return flask.render_template(
-        'token.html',
-        code=generate_code(seed),
-        wait_for_token=WAIT_FOR_TOKEN,
-        email=email
-    )
-
-
-@app.route('/get_token_each_time', methods=['GET'])
-@autehnticated
-def get_token_each_time():
-
-    db = Database()
-
-    email = flask.session.get('email')
-    master_seed = db.get(email)
-    base = master_seed[0:-14]
-    time = master_seed[-14:]
-
-    seed = generate_seed(generate_clock(time), base)
-    db.set(email, seed)
-    db.close()
-
-    return flask.jsonify(code=generate_code(seed))
-
-
-@app.route('/check', methods=['POST', 'GET'])
-def check():
-
-    email = flask.session.get('email')
-    status_code = None
-
-    if flask.request.method == 'POST':
-
-        email = flask.request.form.get('email')
-        code = flask.request.form.get('code')
-
-        db = Database()
-
-        if not db.get(email) or not code:
-            status_code = 3
-
-        else:
-            seed = db.get(email)
-
-            if code == generate_code(seed):
-                status_code = 1
-            else:
-                status_code = 2
-
-        db.close()
-        app.logger.info('status_code: %s' % status_code)
-
-    return flask.render_template(
-        'check.html',
-        status_code=status_code,
-        email=email
-    )
-
-
-@app.route('/register', methods=['POST', 'GET'])
-def register():
-
-    status_code = None
-
-    if flask.request.method == 'POST':
-
-        email = flask.request.form.get('email')
-        name = flask.request.form.get('name')
-
-        if not email or not name:
-
-            status_code = 1
-
-        else:
-
-            db = Database()
-
-            if db.get(email):
-                status_code = 2
-
-            else:
-
-                db.set(email, generate_seed(generate_clock()))
-                db.set('%s_name' % email, name)
-
-                flask.session['email'] = email
-                db.close()
-                return flask.redirect(flask.url_for('get_token'))
-
-            db.close()
-
-        app.logger.info('status_code: %s' % status_code)
-
-    return flask.render_template(
-        'register.html',
-        status_code=status_code
-    )
+app.debug = settings.DEBUG
+app.secret_key = settings.SECRET_KEY
 
+app.register_blueprint(controller)
 
 if __name__ == '__main__':
     app.run()
+DEBUG = True
+WAIT_FOR_TOKEN = 10
+DATABASE_FILE = 'm4cf1'
+CODE_LEN = 6
+SECRET_KEY = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'

templates/token.html

 </head>
 <body>
     {% include 'header.html' %}
-    <div id="code" class="">{{ code }}</div>
+    <div id="code" class="ex">{{ code }}</div>
 </body>
 </html>
 
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.