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