Coverage for apps/comments_views/journal/models.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2024-02-28 09:09 +0000

1from django.contrib.auth.models import User 

2 

3 

4class OIDCUser(User): 

5 """ 

6 Custom User class for OIDC authenticated users. 

7 This Model is not to be stored in DB. 

8 The class is used to: \\ 

9 1. Distinguish OIDC users from "normal" users. \\ 

10 2. Store the OIDC metadata properly in an user object so that \ 

11 it's accessible just like a regular authenticated User. 

12 """ 

13 

14 name: str = "" 

15 provider: str = "" 

16 provider_uid: str = "" 

17 claims = {} 

18 

19 class Meta: 

20 # Prevent the database table creation 

21 managed = False 

22 

23 def save(self, *args, **kwargs): 

24 """Override default save to do nothing""" 

25 

26 def populate_fields(self, claims: dict) -> None: 

27 """Fill the user object with the claims coming from the OP.""" 

28 for key, value in claims.items(): 

29 if hasattr(self, key): 

30 setattr(self, key, value) 

31 self.username = claims["email"] 

32 self.claims = {**claims} 

33 

34 def get_user_id(self) -> int: 

35 return int(self.claims["sub"])