Skip to content

Commit e001690

Browse files
committed
Use POST when sending to third party oauth
Instead of prepopulating a GET request that could generate a session, createa a form with different submit buttons and use that. In the brave new world of AI bots, nobody cares about robots.txt anymore, so we'd get hit by a lot of requests specifically for these logins that were then thrown away because they couldn't log in on the third party site.
1 parent e48157d commit e001690

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

media/css/main.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,6 +1834,12 @@ th.organisation-logo {
18341834
max-width: 650px;
18351835
}
18361836

1837+
/* Buttons that are images */
1838+
button.imagebutton {
1839+
border: 0;
1840+
padding: 0;
1841+
}
1842+
18371843

18381844
/** ALL RESPONSIVE QUERIES HERE */
18391845
/* Small devices (landscape phones, 576px and up)*/

pgweb/account/oauthclient.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from django.conf import settings
22
from django.contrib.auth import login as django_login
3-
from django.http import HttpResponse, HttpResponseRedirect
3+
from django.http import HttpResponse, HttpResponseRedirect, Http404
4+
from django.views.decorators.http import require_POST, require_GET
5+
from django.views.decorators.csrf import csrf_exempt
46
from django.contrib.auth.models import User
57

68
import os
@@ -66,7 +68,10 @@ def _login_oauth(request, provider, authurl, tokenurl, scope, authdatafunc):
6668
redir = '{0}/account/login/{1}/'.format(settings.SITE_ROOT, provider)
6769

6870
oa = OAuth2Session(client_id, scope=scope, redirect_uri=redir)
69-
if 'code' in request.GET:
71+
if request.method == 'GET':
72+
if 'code' not in request.GET:
73+
raise OAuthException("No code provided")
74+
7075
log.info("Completing {0} oauth2 step from {1}".format(provider, get_client_ip(request)))
7176

7277
# Receiving a login request from the provider, so validate data
@@ -284,8 +289,21 @@ def _twitter_auth_data(oa):
284289
_twitter_auth_data)
285290

286291

292+
@require_POST
293+
@csrf_exempt
294+
def initiate_oauth_login(request):
295+
if 'submit' not in request.POST:
296+
return HttpResponse("Invalid post", status=400)
297+
return _oauth_login_dispatch(request.POST['submit'], request)
298+
299+
300+
@require_GET
287301
@queryparams('code', 'state', 'next', 'oauth_verifier')
288302
def login_oauth(request, provider):
303+
return _oauth_login_dispatch(provider, request)
304+
305+
306+
def _oauth_login_dispatch(provider, request):
289307
fn = 'oauth_login_{0}'.format(provider)
290308
m = sys.modules[__name__]
291309
if hasattr(m, fn):
@@ -294,5 +312,7 @@ def login_oauth(request, provider):
294312
except OAuthException as e:
295313
return HttpResponse(e)
296314
except Exception as e:
297-
log.error('Exception during OAuth: %s' % e)
315+
log.error('Exception during OAuth: {}'.format(e))
298316
return HttpResponse('An unhandled exception occurred during the authentication process')
317+
else:
318+
raise Http404()

pgweb/account/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
# Log in, logout, change password etc
4343
re_path(r'^login/$', pgweb.account.views.login),
44+
re_path(r'^login/oauth/$', pgweb.account.oauthclient.initiate_oauth_login),
4445
re_path(r'^logout/$', pgweb.account.views.logout),
4546
re_path(r'^changepwd/$', pgweb.account.views.changepwd),
4647
re_path(r'^changepwd/done/$', pgweb.account.views.change_done),

templates/account/login.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ <h2>Community account sign-in</h2>
4646

4747
{%if oauth_providers%}
4848
<h2>Third party sign in</h2>
49+
<form method="post" action="/account/login/oauth/">
50+
<input type="hidden" name="next" value="{{next}}" />
4951
{%for p,d in oauth_providers%}
50-
<p><a href="/account/login/{{p}}/?next={{next}}"><img src="/media/img/misc/btn_login_{{p}}.png" alt="Sign in with {{p|capfirst}}" /></a></p>
52+
<p><button type="submit" name="submit" value="{{p}}" class="imagebutton"><img src="/media/img/misc/btn_login_{{p}}.png" alt="Sign in with {{p|capfirst}}"></button></p>
5153
{%endfor%}
54+
</form>
5255
{%endif%}
5356

5457
{%endblock%}

0 commit comments

Comments
 (0)