-
-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
return the token as message to the user change MIDDLEWARE_CLASSES (obsolete) to MIDDLEWARE, add required apps, middleware and context processors for admin pages add admin urls to reference project Add admin form for creating AuthTokens, add admin filter for searching AuthTokens Refactor AuthTokenManager, separate methods for token/digest/expiry logic
- Loading branch information
1 parent
271179a
commit 19e34bf
Showing
4 changed files
with
64 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,49 @@ | ||
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.settings import CONSTANTS | ||
from knox import models | ||
|
||
|
||
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) |
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,8 +1,10 @@ | ||
from django.urls import include, re_path | ||
from django.contrib import admin | ||
|
||
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), | ||
] |