Passed
Push — master ( 78516a...8f2739 )
by Piotr
01:18
created

ProviderAuthSerializer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 7
c 4
b 0
f 0
dl 0
loc 63
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
B validate_state() 0 29 4
A validate() 0 20 2
1
from rest_framework import serializers
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
introduced by
Unable to import 'rest_framework'
Loading history...
2
3
from social_core import exceptions
0 ignored issues
show
introduced by
Unable to import 'social_core'
Loading history...
4
from social_django.utils import load_backend, load_strategy
0 ignored issues
show
introduced by
Unable to import 'social_django.utils'
Loading history...
5
6
from djoser.conf import settings
7
8
9
class ProviderAuthSerializer(serializers.Serializer):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
19
        user = validated_data['user']
20
        return settings.SOCIAL_AUTH_TOKEN_STRATEGY.obtain(user)
21
22
    def validate_state(self, value):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Unused Code introduced by
The argument attrs seems to be unused.
Loading history...
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