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
|
|
|
# Dirty hack because PSA does not respect request.data |
24
|
|
|
request = self.context['request'] |
25
|
|
|
request.GET = request.data |
26
|
|
|
|
27
|
|
|
strategy = load_strategy(request) |
28
|
|
|
redirect_uri = strategy.session_get('redirect_uri') |
29
|
|
|
|
30
|
|
|
backend_name = self.context['view'].kwargs['provider'] |
31
|
|
|
backend = load_backend( |
32
|
|
|
strategy, backend_name, redirect_uri=redirect_uri |
33
|
|
|
) |
34
|
|
|
|
35
|
|
|
try: |
36
|
|
|
backend.validate_state() |
37
|
|
|
except exceptions.AuthMissingParameter: |
38
|
|
|
raise serializers.ValidationError( |
39
|
|
|
'State could not be found in request data.' |
40
|
|
|
) |
41
|
|
|
except exceptions.AuthStateMissing: |
42
|
|
|
raise serializers.ValidationError( |
43
|
|
|
'State could not be found in server-side session data.' |
44
|
|
|
) |
45
|
|
|
except exceptions.AuthStateForbidden: |
46
|
|
|
raise serializers.ValidationError( |
47
|
|
|
'Invalid state has been provided.' |
48
|
|
|
) |
49
|
|
|
|
50
|
|
|
return value |
51
|
|
|
|
52
|
|
|
def validate(self, attrs): |
|
|
|
|
53
|
|
|
# Dirty hack because PSA does not respect request.data |
54
|
|
|
request = self.context['request'] |
55
|
|
|
request.GET = request.data |
56
|
|
|
|
57
|
|
|
strategy = load_strategy(request) |
58
|
|
|
redirect_uri = strategy.session_get('redirect_uri') |
59
|
|
|
|
60
|
|
|
backend_name = self.context['view'].kwargs['provider'] |
61
|
|
|
backend = load_backend( |
62
|
|
|
strategy, backend_name, redirect_uri=redirect_uri |
63
|
|
|
) |
64
|
|
|
|
65
|
|
|
try: |
66
|
|
|
user = backend.auth_complete() |
67
|
|
|
except exceptions.AuthException: |
68
|
|
|
raise serializers.ValidationError( |
69
|
|
|
'Failed to finish authentication.' |
70
|
|
|
) |
71
|
|
|
return {'user': user} |
72
|
|
|
|
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.