-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cueobserve #64 Add Authentication Layer
- Loading branch information
Showing
34 changed files
with
762 additions
and
25 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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from django.conf import settings | ||
from django.shortcuts import redirect | ||
from django.http import HttpResponseForbidden | ||
from django.contrib.auth import logout | ||
from django.utils.deprecation import MiddlewareMixin | ||
from django.contrib.auth.decorators import login_required | ||
|
||
class DisableCsrfCheck(MiddlewareMixin): | ||
""" | ||
Middleware class to disable CSRF check for views | ||
""" | ||
|
||
def process_request(self, req): | ||
""" | ||
Process request method to add attr _dont_enforce_csrf_checks | ||
""" | ||
attr = "_dont_enforce_csrf_checks" | ||
if not getattr(req, attr, False): | ||
setattr(req, attr, True) | ||
|
||
|
||
class LoginRequiredMiddleware: | ||
""" | ||
Middleware class to enforce login for every view | ||
""" | ||
|
||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
|
||
def __call__(self, request): | ||
return self.get_response(request) | ||
|
||
def process_view(self, request, view_func, view_args, view_kwargs): # pylint: disable=C0103 | ||
""" | ||
Process view method to check login_exempt decorator and enforce authentication on all views | ||
""" | ||
try: | ||
appName = request.path.split("/")[1] | ||
if appName in ["admin", "accounts"]: | ||
return | ||
except: | ||
pass | ||
authenticationRequired= True if settings.AUTHENTICATION_REQUIRED == "True" else False | ||
if not authenticationRequired: | ||
return | ||
if getattr(view_func, "login_exempt", False): | ||
return | ||
|
||
if not request.user.is_authenticated and request.path.split("/")[2] == "datadownload": | ||
return redirect(settings.LOGIN_REDIRECT_URL) | ||
|
||
if request.user.is_authenticated: | ||
if request.user.status != "Active": | ||
logout(request) | ||
return | ||
|
||
return login_required(view_func)(request, *view_args, **view_kwargs) | ||
|
||
|
||
|
||
|
||
def login_exempt(view): # pylint: disable=C0103 | ||
""" | ||
Decorator for views which needs to be exempted from login | ||
""" | ||
view.login_exempt = True | ||
return 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
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,3 +1,4 @@ | ||
django-allauth==0.41.0 | ||
amqp==5.0.6 | ||
asgiref==3.4.0 | ||
astroid==2.5.6 | ||
|
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
Empty file.
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class UsersConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'users' |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from django.contrib.auth.base_user import BaseUserManager | ||
from django.utils.translation import ugettext_lazy as _ | ||
from django.db.models import Q | ||
|
||
|
||
class CustomUserManager(BaseUserManager): | ||
""" | ||
Custom user model manager where email is the unique identifiers | ||
for authentication instead of usernames. | ||
""" | ||
|
||
def create_user(self, email, password, **extra_fields): | ||
""" | ||
Create and save a User with the given email and password. | ||
""" | ||
if not email: | ||
raise ValueError(_("The Email must be set")) | ||
email = self.normalize_email(email) | ||
user = self.model(email=email, **extra_fields) | ||
user.set_password(password) | ||
user.status = "Active" | ||
user.save() | ||
return user | ||
|
||
def create_superuser(self, email, password, **extra_fields): | ||
""" | ||
Create and save a SuperUser with the given email and password. | ||
""" | ||
# extra_fields.setdefault("is_staff", True) | ||
extra_fields.setdefault("is_superuser", True) | ||
extra_fields.setdefault("is_active", True) | ||
|
||
# if extra_fields.get("is_staff") is not True: | ||
# raise ValueError(_("Superuser must have is_staff=True.")) | ||
if extra_fields.get("is_superuser") is not True: | ||
raise ValueError(_("Superuser must have is_superuser=True.")) | ||
return self.create_user(email, password, **extra_fields) | ||
|
||
def get_queryset(self): | ||
"""returns object.all() without Bot in it""" | ||
return super().get_queryset().filter(~Q(email="Bot@cuebook.ai")) | ||
|
||
def get_bot(self): | ||
"""returns bot which cannot be accessd through querySet""" | ||
return super().get_queryset().get(email="Bot@cuebook.ai") |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Generated by Django 3.2.5 on 2021-08-09 06:54 | ||
|
||
from django.db import migrations, models | ||
import users.models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
('auth', '0012_alter_user_first_name_max_length'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='CustomUser', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('password', models.CharField(max_length=128, verbose_name='password')), | ||
('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')), | ||
('email', users.models.LowercaseEmailField(max_length=254, unique=True, verbose_name='email address')), | ||
('name', models.CharField(default='User', max_length=200)), | ||
('is_active', models.BooleanField(default=False)), | ||
('status', models.CharField(default='Inactive', max_length=20)), | ||
('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={ | ||
'abstract': False, | ||
}, | ||
), | ||
] |
Empty file.
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from django.db import models | ||
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, Group | ||
from django.utils.translation import gettext_lazy as _ | ||
from django.utils import timezone | ||
from datetime import datetime | ||
from .managers import CustomUserManager | ||
from django.contrib.postgres.fields import ArrayField | ||
|
||
# Create your models here. | ||
|
||
class LowercaseEmailField(models.EmailField): | ||
""" | ||
Override EmailField to convert emails to lowercase before saving. | ||
""" | ||
|
||
def convertToLowerCase(self, value): | ||
""" | ||
Convert email to lowercase. | ||
""" | ||
value = super(LowercaseEmailField, self).convertToLowerCase(value) | ||
# Value can be None so check that it's a string before lowercasing. | ||
if isinstance(value, str): | ||
return value.lower() | ||
return value | ||
|
||
class CustomUser(AbstractBaseUser, PermissionsMixin): | ||
email = LowercaseEmailField(_("email address"), unique=True) | ||
name = models.CharField(max_length=200, null=False, default="User") | ||
is_active = models.BooleanField(default=False) | ||
status = models.CharField(max_length=20, default="Inactive") | ||
|
||
USERNAME_FIELD = "email" | ||
REQUIRED_FIELDS = [] | ||
objects = CustomUserManager() | ||
def __str__(self): | ||
return self.email |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
Oops, something went wrong.