Skip to content

Commit 2582a1c

Browse files
authored
Changing the authentication/authorization logic (#14)
* Changed: .env, .gitignore, README.MD, added .pylintrc * fixes in app/api/ * fixes in app/ * front-end fixes
1 parent 6e8941a commit 2582a1c

File tree

29 files changed

+443
-485
lines changed

29 files changed

+443
-485
lines changed

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ REACT_APP_DS_RETURN_URL=http://localhost:3000
2828
REACT_APP_API_BASE_URL=http://localhost:5001/api
2929

3030
# The DS Authentication server
31-
REACT_APP_DS_AUTH_SERVER=https://account-d.docusign.com
31+
DS_AUTH_SERVER=https://account-d.docusign.com
3232

3333
# Demo Docusign API URL
3434
REACT_APP_DS_DEMO_SERVER=https://demo.docusign.net

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
*.pyc
22
.idea
33

4+
.env
5+
__pycache__/
6+
flask_session/
7+
48
# pyenv
59
.python-version
6-
/venv
10+
/venv/
711

812
# Mac/OSX
913
.DS_Store

.pylintrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[MASTER]
2+
disable=
3+
C0114, # (missing-module-docstring)
4+
C0115, # (missing-class-docstring)
5+
C0116, # (missing-function-docstring)

README.MD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ MySure demonstrates the following:
4848
* **DS_PRIVATE_KEY** - Private key string, source or path; for instance: /app/id_rsa
4949
* **REACT_APP_DS_RETURN_URL** - URL where the back end of the application is located (If you run it locally, use `http://localhost:3000`)
5050
* **REACT_APP_API_BASE_URL** - URL where the front end of the application is located; will be used by Docusign to redirect back after signing ceremony (If you run it locally, use `http://localhost:5001/api`)
51-
* **REACT_APP_DS_AUTH_SERVER** - The DocuSign authentication server (for testing purposes, use `https://account-d.docusign.com`)
51+
* **DS_AUTH_SERVER** - The DocuSign authentication server (for testing purposes, use `https://account-d.docusign.com`)
5252
* **REACT_APP_DS_DEMO_SERVER** - Link to the DocuSign demo server (for testing purposes, use `https://demo.docusign.net`)
5353
* **REACT_APP_DS_CLICKWRAP_URL** - Link to the hosted clickwrap client (for testing purposes, use `//demo.docusign.net/clickapi/sdk/latest/docusign-click.js`)
5454

app/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from flask import Flask
22
from flask_cors import CORS
3-
from flask_session import Session
3+
from dotenv import load_dotenv
44

55
from app.api import clickwrap, requests, common, auth
66

77

8+
load_dotenv()
9+
810
URL_PREFIX = '/api'
911

1012
app = Flask(__name__)
@@ -13,5 +15,4 @@
1315
app.register_blueprint(common, url_prefix=URL_PREFIX)
1416
app.register_blueprint(requests, url_prefix=URL_PREFIX)
1517
app.register_blueprint(auth, url_prefix=URL_PREFIX)
16-
Session(app)
1718
cors = CORS(app)

app/api/auth.py

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,84 @@
11
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
33
from flask_cors import cross_origin
44

5-
from app.api.utils import process_error
65
from app.ds_client import DsClient
6+
from .utils import process_error
7+
from .session_data import SessionData
78

89
auth = Blueprint('auth', __name__)
910

1011

1112
@auth.route('/code_grant_auth', methods=['GET'])
12-
@cross_origin(support_creadentials=True)
13+
@cross_origin()
1314
def code_grant_auth():
1415
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)
1819
return jsonify({
1920
'reason': 'Unauthorized',
2021
'response': 'Permissions should be granted for current integration',
21-
'url': url}), 401
22+
'url': url
23+
}), 401
2224

2325

2426
@auth.route('/callback', methods=['POST'])
25-
@cross_origin(support_creadentials=True)
27+
@cross_origin()
2628
def callback():
2729
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'])
3436
except ApiException:
3537
return redirect(url_for("auth.jwt_auth"), code=307)
38+
39+
SessionData.set_auth_data(auth_data)
3640
return jsonify(message="Logged in with code grant"), 200
3741

3842

39-
@auth.route('/jwt_auth', methods=['POST'])
40-
@cross_origin(support_creadentials=True)
43+
@auth.route('/jwt_auth', methods=['GET'])
44+
@cross_origin()
4145
def jwt_auth():
4246
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+
4654
return jsonify(message="Logged in with JWT"), 200
4755

4856

4957
@auth.route('/get_status', methods=['GET'])
50-
@cross_origin(support_creadentials=True)
58+
@cross_origin()
5159
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
5763

5864

5965
@auth.route('/logout', methods=['POST'])
60-
@cross_origin(support_credentials=True)
66+
@cross_origin()
6167
def log_out():
62-
DsClient.destroy()
68+
session.clear()
6369
return jsonify(message="Logged out"), 200
6470

6571

6672
@auth.route('/check_payment', methods=['GET'])
67-
@cross_origin(support_credentials=True)
73+
@cross_origin()
6874
def check_payment():
6975
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

app/api/clickwrap.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from docusign_esign import ApiException
2-
from flask import Blueprint, jsonify, request
2+
from flask import Blueprint, jsonify, request, session
33
from flask_cors import cross_origin
44

55
from app.api.utils import process_error, check_token
@@ -9,20 +9,22 @@
99

1010

1111
@clickwrap.route('/clickwraps/renewal', methods=['POST'])
12-
@cross_origin(supports_credentials=True)
12+
@cross_origin()
1313
@check_token
1414
def insurance_renewal():
1515
"""Create a clickwrap for submitting insurance policy renewal"""
1616
try:
17-
try:
18-
req_json = request.get_json(force=True)
19-
clickwrap_args = {
20-
'terms_name': req_json['terms-name'],
21-
'display_name': req_json['display-name'],
22-
}
23-
except TypeError:
24-
return jsonify(message='Invalid JSON input'), 400
25-
clickwrap = Clickwrap.create(clickwrap_args)
26-
except ApiException as ex:
27-
return process_error(ex)
28-
return jsonify(clickwrap=clickwrap)
17+
req_json = request.get_json(force=True)
18+
except TypeError:
19+
return jsonify(message='Invalid JSON input'), 400
20+
21+
clickwrap_args = {
22+
'terms_name': req_json['terms-name'],
23+
'display_name': req_json['display-name'],
24+
}
25+
26+
try:
27+
clickwrap_ = Clickwrap.create(clickwrap_args, session)
28+
except ApiException as exc:
29+
return process_error(exc)
30+
return jsonify(clickwrap=clickwrap_)

app/api/common.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
common = Blueprint('common', __name__)
44

5-
65
@common.before_app_request
7-
def only_json():
6+
def only_json(): # pylint: disable-msg=inconsistent-return-statements
87
if request.method == 'POST' and not request.is_json:
9-
return jsonify({'error': 'Payload should be a JSON object'}), 400
8+
return jsonify({'error': 'Payload should be a JSON'}), 400

app/api/envelope.py

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)