def init_api(my_app): global app app = my_app global mail_man mail_man = MailMan(app) @app.route('/sso/register', methods=['POST']) @jwt_required def sso_register(): username = get_jwt_identity()['username'] if request.get_json(): payload = request.get_json() domain = payload['domain'] ok = SSOController.sso_register(mailman=mail_man, username=username, domain=domain) if ok: return jsonify(code='success', msg='your domain register success') return jsonify(code='fail', msg='your domain already exists') @app.route('/sso/get_info', methods=['POST']) def get_sso_info(): domain = request.remote_addr payload = request.get_json()['query_data'] secret_key = payload['secret_key'] username = payload['username'] res = SSOController.get_sso_information(domain=domain, secret_key=secret_key, username=username) if res: return jsonify(res) return jsonify({}) @app.route('/sso/my_domains') @jwt_required def get_my_domains(): username = get_jwt_identity()['username'] return jsonify(SSOController.get_my_domains(username))
def init_api(my_app): global app app = my_app global mail_man mail_man = MailMan(app) @app.route('/confirm/register/<path:token>') def confirm_register(token): payload = decode_token(token) if TokenManager.token_expired(payload): return render_template( 'register_confirmation.html', message="Your session expired, Please register again!") success = ConfirmationController.confirm_register(payload['identity']) if success: return render_template('register_confirmation.html', message="You registered successfully!") return render_template('register_confirmation.html', message="Your email or username is duplicated!") @app.route('/confirm/password_reset/<path:token>') def confirm_password_reset(token): payload = decode_token(token) if TokenManager.token_expired(payload): return render_template( 'register_confirmation.html', message= "Your session expired, Please reset your password again!") success = ConfirmationController.confirm_password_reset( payload['identity']) if success: return render_template('register_confirmation.html', message="Your password reset successfully!") return render_template('register_confirmation.html', message="You reset password already!")
def app(): app = create_app(DBConfig.SQLALCHEMY_DATABASE_URI) db = SQLAlchemy(app) table_creators = TableCreators() table_creators.create_all(db, uri=DBConfig.SQLALCHEMY_DATABASE_URI) global mail_man mail_man = MailMan(app) return app
from flask import Flask, jsonify, request from flask_jwt_extended import jwt_required, get_jwt_identity from hieupro.helpers.MailMan import MailMan from hieupro.controller.sso_controller import SSOController app = Flask(__name__) mail_man = MailMan() def mock_get_sso_info(payload): domain = '127.0.0.1' secret_key = payload['secret_key'] username = payload['username'] res = SSOController.get_sso_information(domain=domain, secret_key=secret_key, username=username) if res: return res return None def init_api(my_app): global app app = my_app global mail_man mail_man = MailMan(app) @app.route('/sso/register', methods=['POST']) @jwt_required def sso_register(): username = get_jwt_identity()['username'] if request.get_json():
def init_api(my_app): global app app = my_app global mail_man mail_man = MailMan(app) @app.route('/') def hello_world(): return jsonify(msg="Hello World") @app.route('/decode_token/<path:token>') def decode_my_token(token): payload = decode_token(token) return jsonify(payload=payload['identity']) @app.route('/user', methods=["POST"]) def test_test(): user = request.get_json()['query_data'] return jsonify(msg=user['username']) @app.route('/user/list_users') @jwt_required @admin_required def list_users(): return jsonify( code='success', msg='Got all users information successfully', data=UserController.list_all() ) @app.route('/user/list_all') def list_all(): return jsonify(UserController.list_all()) @app.route('/user/post_token', methods=['HEAD']) @jwt_required def post_token(): return jsonify(payload=get_jwt_identity()) @app.route('/user/logout', methods=['HEAD']) @jwt_required def logout(): payload = get_jwt_identity() action_id = payload['action_id'] UserController.logout(action_id) return jsonify(code='success', msg='Logged out success') @app.route('/user/login', methods=['POST']) def login(): user = request.get_json()['query_data'] payload = UserController.login(user) if payload and payload['token']: return jsonify(code='success', msg='Logged in successfully', token=payload['token']) return jsonify(code='failed', msg='username or password is incorrect', signal=payload['signal']) @app.route('/user/register', methods=['POST']) def register(): user = request.get_json()['query_data'] returned = UserController.register(mail_man, user, False) if returned == REGISTER_SUCCESS: return jsonify(code='success', msg='Registered successfully') elif returned == REGISTER_DUPLICATED_USERNAME_OR_EMAIL: return jsonify(code='failed', msg='username or email is duplicated') elif returned == REGISTER_VALIDATION_FAILED: return jsonify(code='failed', msg='Format validation failed') @app.route('/user/change_password', methods=['POST']) @jwt_required def change_password(): user = request.get_json()['query_data'] username = get_jwt_identity()['username'] returned = UserController.password_change( username=username, old_password=user['password'], new_password=user['new_password'], by_admin=False ) if returned == PASSWORD_CHANGE_SUCCESS: return jsonify(code='success', msg='Password changed successfully') elif returned == PASSWORD_CHANGE_FAILED: return jsonify(code='failed', msg='Old password is incorrect') elif returned == PASSWORD_CHANGE_LAST_FIVE_PASSWORDS: return jsonify(code='failed', msg='Duplicated with last 5 password') elif returned == PASSWORD_CHANGE_VALIDATION_FAILED: return jsonify(code='failed', msg='Provided information is invalid!') @app.route('/user/reset_password', methods=['POST']) def reset(): user = request.get_json()['query_data'] UserController.password_reset(mail_man, user['username'], user['email']) return jsonify(msg='success') @app.route('/user/sso_login', methods=['POST']) def sso_login(): secret_key = SSOServiceUsage.secret_key username = request.get_json()['query_data']['username'] resp = mock_get_sso_info({ 'username': username, 'secret_key': secret_key }) if resp: return jsonify(resp) return jsonify({})