|
|
|
from flask import Flask, request, jsonify
|
|
|
|
from flask_admin.contrib.mongoengine import ModelView
|
|
|
|
from flask_jwt_extended import JWTManager, create_access_token
|
|
|
|
from mongoengine import connect
|
|
|
|
from werkzeug.security import check_password_hash
|
|
|
|
|
|
|
|
from internal_lib.AuthMethots import AuthJWT
|
|
|
|
from models.Group import Group
|
|
|
|
from models.Union import Union
|
|
|
|
from models.User import User
|
|
|
|
from restapi import MongoApi
|
|
|
|
from flask_admin import Admin
|
|
|
|
|
|
|
|
"""
|
|
|
|
Mongodb connection string
|
|
|
|
"""
|
|
|
|
connect('adunatio', host='mongo', username="xcoder", password="4dun4710", authentication_source='admin')
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
app.secret_key = "secret_key+secret_key"
|
|
|
|
app.config["JWT_TOKEN_LOCATION"] = "headers"
|
|
|
|
app.config["JWT_HEADER_NAME"] = "Adunation_Session_Token"
|
|
|
|
app.config["JWT_HEADER_TYPE"] = "Bearer"
|
|
|
|
|
|
|
|
"""
|
|
|
|
flask jwt extended register
|
|
|
|
"""
|
|
|
|
jwt = JWTManager(app)
|
|
|
|
|
|
|
|
"""
|
|
|
|
flask mongorester register
|
|
|
|
"""
|
|
|
|
api = MongoApi(app, authentication_methods=[AuthJWT])
|
|
|
|
api.register_model(User, uri="/api/user")
|
|
|
|
api.register_model(Union, uri="/api/union")
|
|
|
|
|
|
|
|
"""
|
|
|
|
flask admin register
|
|
|
|
"""
|
|
|
|
|
|
|
|
adm = Admin(app)
|
|
|
|
adm.add_view(ModelView(User))
|
|
|
|
adm.add_view(ModelView(Union))
|
|
|
|
adm.add_view(ModelView(Group))
|
|
|
|
|
|
|
|
"""
|
|
|
|
login function
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/auth/login', methods=['POST'])
|
|
|
|
def login():
|
|
|
|
if not request.is_json:
|
|
|
|
return jsonify({"message": "Missing JSON in request", "error": "parameter_error", "status":False}), 400
|
|
|
|
|
|
|
|
username = request.json.get('username', None)
|
|
|
|
password = request.json.get('password', None)
|
|
|
|
if not username:
|
|
|
|
return jsonify({"message": "Missing username parameter", "error": "parameter_error", "status":False}), 400
|
|
|
|
if not password:
|
|
|
|
return jsonify({"message": "Missing password parameter", "error": "parameter_error", "status":False}), 400
|
|
|
|
|
|
|
|
try:
|
|
|
|
user = User.objects.get(username=username)
|
|
|
|
except Exception as e:
|
|
|
|
app.logger.error(e)
|
|
|
|
return jsonify({"message": "Bad username or password", "error": "Unauthorized", "status": False}), 401
|
|
|
|
|
|
|
|
if not check_password_hash(user.password,password):
|
|
|
|
return jsonify({"message": "Bad username or password", "error": "Unauthorized", "status":False}), 401
|
|
|
|
|
|
|
|
# Identity can be any data that is json serializable
|
|
|
|
access_token = create_access_token(identity=str(user.id))
|
|
|
|
return jsonify(access_token=access_token,status=True), 200
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(host="0.0.0.0", port=5000, debug=True)
|