mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 15:12:06 +00:00 
			
		
		
		
	[api] Simplify generated authentication check code (#9806)
This commit is contained in:
		| @@ -617,11 +617,9 @@ void APIServerConnection::on_ping_request(const PingRequest &msg) { | |||||||
|   } |   } | ||||||
| } | } | ||||||
| void APIServerConnection::on_device_info_request(const DeviceInfoRequest &msg) { | void APIServerConnection::on_device_info_request(const DeviceInfoRequest &msg) { | ||||||
|   if (this->check_connection_setup_()) { |   if (this->check_connection_setup_() && !this->send_device_info_response(msg)) { | ||||||
|     if (!this->send_device_info_response(msg)) { |  | ||||||
|     this->on_fatal_error(); |     this->on_fatal_error(); | ||||||
|   } |   } | ||||||
|   } |  | ||||||
| } | } | ||||||
| void APIServerConnection::on_list_entities_request(const ListEntitiesRequest &msg) { | void APIServerConnection::on_list_entities_request(const ListEntitiesRequest &msg) { | ||||||
|   if (this->check_authenticated_()) { |   if (this->check_authenticated_()) { | ||||||
| @@ -650,11 +648,9 @@ void APIServerConnection::on_subscribe_home_assistant_states_request(const Subsc | |||||||
|   } |   } | ||||||
| } | } | ||||||
| void APIServerConnection::on_get_time_request(const GetTimeRequest &msg) { | void APIServerConnection::on_get_time_request(const GetTimeRequest &msg) { | ||||||
|   if (this->check_connection_setup_()) { |   if (this->check_connection_setup_() && !this->send_get_time_response(msg)) { | ||||||
|     if (!this->send_get_time_response(msg)) { |  | ||||||
|     this->on_fatal_error(); |     this->on_fatal_error(); | ||||||
|   } |   } | ||||||
|   } |  | ||||||
| } | } | ||||||
| #ifdef USE_API_SERVICES | #ifdef USE_API_SERVICES | ||||||
| void APIServerConnection::on_execute_service_request(const ExecuteServiceRequest &msg) { | void APIServerConnection::on_execute_service_request(const ExecuteServiceRequest &msg) { | ||||||
| @@ -665,11 +661,9 @@ void APIServerConnection::on_execute_service_request(const ExecuteServiceRequest | |||||||
| #endif | #endif | ||||||
| #ifdef USE_API_NOISE | #ifdef USE_API_NOISE | ||||||
| void APIServerConnection::on_noise_encryption_set_key_request(const NoiseEncryptionSetKeyRequest &msg) { | void APIServerConnection::on_noise_encryption_set_key_request(const NoiseEncryptionSetKeyRequest &msg) { | ||||||
|   if (this->check_authenticated_()) { |   if (this->check_authenticated_() && !this->send_noise_encryption_set_key_response(msg)) { | ||||||
|     if (!this->send_noise_encryption_set_key_response(msg)) { |  | ||||||
|     this->on_fatal_error(); |     this->on_fatal_error(); | ||||||
|   } |   } | ||||||
|   } |  | ||||||
| } | } | ||||||
| #endif | #endif | ||||||
| #ifdef USE_BUTTON | #ifdef USE_BUTTON | ||||||
| @@ -858,11 +852,9 @@ void APIServerConnection::on_bluetooth_gatt_notify_request(const BluetoothGATTNo | |||||||
| #ifdef USE_BLUETOOTH_PROXY | #ifdef USE_BLUETOOTH_PROXY | ||||||
| void APIServerConnection::on_subscribe_bluetooth_connections_free_request( | void APIServerConnection::on_subscribe_bluetooth_connections_free_request( | ||||||
|     const SubscribeBluetoothConnectionsFreeRequest &msg) { |     const SubscribeBluetoothConnectionsFreeRequest &msg) { | ||||||
|   if (this->check_authenticated_()) { |   if (this->check_authenticated_() && !this->send_subscribe_bluetooth_connections_free_response(msg)) { | ||||||
|     if (!this->send_subscribe_bluetooth_connections_free_response(msg)) { |  | ||||||
|     this->on_fatal_error(); |     this->on_fatal_error(); | ||||||
|   } |   } | ||||||
|   } |  | ||||||
| } | } | ||||||
| #endif | #endif | ||||||
| #ifdef USE_BLUETOOTH_PROXY | #ifdef USE_BLUETOOTH_PROXY | ||||||
| @@ -889,11 +881,9 @@ void APIServerConnection::on_subscribe_voice_assistant_request(const SubscribeVo | |||||||
| #endif | #endif | ||||||
| #ifdef USE_VOICE_ASSISTANT | #ifdef USE_VOICE_ASSISTANT | ||||||
| void APIServerConnection::on_voice_assistant_configuration_request(const VoiceAssistantConfigurationRequest &msg) { | void APIServerConnection::on_voice_assistant_configuration_request(const VoiceAssistantConfigurationRequest &msg) { | ||||||
|   if (this->check_authenticated_()) { |   if (this->check_authenticated_() && !this->send_voice_assistant_get_configuration_response(msg)) { | ||||||
|     if (!this->send_voice_assistant_get_configuration_response(msg)) { |  | ||||||
|     this->on_fatal_error(); |     this->on_fatal_error(); | ||||||
|   } |   } | ||||||
|   } |  | ||||||
| } | } | ||||||
| #endif | #endif | ||||||
| #ifdef USE_VOICE_ASSISTANT | #ifdef USE_VOICE_ASSISTANT | ||||||
|   | |||||||
| @@ -2261,18 +2261,15 @@ static const char *const TAG = "api.service"; | |||||||
|             else: |             else: | ||||||
|                 check_func = "this->check_connection_setup_()" |                 check_func = "this->check_connection_setup_()" | ||||||
|  |  | ||||||
|             body = f"if ({check_func}) {{\n" |  | ||||||
|  |  | ||||||
|             # Add the actual handler code, indented |  | ||||||
|             handler_body = "" |  | ||||||
|             if is_void: |             if is_void: | ||||||
|                 handler_body = f"this->{func}(msg);\n" |                 # For void methods, just wrap with auth check | ||||||
|  |                 body = f"if ({check_func}) {{\n" | ||||||
|  |                 body += f"  this->{func}(msg);\n" | ||||||
|  |                 body += "}\n" | ||||||
|             else: |             else: | ||||||
|                 handler_body = f"if (!this->send_{func}_response(msg)) {{\n" |                 # For non-void methods, combine auth check and send response check | ||||||
|                 handler_body += "  this->on_fatal_error();\n" |                 body = f"if ({check_func} && !this->send_{func}_response(msg)) {{\n" | ||||||
|                 handler_body += "}\n" |                 body += "  this->on_fatal_error();\n" | ||||||
|  |  | ||||||
|             body += indent(handler_body) + "\n" |  | ||||||
|                 body += "}\n" |                 body += "}\n" | ||||||
|         else: |         else: | ||||||
|             # No auth check needed, just call the handler |             # No auth check needed, just call the handler | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user