Skip to content

Commit 18f3d52

Browse files
author
James Fuqian
authored
[BB2-975] Fix creds delivery template "fetch" and "download" generated URI issue (#1007)
* add trace for debugging creds delivery error. * fix fetch / download urls.
1 parent efe0268 commit 18f3d52

File tree

3 files changed

+62
-38
lines changed

3 files changed

+62
-38
lines changed

apps/creds/templates/get_creds.html

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
{% endblock %}
88

99
{% block bannerDescription %}
10-
The Blue Button 2.0 API Prod Access Credentialing allows you to obtain the OAUTH2.0 app credentials for your application.
10+
The Blue Button 2.0 API Prod Access Credentialing allows you to obtain the OAUTH2.0 app credentials for your
11+
application.
1112
{% endblock %}
1213

1314
{% block Content %}
@@ -22,33 +23,39 @@ <h2>Organization: {{ org_name }}</h2>
2223
<h2>Application: {{ app_name }}</h2>
2324
<br />
2425

25-
<a href="{{fetch_creds_link}}/?action=fetch" class="ds-c-button ds-u-margin-y--2 ds-c-button--primary ds-u-color--white">Fetch Credentials</a>
26+
<a href="/creds/{{creds_req_id}}/?action=fetch"
27+
class="ds-c-button ds-u-margin-y--2 ds-c-button--primary ds-u-color--white">Fetch Credentials</a>
2628
<br />
2729

28-
<a href="{{fetch_creds_link}}/?action=download" class="ds-c-button ds-u-margin-y--2 ds-c-button--primary ds-u-color--white">Download Credentials</a>
30+
<a href="/creds/{{creds_req_id}}/?action=download"
31+
class="ds-c-button ds-u-margin-y--2 ds-c-button--primary ds-u-color--white">Download Credentials</a>
2932
<br />
3033

3134
{% if fetch == "fetch" %}
3235

33-
<!-- App Credential Section -->
34-
<h2 class="ds-u-margin-bottom--2">App Credentials</h2>
35-
36-
<div class="bb-c-card sandbox-app-card">
36+
<!-- App Credential Section -->
37+
<h2 class="ds-u-margin-bottom--2">App Credentials</h2>
38+
39+
<div class="bb-c-card sandbox-app-card">
3740
<!-- Application Credentials -->
3841
<form class="app-credentials">
39-
<span class="copy-confirmation" id="confirm-id-{{ client_id }}" aria-live="assertive" aria-role="alert">ID Copied to Clipboard</span>
40-
<span class="copy-confirmation" id="confirm-secret-{{ client_id }}" aria-live="assertive" aria-role="alert">Secret Copied to Clipboard</span>
42+
<span class="copy-confirmation" id="confirm-id-{{ client_id }}" aria-live="assertive"
43+
aria-role="alert">ID Copied to Clipboard</span>
44+
<span class="copy-confirmation" id="confirm-secret-{{ client_id }}" aria-live="assertive"
45+
aria-role="alert">Secret Copied to Clipboard</span>
4146
<a class="app-credentials-toggle" id="{{ client_id }}" tabindex="0">Show/Hide Credentials</a>
4247
<label for"id-{{ application.client_id }}">Client ID</label>
43-
<input type="password" value="{{ client_id }}" id="id-{{ client_id }}" onclick="copyCredential(this.id)" />
48+
<input type="password" value="{{ client_id }}" id="id-{{ client_id }}"
49+
onclick="copyCredential(this.id)" />
4450
<label for"secret-{{ application.client_secret }}">Client Secret</label>
45-
<input type="password" value="{{ client_secret }}" id="secret-{{ client_id }}" onclick="copyCredential(this.id)" />
51+
<input type="password" value="{{ client_secret }}" id="secret-{{ client_id }}"
52+
onclick="copyCredential(this.id)" />
4653
</form>
47-
</div>
54+
</div>
4855

49-
{% endif %}
56+
{% endif %}
5057
</div>
5158
</div>
5259
</div>
53-
54-
{% endblock %}
60+
61+
{% endblock %}

apps/creds/utils.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,27 @@ def get_creds_by_id(creds_request_id: str):
1919

2020

2121
def get_creds_by_obj(creds_req: CredentialingReqest):
22-
creds_dict = {"user_name": None,
23-
"org_name": None,
24-
"app_id": None,
25-
"app_name": None,
26-
"client_id": None,
27-
"client_secret": None}
22+
creds_dict = {
23+
"user_name": None,
24+
"org_name": None,
25+
"app_id": None,
26+
"app_name": None,
27+
"client_id": None,
28+
"client_secret": None,
29+
}
2830
if creds_req:
2931

3032
app = Application.objects.get(pk=creds_req.application_id)
3133

3234
if app:
33-
creds_dict.update({
34-
"app_id": app.id,
35-
"app_name": app.name,
36-
"client_id": app.client_id,
37-
"client_secret": app.client_secret, })
35+
creds_dict.update(
36+
{
37+
"app_id": app.id,
38+
"app_name": app.name,
39+
"client_id": app.client_id,
40+
"client_secret": app.client_secret,
41+
}
42+
)
3843

3944
user = User.objects.get(pk=app.user_id)
4045

apps/creds/views.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from rest_framework import exceptions, status
99
from rest_framework.views import APIView
1010

11-
from apps.creds.utils import get_creds_by_obj, get_url
11+
from apps.creds.utils import get_creds_by_obj
1212
from .models import CredentialingReqest
1313

1414
Application = get_application_model()
@@ -19,20 +19,22 @@ def get(self, request, *args, **kwargs):
1919
logger = logging.getLogger(logging.AUDIT_CREDS_REQUEST_LOGGER, request)
2020

2121
creds_req_id = kwargs.get("prod_cred_req_id")
22-
2322
creds_req = self._get_creds_req(creds_req_id)
2423

2524
# check if expired
2625
if self._is_expired(creds_req):
27-
raise exceptions.PermissionDenied("Generated credentialing request expired.", code=status.HTTP_403_FORBIDDEN)
26+
raise exceptions.PermissionDenied(
27+
"Generated credentialing request expired.",
28+
code=status.HTTP_403_FORBIDDEN,
29+
)
2830

2931
creds_dict = get_creds_by_obj(creds_req)
3032
# fetch creds request and update visits count and relevant timestamps
3133
creds_req.visits_count = creds_req.visits_count + 1
3234
creds_req.last_visit = datetime.datetime.now(datetime.timezone.utc)
3335

34-
ctx = {"fetch_creds_link": get_url(creds_req_id)}
35-
ctx.update(creds_dict)
36+
ctx = creds_dict
37+
ctx.update({"creds_req_id": creds_req_id})
3638

3739
log_dict = {
3840
"type": "credentials request",
@@ -50,8 +52,10 @@ def get(self, request, *args, **kwargs):
5052
log_dict.update(action=action)
5153
else:
5254
# already fetched, fetch again forbidden
53-
raise exceptions.PermissionDenied("Credentials already fetched (download), doing it again not allowed.",
54-
code=status.HTTP_403_FORBIDDEN)
55+
raise exceptions.PermissionDenied(
56+
"Credentials already fetched (download), doing it again not allowed.",
57+
code=status.HTTP_403_FORBIDDEN,
58+
)
5559
else:
5660
# do not give out creds yet if not a fetch request
5761
if "client_id" in ctx:
@@ -65,20 +69,28 @@ def get(self, request, *args, **kwargs):
6569

6670
if action == "download":
6771
response = JsonResponse(creds_dict)
68-
response['Content-Disposition'] = 'attachment; filename="{}.json"'.format(creds_req_id)
72+
response["Content-Disposition"] = 'attachment; filename="{}.json"'.format(
73+
creds_req_id
74+
)
6975
return response
7076
else:
71-
return render(request, 'get_creds.html', ctx)
77+
return render(request, "get_creds.html", ctx)
7278

7379
def _is_expired(self, creds_req):
74-
t_elapsed_since_created = datetime.datetime.now(datetime.timezone.utc) - creds_req.created_at
75-
return t_elapsed_since_created.seconds > settings.CREDENTIALS_REQUEST_URL_TTL * 60
80+
t_elapsed_since_created = (
81+
datetime.datetime.now(datetime.timezone.utc) - creds_req.created_at
82+
)
83+
return (
84+
t_elapsed_since_created.seconds > settings.CREDENTIALS_REQUEST_URL_TTL * 60
85+
)
7686

7787
def _get_creds_req(self, id):
7888

7989
if not id:
8090
# bad request
81-
raise exceptions.ValidationError("Credentialing request ID missing.", code=status.HTTP_400_BAD_REQUEST)
91+
raise exceptions.ValidationError(
92+
"Credentialing request ID missing.", code=status.HTTP_400_BAD_REQUEST
93+
)
8294

8395
creds_req = None
8496

0 commit comments

Comments
 (0)