From e82bc00ed23d91016a7cb70f8012e95f76b47827 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 10 Dec 2017 11:16:39 +0400 Subject: [PATCH 1/2] fix styles --- static/css/main.less | 5 +++++ templates/teamlogic/stadions.html | 12 +++++------- templates/teamlogic/teams.html | 8 +++----- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/static/css/main.less b/static/css/main.less index 56ea913..c751eb5 100644 --- a/static/css/main.less +++ b/static/css/main.less @@ -9,6 +9,11 @@ @import "matchPage.less"; @import "articles.less"; +.item-wrapper { + display: inline-flex; + align-items: center; +} + .social-label { width: 100%; padding-top: 7px; diff --git a/templates/teamlogic/stadions.html b/templates/teamlogic/stadions.html index 1448b8a..91ff3fe 100644 --- a/templates/teamlogic/stadions.html +++ b/templates/teamlogic/stadions.html @@ -9,18 +9,16 @@

Стадионы

{% for stadion in object_list %} -
-
-
+
+
{% thumbnail stadion.image "64x64" crop="center" as im %} - + {% endthumbnail %}
- -
{% endfor %}
{% endblock %} diff --git a/templates/teamlogic/teams.html b/templates/teamlogic/teams.html index d1dbe0a..db8a146 100644 --- a/templates/teamlogic/teams.html +++ b/templates/teamlogic/teams.html @@ -11,20 +11,18 @@

Команды

{% for team in object_list %} -
-
-
+
+
{% thumbnail team.image "64x64" crop="center" as im %} {% endthumbnail %}
- -
{% endfor %}
{% pager %} From 5e278582e94019d9ce26ad0b055dfd61d7c9cc96 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 10 Dec 2017 13:03:48 +0400 Subject: [PATCH 2/2] Vk autherntication --- ADFS/views.py | 81 ++++++++++++++++++++++++++-------- config/oathConfigs.json | 13 ++++++ config/oauthConfigsSecret.json | 12 +++++ static/js/login.js | 6 ++- untitled1/settings.py | 12 +++++ untitled1/urls.py | 1 + 6 files changed, 105 insertions(+), 20 deletions(-) create mode 100644 config/oathConfigs.json create mode 100644 config/oauthConfigsSecret.json diff --git a/ADFS/views.py b/ADFS/views.py index 378a578..888f8a6 100644 --- a/ADFS/views.py +++ b/ADFS/views.py @@ -3,19 +3,26 @@ from django.http import HttpResponse, HttpResponseRedirect from django.views.decorators.csrf import csrf_exempt from django.shortcuts import redirect +from django.conf import settings from .models import Attention, ADFSUser from .forms import ContactForm -from django.contrib.auth import authenticate, login, logout +from django.contrib.auth import authenticate, login from base64 import b64decode import base64 from django.core.files.base import ContentFile import requests -from urlparse import parse_qs import json -# Create your views here. + +try: + import urlparse + from urllib import urlencode +except: + import urllib.parse as urlparse + from urllib.parse import urlencode + def decode_base64(data): """Decode base64, padding being optional. @@ -26,14 +33,16 @@ def decode_base64(data): """ missing_padding = len(data) % 4 if missing_padding != 0: - data += b'='* (4 - missing_padding) + data += b'=' * (4 - missing_padding) return base64.decodestring(data) + def survey(request): template = loader.get_template('base_react.html') context = RequestContext(request, {}) return HttpResponse(template.render(context)) + def regl(request): template = loader.get_template('reglam.html') context = RequestContext(request, {}) @@ -59,6 +68,7 @@ def reglament(request): context = RequestContext(request, {}) return HttpResponse(template.render(context)) + def register_attention(request): form = ContactForm() if request.method == 'POST': @@ -76,6 +86,7 @@ def register_attention(request): return HttpResponseRedirect('/attention/%i' % a.id) return render(request, 'attention.html', {'form': form}) + @csrf_exempt def register(request): data = request.POST.dict() @@ -84,11 +95,11 @@ def register(request): try: user = ADFSUser.objects.create_user( - username=data['login'], - email=data['email'], - password=data['password']) + username=data['login'], + email=data['email'], + password=data['password']) - if data.get('avatar', None) != None: + if data.get('avatar', None): user.avatar = ContentFile(b64decode(data['avatar']), 'rosimka.png') user.save() @@ -102,6 +113,7 @@ def register(request): content_type='application/json', status=201) + @csrf_exempt def autorisation(request): if request.method == 'GET': @@ -123,7 +135,9 @@ def autorisation(request): print("The password is valid, but the account is disabled!") else: print("The username and password were incorrect.") - return HttpResponse(json.dumps({ 'error': 'Incorrect login or password' }), status=403) + return HttpResponse( + json.dumps({'error': 'Incorrect login or password'}), + status=403) if user is not None: context = RequestContext(request, { @@ -133,20 +147,50 @@ def autorisation(request): else: context = RequestContext(request, {}) template = loader.get_template('gratulations.html') - return HttpResponse(json.dumps({ 'login': user.username, 'active': t })) + return HttpResponse(json.dumps({'login': user.username, 'active': t})) + + +@csrf_exempt +def autorisation_vk(request): + code = request.GET['code'] + query_string = urllib.urlencode({ + 'client_id': settings.OAUTH_PUBLIC_CONFIGS['vk']['client_id'], + 'code': code, + 'client_secret': settings.OAUTH_PRIVATE_CONFIGS['vk']['client_secret'], + 'redirect_uri': settings.OAUTH_PUBLIC_CONFIGS['vk']['redirect_uri'], + }) + + r = requests.get("https://oauth.vk.com/access_token?%s" % query_string) + params = json.loads(r.text) + access_token = params['access_token'] @csrf_exempt def autorisation_github(request): try: code = request.GET['code'] - r = requests.post("https://github.com/login/oauth/access_token", - data = {'client_id':'335f38f2aab459864d81', 'client_secret': '1aba874073116d193e0f324e1382df5c4a25b8d3', 'code': code, 'accept': 'application/json'}) + client_id = settings.OAUTH_PUBLIC_CONFIGS['github']['client_id'] + client_secret = \ + settings.OAUTH_PRIVATE_CONFIGS['github']['client_secret'] - params = parse_qs(r.text) + r = requests.post("https://github.com/login/oauth/access_token", + data={ + 'client_id': client_id, + 'client_secret': client_secret, + 'code': code, + 'accept': 'application/json', + }) + + params = urlparse.parse_qs(r.text) access_token = params['access_token'] - response = requests.get("https://api.github.com/user/emails?access_token=%s" % access_token[0]).json() - response_user = requests.get("https://api.github.com/user?access_token=%s" % access_token[0]).json() + response = requests.get( + "https://api.github.com/user/emails?access_token=%s" % + access_token[0]).json() + + response_user = requests.get( + "https://api.github.com/user?access_token=%s" % + access_token[0]).json() + emails = [] for email in response: if email['verified']: @@ -162,9 +206,9 @@ def autorisation_github(request): return HttpResponse(template.render(context)) user = ADFSUser.objects.create_user( - username=response_user['login'], - email=response[0]['email'], - password='rasim') + username=response_user['login'], + email=response[0]['email'], + password='rasim') new_user = authenticate(username=user.username, api=True) login(request, new_user) @@ -174,5 +218,6 @@ def autorisation_github(request): except Exception: return redirect('/') + def is_gast(request): return not request.user.is_authenticated() diff --git a/config/oathConfigs.json b/config/oathConfigs.json new file mode 100644 index 0000000..eb31012 --- /dev/null +++ b/config/oathConfigs.json @@ -0,0 +1,13 @@ +{ + "github": { + "client_id": "335f38f2aab459864d81" + }, + "facebook": { + "client_id": "524576674585233" + }, + "vk": { + "client_id": "6292497", + "scope": "4259840", + "redirect_uri": "https://adf-saratov.ru/vk" + } +} diff --git a/config/oauthConfigsSecret.json b/config/oauthConfigsSecret.json new file mode 100644 index 0000000..e50fd5a --- /dev/null +++ b/config/oauthConfigsSecret.json @@ -0,0 +1,12 @@ +{ + "github": { + "client_secret": "1aba874073116d193e0f324e1382df5c4a25b8d3" + }, + "vk": { + "client_secret": "vjUMBpqya7XshaEcbcDP", + "service_secret": "0c96887c0c96887c0c96887c000cf68c6d00c960c96887c56ba56c8f1cf765dcaf9408b" + }, + "facebook": { + "client_secret": "rasim" + } +} diff --git a/static/js/login.js b/static/js/login.js index 3ce9f7b..3ffb6ad 100644 --- a/static/js/login.js +++ b/static/js/login.js @@ -4,6 +4,7 @@ import ReactDOM from 'react-dom'; import ReactCSSTransitionGroup from 'react-addons-css-transition-group'; import { RegisterForm } from './components/registerForm'; import configs from './config'; +import oauthConfigs from '../../config/oathConfigs.json'; const FontAwesome = require('react-fontawesome'); @@ -96,8 +97,9 @@ export const renderLoginForm = () => {
Войти через социальные сети
- - + + +
diff --git a/untitled1/settings.py b/untitled1/settings.py index 41d6dcd..39ffea6 100644 --- a/untitled1/settings.py +++ b/untitled1/settings.py @@ -10,6 +10,8 @@ # Build paths inside the project like this: os.path.join(BASE_DIR, ...) import os +import json + BASE_DIR = os.path.dirname(os.path.dirname(__file__)) @@ -136,6 +138,16 @@ # pagination PAGINATE_BY = 10 +# oath configs for 3-rd party services (github, facebook, vk) +OAUTH_PUBLIC_CONFIGS = None +OAUTH_PRIVATE_CONFIGS = None + +with open("./config/oathConfigs.json") as f: + OAUTH_PUBLIC_CONFIGS = json.loads(f.read()) + +with open("./config/oauthConfigsSecret.json") as f: + OAUTH_PRIVATE_CONFIGS = json.loads(f.read()) + try: from settings_local import * except: diff --git a/untitled1/urls.py b/untitled1/urls.py index 4df0df8..cf805c9 100644 --- a/untitled1/urls.py +++ b/untitled1/urls.py @@ -31,6 +31,7 @@ class UserViewSet(viewsets.ModelViewSet): url(r'^tinymce/', include('tinymce.urls')), url(r'logic/', include(teamlogic_urls)), url(r'^login/github', views.autorisation_github), + url(r'^vk', views.autorisation_vk), url(r'^login/', views.autorisation), url(r'^register/', views.register), url(r'^survey', views.survey),