From a6a9cbb20a202bf97a3815fa7da001eef321fe5c Mon Sep 17 00:00:00 2001 From: Paola Date: Tue, 15 Oct 2024 15:39:15 -0300 Subject: [PATCH] chore: validates Moodle token and checks for missing functions in a single function - meaning a single call to Moodle API --- app/controllers/rooms_controller.rb | 14 +++++----- lib/moodle.rb | 40 +++++++++-------------------- 2 files changed, 19 insertions(+), 35 deletions(-) diff --git a/app/controllers/rooms_controller.rb b/app/controllers/rooms_controller.rb index 9418efec..2c88f9b7 100644 --- a/app/controllers/rooms_controller.rb +++ b/app/controllers/rooms_controller.rb @@ -358,20 +358,20 @@ def setup_moodle_groups 'core_group_get_course_groups' ] - valid_moodle_token = Moodle::API.valid_token?(moodle_token, {nonce: @app_launch.nonce}) - unless valid_moodle_token + # Validates the configured Moodle Token and checks for missing functions + # - token_validation_result[:valid_token] indicates if the token is valid (`true`) or not (`false`) + # - token_validation_result[:missing_functions] has a list of missing function (if there is any) + token_validation_result = Moodle::API.validate_token_and_check_missing_functions(moodle_token, wsfunctions, {nonce: @app_launch.nonce}) + if token_validation_result[:valid_token] == false Rails.logger.error 'Invalid or not found Moodle token' set_error('room', 'moodle_invalid_token', 500) @error[:explanation] = t("error.room.moodle_invalid_token.explanation") respond_with_error(@error) return - end - - missing_functions = Moodle::API.missing_token_functions(moodle_token, wsfunctions, {nonce: @app_launch.nonce}) - if missing_functions.any? + elsif token_validation_result[:missing_functions].any? Rails.logger.error 'A function required for the groups feature is not configured in the Moodle service' set_error('room', 'moodle_token_function_missing', :forbidden) - @error[:explanation] = t("error.room.moodle_token_function_missing.explanation", missing_functions: missing_functions) + @error[:explanation] = t("error.room.moodle_token_function_missing.explanation", missing_functions: token_validation_result[:missing_functions]) respond_with_error(@error) return end diff --git a/lib/moodle.rb b/lib/moodle.rb index 163edc15..aab42208 100644 --- a/lib/moodle.rb +++ b/lib/moodle.rb @@ -189,7 +189,7 @@ def self.token_functions_configured?(moodle_token, wsfunctions, opts={}) end end - def self.missing_token_functions(moodle_token, wsfunctions, opts={}) + def self.validate_token_and_check_missing_functions(moodle_token, wsfunctions, opts={}) params = { wstoken: moodle_token.token, wsfunction: 'core_webservice_get_site_info', @@ -203,47 +203,31 @@ def self.missing_token_functions(moodle_token, wsfunctions, opts={}) "wsfunction=core_webservice_get_site_info " \ "#{('nonce=' + opts[:nonce].to_s + ' ') if opts[:nonce]}" + validation_result = {} if result['exception'].present? + # Checks for an error indicating that the configured token is invalid + validation_result[:valid_token] = false if result['errorcode'] == 'invalidtoken' + Rails.logger.error(log_labels + "message=\"#{result}\"") - return wsfunctions + validation_result[:missing_functions] = wsfunctions + + return validation_result end # Gets all registered function names function_names = result['functions'].map { |hash| hash['name'] } # Checks if every element of wsfunctions is listed on the function_names list - missing_functions = wsfunctions - function_names + validation_result[:missing_functions] = wsfunctions - function_names - if missing_functions.empty? + if validation_result[:missing_functions].empty? Rails.logger.info(log_labels + "message=\"Every necessary " \ "function is correctly configured in the Moodle Token service.\"") else Rails.logger.warn(log_labels + "message=\"The following functions are not configured " \ - "in the Moodle Token service: #{missing_functions}.\"") + "in the Moodle Token service: #{validation_result[:missing_functions]}.\"") end - missing_functions - end - - def self.valid_token?(moodle_token, opts={}) - params = { - wstoken: moodle_token.token, - wsfunction: 'core_webservice_get_site_info', - moodlewsrestformat: 'json', - } - result = post(moodle_token.url, params) - - log_labels = "[MOODLE API] url=#{moodle_token.url} " \ - "token_id=#{moodle_token.id} " \ - "duration=#{result['duration']&.round(3)}s " \ - "wsfunction=core_webservice_get_site_info " \ - "#{('nonce=' + opts[:nonce].to_s + ' ') if opts[:nonce]}" - - if result['exception'].present? && result['errorcode'] == 'invalidtoken' - Rails.logger.error(log_labels + "message=\"#{result}\"") - return false - else - return true - end + validation_result end def self.post(host_url, params)