Coverage for jutil/auth.py: 100%

# Coverage for jutil/auth.py : 100%

## 15 statements

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 from typing import Union, Optional

2

3 from django.contrib.auth.models import User

4 from django.http.request import HttpRequest

5 from rest_framework.exceptions import NotAuthenticated

6 from rest_framework.request import Request

7

8

9 def require_auth(request: Union[Request, HttpRequest], exceptions: bool = True) -> Optional[User]:

10 """

11 Returns authenticated User.

12 :param request: HttpRequest

13 :param exceptions: Raise (NotAuthenticated) exception. Default is True.

14 :return: User

15 """

16 if not request.user or not request.user.is_authenticated:

17 if exceptions:

18 raise NotAuthenticated()

19 return None

20 return request.user # type: ignore

21

22

23 class AuthUserMixin:

24 @property

25 def auth_user(self) -> User:

26 """

27 Returns authenticated user.

28 :return: User

29 """

30 return require_auth(self.request, exceptions=True) # type: ignore