forked from oyd/Adunatio
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
2.9 KiB
90 lines
2.9 KiB
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 ,generate_password_hash
|
|
|
|
from internal_lib.AuthMethots import AuthJWT
|
|
from models.Account import Account
|
|
from models.Group import Group, PaymentGroup
|
|
from models.Payment import Payments
|
|
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/")
|
|
api.register_model(Group, uri="/api/group/")
|
|
api.register_model(PaymentGroup, uri="/api/payment_group/")
|
|
api.register_model(Account, uri="/api/accounts/")
|
|
api.register_model(Payments, uri="/api/payments/")
|
|
|
|
"""
|
|
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
|
|
|
|
app.logger.error(password)
|
|
app.logger.error(username)
|
|
app.logger.error(generate_password_hash(password))
|
|
app.logger.error(user.password)
|
|
app.logger.error(check_password_hash(pwhash=user.password,password=password))
|
|
|
|
if check_password_hash(pwhash=user.password,password=password) == False:
|
|
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)
|
|
|