1
|
|
|
from rest_framework import serializers |
|
|
|
|
2
|
|
|
|
3
|
|
|
from social_core import exceptions |
|
|
|
|
4
|
|
|
from social_django.utils import load_backend, load_strategy |
|
|
|
|
5
|
|
|
|
6
|
|
|
from djoser.conf import settings |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class ProviderAuthSerializer(serializers.Serializer): |
|
|
|
|
10
|
|
|
# GET auth token |
11
|
|
|
token = serializers.CharField(read_only=True) |
12
|
|
|
user = serializers.CharField(read_only=True) |
13
|
|
|
|
14
|
|
|
# POST OAuth/OpenID values |
15
|
|
|
code = serializers.CharField(write_only=True) |
16
|
|
|
state = serializers.CharField(required=False, write_only=True) |
17
|
|
|
|
18
|
|
|
def create(self, validated_data): |
|
|
|
|
19
|
|
|
user = validated_data['user'] |
20
|
|
|
return settings.SOCIAL_AUTH_TOKEN_STRATEGY.obtain(user) |
21
|
|
|
|
22
|
|
|
def validate_state(self, value): |
|
|
|
|
23
|
|
|
strategy = load_strategy(self.context['request']) |
24
|
|
|
redirect_uri = strategy.session_get('redirect_uri') |
25
|
|
|
|
26
|
|
|
backend_name = self.context['view'].kwargs['provider'] |
27
|
|
|
backend = load_backend( |
28
|
|
|
strategy, backend_name, redirect_uri=redirect_uri |
29
|
|
|
) |
30
|
|
|
|
31
|
|
|
try: |
32
|
|
|
backend.validate_state() |
33
|
|
|
except exceptions.AuthException: |
34
|
|
|
raise serializers.ValidationError('State could not be verified.') |
35
|
|
|
|
36
|
|
|
def validate(self, attrs): |
|
|
|
|
37
|
|
|
strategy = load_strategy(self.context['request']) |
38
|
|
|
redirect_uri = strategy.session_get('redirect_uri') |
39
|
|
|
|
40
|
|
|
backend_name = self.context['view'].kwargs['provider'] |
41
|
|
|
backend = load_backend( |
42
|
|
|
strategy, backend_name, redirect_uri=redirect_uri |
43
|
|
|
) |
44
|
|
|
|
45
|
|
|
try: |
46
|
|
|
user = backend.auth_complete() |
47
|
|
|
except exceptions.AuthException: |
48
|
|
|
raise serializers.ValidationError( |
49
|
|
|
'Failed to finish authentication.' |
50
|
|
|
) |
51
|
|
|
return {'user': user} |
52
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.