Coverage for polar/oauth2/exceptions.py: 85%

13 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-12-05 15:52 +0000

1from typing import Any 1a

2 

3from authlib.oauth2.rfc6750 import InvalidTokenError as _InvalidTokenError 1a

4 

5from polar.config import settings 1a

6 

7 

8class InvalidTokenError(_InvalidTokenError): 1a

9 def __init__(self, description: str | None = None, **extra_attributes: Any) -> None: 1a

10 super().__init__( 

11 description, realm=settings.WWW_AUTHENTICATE_REALM, **extra_attributes 

12 ) 

13 

14 

15class InsufficientScopeError(_InvalidTokenError): 1a

16 """The request requires higher privileges than provided by the 

17 access token. The resource server SHOULD respond with the HTTP 

18 403 (Forbidden) status code and MAY include the "scope" 

19 attribute with the scope necessary to access the protected 

20 resource. 

21 

22 https://tools.ietf.org/html/rfc6750#section-3.1 

23 

24 We don't use the one provided by Authlib because it doesn't have 

25 the logic to handle the headers attribute, contrary to `InvalidTokenError` 🤷‍♂️ 

26 """ 

27 

28 error = "insufficient_scope" 1a

29 description = ( 1a

30 "The request requires higher privileges than provided by the access token." 

31 ) 

32 status_code = 403 1a

33 

34 def __init__(self, required_scopes: set[str]) -> None: 1a

35 super().__init__( 

36 realm=settings.WWW_AUTHENTICATE_REALM, scope=" ".join(required_scopes) 

37 ) 

38 

39 

40__all__ = ["InvalidTokenError", "InsufficientScopeError"] 1a