1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-06-25 14:43:15 +00:00
seahub/api2/authentication.py
2012-12-14 20:53:36 +08:00

37 lines
1.1 KiB
Python

from rest_framework.authentication import BaseAuthentication
from models import Token
from base.accounts import User
class TokenAuthentication(BaseAuthentication):
"""
Simple token based authentication.
Clients should authenticate by passing the token key in the "Authorization"
HTTP header, prepended with the string "Token ". For example:
Authorization: Token 401f7ac837da42b97f613d789819ff93537bee6a
"""
model = Token
"""
A custom token model may be used, but must have the following properties.
* key -- The string identifying the token
* user -- The user to which the token belongs
"""
def authenticate(self, request):
auth = request.META.get('HTTP_AUTHORIZATION', '').split()
if len(auth) == 2 and auth[0].lower() == "token":
key = auth[1]
try:
token = self.model.objects.get(key=key)
except self.model.DoesNotExist:
return None
user = User.objects.get(email=token.user)
if user.is_active:
return (user, token)