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

Facilitate replacing AuthToken model #252

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion knox/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def authenticate_credentials(self, token):
'''
msg = _('Invalid token.')
token = token.decode("utf-8")
for auth_token in AuthToken.objects.filter(
for auth_token in self.model.objects.filter(
token_key=token[:CONSTANTS.TOKEN_KEY_LENGTH]):
if self._cleanup_token(auth_token):
continue
Expand Down
8 changes: 7 additions & 1 deletion knox/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def create(self, user, expiry=knox_settings.TOKEN_TTL):
return instance, token


class AuthToken(models.Model):
class AbstractAuthToken(models.Model):
class Meta:
abstract = True

objects = AuthTokenManager()

Expand All @@ -37,3 +39,7 @@ class AuthToken(models.Model):

def __str__(self):
return '%s : %s' % (self.digest, self.user)


class AuthToken(AbstractAuthToken):
pass
3 changes: 2 additions & 1 deletion knox/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
class LoginView(APIView):
authentication_classes = api_settings.DEFAULT_AUTHENTICATION_CLASSES
permission_classes = (IsAuthenticated,)
model = AuthToken

def get_context(self):
return {'request': self.request, 'format': self.format_kwarg, 'view': self}
Expand Down Expand Up @@ -60,7 +61,7 @@ def post(self, request, format=None):
status=status.HTTP_403_FORBIDDEN
)
token_ttl = self.get_token_ttl()
instance, token = AuthToken.objects.create(request.user, token_ttl)
instance, token = self.model.objects.create(request.user, token_ttl)
user_logged_in.send(sender=request.user.__class__,
request=request, user=request.user)
data = self.get_post_response_data(request, token, instance)
Expand Down