| from rest_framework.authentication import TokenAuthentication | |
| from rest_framework.exceptions import AuthenticationFailed | |
| class CookieTokenAuthentication(TokenAuthentication): | |
| def authenticate(self, request): | |
| # First try the Authorization header (standard Token or Bearer token) | |
| auth = request.META.get("HTTP_AUTHORIZATION", "").split() | |
| if auth and auth[0].lower() in ["bearer", "token"]: | |
| if len(auth) == 1: | |
| raise AuthenticationFailed( | |
| "Invalid token header. No credentials provided." | |
| ) | |
| elif len(auth) > 2: | |
| raise AuthenticationFailed( | |
| "Invalid token header. Token string should not contain spaces." | |
| ) | |
| token = auth[1] | |
| return self.authenticate_credentials(token) | |
| # Then try the cookie as fallback | |
| token = request.COOKIES.get("authToken") | |
| if not token: | |
| return None | |
| try: | |
| return self.authenticate_credentials(token) | |
| except AuthenticationFailed: | |
| return None | |
Xet Storage Details
- Size:
- 1.11 kB
- Xet hash:
- eadc6fa57b05d877388ca59edf45833ffd497bef0b82eb9febf8683277d91012
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.