• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

oauthlib / oauthlib / 15165802654

21 May 2025 03:07PM UTC coverage: 92.992%. Remained the same
15165802654

Pull #901

github

web-flow
Merge efbcfd8e8 into dab6a5ae1
Pull Request #901: Fixed linter issue, renamed unittest folder to avoid name conflict

965 of 1071 branches covered (90.1%)

Branch coverage included in aggregate %.

3082 of 3281 relevant lines covered (93.93%)

5.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.63
/oauthlib/oauth2/rfc8628/grant_types/device_code.py
1
from __future__ import annotations
6✔
2
import json
6✔
3

4
from typing import Callable
6✔
5

6
from oauthlib import common # noqa: TC001
7

8
from oauthlib.oauth2.rfc6749 import errors as rfc6749_errors
6✔
9
from oauthlib.oauth2.rfc6749.grant_types.base import GrantTypeBase
6✔
10

11

12
class DeviceCodeGrant(GrantTypeBase):
6✔
13
    def create_authorization_response(
6✔
14
        self, request: common.Request, token_handler: Callable
15
    ) -> tuple[dict, str, int]:
16
        """
17
        Validate the device flow request -> create the access token
18
        -> persist the token -> return the token.
19
        """
20
        headers = self._get_default_headers()
×
21
        try:
×
22
            self.validate_token_request(request)
×
23
        except rfc6749_errors.OAuth2Error as e:
×
24
            headers.update(e.headers)
×
25
            return headers, e.json, e.status_code
×
26

27
        token = token_handler.create_token(request, refresh_token=False)
×
28

29
        for modifier in self._token_modifiers:
×
30
            token = modifier(token)
×
31

32
        self.request_validator.save_token(token, request)
×
33

34
        return self.create_token_response(request, token_handler)
×
35

36
    def validate_token_request(self, request: common.Request) -> None:
6✔
37
        """
38
        Performs the necessary check against the request to ensure
39
        it's allowed to retrieve a token.
40
        """
41
        for validator in self.custom_validators.pre_token:
6✔
42
            validator(request)
6✔
43

44
        if not getattr(request, "grant_type", None):
6!
45
            raise rfc6749_errors.InvalidRequestError(
×
46
                "Request is missing grant type.", request=request
47
            )
48

49
        if request.grant_type != "urn:ietf:params:oauth:grant-type:device_code":
6✔
50
            raise rfc6749_errors.UnsupportedGrantTypeError(request=request)
6✔
51

52
        for param in ("grant_type", "scope"):
6✔
53
            if param in request.duplicate_params:
6✔
54
                raise rfc6749_errors.InvalidRequestError(
6✔
55
                    description=f"Duplicate {param} parameter.", request=request
56
                )
57

58
        if not self.request_validator.authenticate_client(request):
6!
59
            raise rfc6749_errors.InvalidClientError(request=request)
×
60
        elif not hasattr(request.client, "client_id"):
6✔
61
            raise NotImplementedError(
62
                "Authenticate client must set the "
63
                "request.client.client_id attribute "
64
                "in authenticate_client."
65
            )
66

67
        # Ensure client is authorized use of this grant type
68
        self.validate_grant_type(request)
6✔
69

70
        request.client_id = request.client_id or request.client.client_id
6✔
71
        self.validate_scopes(request)
6✔
72

73
        for validator in self.custom_validators.post_token:
6✔
74
            validator(request)
6✔
75

76
    def create_token_response(
6✔
77
        self, request: common.Request, token_handler: Callable
78
    ) -> tuple[dict, str, int]:
79
        """Return token or error in json format.
80

81
        :param request: OAuthlib request.
82
        :type request: oauthlib.common.Request
83
        :param token_handler: A token handler instance, for example of type
84
                              oauthlib.oauth2.BearerToken.
85

86
        If the access token request is valid and authorized, the
87
        authorization server issues an access token and optional refresh
88
        token as described in `Section 5.1`_.  If the request failed client
89
        authentication or is invalid, the authorization server returns an
90
        error response as described in `Section 5.2`_.
91
        .. _`Section 5.1`: https://tools.ietf.org/html/rfc6749#section-5.1
92
        .. _`Section 5.2`: https://tools.ietf.org/html/rfc6749#section-5.2
93
        """
94
        headers = self._get_default_headers()
6✔
95
        try:
6✔
96
            if self.request_validator.client_authentication_required(
6✔
97
                request
98
            ) and not self.request_validator.authenticate_client(request):
99
                raise rfc6749_errors.InvalidClientError(request=request)
6✔
100

101
            self.validate_token_request(request)
6✔
102

103
        except rfc6749_errors.OAuth2Error as e:
6✔
104
            headers.update(e.headers)
6✔
105
            return headers, e.json, e.status_code
6✔
106

107
        token = token_handler.create_token(request, self.refresh_token)
6✔
108

109
        self.request_validator.save_token(token, request)
6✔
110

111
        return headers, json.dumps(token), 200
6✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc