Skip to content

Commit fd64f0e

Browse files
committed
Don't allow users to set the client ID or client secret, and only display the client secret once
1 parent cdf0f15 commit fd64f0e

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

oauth2_provider/templates/oauth2_provider/application_detail.html

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,20 @@ <h3 class="block-center-heading">{{ application.name }}</h3>
77

88
<ul class="unstyled">
99
<li>
10-
<p><b>{% trans "Client id" %}</b></p>
10+
<p><b>{% trans "Client ID" %}</b></p>
1111
<p>{{ application.client_id }}</p>
1212
</li>
1313

14+
{% if client_secret %}
15+
<li>
16+
<p><b>{% trans "Client secret" %}</b></p>
17+
<p>{{ client_secret }}</p>
18+
{% if show_client_secret_once %}
19+
<p class="error">{% translate "This will only be displayed once - copy it now!" %}</p>
20+
{% endif %}
21+
</li>
22+
{% endif %}
23+
1424
<li>
1525
<p><b>{% trans "Hash client secret" %}</b></p>
1626
<p>{{ application.hash_client_secret|yesno:_("yes,no") }}</p>

oauth2_provider/views/application.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ class ApplicationRegistration(LoginRequiredMixin, CreateView):
2222
View used to register a new Application for the request.user
2323
"""
2424

25+
context_object_name = "application"
2526
template_name = "oauth2_provider/application_registration_form.html"
27+
success_template_name = "oauth2_provider/application_detail.html"
2628

2729
def get_form_class(self):
2830
"""
@@ -32,8 +34,6 @@ def get_form_class(self):
3234
get_application_model(),
3335
fields=(
3436
"name",
35-
"client_id",
36-
"client_secret",
3737
"hash_client_secret",
3838
"client_type",
3939
"authorization_grant_type",
@@ -46,7 +46,22 @@ def get_form_class(self):
4646

4747
def form_valid(self, form):
4848
form.instance.user = self.request.user
49-
return super().form_valid(form)
49+
if not form.cleaned_data["hash_client_secret"]:
50+
return super().form_valid(form)
51+
52+
client_secret = form.instance.client_secret
53+
self.object = form.save()
54+
return self.response_class(
55+
request=self.request,
56+
template=self.success_template_name,
57+
context=self.get_context_data(
58+
client_secret=client_secret,
59+
show_client_secret_once=self.object.hash_client_secret,
60+
**{self.context_object_name: self.object},
61+
),
62+
using=self.template_engine,
63+
content_type=self.content_type,
64+
)
5065

5166

5267
class ApplicationDetail(ApplicationOwnerIsUserMixin, DetailView):
@@ -57,6 +72,12 @@ class ApplicationDetail(ApplicationOwnerIsUserMixin, DetailView):
5772
context_object_name = "application"
5873
template_name = "oauth2_provider/application_detail.html"
5974

75+
def get_context_data(self, **kwargs):
76+
ctx = super().get_context_data(**kwargs)
77+
if not ctx["application"].hash_client_secret:
78+
ctx["client_secret"] = ctx["application"].client_secret
79+
return ctx
80+
6081

6182
class ApplicationList(ApplicationOwnerIsUserMixin, ListView):
6283
"""

0 commit comments

Comments
 (0)