Coverage for src/lexigram/auth/cli/contributor.py: 0%

23 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-25 12:26 +0800

1"""Auth CLI contributor definitions.""" 

2 

3from __future__ import annotations 

4 

5from lexigram.contracts.cli.contributions import ( 

6 CommandContribution, 

7 DoctorCheckContribution, 

8 HealthCheckContribution, 

9 HookContribution, 

10 SchemaSetupContribution, 

11 ShellContextContribution, 

12) 

13from lexigram.contracts.cli.types import GeneratorDefinition 

14 

15_GENERATOR_DEFINITIONS: tuple[GeneratorDefinition, ...] = ( 

16 GeneratorDefinition( 

17 name="auth_guard", 

18 title="Generate Auth Guard", 

19 description="Generate an authentication/authorization guard", 

20 contributor="auth", 

21 generator_path="lexigram.auth.cli.generators.auth_guard:AuthGuardGenerator", 

22 default_output_dir="src/guards", 

23 category="auth", 

24 ), 

25 GeneratorDefinition( 

26 name="auth_policy", 

27 title="Generate Auth Policy", 

28 description="Generate an authorization policy", 

29 contributor="auth", 

30 generator_path="lexigram.auth.cli.generators.auth_policy:AuthPolicyGenerator", 

31 default_output_dir="src/policies", 

32 category="auth", 

33 ), 

34 GeneratorDefinition( 

35 name="guard", 

36 title="Generate Guard", 

37 description="Generate an authorization guard", 

38 contributor="auth", 

39 generator_path="lexigram.auth.cli.generators.guard:AuthGuardGenerator", 

40 default_output_dir="src/guards", 

41 category="auth", 

42 ), 

43) 

44 

45 

46class AuthCliContributor: 

47 """CLI contributor for the lexigram-auth package.""" 

48 

49 @property 

50 def contributor_id(self) -> str: 

51 """Return the contributor identifier.""" 

52 return "auth" 

53 

54 def get_generators(self) -> list[GeneratorDefinition]: 

55 """Return generator definitions for auth.""" 

56 return list(_GENERATOR_DEFINITIONS) 

57 

58 def get_commands(self) -> list[CommandContribution]: 

59 """Return the contributed `auth` command group.""" 

60 return [ 

61 CommandContribution( 

62 name="auth", 

63 help="Authentication and authorization management commands", 

64 app_factory_path="lexigram.auth.cli.commands:create_auth_app", 

65 contributor="auth", 

66 category="security", 

67 requires_app_context=True, 

68 ), 

69 ] 

70 

71 def get_health_checks(self) -> list[HealthCheckContribution]: 

72 """Return auth service health check.""" 

73 return [ 

74 HealthCheckContribution( 

75 name="auth_service_status", 

76 description="Verify auth service and JWT manager are operational", 

77 check_path="lexigram.auth.cli.checks:check_auth_service", 

78 contributor="auth", 

79 category="security", 

80 timeout=5.0, 

81 critical=True, 

82 ), 

83 ] 

84 

85 def get_doctor_checks(self) -> list[DoctorCheckContribution]: 

86 """Return auth configuration doctor checks.""" 

87 return [ 

88 DoctorCheckContribution( 

89 name="jwt_secret_configured", 

90 description="Check JWT_SECRET env var or auth.jwt.secret config", 

91 check_path="lexigram.auth.cli.doctor:check_jwt_secret", 

92 contributor="auth", 

93 category="security", 

94 ), 

95 DoctorCheckContribution( 

96 name="auth_config_valid", 

97 description="Validate auth section in application.yaml", 

98 check_path="lexigram.auth.cli.doctor:check_auth_config", 

99 contributor="auth", 

100 category="security", 

101 ), 

102 ] 

103 

104 def get_shell_context(self) -> list[ShellContextContribution]: 

105 """Return auth shell context.""" 

106 return [ 

107 ShellContextContribution( 

108 name="auth", 

109 description="AuthenticationService for interactive use", 

110 factory_path="lexigram.auth.cli.shell:provide_auth", 

111 contributor="auth", 

112 ), 

113 ] 

114 

115 def get_hooks(self) -> list[HookContribution]: 

116 """Return auth lifecycle hooks.""" 

117 return [ 

118 HookContribution( 

119 event="post_command", 

120 handler_path="lexigram.auth.cli.hooks:log_auth_command", 

121 contributor="auth", 

122 priority=90, 

123 ), 

124 ] 

125 

126 def get_schema_setup(self) -> list[SchemaSetupContribution]: 

127 """Return schema setup contributions for auth.""" 

128 return [ 

129 SchemaSetupContribution( 

130 name="auth.oauth_identities", 

131 description="OAuth identity linkage storage", 

132 setup_fn_path="lexigram.auth.cli.schema_setup:ensure_oauth_identities", 

133 contributor=self.contributor_id, 

134 ), 

135 ] 

136 

137 

138__all__ = ["AuthCliContributor"]