|
| 1 | +from docusign_esign import ApiException |
| 2 | +from flask import Blueprint, jsonify, request, redirect, url_for |
| 3 | +from flask_cors import cross_origin |
| 4 | + |
| 5 | +from app.api.utils import process_error |
| 6 | +from app.ds_client import DsClient |
| 7 | + |
| 8 | +auth = Blueprint('auth', __name__) |
| 9 | + |
| 10 | + |
| 11 | +@auth.route('/code_grant_auth', methods=['GET']) |
| 12 | +@cross_origin(support_creadentials=True) |
| 13 | +def code_grant_auth(): |
| 14 | + try: |
| 15 | + url = DsClient.code_auth() |
| 16 | + except ApiException as ex: |
| 17 | + return process_error(ex) |
| 18 | + return jsonify({ |
| 19 | + 'reason': 'Unauthorized', |
| 20 | + 'response': 'Permissions should be granted for current integration', |
| 21 | + 'url': url}), 401 |
| 22 | + |
| 23 | + |
| 24 | +@auth.route('/callback', methods=['POST']) |
| 25 | +@cross_origin(support_creadentials=True) |
| 26 | +def callback(): |
| 27 | + try: |
| 28 | + try: |
| 29 | + req_json = request.get_json(force=True) |
| 30 | + code = req_json['code'] |
| 31 | + except TypeError: |
| 32 | + return jsonify(message='Invalid json input'), 400 |
| 33 | + DsClient.callback(code) |
| 34 | + except ApiException: |
| 35 | + return redirect(url_for("auth.jwt_auth"), code=307) |
| 36 | + return jsonify(message="Logged in with code grant"), 200 |
| 37 | + |
| 38 | + |
| 39 | +@auth.route('/jwt_auth', methods=['POST']) |
| 40 | +@cross_origin(support_creadentials=True) |
| 41 | +def jwt_auth(): |
| 42 | + try: |
| 43 | + DsClient.update_token() |
| 44 | + except ApiException as ex: |
| 45 | + return process_error(ex) |
| 46 | + return jsonify(message="Logged in with JWT"), 200 |
| 47 | + |
| 48 | + |
| 49 | +@auth.route('/get_status', methods=['GET']) |
| 50 | +@cross_origin(support_creadentials=True) |
| 51 | +def get_status(): |
| 52 | + if DsClient.code_grant: |
| 53 | + return jsonify(logged=DsClient.logged, auth_type="code_grant"), 200 |
| 54 | + elif DsClient.jwt_auth: |
| 55 | + return jsonify(logged=DsClient.logged, auth_type="jwt"), 200 |
| 56 | + return jsonify(logged=DsClient.logged, auth_type="undefined"), 200 |
| 57 | + |
| 58 | + |
| 59 | +@auth.route('/logout', methods=['POST']) |
| 60 | +@cross_origin(support_credentials=True) |
| 61 | +def log_out(): |
| 62 | + DsClient.destroy() |
| 63 | + return jsonify(message="Logged out"), 200 |
| 64 | + |
| 65 | + |
| 66 | +@auth.route('/check_payment', methods=['GET']) |
| 67 | +@cross_origin(support_credentials=True) |
| 68 | +def check_payment(): |
| 69 | + try: |
| 70 | + if DsClient.check_payment_gateway(): |
| 71 | + return jsonify(message="User has a payment gateway account"), 200 |
| 72 | + else: |
| 73 | + return jsonify(message="User doesn't have a payment gateway account"), 402 |
| 74 | + except ApiException as ex: |
| 75 | + return process_error(ex) |
0 commit comments