Passed
Push — master ( 8aa213...c2c2f1 )
by Piotr
01:15
created

ProviderAuthSerializer._validate_state()   B

Complexity

Conditions 4

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
rs 8.5806
cc 4
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
    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...
15
        user = validated_data['user']
16
        return settings.SOCIAL_AUTH_TOKEN_STRATEGY.obtain(user)
17
18
    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...
19
        request = self.context['request']
20
        if 'state' in request.GET:
21
            self._validate_state(request.GET['state'])
22
23
        strategy = load_strategy(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
            user = backend.auth_complete()
33
        except exceptions.AuthException as e:
0 ignored issues
show
Coding Style Naming introduced by
The name e does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
34
            raise serializers.ValidationError(str(e))
35
        return {'user': user}
36
37
    def _validate_state(self, value):
38
        request = self.context['request']
39
        strategy = load_strategy(request)
40
        redirect_uri = strategy.session_get('redirect_uri')
41
42
        backend_name = self.context['view'].kwargs['provider']
43
        backend = load_backend(
44
            strategy, backend_name, redirect_uri=redirect_uri
45
        )
46
47
        try:
48
            backend.validate_state()
49
        except exceptions.AuthMissingParameter:
50
            raise serializers.ValidationError(
51
                'State could not be found in request data.'
52
            )
53
        except exceptions.AuthStateMissing:
54
            raise serializers.ValidationError(
55
                'State could not be found in server-side session data.'
56
            )
57
        except exceptions.AuthStateForbidden:
58
            raise serializers.ValidationError(
59
                'Invalid state has been provided.'
60
            )
61
62
        return value
63