Coverage for gws-app/gws/base/auth/provider.py: 81%

16 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-05 13:35 +0200

1"""Base authentication provider.""" 

2 

3from typing import Optional 

4 

5import gws 

6import gws.lib.jsonx 

7 

8from . import user as user_api 

9 

10 

11class Config(gws.Config): 

12 """Auth provider config.""" 

13 

14 allowedMethods: Optional[list[str]] 

15 """Authentication methods this provider accepts.""" 

16 

17 

18class Object(gws.AuthProvider): 

19 """Base authentication provider. 

20 

21 Reads the ``allowedMethods`` option and serializes users as JSON of all their 

22 fields, roles, attributes and data. Subclasses implement authentication and 

23 user lookup; the default ``authenticate`` returns no user. 

24 """ 

25 

26 def configure(self): 

27 self.allowedMethods = self.cfg('allowedMethods', default=[]) 

28 

29 def authenticate(self, method, credentials): 

30 return None 

31 

32 def serialize_user(self, user): 

33 return gws.lib.jsonx.to_string(user_api.to_dict(user)) 

34 

35 def unserialize_user(self, data): 

36 d = gws.lib.jsonx.from_string(data) 

37 return user_api.from_dict(self, d) if d else None