This repository has been archived by the owner on May 3, 2024. It is now read-only.
forked from EGCETSII/decide
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(social-authentication): add github authentication to booth
- Loading branch information
Showing
3 changed files
with
74 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
@@ -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> | ||
|
@@ -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> | ||
|
||
|
@@ -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("="); | ||
|
@@ -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 * | ||
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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()); | ||
|
@@ -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; | ||
|