Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django Admin integation fix adding AuthToken #330

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
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
42 changes: 41 additions & 1 deletion knox/admin.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,50 @@
from django.contrib import admin
from django import forms
from django.contrib import admin, messages
from django.contrib.auth import get_user_model

from knox import models
from knox.settings import CONSTANTS


class AuthTokenCreateForm(forms.ModelForm):

def __init__(self, *args, **kwargs):
super(AuthTokenCreateForm, self).__init__(*args, **kwargs)
self.token = None

class Meta:
model = models.AuthToken
fields = ['user', 'expiry']

def save(self, commit=True):
obj = super(AuthTokenCreateForm, self).save(commit=False)
digest, token = models.get_digest_token()
obj.digest = digest
obj.token_key = token[:CONSTANTS.TOKEN_KEY_LENGTH]
self.token = token
if commit:
obj.save()
obj.save_m2m()
return obj


@admin.register(models.AuthToken)
class AuthTokenAdmin(admin.ModelAdmin):
add_form = AuthTokenCreateForm
list_display = ('digest', 'user', 'created', 'expiry',)
# We dont know how a custom User model looks like, but is must have a USERNAME_FIELD
search_fields = ['digest', 'token_key', 'user__'+get_user_model().USERNAME_FIELD]
fields = ()
raw_id_fields = ('user',)

def get_form(self, request, obj=None, **kwargs):
defaults = {}
if obj is None:
defaults['form'] = self.add_form
defaults.update(kwargs)
return super(AuthTokenAdmin, self).get_form(request, obj, **defaults)

def save_model(self, request, obj, form, change):
if not change:
self.message_user(request, "TOKEN " + form.token, messages.INFO)
super(AuthTokenAdmin, self).save_model(request, obj, form, change)
16 changes: 14 additions & 2 deletions knox/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,27 @@
User = settings.AUTH_USER_MODEL


def get_expiry(expiry):
if expiry is not None:
expiry = timezone.now() + expiry
return expiry


def get_digest_token(prefix=knox_settings.TOKEN_PREFIX):
token = prefix + crypto.create_token_string()
digest = crypto.hash_token(token)
return digest, token


class AuthTokenManager(models.Manager):
def create(
self,
user,
expiry=knox_settings.TOKEN_TTL,
prefix=knox_settings.TOKEN_PREFIX
):
token = prefix + crypto.create_token_string()
digest = crypto.hash_token(token)

digest, token = get_digest_token(prefix)
if expiry is not None:
expiry = timezone.now() + expiry
instance = super(AuthTokenManager, self).create(
Expand Down
9 changes: 7 additions & 2 deletions knox_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'knox',
)

MIDDLEWARE_CLASSES = (
MIDDLEWARE = (
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)

ROOT_URLCONF = 'knox_project.urls'
Expand All @@ -32,6 +36,7 @@
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
Expand Down
2 changes: 2 additions & 0 deletions knox_project/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from django.contrib import admin
from django.urls import include, re_path

from .views import RootView

urlpatterns = [
re_path(r'^api/', include('knox.urls')),
re_path(r'^api/$', RootView.as_view(), name="api-root"),
re_path(r'^admin/', admin.site.urls),
]
Loading