...
 
Commits (2)
......@@ -7,14 +7,63 @@
{% block login_status %}
{% if DEBUG_REGISTER_STATUS %}
{{ block.super }}
{{ block.psuper }}
{% endif %}
{% endblock %}
{% block content %}
<form method="post" action=".">
<form id="formilo" method="post" action="." data-woo-url="{{ WOO_URL }}">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="{% trans 'Submit' %}" />
</form>
{% endblock %}
{% block js_scripts %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/zepto/1.2.0/zepto.min.js"></script>
<script>
var formilo = document.getElementById('formilo');
var cartURL = null;
var WOO_URL = formilo.getAttribute('data-woo-url');
var kuketol;
$(formilo).on('submit', post_form);
function post_form(evt) {
evt.preventDefault();
$.post(
formilo.action,
$(formilo).serialize(),
function (data, status, xhr) {
if (xhr.status == 302) {
confirm_token(xhr);
} else {
$('body').html(data);
}
}
)
return false;
}
function gimmie_cookie(name) {
var kuketoj = document.cookie.split(';').filter((item) => item.trim().startsWith(name+'='));
if (kuketoj.length) {
return kuketoj.pop().split('=')[1];
}
}
function confirm_token(xhr) {
$.ajax({
type: 'POST',
url: WOO_URL + '/wp-json/jwt-auth/v1/token/validate',
headers: {
'Authorization': 'Bearer: ' + gimmie_cookie('kuketo'),
}
})
.complete(function(xhr, status){
console.log(arguments);
});
}
</script>
{% endblock %}
import logging
from django import forms
from django.conf import settings
from django.contrib.auth import logout
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.contrib.auth import get_user_model
from captcha.fields import CaptchaField
from cart.models import ChosenReward
from ldapregister.forms import RegistrationForm
from registration.backends.simple.views import RegistrationView
from woocommerce import API as WOOCOMMERCE_API
log = logging.getLogger(__name__)
User = get_user_model()
class CartRegistrationForm(RegistrationForm):
......@@ -22,6 +30,7 @@ class CartRegistrationView(RegistrationView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['DEBUG_REGISTER_STATUS'] = settings.DEBUG_REGISTER_STATUS
context['WOO_URL'] = settings.WOO_URL
return context
def get(self, *args, **kwargs):
......@@ -81,3 +90,32 @@ class CartRegistrationView(RegistrationView):
reward = ChosenReward(user=user, reward=self.reward)
reward.save()
return user
def _jwt_token(self, username, password):
# do the JWT thingy
jwt_wcapi = WOOCOMMERCE_API(
url=settings.WOO_URL,
consumer_key=settings.WOO_CONSUMER_KEY,
consumer_secret=settings.WOO_CONSUMER_SECRET,
wp_api=True,
version="jwt-auth/v1",
query_string_auth=settings.WOO_QUERY_STRING_AUTH,
)
jwt_response = jwt_wcapi.post("token", {"username": username, "password": password})
jwt_json = jwt_response.json()
jwt_token = jwt_json.get("token", None)
return jwt_token
def form_valid(self, form):
"""
Pre authenticate user on shop/wordpress with JWT/
response is an instance of HttpResponseRedirect
"""
response = super().form_valid(form)
# Authorization and Access-Control-Allow-Origin headers don't really reach javascript
token = self._jwt_token(
username=form.cleaned_data[User.USERNAME_FIELD],
password=form.cleaned_data['password1']
)
response.set_cookie('kuketo', token)
return response
......@@ -72,6 +72,6 @@ SPDX-License-Identifier: AGPL-3.0
</div>
</footer>
{% block js_scripts %}{% endblock %}
</body>
</html>