Fundraising in PDF.
[wolnelektury.git] / src / api / drf_auth.py
1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
3 #
4 from oauthlib.oauth1 import ResourceEndpoint
5 from rest_framework.authentication import BaseAuthentication
6 from .request_validator import PistonRequestValidator
7 from .utils import oauthlib_request
8
9
10 class PistonOAuthAuthentication(BaseAuthentication):
11     def __init__(self):
12         validator = PistonRequestValidator()
13         self.provider = ResourceEndpoint(validator)
14
15     def authenticate_header(self, request):
16         return 'OAuth realm="API"'
17
18     def authenticate(self, request):
19         v, r = self.provider.validate_protected_resource_request(
20             **oauthlib_request(request)
21         )
22         if v:
23             return r.token.user, r.token