--- /dev/null
+var page = require('webpage').create();
+var system = require('system');
+
+var input = JSON.parse(system.stdin.read(2000)); // no .readAll()...
+
+page.open(input.address, function() {
+ page.onLoadFinished = function() {
+ console.log(JSON.stringify({'result': 'ok', 'cookies': page.cookies}));
+ phantom.exit();
+ }
+ page.evaluate(function(input) {
+ if ($(input.username_locator).length == 0) {
+ console.log(JSON.stringify({'result': 'ok', 'cookies': page.cookies}));
+ phantom.exit();
+ }
+ $(input.username_locator).val(input.username_value);
+ $(input.password_locator).val(input.password_value);
+ $(input.username_locator).parents('form').find('input[type=submit]').click();
+ }, input);
+});
https://docs.djangoproject.com/en/1.7/ref/settings/
"""
+from django.conf import global_settings
+
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
+ 'gadjo',
)
MIDDLEWARE_CLASSES = (
STATIC_URL = '/_mandaye/static/'
+# Serve xstatic files, required for gadjo
+STATICFILES_FINDERS = global_settings.STATICFILES_FINDERS + \
+ ('gadjo.finders.XStaticFinder',)
+
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'mandayejs', 'static'),
)
os.path.join(BASE_DIR, 'mandayejs', 'templates'),
)
+LOGIN_URL = 'auth_login'
LOGIN_REDIRECT_URL = 'post-login'
+
+local_settings_file = os.environ.get('MANDAYEJS_SETTINGS_FILE',
+ os.path.join(os.path.dirname(__file__), 'local_settings.py'))
+if os.path.exists(local_settings_file):
+ execfile(local_settings_file)
--- /dev/null
+$(function() {
+ $('#post-login-frame').on('load', function() {
+ window.location = '/';
+ });
+});
+{% load gadjo staticfiles %}
<html>
+<head>
+ <script src="{% xstatic 'jquery' 'jquery.min.js' %}"></script>
+ <script src="{% static 'mandaye.post.js' %}"></script>
+</head>
<body>
- post login
+ Please wait...
+ <br/>
+ <iframe id="post-login-frame" src="{% url 'post-login-do' %}" style="display: none;">
+ </iframe>
</body>
</html>
url(r'^_mandaye/login/$', 'mandayejs.views.login', name='auth_login'),
url(r'^_mandaye/logout/$', 'mandayejs.views.logout', name='auth_logout'),
url(r'^_mandaye/post-login/$', 'mandayejs.views.post_login', name='post-login'),
+ url(r'^_mandaye/post-login-do/$', 'mandayejs.views.post_login_do', name='post-login-do'),
url(r'^_mandaye/admin/', include(admin.site.urls)),
)
+import json
+import os
+import subprocess
+import urlparse
+
+from django.conf import settings
from django.contrib.auth import views as auth_views
from django.contrib.auth import logout as auth_logout
+from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.views.generic.base import TemplateView
class PostLogin(TemplateView):
template_name = 'mandaye/post-login.html'
-post_login = PostLogin.as_view()
+ def get_context_data(self, **kwargs):
+ context = super(PostLogin, self).get_context_data(**kwargs)
+ context['address'] = self.request.build_absolute_uri('/login'),
+ return context
+
+post_login = login_required(PostLogin.as_view())
+
+def post_login_do(request, *args, **kwargs):
+ # XXX: this should come from both configuration (address and locators) and
+ # user database (credentials)
+ login_info = {
+ 'username_locator': '#username',
+ 'username_value': settings.MANDAYE_USERNAME,
+ 'password_locator': '#password',
+ 'password_value': settings.MANDAYE_PASSWORD,
+ 'address': request.build_absolute_uri('/login'),
+ 'cookies': [],
+ }
+ phantom = subprocess.Popen(['/usr/bin/phantomjs',
+ '--ignore-ssl-errors=yes',
+ '--ssl-protocol=any',
+ os.path.join(settings.BASE_DIR, 'do_login.js')],
+ close_fds=True,
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE)
+ stdout, stderr = phantom.communicate(json.dumps(login_info))
+ result = json.loads(stdout)
+ if result.get('result') != 'ok':
+ return HttpResponseRedirect('/')
+ response = HttpResponseRedirect('/')
+ for cookie in result.get('cookies'):
+ response.set_cookie(
+ key=cookie.get('name'),
+ value=cookie.get('value'),
+ path=cookie.get('path'),
+ secure=cookie.get('secure'),
+ httponly=cookie.get('httponly'),
+ )
+ return response