Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ verify_ssl = true
[dev-packages]
autopep8 = "*"
pylint = "*"
black = "==19.3b0"
isort = "==4.3.21"

[packages]
django = "==2.2.3"
Expand All @@ -18,6 +20,10 @@ psycopg2 = "==2.7.5"
django-cors-headers = "==3.0.2"
gunicorn = "==19.9.0"
django-heroku = "==0.3.1"
django-phonenumber-field = "==3.0.1"
phonenumbers = "==8.10.14"
pyjwt = "==1.7.1"
django-filter = "==2.1.0"

[requires]
python_version = "3.7"
216 changes: 146 additions & 70 deletions Pipfile.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
web: gunicorn app.wsgi
release: python manage.py migrate
web: gunicorn dscountr.wsgi
5 changes: 5 additions & 0 deletions app/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class MyappConfig(AppConfig):
name = "app"
23 changes: 23 additions & 0 deletions app/auth_backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend

User = get_user_model()


class PasswordlessAuthBackend(ModelBackend):
"""Log in to Django without providing a password.

"""

def authenticate(self, username=None):
try:
return User.objects.get(username=username)
except User.DoesNotExist:
return None

def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
Empty file added app/authentication/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions app/authentication/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register models here.
74 changes: 74 additions & 0 deletions app/authentication/backends.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import jwt

from django.conf import settings
from datetime import datetime

from decouple import config

from rest_framework import authentication, exceptions

from .models import User


class JWTAuthentication(authentication.BaseAuthentication):
authentication_header_prefix = 'Token'

def authenticate(self, request):
"""
The `authenticate` method is called on every request regardless of
whether the endpoint requires authentication.
"""
request.user = None

auth_header = authentication.get_authorization_header(request).split()
auth_header_prefix = self.authentication_header_prefix.lower()

if not auth_header:
return None

if len(auth_header) == 1:
return None

elif len(auth_header) > 2:
return None

prefix = auth_header[0].decode('utf-8')
token = auth_header[1].decode('utf-8')

if prefix.lower() != auth_header_prefix:
return None

return self._authenticate_credentials(request, token)

def _authenticate_credentials(self, request, token):
"""
Try to authenticate the given credentials. If authentication is
successful, return the user and token. If not, throw an error.
"""
try:
payload = jwt.decode(token, config('SECRET_KEY'))
except:
msg = 'Invalid authentication. Could not decode token.'
raise exceptions.AuthenticationFailed(msg)

try:
user = User.objects.get(pk=payload['id'])
except User.DoesNotExist:
msg = 'No user matching this token was found.'
raise exceptions.AuthenticationFailed(msg)

if not user.is_active:
msg = 'This user has been deactivated.'
raise exceptions.AuthenticationFailed(msg)

return (user, token)

def generate_token(self, user_id):
"""
gerates jwt token by encoding registered user id
"""
payload = {
'id': user_id,
'iat': datetime.utcnow()
}
return jwt.encode(payload, config('SECRET_KEY')).decode('UTF-8')
1 change: 1 addition & 0 deletions app/authentication/choices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GENDER_CHOICES = (("M", "Male"), ("F", "Female"), ("A", "Alien"))
43 changes: 43 additions & 0 deletions app/authentication/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Generated by Django 2.2.3 on 2019-07-02 20:00

import app.authentication.models
from django.db import migrations, models
import phonenumber_field.modelfields


class Migration(migrations.Migration):

initial = True

dependencies = [
('auth', '0011_update_proxy_permissions'),
]

operations = [
migrations.CreateModel(
name='User',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('username', models.CharField(db_index=True, max_length=255, unique=True)),
('email', models.EmailField(db_index=True, max_length=254, unique=True)),
('phone_number', phonenumber_field.modelfields.PhoneNumberField(db_index=True, max_length=128, region=None, unique=True)),
('date_of_birth', models.DateField()),
('gender', models.CharField(choices=[('M', 'Male'), ('F', 'Female')], max_length=1)),
('password', models.CharField(max_length=128)),
('is_active', models.BooleanField(default=True)),
('is_staff', models.BooleanField(default=False)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
],
options={
'verbose_name_plural': 'All Users',
},
managers=[
('objects', app.authentication.models.UserManager()),
],
),
]
18 changes: 18 additions & 0 deletions app/authentication/migrations/0002_auto_20190708_1300.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.3 on 2019-07-08 13:00

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('authentication', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='user',
name='gender',
field=models.CharField(choices=[('M', 'Male'), ('F', 'Female'), ('A', 'Alien')], max_length=1),
),
]
18 changes: 18 additions & 0 deletions app/authentication/migrations/0003_auto_20190711_1054.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.3 on 2019-07-11 10:54

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('authentication', '0002_auto_20190708_1300'),
]

operations = [
migrations.AlterField(
model_name='user',
name='password',
field=models.CharField(max_length=128, verbose_name='password'),
),
]
18 changes: 18 additions & 0 deletions app/authentication/migrations/0004_auto_20190711_1539.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.3 on 2019-07-11 15:39

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('authentication', '0003_auto_20190711_1054'),
]

operations = [
migrations.AlterField(
model_name='user',
name='username',
field=models.CharField(blank=True, db_index=True, max_length=255, null=True, unique=True),
),
]
18 changes: 18 additions & 0 deletions app/authentication/migrations/0005_auto_20190711_1828.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.3 on 2019-07-11 18:28

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('authentication', '0004_auto_20190711_1539'),
]

operations = [
migrations.AlterField(
model_name='user',
name='password',
field=models.CharField(blank=True, max_length=255, null=True),
),
]
18 changes: 18 additions & 0 deletions app/authentication/migrations/0006_auto_20190711_1933.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.3 on 2019-07-11 19:33

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('authentication', '0005_auto_20190711_1828'),
]

operations = [
migrations.AlterField(
model_name='user',
name='password',
field=models.CharField(default='!1xKZFUMiyDaS7Exy0CurdiaQ1Pp88xxHpN0kw3pb', max_length=255),
),
]
29 changes: 29 additions & 0 deletions app/authentication/migrations/0007_auto_20190716_0917.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 2.2.3 on 2019-07-16 09:17

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('authentication', '0006_auto_20190711_1933'),
]

operations = [
migrations.RemoveField(
model_name='user',
name='username',
),
migrations.AddField(
model_name='user',
name='first_name',
field=models.CharField(default='default', max_length=50),
preserve_default=False,
),
migrations.AddField(
model_name='user',
name='last_name',
field=models.CharField(default='default', max_length=50),
preserve_default=False,
),
]
22 changes: 22 additions & 0 deletions app/authentication/migrations/0008_auto_20190717_1802.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 2.2.3 on 2019-07-17 18:02

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('authentication', '0007_auto_20190716_0917'),
]

operations = [
migrations.AlterModelOptions(
name='user',
options={'ordering': ['-id'], 'verbose_name_plural': 'All Users'},
),
migrations.AlterField(
model_name='user',
name='password',
field=models.CharField(default='YouAndM3', max_length=255),
),
]
18 changes: 18 additions & 0 deletions app/authentication/migrations/0009_auto_20190722_1140.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.3 on 2019-07-22 11:40

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('authentication', '0008_auto_20190717_1802'),
]

operations = [
migrations.AlterField(
model_name='user',
name='password',
field=models.CharField(default='!1xKZFUMiyDaS7Exy0CurdiaQ1Pp88xxHpN0kw3pb', max_length=255),
),
]
Empty file.
Loading