Coverage for gws-app/gws/plugin/auth_method/web/action.py: 85%

39 statements  

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

1"""The ``auth`` action, the client API of the web authentication method.""" 

2 

3from typing import cast 

4 

5import gws 

6import gws.base.action 

7 

8from . import core 

9 

10 

11@gws.ext.config.action('auth') 

12class Config(gws.base.action.Config): 

13 """Login, logout and session checks for web-based authentication.""" 

14 

15 pass 

16 

17 

18@gws.ext.props.action('auth') 

19class Props(gws.base.action.Props): 

20 pass 

21 

22 

23@gws.ext.object.action('auth') 

24class Object(gws.base.action.Object): 

25 """Action for web logins, logouts and session checks. 

26 

27 Provides the client API of the web authentication method and passes the 

28 requests to that method. 

29 """ 

30 

31 method: core.Object 

32 """The web authentication method.""" 

33 

34 def configure(self): 

35 m = self.configure_method() 

36 if not m: 

37 raise gws.Error('web authorization method required') 

38 self.method = m 

39 

40 def configure_method(self): 

41 """Find the web authentication method. 

42 

43 Returns: 

44 The first configured method of type ``web``, or ``None``. 

45 """ 

46 for m in self.root.app.authMgr.methods: 

47 if m.extType == 'web': 

48 return cast(core.Object, m) 

49 

50 @gws.ext.command.api('authCheck') 

51 def check(self, req: gws.WebRequester, p: gws.Request) -> core.UserResponse: 

52 """Return the current user.""" 

53 if req.user.isGuest: 

54 return core.UserResponse(user=None) 

55 return core.UserResponse(user=gws.props_of(req.user, req.user)) 

56 

57 @gws.ext.command.api('authLogin') 

58 def login(self, req: gws.WebRequester, p: core.LoginRequest) -> core.LoginResponse: 

59 """Log in with a username and a password.""" 

60 return self.method.handle_login(req, p) 

61 

62 @gws.ext.command.api('authMfaVerify') 

63 def mfa_verify(self, req: gws.WebRequester, p: core.MfaVerifyRequest) -> core.LoginResponse: 

64 """Verify a multi-factor code and complete the login.""" 

65 return self.method.handle_mfa_verify(req, p) 

66 

67 @gws.ext.command.api('authMfaRestart') 

68 def mfa_restart(self, req: gws.WebRequester, p: gws.Request) -> core.LoginResponse: 

69 """Restart the multi-factor transaction, for example to send a new code.""" 

70 return self.method.handle_mfa_restart(req, p) 

71 

72 @gws.ext.command.api('authLogout') 

73 def logout(self, req: gws.WebRequester, p: gws.Request) -> core.LogoutResponse: 

74 """Log out the current user.""" 

75 return self.method.handle_logout(req)