from django.conf import settings
from django.contrib.auth import login as django_login
-from django.http import HttpResponse, HttpResponseRedirect
+from django.http import HttpResponse, HttpResponseRedirect, Http404
+from django.views.decorators.http import require_POST, require_GET
+from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.models import User
import os
redir = '{0}/account/login/{1}/'.format(settings.SITE_ROOT, provider)
oa = OAuth2Session(client_id, scope=scope, redirect_uri=redir)
- if 'code' in request.GET:
+ if request.method == 'GET':
+ if 'code' not in request.GET:
+ raise OAuthException("No code provided")
+
log.info("Completing {0} oauth2 step from {1}".format(provider, get_client_ip(request)))
# Receiving a login request from the provider, so validate data
_twitter_auth_data)
+@require_POST
+@csrf_exempt
+def initiate_oauth_login(request):
+ if 'submit' not in request.POST:
+ return HttpResponse("Invalid post", status=400)
+ return _oauth_login_dispatch(request.POST['submit'], request)
+
+
+@require_GET
@queryparams('code', 'state', 'next', 'oauth_verifier')
def login_oauth(request, provider):
+ return _oauth_login_dispatch(provider, request)
+
+
+def _oauth_login_dispatch(provider, request):
fn = 'oauth_login_{0}'.format(provider)
m = sys.modules[__name__]
if hasattr(m, fn):
except OAuthException as e:
return HttpResponse(e)
except Exception as e:
- log.error('Exception during OAuth: %s' % e)
+ log.error('Exception during OAuth: {}'.format(e))
return HttpResponse('An unhandled exception occurred during the authentication process')
+ else:
+ raise Http404()
# Log in, logout, change password etc
re_path(r'^login/$', pgweb.account.views.login),
+ re_path(r'^login/oauth/$', pgweb.account.oauthclient.initiate_oauth_login),
re_path(r'^logout/$', pgweb.account.views.logout),
re_path(r'^changepwd/$', pgweb.account.views.changepwd),
re_path(r'^changepwd/done/$', pgweb.account.views.change_done),
{%if oauth_providers%}
<h2>Third party sign in</h2>
+<form method="post" action="/account/login/oauth/">
+ <input type="hidden" name="next" value="{{next}}" />
{%for p,d in oauth_providers%}
-<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>
+ <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>
{%endfor%}
+</form>
{%endif%}
{%endblock%}