Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
feat(social-authentication): add github authentication to booth
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahydul committed Nov 23, 2023
1 parent 579062f commit df3e700
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 9 deletions.
5 changes: 3 additions & 2 deletions decide/authentication/urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from django.urls import include, path
from rest_framework.authtoken.views import obtain_auth_token

from .views import GetUserView, LogoutView, RegisterView
from .views import GetUserView, LogoutView, RegisterView, account, ObtainSocialAuthTokenView


urlpatterns = [
path('login/', obtain_auth_token),
path('login-social/', ObtainSocialAuthTokenView.as_view()),
path('logout/', LogoutView.as_view()),
path('getuser/', GetUserView.as_view()),
path('register/', RegisterView.as_view()),
]
]
22 changes: 21 additions & 1 deletion decide/authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,29 @@
from rest_framework.views import APIView
from rest_framework.authtoken.models import Token
from django.contrib.auth.models import User
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.db import IntegrityError
from django.shortcuts import get_object_or_404
from django.shortcuts import get_object_or_404, render
from django.core.exceptions import ObjectDoesNotExist

from allauth.socialaccount.models import SocialToken
from allauth.account.adapter import DefaultAccountAdapter
from allauth.utils import get_request_param

from .serializers import UserSerializer


class ObtainSocialAuthTokenView(APIView):

def post(self, request):
userid = request.data.get('userid', '')
user = get_object_or_404(User,id=userid)

# socialtoken = SocialToken.objects.get(account__user=request.user, account__provider='github')
token, created = Token.objects.get_or_create(user=user)

return Response({'token': token.key})

class GetUserView(APIView):
def post(self, request):
key = request.data.get('token', '')
Expand Down Expand Up @@ -53,3 +69,7 @@ def post(self, request):
except IntegrityError:
return Response({}, status=HTTP_400_BAD_REQUEST)
return Response({'user_pk': user.pk, 'token': token.key}, HTTP_201_CREATED)


def account(request):
return render(request, 'account.html')
56 changes: 50 additions & 6 deletions decide/booth/templates/booth/booth.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% extends "base.html" %}
{% load i18n static %}
{% load socialaccount %}

{% block extrahead %}
<link type="text/css" rel="stylesheet"
Expand Down Expand Up @@ -109,6 +110,17 @@ <h5 class="modal-title" id="registerModalLabel">Login</h5>
<button class="btn btn-primary mt-3" type="submit" data-bs-dismiss="modal">{% trans "Login" %}</button>
</form>
</div>

<div class="modal-body">
<h5 class="modal-title" id="registerModalLabel">Or login with Github:

<a title="GitHub" href="/accounts/github/login/?next={{request.path}}">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-github" viewBox="0 0 16 16" style="--darkreader-inline-fill: currentColor;" data-darkreader-inline-fill="">
<path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path>
</svg>
</a>
</h5>
</div>
</div>
</div>
</div>
Expand Down Expand Up @@ -151,7 +163,7 @@ <h2>[[ voting.question.desc ]]</h2>
<script src="{% static "crypto/elgamal.js" %}"></script>

<!-- Vuejs -->
<script src="https://unpkg.com/vue@latest"</script>
<script src="https://unpkg.com/vue@latest"></script>
<script src="https://unpkg.com/babel-polyfill@latest/dist/polyfill.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/js/bootstrap.js"></script>

Expand Down Expand Up @@ -184,11 +196,14 @@ <h2>[[ voting.question.desc ]]</h2>
}
},
beforeMount() {
this.init()
this.init();
ElGamal.BITS = this.keybits;
},
methods: {
init() {
// If there is not a social account logged in: usual pipeline
{% if not user.socialaccount_set.exists%}

var cookies = document.cookie.split("; ");
cookies.forEach((c) => {
var cs = c.split("=");
Expand All @@ -197,6 +212,24 @@ <h2>[[ voting.question.desc ]]</h2>
this.getUser();
}
});

// If there is a social account (with SocialToken) logged in we create a django Token
{% else %}
var data = {userid: "{{user.socialaccount_set.all.0.user.id}}"};
this.postData('{% url "gateway" "authentication" "/login-social/" %}', data)
.then(data => {
document.cookie = 'decide='+data.token+';';
this.token = data.token;
this.getUser();
this.alertShow = false;
})
.catch(error => {
this.showAlert("danger", '{% trans "Error: " %}' + error);
});

{% endif %}


},
postData(url, data) {
// Default options are marked with *
Expand All @@ -223,7 +256,7 @@ <h2>[[ voting.question.desc ]]</h2>
},
onSubmitLogin(evt) {
evt.preventDefault();
this.postData("{% url "gateway" "authentication" "/login/" %}", this.form)
this.postData('{% url "gateway" "authentication" "/login/" %}', this.form)
.then(data => {
document.cookie = 'decide='+data.token+';';
this.token = data.token;
Expand All @@ -236,7 +269,7 @@ <h2>[[ voting.question.desc ]]</h2>
},
getUser(evt) {
var data = {token: this.token};
this.postData("{% url "gateway" "authentication" "/getuser/" %}", data)
this.postData('{% url "gateway" "authentication" "/getuser/" %}', data)
.then(data => {
this.user = data;
this.signup = false;
Expand All @@ -248,13 +281,24 @@ <h2>[[ voting.question.desc ]]</h2>
decideLogout(evt) {
evt.preventDefault();
var data = {token: this.token};
this.postData("{% url "gateway" "authentication" "/logout/" %}", data);
this.postData('{% url "gateway" "authentication" "/logout/" %}', data);

data = this.user;

this.token = null;
this.user = null;
this.alertShow = false;
document.cookie = 'decide=;';
this.signup = true;
this.successVote = false;

// If there is a social account logged in we need to logout from it
{% if user.socialaccount_set.exists %}
console.log("LOGING OUT FROM SOCIAL");
// fetch('/accounts/logout');
fetch("{% url 'account_logout' %}");
{% endif %}

},
decideEncrypt() {
var bigmsg = BigInt.fromJSONObject(this.selected.toString());
Expand All @@ -270,7 +314,7 @@ <h2>[[ voting.question.desc ]]</h2>
voter: this.user.id,
token: this.token
}
this.postData("{% url "gateway" "store" "/" %}", data)
this.postData('{% url "gateway" "store" "/" %}', data)
.then(data => {
this.successVote = true;
this.alertShow = false;
Expand Down

0 comments on commit df3e700

Please sign in to comment.