Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PRT-2279] Validates Moodle token and checks for missing functions in a single function #122

Open
wants to merge 1 commit into
base: master-elos
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions app/controllers/rooms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 12 additions & 28 deletions lib/moodle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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)
Expand Down