Merge commit 'afb3cc28'
[wolnelektury.git] / src / api / drf_auth.py
1 """
2 Transitional code: bridge between Piston's OAuth implementation
3 and DRF views.
4 """
5 from piston.authentication import OAuthAuthentication
6 from rest_framework.authentication import BaseAuthentication
7
8
9 class PistonOAuthAuthentication(BaseAuthentication):
10     def __init__(self):
11         self.piston_auth = OAuthAuthentication()
12
13     def authenticate_header(self, request):
14         return 'OAuth realm="API"'
15
16     def authenticate(self, request):
17         if self.piston_auth.is_valid_request(request):
18             consumer, token, parameters = self.piston_auth.validate_token(request)
19             if consumer and token:
20                 return token.user, token