Skip to content

Commit e0cdbae

Browse files
committed
Use rest-client for authentication request
1 parent ce1f10a commit e0cdbae

2 files changed

Lines changed: 27 additions & 18 deletions

File tree

app/controllers/api/v8/users_controller.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,12 @@ def set_password_managed_by_courses_mooc_fi
175175
only_admins!
176176

177177
User.transaction do
178-
@user = User.find_by!(id: params[:id])
179-
@user.password_managed_by_courses_mooc_fi = true
180-
@user.password_hash = nil
181-
@user.salt = nil
182-
@user.argon_hash = nil
183-
raise ActiveRecord::Rollback if !@user.errors.empty? || !@user.save
178+
user = User.find_by!(id: params[:id])
179+
user.password_managed_by_courses_mooc_fi = true
180+
user.password_hash = nil
181+
user.salt = nil
182+
user.argon_hash = nil
183+
raise ActiveRecord::Rollback if !user.errors.empty? || !user.save
184184
return render json: {
185185
status: 'Password managed by courses.mooc.fi set to true and password deleted.'
186186
}

app/models/user.rb

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require 'rest-client'
4+
35
class User < ApplicationRecord
46
include Comparable
57
include Gravtastic
@@ -151,20 +153,27 @@ def self.authenticate(login, submitted_password)
151153
end
152154

153155
def authenticate_via_courses_mooc_fi(email, submitted_password)
154-
uri = URI.parse('https://courses.mooc.fi/api/v0/tmc-server/auth')
155-
http = Net::HTTP.new(uri.host, uri.port)
156-
http.use_ssl = (uri.scheme == 'https')
157-
request = Net::HTTP::Post.new(uri.path, { 'Content-Type' => 'application/json' })
158-
request.body = { email: email, password: submitted_password }.to_json
159-
160-
response = http.request(request)
161-
return false unless response.is_a?(Net::HTTPSuccess)
156+
auth_url = SiteSetting.value('courses_mooc_fi_auth_url')
157+
response = RestClient.post(
158+
auth_url,
159+
{ email: email, password: submitted_password }.to_json,
160+
{ content_type: :json, accept: :json }
161+
)
162162

163163
data = JSON.parse(response.body)
164-
data['authenticated'] == true
165-
rescue StandardError => e
166-
Rails.logger.error("MOOC.fi authentication failed: #{e}")
167-
false
164+
unless data["authenticated"] == true
165+
raise "Authentication via courses.mooc.fi failed for #{email}"
166+
end
167+
168+
true
169+
rescue RestClient::Unauthorized, RestClient::Forbidden
170+
raise "Authentication rejected by courses.mooc.fi for #{email}"
171+
rescue RestClient::ExceptionWithResponse => e
172+
Rails.logger.error("Authentication via courses.mooc.fi error: #{e.response}")
173+
raise "Authentication via courses.mooc.fi failed: #{e.message}"
174+
rescue => e
175+
Rails.logger.error("Unexpected error during authentication via courses.mooc.fi: #{e.message}")
176+
raise "Unexpected error while authenticating via courses.mooc.fi: #{e.message}"
168177
end
169178

170179
def password_reset_key

0 commit comments

Comments
 (0)