Skip to content

Commit

Permalink
Add scope to default app-route & Add auth hook
Browse files Browse the repository at this point in the history
  • Loading branch information
noahziheng committed Jun 18, 2018
1 parent 9d42125 commit fd004f5
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion libfreeiot/adapters/mqtt/Parse/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
Author: Noah Gao
Updated at: 2018-2-2
"""
from bson import DBRef, ObjectId
from datetime import datetime
from bson import DBRef, ObjectId
from .. import Constant
from . import sys, mongo

Expand Down
8 changes: 6 additions & 2 deletions libfreeiot/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@

print('FreeIOT Version: %s' % version.__version__)

scope = dict()

def create_flask_app():
""" Function for create flask application instance """
return create_app(os.getenv('FLASK_CONFIG') or 'default')
global scope
return create_app(os.getenv('FLASK_CONFIG') or 'default', scope)

def run(port = int(os.environ.get("APP_PORT")),
host = os.environ.get('APP_HOST', '127.0.0.1'),
adapters = None,
app = None):
""" Main Method for running application """
global scope

if app is None:
app = create_flask_app() # Create Flask Application
scope = dict()

# 代入数据库句柄到 Adapter 作用域
from .core import mongo
Expand Down
6 changes: 4 additions & 2 deletions libfreeiot/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

mongo = PyMongo()

def create_app(config_name):
def create_app(config_name, scope = None):
"""
Function for create Flask App instance
"""
if scope is None:
scope = dict()
app = Flask(__name__) # Initialize app

# Import project config
Expand All @@ -24,5 +26,5 @@ def create_app(config_name):
mongo.init_app(app)

from libfreeiot.core.routes import create_routes
app, api = create_routes(app) # Initialize api services with Routes definition
app, api = create_routes(app, scope) # Initialize api services with Routes definition
return app
12 changes: 9 additions & 3 deletions libfreeiot/core/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

JWT_EXPIRES = 7 * 24 * 3600

def create_routes(app):
def create_routes(app, scope = None):
'''
Function for create routes
'''
if scope is None:
scope = dict()
app.config['JWT_SECRET_KEY'] = 'super-secret' # Change this!
app.config['JWT_EXPIRES'] = datetime.timedelta(7)
app.config['UPLOAD_FOLDER'] = os.path.join(os.getcwd(), '/images')
Expand All @@ -41,8 +43,12 @@ def auth():
return jsonify({"msg": "Missing username parameter"}), 400
if not password:
return jsonify({"msg": "Missing password parameter"}), 400
if username != 'admin' or password != 'admin':
return jsonify({"msg": "Bad username or password"}), 401
if 'auth'in scope.keys():
if not scope["auth"].login(username, password):
return jsonify({"msg": "Bad username or password"}), 401
else:
if username != 'admin' or password == 'admin':
return jsonify({"msg": "Bad username or password"}), 401

return jsonify({'jwt': create_jwt(identity=username)}), 200

Expand Down
2 changes: 1 addition & 1 deletion libfreeiot/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
"""

script_name = 'libfreeiot'
__version__ = '0.9.13'
__version__ = '0.9.14'
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""
Library's Setuptools Script
"""
import imp
import os
from setuptools import setup, find_packages
import imp, os

here = os.path.abspath(os.path.dirname(__file__))

Expand Down

0 comments on commit fd004f5

Please sign in to comment.