refactor finalize/oauth flow
This commit is contained in:
parent
e4b60c1ac2
commit
5535ea5fb5
6 changed files with 16 additions and 36 deletions
|
@ -18,7 +18,7 @@ class Hoster(ABC):
|
||||||
return Package.objects.filter(home_repo__startswith=self.base_url)
|
return Package.objects.filter(home_repo__startswith=self.base_url)
|
||||||
|
|
||||||
def _get_callback_uri(self, request):
|
def _get_callback_uri(self, request):
|
||||||
return request.build_absolute_uri(reverse('editor.finalize.oauth.callback', kwargs={'hoster': self.name}))
|
return request.build_absolute_uri(reverse('editor.oauth.callback', kwargs={'hoster': self.name}))
|
||||||
|
|
||||||
def _get_session_data(self, request):
|
def _get_session_data(self, request):
|
||||||
request.session.modified = True
|
request.session.modified = True
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
<form action="{% url 'editor.finalize' %}" method="POST" name="redirect">
|
<form action="{% url 'editor.finalize' %}" method="POST" name="redirect">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<input type="hidden" name="data" value="{{ data }}">
|
<input type="hidden" name="data" value="{{ data }}">
|
||||||
<input type="hidden" name="check" value="1">
|
<input type="hidden" name="action" value="check">
|
||||||
<img src="{% static 'img/loader.gif' %}">
|
<img src="{% static 'img/loader.gif' %}">
|
||||||
Redirecting…
|
Redirecting…
|
||||||
</form>
|
</form>
|
||||||
|
|
|
@ -42,9 +42,10 @@
|
||||||
<h2>Sign in with {{ hoster.title }}</h2>
|
<h2>Sign in with {{ hoster.title }}</h2>
|
||||||
<p>Please sign in to continue and propose your edit.</p>
|
<p>Please sign in to continue and propose your edit.</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<form action="{% url 'editor.finalize.oauth' %}" method="POST" data-task="{{ task }}">
|
<form action="{% url 'editor.finalize' %}" method="POST" data-task="{{ task }}">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<input type="hidden" name="data" value="{{ data }}">
|
<input type="hidden" name="data" value="{{ data }}">
|
||||||
|
<input type="hidden" name="action" value="oauth">
|
||||||
<p>
|
<p>
|
||||||
<button type="submit" class="btn btn-lg btn-primary">Sign in with {{ hoster.title }}</button><br>
|
<button type="submit" class="btn btn-lg btn-primary">Sign in with {{ hoster.title }}</button><br>
|
||||||
<small><em>
|
<small><em>
|
||||||
|
|
|
@ -1,15 +1,12 @@
|
||||||
from django.conf.urls import url
|
from django.conf.urls import url
|
||||||
from django.views.generic import TemplateView
|
from django.views.generic import TemplateView
|
||||||
|
|
||||||
from c3nav.editor.views import (edit_feature, finalize, finalize_oauth_callback, finalize_oauth_progress,
|
from c3nav.editor.views import edit_feature, finalize, oauth_callback
|
||||||
finalize_oauth_redirect)
|
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^$', TemplateView.as_view(template_name='editor/map.html'), name='editor.index'),
|
url(r'^$', TemplateView.as_view(template_name='editor/map.html'), name='editor.index'),
|
||||||
url(r'^features/(?P<feature_type>[^/]+)/add/$', edit_feature, name='editor.feature.add'),
|
url(r'^features/(?P<feature_type>[^/]+)/add/$', edit_feature, name='editor.feature.add'),
|
||||||
url(r'^features/edit/(?P<name>[^/]+)/$', edit_feature, name='editor.feature.edit'),
|
url(r'^features/edit/(?P<name>[^/]+)/$', edit_feature, name='editor.feature.edit'),
|
||||||
url(r'^finalize/$', finalize, name='editor.finalize'),
|
url(r'^finalize/$', finalize, name='editor.finalize'),
|
||||||
url(r'^finalize/oauth/$', finalize_oauth_redirect, name='editor.finalize.oauth'),
|
url(r'^oauth/(?P<hoster>[^/]+)/callback$', oauth_callback, name='editor.oauth.callback')
|
||||||
url(r'^finalize/oauth/progress$', finalize_oauth_progress, name='editor.finalize.oauth.progress'),
|
|
||||||
url(r'^finalize/oauth/(?P<hoster>[^/]+)/callback$', finalize_oauth_callback, name='editor.finalize.oauth.callback')
|
|
||||||
]
|
]
|
||||||
|
|
|
@ -99,7 +99,8 @@ def edit_feature(request, feature_type=None, name=None):
|
||||||
def finalize(request):
|
def finalize(request):
|
||||||
if 'data' not in request.POST:
|
if 'data' not in request.POST:
|
||||||
raise SuspiciousOperation('Missing data.')
|
raise SuspiciousOperation('Missing data.')
|
||||||
data = signing.loads(request.POST['data'])
|
raw_data = request.POST['data']
|
||||||
|
data = signing.loads(raw_data)
|
||||||
|
|
||||||
if data['type'] != 'editor.edit':
|
if data['type'] != 'editor.edit':
|
||||||
raise SuspiciousOperation('Wrong data type.')
|
raise SuspiciousOperation('Wrong data type.')
|
||||||
|
@ -109,8 +110,12 @@ def finalize(request):
|
||||||
if package is not None:
|
if package is not None:
|
||||||
hoster = get_hoster_for_package(package)
|
hoster = get_hoster_for_package(package)
|
||||||
|
|
||||||
if request.POST.get('check'):
|
action = request.POST.get('action')
|
||||||
|
if action == 'check':
|
||||||
hoster.check_state(request)
|
hoster.check_state(request)
|
||||||
|
elif action == 'oauth':
|
||||||
|
hoster.set_tmp_data(request, raw_data)
|
||||||
|
return redirect(hoster.get_auth_uri(request))
|
||||||
|
|
||||||
hoster_state = hoster.get_state(request)
|
hoster_state = hoster.get_state(request)
|
||||||
hoster_error = hoster.get_error(request) if hoster_state == 'logged_out' else None
|
hoster_error = hoster.get_error(request) if hoster_state == 'logged_out' else None
|
||||||
|
@ -123,7 +128,7 @@ def finalize(request):
|
||||||
form = CommitForm({'commit_msg': data['commit_msg']})
|
form = CommitForm({'commit_msg': data['commit_msg']})
|
||||||
|
|
||||||
return render(request, 'editor/finalize.html', {
|
return render(request, 'editor/finalize.html', {
|
||||||
'data': request.POST['data'],
|
'data': raw_data,
|
||||||
'action': data['action'],
|
'action': data['action'],
|
||||||
'commit_id': data['commit_id'],
|
'commit_id': data['commit_id'],
|
||||||
'commit_form': form,
|
'commit_form': form,
|
||||||
|
@ -136,30 +141,7 @@ def finalize(request):
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@require_POST
|
def oauth_callback(request, hoster):
|
||||||
def finalize_oauth_progress(request):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
@require_POST
|
|
||||||
def finalize_oauth_redirect(request):
|
|
||||||
if 'data' not in request.POST:
|
|
||||||
raise SuspiciousOperation('Missing data.')
|
|
||||||
data = signing.loads(request.POST['data'])
|
|
||||||
|
|
||||||
if data['type'] != 'editor.edit':
|
|
||||||
raise SuspiciousOperation('Wrong data type.')
|
|
||||||
|
|
||||||
package = Package.objects.filter(name=data['package_name']).first()
|
|
||||||
hoster = None
|
|
||||||
if package is not None:
|
|
||||||
hoster = get_hoster_for_package(package)
|
|
||||||
|
|
||||||
hoster.set_tmp_data(request, data)
|
|
||||||
return redirect(hoster.get_auth_uri(request))
|
|
||||||
|
|
||||||
|
|
||||||
def finalize_oauth_callback(request, hoster):
|
|
||||||
hoster = hosters.get(hoster)
|
hoster = hosters.get(hoster)
|
||||||
if hoster is None:
|
if hoster is None:
|
||||||
raise Http404
|
raise Http404
|
||||||
|
@ -167,4 +149,4 @@ def finalize_oauth_callback(request, hoster):
|
||||||
data = hoster.get_tmp_data(request)
|
data = hoster.get_tmp_data(request)
|
||||||
hoster.handle_callback_request(request)
|
hoster.handle_callback_request(request)
|
||||||
|
|
||||||
return render(request, 'editor/finalize_oauth_callback.html', {'data': data})
|
return render(request, 'editor/oauth_callback.html', {'data': data})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue