diff --git a/CONFER/settings.py b/CONFER/settings.py index 7801f74..c67b437 100644 --- a/CONFER/settings.py +++ b/CONFER/settings.py @@ -43,9 +43,13 @@ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + # libs 'rest_framework', - 'conferences', 'corsheaders', + 'social_django', + # apps + 'accounts', + 'conferences', ] MIDDLEWARE = [ @@ -72,6 +76,8 @@ 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', + 'social_django.context_processors.backends', + 'social_django.context_processors.login_redirect', ], }, }, @@ -134,7 +140,76 @@ 'localhost:8080' ) +AUTHENTICATION_BACKENDS = ( + 'social_core.backends.github.GithubOAuth2', + 'django.contrib.auth.backends.ModelBackend' +) + +REST_FRAMEWORK = { + 'DEFAULT_AUTHENTICATION_CLASSES': ( + 'rest_framework.authentication.BasicAuthentication', + 'rest_framework.authentication.SessionAuthentication' + ) +} + # GOOGLE API GOOGLE_API_BASE_URL = 'http://maps.google.com' -GOOGLE_GEOCODE_ENDPOINT = '{base_url}/maps/api/geocode/json'.format(base_url=GOOGLE_API_BASE_URL) \ No newline at end of file +GOOGLE_GEOCODE_ENDPOINT = '{base_url}/maps/api/geocode/json'.format(base_url=GOOGLE_API_BASE_URL) + +# python-social-auth settings +SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/' +SOCIAL_AUTH_LOGIN_URL = '/login' +SOCIAL_AUTH_PIPELINE = ( + # Get the information we can about the user and return it in a simple + # format to create the user instance later. On some cases the details are + # already part of the auth response from the provider, but sometimes this + # could hit a provider API. + 'social_core.pipeline.social_auth.social_details', + + # Get the social uid from whichever service we're authing thru. The uid is + # the unique identifier of the given user in the provider. + 'social_core.pipeline.social_auth.social_uid', + + # Verifies that the current auth process is valid within the current + # project, this is where emails and domains whitelists are applied (if + # defined). + 'social_core.pipeline.social_auth.auth_allowed', + + # Checks if the current social-account is already associated in the site. + 'social_core.pipeline.social_auth.social_user', + + # Make up a username for this person, appends a random string at the end if + # there's any collision. + 'social_core.pipeline.user.get_username', + + # Send a validation email to the user to verify its email address. + # Disabled by default. + # 'social_core.pipeline.mail.mail_validation', + + # Associates the current social details with another user account with + # a similar email address. Disabled by default. + 'social_core.pipeline.social_auth.associate_by_email', + + # Create a user account if we haven't found one yet. + 'social_core.pipeline.user.create_user', + + # Create the record that associates the social account with the user. + 'social_core.pipeline.social_auth.associate_user', + + # Populate the extra_data field in the social record with the values + # specified by settings (and the default ones like access_token, etc). + 'social_core.pipeline.social_auth.load_extra_data', + + # Update the user record with any changed info from the auth service. + 'social_core.pipeline.user.user_details', +) + + +SOCIAL_AUTH_GITHUB_KEY = os.environ.get('SOCIAL_AUTH_GITHUB_KEY') +SOCIAL_AUTH_GITHUB_SECRET = os.environ.get('SOCIAL_AUTH_GITHUB_SECRET') + +SOCIAL_AUTH_GITHUB_SCOPE = [ + 'user', + 'user:email', +] diff --git a/CONFER/urls.py b/CONFER/urls.py index 19d4914..36b8197 100644 --- a/CONFER/urls.py +++ b/CONFER/urls.py @@ -19,6 +19,7 @@ urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')), - url(r'', include('api.urls')), url(r'^conferences/', include('conferences.urls')), + url('', include('social_django.urls', namespace='social')), + url(r'', include('api.urls')), ] diff --git a/accounts/__init__.py b/accounts/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/accounts/admin.py b/accounts/admin.py new file mode 100644 index 0000000..885b3ab --- /dev/null +++ b/accounts/admin.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.contrib import admin + +from .models import Profile + +admin.site.register(Profile) diff --git a/accounts/apps.py b/accounts/apps.py new file mode 100644 index 0000000..72860c4 --- /dev/null +++ b/accounts/apps.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.apps import AppConfig + + +class AccountsConfig(AppConfig): + name = 'accounts' diff --git a/accounts/migrations/0001_initial.py b/accounts/migrations/0001_initial.py new file mode 100644 index 0000000..5b0121b --- /dev/null +++ b/accounts/migrations/0001_initial.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11 on 2017-04-13 18:56 +from __future__ import unicode_literals + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='Profile', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/accounts/migrations/__init__.py b/accounts/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/accounts/models.py b/accounts/models.py new file mode 100644 index 0000000..8699490 --- /dev/null +++ b/accounts/models.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +from django.contrib.auth.models import User +from django.db import models +from django.db.models.signals import post_save +from django.dispatch import receiver + + +class Profile(models.Model): + user = models.OneToOneField(User, on_delete=models.CASCADE) + + def __str__(self): + return self.user + + +@receiver(post_save, sender=User) +def create_user_profile(sender, instance, created, **kwargs): + if created: + Profile.objects.create(user=instance) + + +@receiver(post_save, sender=User) +def save_user_profile(sender, instance, **kwargs): + instance.profile.save() diff --git a/accounts/tests.py b/accounts/tests.py new file mode 100644 index 0000000..5982e6b --- /dev/null +++ b/accounts/tests.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.test import TestCase + +# Create your tests here. diff --git a/accounts/views.py b/accounts/views.py new file mode 100644 index 0000000..e784a0b --- /dev/null +++ b/accounts/views.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.shortcuts import render + +# Create your views here. diff --git a/requirements.txt b/requirements.txt index 1bb6c62..9852d56 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,7 @@ -django -djangorestframework -markdown -django-filter -requests -django-cors-headers +django==1.11 +djangorestframework==3.6.2 +markdown==2.6.8 +django-filter==1.0.2 +requests==2.13.0 +django-cors-headers==2.0.2 +social-auth-app-django==1.1.0 \ No newline at end of file